3 SELECT evergreen.upgrade_deps_block_check('0587', :eg_version);
5 CREATE OR REPLACE FUNCTION maintain_control_numbers() RETURNS TRIGGER AS $func$
8 use MARC::File::XML (BinaryEncoding => 'UTF-8');
11 use Unicode::Normalize;
13 MARC::Charset->assume_unicode(1);
15 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
16 my $schema = $_TD->{table_schema};
17 my $rec_id = $_TD->{new}{id};
19 # Short-circuit if maintaining control numbers per MARC21 spec is not enabled
20 my $enable = spi_exec_query("SELECT enabled FROM config.global_flag WHERE name = 'cat.maintain_control_numbers'");
21 if (!($enable->{processed}) or $enable->{rows}[0]->{enabled} eq 'f') {
25 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
26 my $ou_cni = 'EVRGRN';
29 if ($schema eq 'serial') {
30 $owner = $_TD->{new}{owning_lib};
32 # are.owner and bre.owner can be null, so fall back to the consortial setting
33 $owner = $_TD->{new}{owner} || 1;
36 my $ous_rv = spi_exec_query("SELECT value FROM actor.org_unit_ancestor_setting('cat.marc_control_number_identifier', $owner)");
37 if ($ous_rv->{processed}) {
38 $ou_cni = $ous_rv->{rows}[0]->{value};
39 $ou_cni =~ s/"//g; # Stupid VIM syntax highlighting"
41 # Fall back to the shortname of the OU if there was no OU setting
42 $ous_rv = spi_exec_query("SELECT shortname FROM actor.org_unit WHERE id = $owner");
43 if ($ous_rv->{processed}) {
44 $ou_cni = $ous_rv->{rows}[0]->{shortname};
48 my ($create, $munge) = (0, 0);
50 my @scns = $record->field('035');
52 foreach my $id_field ('001', '003') {
54 my @controls = $record->field($id_field);
56 if ($id_field eq '001') {
57 $spec_value = $rec_id;
59 $spec_value = $ou_cni;
62 # Create the 001/003 if none exist
63 if (scalar(@controls) == 1) {
64 # Only one field; check to see if we need to munge it
65 unless (grep $_->data() eq $spec_value, @controls) {
69 # Delete the other fields, as with more than 1 001/003 we do not know which 003/001 to match
70 foreach my $control (@controls) {
71 $record->delete_field($control);
73 $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value));
78 my $cn = $record->field('001')->data();
79 # Special handling of OCLC numbers, often found in records that lack 003
80 if ($cn =~ /^oc[nm]/) {
81 $cn =~ s/^oc[nm]0*(\d+)/$1/;
82 $record->field('003')->data('OCoLC');
86 # Now, if we need to munge the 001, we will first push the existing 001/003
87 # into the 035; but if the record did not have one (and one only) 001 and 003
88 # to begin with, skip this process
89 if ($munge and not $create) {
91 my $scn = "(" . $record->field('003')->data() . ")" . $cn;
93 # Do not create duplicate 035 fields
94 unless (grep $_->subfield('a') eq $scn, @scns) {
95 $record->insert_fields_ordered(MARC::Field->new('035', '', '', 'a' => $scn));
99 # Set the 001/003 and update the MARC
100 if ($create or $munge) {
101 $record->field('001')->data($rec_id);
102 $record->field('003')->data($ou_cni);
104 my $xml = $record->as_xml_record();
106 $xml =~ s/^<\?xml.+\?\s*>//go;
107 $xml =~ s/>\s+</></go;
108 $xml =~ s/\p{Cc}//go;
110 # Embed a version of OpenILS::Application::AppUtils->entityize()
111 # to avoid having to set PERL5LIB for PostgreSQL as well
113 # If we are going to convert non-ASCII characters to XML entities,
114 # we had better be dealing with a UTF8 string to begin with
115 $xml = decode_utf8($xml);
119 # Convert raw ampersands to entities
120 $xml =~ s/&(?!\S+;)/&/gso;
122 # Convert Unicode characters to entities
123 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
125 $xml =~ s/[\x00-\x1f]//go;
126 $_TD->{new}{marc} = $xml;
132 $func$ LANGUAGE PLPERLU;
134 INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath) VALUES
135 (29, 'identifier', 'scn', oils_i18n_gettext(28, 'System Control Number', 'cmf', 'label'), 'marcxml', $$//marc:datafield[@tag='035']/marc:subfield[@code="a"]$$);
136 INSERT INTO config.metabib_field ( id, field_class, name, label, format, xpath) VALUES
137 (30, 'identifier', 'lccn', oils_i18n_gettext(28, 'LC Control Number', 'cmf', 'label'), 'marcxml', $$//marc:datafield[@tag='010']/marc:subfield[@code="a" or @code='z']$$);
139 -- Far from perfect, but much faster than reingesting every record
140 INSERT INTO metabib.identifier_field_entry(source, field, value)
141 SELECT record, 29, value FROM metabib.full_rec WHERE tag = '035' AND subfield = 'a';
142 INSERT INTO metabib.identifier_field_entry(source, field, value)
143 SELECT record, 30, value FROM metabib.full_rec WHERE tag = '010' AND subfield IN ('a', 'z');