From: Galen Charlton Date: Mon, 10 Sep 2012 15:42:20 +0000 (-0400) Subject: new function to set indicators in MARC fields X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=commitdiff_plain;h=a220c56b038fc03e95ba7e5242931cb621cc6ee3 new function to set indicators in MARC fields migration_tools.set_leader(marc, tag, indicator_pos, value): Set indicator value of a specified MARC field. The first argument is a MARCXML string. The second argument is a MARC tag. The third argument is the indicator position, either 1 or 2. The fourth argument is the character to set the indicator value to. All occurences of the specified field will be changed. The function returns the revised MARCXML string. Signed-off-by: Galen Charlton --- diff --git a/sql/base/base.sql b/sql/base/base.sql index 1d35074..16e6b3a 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -1833,3 +1833,43 @@ The first argument is an array of tag/subfield specifiers, e.g., ARRAY['001', '2 The second argument is an array of text containing the values to plug into each field. If the value for a given field is NULL or the empty string, it is not inserted. $$; + +CREATE OR REPLACE FUNCTION migration_tools.set_indicator (TEXT, TEXT, INTEGER, CHAR(1)) RETURNS TEXT AS $func$ + +my ($marcxml, $tag, $pos, $value) = @_; + +use MARC::Record; +use MARC::File::XML (BinaryEncoding => 'UTF-8'); +use MARC::Charset; +use strict; + +MARC::Charset->assume_unicode(1); + +elog(ERROR, 'indicator position must be either 1 or 2') unless $pos =~ /^[12]$/; +elog(ERROR, 'MARC tag must be numeric') unless $tag =~ /^\d{3}$/; +elog(ERROR, 'MARC tag must not be control field') if $tag =~ /^00/; +elog(ERROR, 'Value must be exactly one character') unless $value =~ /^.$/; + +my $xml = $marcxml; +eval { + my $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8'); + + foreach my $field ($marc->field($tag)) { + $field->update("ind$pos" => $value); + } + $xml = $marc->as_xml_record; + $xml =~ s/^<\?.+?\?>$//mo; + $xml =~ s/\n//sgo; + $xml =~ s/>\s+