From d6c0bc0e4af0483608bc5e86806d7b48c3b8ced2 Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Mon, 20 Apr 2015 12:10:02 -0400 Subject: [PATCH] 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 --- sql/base/base.sql | 33 ++++++++++++++++++++++++++++++++- 1 files changed, 32 insertions(+), 1 deletions(-) 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+