Add clean_marc function to OpenILS::Utils::Normalize.
authorJason Stephenson <jstephenson@mvlc.org>
Wed, 9 Nov 2011 20:34:27 +0000 (15:34 -0500)
committerDan Scott <dscott@laurentian.ca>
Wed, 7 Mar 2012 21:02:43 +0000 (16:02 -0500)
Add a library function to clean up MARC records for how we like to
store them in the biblio.record_entry table. Having this in a library
will reduce code duplication.

Also, replace nearly identical code in OpenILS::Application::Vandelay
and OpenILS::Application::Acq::Order with calls to this new function.

Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Signed-off-by: Dan Scott <dscott@laurentian.ca>

Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Order.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm
Open-ILS/src/perlmods/lib/OpenILS/Utils/Normalize.pm

index 1b1aff3..9eef738 100644 (file)
@@ -181,6 +181,7 @@ use OpenSRF::Utils::JSON;
 use OpenSRF::AppSession;
 use OpenILS::Utils::Fieldmapper;
 use OpenILS::Utils::CStoreEditor q/:funcs/;
+use OpenILS::Utils::Normalize qw/clean_marc/;
 use OpenILS::Const qw/:const/;
 use OpenSRF::EX q/:try/;
 use OpenILS::Application::AppUtils;
@@ -1258,13 +1259,7 @@ sub upload_records {
         last unless $r;
 
                try {
-            ($xml = $r->as_xml_record()) =~ s/\n//sog;
-            $xml =~ s/^<\?xml.+\?\s*>//go;
-            $xml =~ s/>\s+</></go;
-            $xml =~ s/\p{Cc}//go;
-            $xml = $U->entityize($xml);
-            $xml =~ s/[\x00-\x1f]//go;
-
+            $xml = clean_marc($r);
                } catch Error with {
                        $err = shift;
                        $logger->warn("Proccessing XML of record $count in set $key failed with error $err.  Skipping this record");
index b30d652..c4d4332 100644 (file)
@@ -9,6 +9,7 @@ use OpenSRF::Utils::SettingsClient;
 use OpenSRF::Utils::Cache;
 use OpenILS::Utils::Fieldmapper;
 use OpenILS::Utils::CStoreEditor qw/:funcs/;
+use OpenILS::Utils::Normalize qw/clean_marc/;
 use MARC::Batch;
 use MARC::Record;
 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
@@ -285,12 +286,7 @@ sub process_spool {
         $logger->info("processing record $count");
 
         try {
-            (my $xml = $r->as_xml_record()) =~ s/\n//sog;
-            $xml =~ s/^<\?xml.+\?\s*>//go;
-            $xml =~ s/>\s+</></go;
-            $xml =~ s/\p{Cc}//go;
-            $xml = $U->entityize($xml);
-            $xml =~ s/[\x00-\x1f]//go;
+            my $xml = clean_marc($r);
 
             my $qrec;
             # Check the leader to ensure we've got something resembling the expected
index e3e699f..9ddca6e 100644 (file)
@@ -3,9 +3,13 @@ use strict;
 use warnings;
 use Unicode::Normalize;
 use Encode;
+use UNIVERSAL qw/isa/;
+use MARC::Record;
+use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
+use OpenILS::Application::AppUtils;
 
 use Exporter 'import';
-our @EXPORT_OK = qw( naco_normalize search_normalize );
+our @EXPORT_OK = qw( clean_marc naco_normalize search_normalize );
 
 sub naco_normalize {
     my $str = decode_utf8(shift);
@@ -97,4 +101,24 @@ sub _normalize_codes {
     return lc $str;
 }
 
+# Cleans up a MARC::Record or MARCXML string for storage in the
+# Open-ILS database.
+#
+# Takes either a MARC::Record or a string of MARCXML.
+#
+# Returns a string of MARCXML as Open-ILS likes to store it.
+#
+# Assumes input is already in UTF-8.
+sub clean_marc {
+    my $input = shift;
+    my $xml = (isa $input, 'MARC::Record') ? $input->as_xml_record() : $input;
+    $xml =~ s/\n//sog;
+    $xml =~ s/^<\?xml.+\?\s*>//go;
+    $xml =~ s/>\s+</></go;
+    $xml =~ s/\p{Cc}//go;
+    $xml = OpenILS::Application::AppUtils->entityize($xml);
+    $xml =~ s/[\x00-\x1f]//go;
+    return $xml;
+}
+
 1;