From: Chris Sharp Date: Mon, 12 Jan 2015 13:40:47 +0000 (-0500) Subject: adding function used to mass-update talking books subject headings X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=0cdb4bbc8b36ab72eb216dc440e97926876e5066;p=contrib%2Fpines.git adding function used to mass-update talking books subject headings --- diff --git a/sql/talking_books_function.sql b/sql/talking_books_function.sql new file mode 100644 index 0000000..b512e01 --- /dev/null +++ b/sql/talking_books_function.sql @@ -0,0 +1,52 @@ +CREATE OR REPLACE FUNCTION csharp.remove_talking_books_headings(record BIGINT) RETURNS TEXT AS $func$ +use strict; +use MARC::Record; +use MARC::File::XML (BinaryEncoding => 'UTF-8'); +use MARC::Charset; +use Encode; +use Unicode::Normalize; + +MARC::Charset->assume_unicode(1); + +my $q = spi_prepare('SELECT marc FROM biblio.record_entry WHERE id = $1', 'BIGINT'); +my $marc = spi_exec_prepared($q, $_[0])->{rows}->[0]->{marc}; + +my $record = MARC::Record->new_from_xml($marc); + +foreach my $field ( $record->field('65.') ) { + my $subfield; + if ( $subfield = $field->subfield("a") and $subfield =~ /^(children's )?audiobooks\.?$/i ) { + $record->delete_field($field); + } elsif ( $subfield = $field->subfield("a") and $subfield =~ /^talking books for children\.?/i ) { + my $new_field = MARC::Field->new( + '650','','0', a => "Children's audiobooks." ); + $field->replace_with($new_field); + } elsif ( $subfield = $field->subfield("a") and $subfield =~ /^talking books\.?$/i ) { + my $new_field = MARC::Field->new( + '650','','0', a => "Audiobooks." ); + $field->replace_with($new_field); + } +} + + +my $xml = $record->as_xml_record(); +$xml =~ s/\n//sgo; +$xml =~ s/^<\?xml.+\?\s*>//go; +$xml =~ s/>\s+entityize() +# to avoid having to set PERL5LIB for PostgreSQL as well + +$xml = NFC($xml); + +# Convert raw ampersands to entities +$xml =~ s/&(?!\S+;)/&/gso; + +# Convert Unicode characters to entities +$xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe; + +$xml =~ s/[\x00-\x1f]//go; + +return $xml; +$func$ LANGUAGE PLPERLU;