]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0587.maintain_control_numbers_with_OCLC_awareness.sql
LP#1206936 - Fix wrong billing info in money.transaction_billing_summary
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0587.maintain_control_numbers_with_OCLC_awareness.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('0587', :eg_version);
4
5 CREATE OR REPLACE FUNCTION maintain_control_numbers() RETURNS TRIGGER AS $func$
6 use strict;
7 use MARC::Record;
8 use MARC::File::XML (BinaryEncoding => 'UTF-8');
9 use MARC::Charset;
10 use Encode;
11 use Unicode::Normalize;
12
13 MARC::Charset->assume_unicode(1);
14
15 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
16 my $schema = $_TD->{table_schema};
17 my $rec_id = $_TD->{new}{id};
18
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') {
22     return;
23 }
24
25 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
26 my $ou_cni = 'EVRGRN';
27
28 my $owner;
29 if ($schema eq 'serial') {
30     $owner = $_TD->{new}{owning_lib};
31 } else {
32     # are.owner and bre.owner can be null, so fall back to the consortial setting
33     $owner = $_TD->{new}{owner} || 1;
34 }
35
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"
40 } else {
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};
45     }
46 }
47
48 my ($create, $munge) = (0, 0);
49
50 my @scns = $record->field('035');
51
52 foreach my $id_field ('001', '003') {
53     my $spec_value;
54     my @controls = $record->field($id_field);
55
56     if ($id_field eq '001') {
57         $spec_value = $rec_id;
58     } else {
59         $spec_value = $ou_cni;
60     }
61
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) {
66             $munge = 1;
67         }
68     } else {
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);
72         }
73         $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value));
74         $create = 1;
75     }
76 }
77
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');
83     $create = 0;
84 }
85
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) {
90
91     my $scn = "(" . $record->field('003')->data() . ")" . $cn;
92
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));
96     }
97 }
98
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);
103
104     my $xml = $record->as_xml_record();
105     $xml =~ s/\n//sgo;
106     $xml =~ s/^<\?xml.+\?\s*>//go;
107     $xml =~ s/>\s+</></go;
108     $xml =~ s/\p{Cc}//go;
109
110     # Embed a version of OpenILS::Application::AppUtils->entityize()
111     # to avoid having to set PERL5LIB for PostgreSQL as well
112
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);
116
117     $xml = NFC($xml);
118
119     # Convert raw ampersands to entities
120     $xml =~ s/&(?!\S+;)/&amp;/gso;
121
122     # Convert Unicode characters to entities
123     $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
124
125     $xml =~ s/[\x00-\x1f]//go;
126     $_TD->{new}{marc} = $xml;
127
128     return "MODIFY";
129 }
130
131 return;
132 $func$ LANGUAGE PLPERLU;
133
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']$$);
138
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');
144
145 COMMIT;