adding function used to mass-update talking books subject headings
authorChris Sharp <csharp@georgialibraries.org>
Mon, 12 Jan 2015 13:40:47 +0000 (08:40 -0500)
committerChris Sharp <csharp@georgialibraries.org>
Mon, 12 Jan 2015 13:40:47 +0000 (08:40 -0500)
sql/talking_books_function.sql [new file with mode: 0644]

diff --git a/sql/talking_books_function.sql b/sql/talking_books_function.sql
new file mode 100644 (file)
index 0000000..b512e01
--- /dev/null
@@ -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+</></go;
+$xml =~ s/\p{Cc}//go;
+
+# Embed a version of OpenILS::Application::AppUtils->entityize()
+# to avoid having to set PERL5LIB for PostgreSQL as well
+
+$xml = NFC($xml);
+
+# Convert raw ampersands to entities
+$xml =~ s/&(?!\S+;)/&amp;/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;