script to make sql for bib overlays collab/phasefx/overlay_script
authorJason Etheridge <jason@esilibrary.com>
Mon, 23 Sep 2013 13:48:35 +0000 (09:48 -0400)
committerJason Etheridge <jason@esilibrary.com>
Mon, 23 Sep 2013 14:04:28 +0000 (10:04 -0400)
Example:

./make_overlay_sql_from_marc.pl marc_file_with_901c_tags.xml > overlay.sql 2> rejected_marc_files.xml

Signed-off-by: Jason Etheridge <jason@esilibrary.com>
make_overlay_sql_from_marc.pl [new file with mode: 0755]

diff --git a/make_overlay_sql_from_marc.pl b/make_overlay_sql_from_marc.pl
new file mode 100755 (executable)
index 0000000..5ec4cc1
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+# Feed this script a list of UTF8 MARC-XML files as parameters.  Generated SQL goes to STDOUT,
+# MARC missing a single 901c value gets spit back out to STDERR.  Be sure to update the database
+# credentials below.
+
+use MARC::Batch;
+use MARC::File::XML ( BinaryEncoding => 'utf-8' );
+use MARC::Field;
+use Unicode::Normalize;
+use DBI;
+
+# DBI is just used for quoting, but the method does need a handle to the database
+my $DBI_RESOURCE = "dbi:Pg:dbname=evergreen;host=localhost;port=5432";
+my $DBI_USER = 'evergreen';
+my $DBI_PASSWD = 'evergreen';
+my $dbh = DBI->connect($DBI_RESOURCE, $DBI_USER, $DBI_PASSWD) or die("Database error: $DBI::errstr");
+
+binmode(STDOUT, ':utf8');
+binmode(STDIN, ':utf8');
+binmode(STDERR, ':utf8');
+
+print STDERR MARC::File::XML::header();
+
+foreach my $argnum ( 0 .. $#ARGV ) {
+
+    my $M;
+    open $M, '<:utf8', $ARGV[$argnum];
+    my $batch = MARC::Batch->new('XML',$M);
+
+       $batch->strict_off();
+       $batch->warnings_off();
+
+       while ( my $record = $batch->next() ) {
+
+        my @my901 = $record->field('901');
+        if (scalar(@my901) == 0 || scalar(@my901) > 1) {
+            print STDERR MARC::File::XML::record( $record );
+        } else {
+            my @my901c = $my901[0]->subfield('c');
+            if (scalar(@my901c) == 0 || scalar(@my901c) > 1) {
+                print STDERR MARC::File::XML::record( $record );
+            } else {
+                print "UPDATE biblio.record_entry SET marc = "
+                    . $dbh->quote( $record->as_xml_record() )
+                    . " WHERE id = " . $dbh->quote( $my901c[0] ) . ";\n";
+            }
+        }
+       }
+}
+
+print STDERR MARC::File::XML::footer();