Fix syntax errors, adjust URL logic, clean ISBNs
authorJeff Godin <jgodin@tadl.org>
Sat, 28 Apr 2012 21:26:40 +0000 (17:26 -0400)
committerBen Shum <bshum@biblio.org>
Wed, 12 Dec 2012 15:50:36 +0000 (10:50 -0500)
Fixing some syntax errors due to previous re-factor without
subsequent testing.

Adjusting some variable names for clarity.

Clean ISBN values using Business::ISBN

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

index bc3a591..539cdd4 100644 (file)
@@ -83,43 +83,58 @@ sub handler {
 
     my $cgi = CGI->new;
     my @path_parts = split( /\//, $r->unparsed_uri );
-    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;
 
     # Intended URL formats
     # /opac/extras/ac/jacket/medium/ISBN_VALUE      -- classic keyed-off-isbn
+    # /opac/extras/ac/-3/-2/-1
     # /opac/extras/ac/jacket/medium/r/RECORD_ID     -- provide record id (bre.id)
-    # /opac/extras/ac/jacket/medium/m/RECORD_ID     -- future use for metarecord id
+    # /opac/extras/ac/-4/-3/-2/-1
+    # /opac/extras/ac/jacket/medium/m/RECORD_ID     -- XXX: future use for metarecord id
+
+    my $keytype_in_url = $path_parts[-2];  # if not in one of m, r, this will be the $format
+
+    my $type;
+    my $format;
+    my $keytype;
+    my $keyvalue;
 
-    if ($keypart1 =~ m/^(r|m)$/) {
-        # XXX: metarecord not yet supported
+    if ($keytype_in_url =~ m/^(r|m)$/) {
+        $type = $path_parts[-4];
+        $format = $path_parts[-3];
+        $keyvalue = $path_parts[-1]; # a record/metarecord id
         $keytype = 'record';
     } else {
+        $type = $path_parts[-3];
+        $format = $path_parts[-2];
+        $keyvalue = $path_parts[-1]; # an isbn
         $keytype = 'isbn';
     }
 
-    if ($keytype == "isbn") { # if this request uses isbn for the key
+    my $res;
+    my $keyhash;
+    my $cachekey;
+
+    if ($keytype eq "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;
+        $keyhash->{"isbn"} = [$keyvalue];
+        $cachekey = $keyvalue;
     } else {
-        my $key_data = get_rec_keys($keypart2);
-        my @isbns = grep {$_->{tag} eq '020'} @$key_data;
+        my $key_data = get_rec_keys($keyvalue);
+        my @raw_isbns = grep {$_->{tag} eq '020'} @$key_data;
         my @upcs = grep {$_->{tag} eq '024'} @$key_data;
 
+        my @isbns = [map {
+            my $isbn_obj = Business::ISBN->new($_);
+            return $isbn->as_string([]);
+        } @raw_isbns];
+
         $keyhash = {
             isbn => [map {$_->{value}} @isbns],
             upc => [map {$_->{value}} @upcs]
         };
 
-        # TODO clean isbn
+        $cachekey = $keytype . '_' . $keyvalue;
     }
 
     child_init() unless $handler;
@@ -136,14 +151,14 @@ sub handler {
 
     # XXX DEBUG
     use OpenSRF::Utils::JSON;
-    $logger->info("Added Content Keys: " . OpenSRF::Utils::JSON->perl2JSON($keys));
+    $logger->info("Added Content Keys: " . OpenSRF::Utils::JSON->perl2JSON($keyhash));
 
     try {
         $data = $handler->$method($keyhash);
     } catch Error with {
         $err = shift;
         decr_error_countdown();
-        $logger->debug("added content handler failed: $method($keyhash) => $err"); # XXX: logs unhelpful hashref
+        $logger->debug("added content handler failed: $method($keytype/$keyvalue) => $err"); # XXX: logs unhelpful hashref
     };
 
     return Apache2::Const::NOT_FOUND if $err;
@@ -151,13 +166,13 @@ sub handler {
 
     if(!$data) {
         # if the AC lookup found no corresponding data, cache that information
-        $logger->debug("added content handler returned no results $method($id)") unless $data;
-        $AC->cache_result($type, $format, $id, {nocontent=>1});
+        $logger->debug("added content handler returned no results $method($keytype/$keyvalue)") unless $data;
+        $AC->cache_result($type, $format, $cachekey, {nocontent=>1});
         return Apache2::Const::NOT_FOUND;
     }
     
     $AC->print_content($data);
-    $AC->cache_result($type, $format, $id, $data);
+    $AC->cache_result($type, $format, $cachekey, $data);
 
     reset_error_countdown();
     return Apache2::Const::OK;