From: Jason Etheridge Date: Mon, 20 Apr 2015 16:10:02 +0000 (-0400) Subject: tweak migration_tools.insert_856_9 X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=commitdiff_plain;h=d6c0bc0e4af0483608bc5e86806d7b48c3b8ced2;hp=564b77c84e38ff7b6f50a61a4c4c42b9d54e15e8 tweak migration_tools.insert_856_9 rename original to migration_tools.insert_856_9_conditional, since it has behavior based on 856$u content. New version simply adds a $9 if one does not already exist (regardless of org unit referenced) Signed-off-by: Jason Etheridge --- diff --git a/sql/base/base.sql b/sql/base/base.sql index a724832..49d79db 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -1437,7 +1437,7 @@ END; $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION migration_tools.insert_856_9 (TEXT, TEXT) RETURNS TEXT AS $$ +CREATE OR REPLACE FUNCTION migration_tools.insert_856_9_conditional (TEXT, TEXT) RETURNS TEXT AS $$ ## USAGE: UPDATE biblio.record_entry SET marc = migration_tools.insert_856_9(marc, 'ABC') WHERE [...]; @@ -1469,6 +1469,37 @@ CREATE OR REPLACE FUNCTION migration_tools.insert_856_9 (TEXT, TEXT) RETURNS TEX $$ LANGUAGE PLPERLU STABLE; +CREATE OR REPLACE FUNCTION migration_tools.insert_856_9 (TEXT, TEXT) RETURNS TEXT AS $$ + + ## USAGE: UPDATE biblio.record_entry SET marc = migration_tools.insert_856_9(marc, 'ABC') WHERE [...]; + + my ($marcxml, $shortname) = @_; + + use MARC::Record; + use MARC::File::XML; + + my $xml = $marcxml; + + eval { + my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8'); + + foreach my $field ( $marc->field('856') ) { + if ( ! $field->as_string('9') ) { + $field->add_subfields( '9' => $shortname ); + } + } + + $xml = $marc->as_xml_record; + $xml =~ s/^<\?.+?\?>$//mo; + $xml =~ s/\n//sgo; + $xml =~ s/>\s+