From 871fab76de691ad1bcc0331c1f27b3b4d3634a47 Mon Sep 17 00:00:00 2001 From: Dan Scott Date: Thu, 21 Jul 2011 11:12:36 -0400 Subject: [PATCH] Add an unwrapped upgrade script for OCLC-aware control numbers The upgrade script creates the SCN and LCCN search indexes and populates them using metabib.full_rec as a source; cheating, but the normalized values will work for non-exact search purposes. Sites can reingest all of their bibs if they want exact search to work. Also, refactor maintain_control_numbers() using a suggestion by Dan Wells to eliminate some duplicate code. Signed-off-by: Dan Scott Signed-off-by: Bill Erickson --- Open-ILS/src/sql/Pg/002.functions.config.sql | 8 +- Open-ILS/src/sql/Pg/950.data.seed-values.sql | 4 +- ...aintain_control_numbers_with_OCLC_awareness.sql | 148 +++++++++++++++++++++ 3 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/xxxx.maintain_control_numbers_with_OCLC_awareness.sql diff --git a/Open-ILS/src/sql/Pg/002.functions.config.sql b/Open-ILS/src/sql/Pg/002.functions.config.sql index 227292947f..fefae05111 100644 --- a/Open-ILS/src/sql/Pg/002.functions.config.sql +++ b/Open-ILS/src/sql/Pg/002.functions.config.sql @@ -567,6 +567,7 @@ my $cn = $record->field('001')->data(); if ($cn =~ /^oc[nm]/) { $cn =~ s/^oc[nm]0*(\d+)/$1/; $record->field('003')->data('OCoLC'); + $create = 0; } # Now, if we need to munge the 001, we will first push the existing 001/003 @@ -587,13 +588,6 @@ if ($munge and not $create) { # Set the 001/003 and update the MARC if ($create or $munge) { - my $scn = "(" . $record->field('003')->data() . ")" . $cn; - - # Do not create duplicate 035 fields - unless (grep $_->subfield('a') eq $scn, @scns) { - $record->insert_fields_ordered(MARC::Field->new('035', '', '', 'a' => $scn)); - } - $record->field('001')->data($rec_id); $record->field('003')->data($ou_cni); diff --git a/Open-ILS/src/sql/Pg/950.data.seed-values.sql b/Open-ILS/src/sql/Pg/950.data.seed-values.sql index 8a88cad231..fd4c6c43f2 100644 --- a/Open-ILS/src/sql/Pg/950.data.seed-values.sql +++ b/Open-ILS/src/sql/Pg/950.data.seed-values.sql @@ -98,7 +98,9 @@ INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath ) INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath, search_field, facet_field) VALUES (28, 'identifier', 'authority_id', oils_i18n_gettext(28, 'Authority Record ID', 'cmf', 'label'), 'marcxml', '//marc:datafield/marc:subfield[@code="0"]', FALSE, TRUE); INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath) VALUES - (29, 'identifier', 'system_control_number', oils_i18n_gettext(28, 'System Control Number', 'cmf', 'label'), 'marcxml', $$//marc:datafield[@tag='035']/marc:subfield[@code="a"]$$); + (29, 'identifier', 'scn', oils_i18n_gettext(28, 'System Control Number', 'cmf', 'label'), 'marcxml', $$//marc:datafield[@tag='035']/marc:subfield[@code="a"]$$); +INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath) VALUES + (30, 'identifier', 'lccn', oils_i18n_gettext(28, 'LC Control Number', 'cmf', 'label'), 'marcxml', $$//marc:datafield[@tag='010']/marc:subfield[@code="a" or @code='z']$$); SELECT SETVAL('config.metabib_field_id_seq'::TEXT, (SELECT MAX(id) FROM config.metabib_field), TRUE); diff --git a/Open-ILS/src/sql/Pg/upgrade/xxxx.maintain_control_numbers_with_OCLC_awareness.sql b/Open-ILS/src/sql/Pg/upgrade/xxxx.maintain_control_numbers_with_OCLC_awareness.sql new file mode 100644 index 0000000000..dd386f0d25 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/xxxx.maintain_control_numbers_with_OCLC_awareness.sql @@ -0,0 +1,148 @@ +BEGIN; + +CREATE OR REPLACE FUNCTION maintain_control_numbers() RETURNS TRIGGER 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 $record = MARC::Record->new_from_xml($_TD->{new}{marc}); +my $schema = $_TD->{table_schema}; +my $rec_id = $_TD->{new}{id}; + +# Short-circuit if maintaining control numbers per MARC21 spec is not enabled +my $enable = spi_exec_query("SELECT enabled FROM config.global_flag WHERE name = 'cat.maintain_control_numbers'"); +if (!($enable->{processed}) or $enable->{rows}[0]->{enabled} eq 'f') { + return; +} + +# Get the control number identifier from an OU setting based on $_TD->{new}{owner} +my $ou_cni = 'EVRGRN'; + +my $owner; +if ($schema eq 'serial') { + $owner = $_TD->{new}{owning_lib}; +} else { + # are.owner and bre.owner can be null, so fall back to the consortial setting + $owner = $_TD->{new}{owner} || 1; +} + +my $ous_rv = spi_exec_query("SELECT value FROM actor.org_unit_ancestor_setting('cat.marc_control_number_identifier', $owner)"); +if ($ous_rv->{processed}) { + $ou_cni = $ous_rv->{rows}[0]->{value}; + $ou_cni =~ s/"//g; # Stupid VIM syntax highlighting" +} else { + # Fall back to the shortname of the OU if there was no OU setting + $ous_rv = spi_exec_query("SELECT shortname FROM actor.org_unit WHERE id = $owner"); + if ($ous_rv->{processed}) { + $ou_cni = $ous_rv->{rows}[0]->{shortname}; + } +} + +my ($create, $munge) = (0, 0); + +my @scns = $record->field('035'); + +foreach my $id_field ('001', '003') { + my $spec_value; + my @controls = $record->field($id_field); + + if ($id_field eq '001') { + $spec_value = $rec_id; + } else { + $spec_value = $ou_cni; + } + + # Create the 001/003 if none exist + if (scalar(@controls) == 1) { + # Only one field; check to see if we need to munge it + unless (grep $_->data() eq $spec_value, @controls) { + $munge = 1; + } + } else { + # Delete the other fields, as with more than 1 001/003 we do not know which 003/001 to match + foreach my $control (@controls) { + unless ($control->data() eq $spec_value) { + $record->delete_field($control); + } + } + $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value)); + $create = 1; + } +} + +my $cn = $record->field('001')->data(); +# Special handling of OCLC numbers, often found in records that lack 003 +if ($cn =~ /^oc[nm]/) { + $cn =~ s/^oc[nm]0*(\d+)/$1/; + $record->field('003')->data('OCoLC'); + $create = 0; +} + +# Now, if we need to munge the 001, we will first push the existing 001/003 +# into the 035; but if the record did not have one (and one only) 001 and 003 +# to begin with, skip this process +if ($munge and not $create) { + + my $scn = "(" . $record->field('003')->data() . ")" . $cn; + + # Do not create duplicate 035 fields + unless (grep $_->subfield('a') eq $scn, @scns) { + $record->insert_fields_ordered(MARC::Field->new('035', '', '', 'a' => $scn)); + } + + # Update the list of SCNs to avoid duplicates + @scns = $record->field('035'); +} + +# Set the 001/003 and update the MARC +if ($create or $munge) { + $record->field('001')->data($rec_id); + $record->field('003')->data($ou_cni); + + 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 + + # If we are going to convert non-ASCII characters to XML entities, + # we had better be dealing with a UTF8 string to begin with + $xml = decode_utf8($xml); + + $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; + $_TD->{new}{marc} = $xml; + + return "MODIFY"; +} + +return; +$func$ LANGUAGE PLPERLU; + +INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath) VALUES + (29, 'identifier', 'scn', oils_i18n_gettext(28, 'System Control Number', 'cmf', 'label'), 'marcxml', $$//marc:datafield[@tag='035']/marc:subfield[@code="a"]$$); +INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath) VALUES + (30, 'identifier', 'lccn', oils_i18n_gettext(28, 'LC Control Number', 'cmf', 'label'), 'marcxml', $$//marc:datafield[@tag='010']/marc:subfield[@code="a" or @code='z']$$); + +-- Far from perfect, but much faster than reingesting every record +INSERT INTO metabib.identifier_field_entry(source, field, value) + SELECT record, 29, value FROM metabib.full_rec WHERE tag = '035' AND subfield = 'a'; +INSERT INTO metabib.identifier_field_entry(source, field, value) + SELECT record, 30, value FROM metabib.full_rec WHERE tag = '010' AND subfield IN ('a', 'z'); + +COMMIT; -- 2.11.0