Attempt to validate ISBNs in AddedContent.pm. collab/dyrcona/added_content_isbn_twiddling
authorJason Stephenson <jstephenson@mvlc.org>
Wed, 9 Jul 2014 20:34:26 +0000 (16:34 -0400)
committerJason Stephenson <jstephenson@mvlc.org>
Wed, 9 Jul 2014 20:53:35 +0000 (16:53 -0400)
When getting ISBNs from the database, they often contain extra stuff
after the ISBN that sometimes causes problems for Business::ISBN.

This commit attempts to resolve some of that by looking only for the
part of the ISBN data that resembles an ISBN and using just that part.
If the ISBN data doesn't look like an ISBN, then it is discarded.

Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent.pm

index e1c0bf0..f656995 100644 (file)
@@ -143,10 +143,19 @@ sub handler {
         my @upcs  = grep {$_->{tag} eq '024'} @$key_data;
 
         map {
-            my $isbn_obj = Business::ISBN->new($_->{value});
-            my $isbn_str;
-            $isbn_str = $isbn_obj->as_string([]) if defined($isbn_obj);
-            $_->{value} = $isbn_str;
+            # Attempt to validate the ISBN.
+            # strip out hyphens;
+            $_->{value} =~ s/-//g;
+            #pull out the first chunk that looks like an ISBN:
+            if ($_->{value} =~ /([0-9xX]{10}(?:[0-9xX]{3})?)/) {
+                $_->{value} = $1;
+                my $isbn_obj = Business::ISBN->new($_->{value});
+                my $isbn_str;
+                $isbn_str = $isbn_obj->as_string([]) if defined($isbn_obj);
+                $_->{value} = $isbn_str;
+            } else {
+                undef $_->{value};
+            }
             undef $_ if !defined($_->{value});
         } @isbns;