AC by rec id: commit previously abandoned work
authorJeff Godin <jgodin@tadl.org>
Wed, 25 Apr 2012 08:23:28 +0000 (04:23 -0400)
committerJeff Godin <jgodin@tadl.org>
Sat, 28 Apr 2012 14:09:05 +0000 (10:09 -0400)
Commit of previously abandoned work, expect to build on and
finish this feature.

Signed-off-by: Jeff Godin <jgodin@tadl.org>
Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent/Syndetic.pm

index 6d94135..dad6faf 100644 (file)
@@ -76,48 +76,68 @@ sub child_init {
 sub handler {
 
     my $r   = shift;
+
+    # If the URL requested matches a file on the filesystem, have Apache serve that file
+    # this allows for local content (most typically images) to be used for some requests
     return Apache2::Const::DECLINED if (-e $r->filename);
 
     my $cgi = CGI->new;
     my @path_parts = split( /\//, $r->unparsed_uri );
-    my $type = $path_parts[-3];
-    my $format = $path_parts[-2];
-    my $id = $path_parts[-1];
+    my $type = $path_parts[-4];
+    my $format = $path_parts[-3];
+    my $keypart1 = $path_parts[-2]; # $keypart1 == either an isbn, or one of m, r
+    my $keypart2 = $path_parts[-1]; # $keypart2 == either undef (if keypart1 was an isbn), or the record/metarecord id
     my $res;
+    my $keytype;
+    my $keyhash;
+    my $cachekey;
+
+    if ($keypart1 =~ m/^(r|m)$/) {
+        $keytype = 'record';
+    } else {
+        $keytype = 'isbn';
+    }
+
+    if ($keytype == "isbn") { # if this request uses isbn for the key
+        # craft a hash with the single isbn, because that's all we will have
+        $keyhash = {};
+        $keyhash->{"isbn"} = [$keypart1];
+        $cachekey = $keypart1;
+    } else {
+        my $key_data = get_rec_keys($keypart2);
+        my @isbns = grep {$_->{tag} eq '020'} @$key_data;
+        my @upcs = grep {$_->{tag} eq '024'} @$key_data;
+
+        $keyhash = {
+            isbn => [map {$_->{value}} @isbns],
+            upc => [map {$_->{value}} @upcs]
+        };
+
+        # TODO clean isbn
+    }
 
     child_init() unless $handler;
 
-    return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $id;
+    return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $keyhash;
 
     my $err;
     my $data;
     my $method = "${type}_${format}";
 
     return Apache2::Const::NOT_FOUND unless $handler->can($method);
-    return $res if defined($res = $AC->serve_from_cache($type, $format, $id));
+    return $res if defined($res = $AC->serve_from_cache($type, $format, $cachekey));
     return Apache2::Const::NOT_FOUND unless $AC->lookups_enabled;
 
-    my $key_data = get_rec_keys($id);
-    my @isbns = grep {$_->{tag} eq '020'} @$key_data;
-    my @upcs = grep {$_->{tag} eq '024'} @$key_data;
-
-    my $keys = {
-        isbn => [map {$_->{value}} @isbns],
-        upc => [map {$_->{value}} @upcs]
-    };
-
-    # TODO clean isbn
-
     # XXX DEBUG
     use OpenSRF::Utils::JSON;
     $logger->info("Added Content Keys: " . OpenSRF::Utils::JSON->perl2JSON($keys));
 
     try {
-        $data = $handler->$method($keys);
+        $data = $handler->$method($keyhash);
     } catch Error with {
         $err = shift;
         decr_error_countdown();
-        $logger->debug("added content handler failed: $method($id) => $err");
+        $logger->debug("added content handler failed: $method($keyhash) => $err"); # XXX: logs unhelpful hashref
     };
 
     return Apache2::Const::NOT_FOUND if $err;
index 6ae2a5a..5240752 100644 (file)
@@ -30,21 +30,21 @@ sub userid {
 
 # --------------------------------------------------------------------------
 sub jacket_small {
-    my( $self, $key ) = @_;
+    my( $self, $keys ) = @_;
     return $self->send_img(
-        $self->fetch_response('sc.gif', $key, 1));
+        $self->fetch_response('sc.gif', $keys, 1));
 }
 
 sub jacket_medium {
-    my( $self, $key ) = @_;
+    my( $self, $keys ) = @_;
     return $self->send_img(
-        $self->fetch_response('mc.gif', $key, 1));
+        $self->fetch_response('mc.gif', $keys, 1));
 
 }
 sub jacket_large {
-    my( $self, $key ) = @_;
+    my( $self, $keys ) = @_;
     return $self->send_img(
-        $self->fetch_response('lc.gif', $key, 1));
+        $self->fetch_response('lc.gif', $keys, 1));
 }
 
 # --------------------------------------------------------------------------
@@ -251,9 +251,14 @@ sub fetch_content {
 
 # returns the HTTP response object from the URL fetch
 sub fetch_response {
-    my( $self, $page, $key, $notype ) = @_;
+    my( $self, $page, $keys, $notype ) = @_;
     my $uname = $self->userid;
-    my $url = $self->base_url . "?isbn=$key/$page&client=$uname" . (($notype) ? '' : "&type=rw12");
+
+    # Fetch single isbn and single upc
+    my $isbn = $keys->{isbn}[0];
+    my $upc = $keys->{upc}[0];
+
+    my $url = $self->base_url . "?isbn=$isbn/$page&client=$uname" . (($notype) ? '' : "&type=rw12");
     return $AC->get_url($url);
 }