]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1077.schema.lp1714026_maintain_control_numbers.sql
LP1889113 Staff catalog record holds sticky org select
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1077.schema.lp1714026_maintain_control_numbers.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1077', :eg_version); -- csharp/gmcharlt
4
5 -- if the "public" version of this function exists, drop it to prevent confusion/trouble
6
7 -- drop triggers that depend on this first
8 DROP TRIGGER IF EXISTS c_maintain_control_numbers ON biblio.record_entry;
9 DROP TRIGGER IF EXISTS c_maintain_control_numbers ON serial.record_entry;
10 DROP TRIGGER IF EXISTS c_maintain_control_numbers ON authority.record_entry;
11
12 DROP FUNCTION IF EXISTS public.maintain_control_numbers();
13
14 -- create the function within the "evergreen" schema
15
16 CREATE OR REPLACE FUNCTION evergreen.maintain_control_numbers() RETURNS TRIGGER AS $func$
17 use strict;
18 use MARC::Record;
19 use MARC::File::XML (BinaryEncoding => 'UTF-8');
20 use MARC::Charset;
21 use Encode;
22 use Unicode::Normalize;
23
24 MARC::Charset->assume_unicode(1);
25
26 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
27 my $schema = $_TD->{table_schema};
28 my $rec_id = $_TD->{new}{id};
29
30 # Short-circuit if maintaining control numbers per MARC21 spec is not enabled
31 my $enable = spi_exec_query("SELECT enabled FROM config.global_flag WHERE name = 'cat.maintain_control_numbers'");
32 if (!($enable->{processed}) or $enable->{rows}[0]->{enabled} eq 'f') {
33     return;
34 }
35
36 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
37 my $ou_cni = 'EVRGRN';
38
39 my $owner;
40 if ($schema eq 'serial') {
41     $owner = $_TD->{new}{owning_lib};
42 } else {
43     # are.owner and bre.owner can be null, so fall back to the consortial setting
44     $owner = $_TD->{new}{owner} || 1;
45 }
46
47 my $ous_rv = spi_exec_query("SELECT value FROM actor.org_unit_ancestor_setting('cat.marc_control_number_identifier', $owner)");
48 if ($ous_rv->{processed}) {
49     $ou_cni = $ous_rv->{rows}[0]->{value};
50     $ou_cni =~ s/"//g; # Stupid VIM syntax highlighting"
51 } else {
52     # Fall back to the shortname of the OU if there was no OU setting
53     $ous_rv = spi_exec_query("SELECT shortname FROM actor.org_unit WHERE id = $owner");
54     if ($ous_rv->{processed}) {
55         $ou_cni = $ous_rv->{rows}[0]->{shortname};
56     }
57 }
58
59 my ($create, $munge) = (0, 0);
60
61 my @scns = $record->field('035');
62
63 foreach my $id_field ('001', '003') {
64     my $spec_value;
65     my @controls = $record->field($id_field);
66
67     if ($id_field eq '001') {
68         $spec_value = $rec_id;
69     } else {
70         $spec_value = $ou_cni;
71     }
72
73     # Create the 001/003 if none exist
74     if (scalar(@controls) == 1) {
75         # Only one field; check to see if we need to munge it
76         unless (grep $_->data() eq $spec_value, @controls) {
77             $munge = 1;
78         }
79     } else {
80         # Delete the other fields, as with more than 1 001/003 we do not know which 003/001 to match
81         foreach my $control (@controls) {
82             $record->delete_field($control);
83         }
84         $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value));
85         $create = 1;
86     }
87 }
88
89 my $cn = $record->field('001')->data();
90 # Special handling of OCLC numbers, often found in records that lack 003
91 if ($cn =~ /^o(c[nm]|n)\d/) {
92     $cn =~ s/^o(c[nm]|n)0*(\d+)/$2/;
93     $record->field('003')->data('OCoLC');
94     $create = 0;
95 }
96
97 # Now, if we need to munge the 001, we will first push the existing 001/003
98 # into the 035; but if the record did not have one (and one only) 001 and 003
99 # to begin with, skip this process
100 if ($munge and not $create) {
101
102     my $scn = "(" . $record->field('003')->data() . ")" . $cn;
103
104     # Do not create duplicate 035 fields
105     unless (grep $_->subfield('a') eq $scn, @scns) {
106         $record->insert_fields_ordered(MARC::Field->new('035', '', '', 'a' => $scn));
107     }
108 }
109
110 # Set the 001/003 and update the MARC
111 if ($create or $munge) {
112     $record->field('001')->data($rec_id);
113     $record->field('003')->data($ou_cni);
114
115     my $xml = $record->as_xml_record();
116     $xml =~ s/\n//sgo;
117     $xml =~ s/^<\?xml.+\?\s*>//go;
118     $xml =~ s/>\s+</></go;
119     $xml =~ s/\p{Cc}//go;
120
121     # Embed a version of OpenILS::Application::AppUtils->entityize()
122     # to avoid having to set PERL5LIB for PostgreSQL as well
123
124     $xml = NFC($xml);
125
126     # Convert raw ampersands to entities
127     $xml =~ s/&(?!\S+;)/&amp;/gso;
128
129     # Convert Unicode characters to entities
130     $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
131
132     $xml =~ s/[\x00-\x1f]//go;
133     $_TD->{new}{marc} = $xml;
134
135     return "MODIFY";
136 }
137
138 return;
139 $func$ LANGUAGE PLPERLU;
140
141 -- re-create the triggers
142 CREATE TRIGGER c_maintain_control_numbers BEFORE INSERT OR UPDATE ON serial.record_entry FOR EACH ROW EXECUTE PROCEDURE evergreen.maintain_control_numbers();
143 CREATE TRIGGER c_maintain_control_numbers BEFORE INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE evergreen.maintain_control_numbers();
144 CREATE TRIGGER c_maintain_control_numbers BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE evergreen.maintain_control_numbers();
145
146 COMMIT;