]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0464.function.maintain_control_numbers.sql
LP#1117808: Stamping upgrade scripts for extend use of merge profiles
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0464.function.maintain_control_numbers.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0464'); -- dbs
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 Encode;
10 use Unicode::Normalize;
11
12 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
13 my $schema = $_TD->{table_schema};
14 my $rec_id = $_TD->{new}{id};
15
16 # Short-circuit if maintaining control numbers per MARC21 spec is not enabled
17 my $enable = spi_exec_query("SELECT enabled FROM config.global_flag WHERE name = 'cat.maintain_control_numbers'");
18 if (!($enable->{processed}) or $enable->{rows}[0]->{enabled} eq 'f') {
19     return;
20 }
21
22 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
23 my $ou_cni = 'EVRGRN';
24
25 my $owner;
26 if ($schema eq 'serial') {
27     $owner = $_TD->{new}{owning_lib};
28 } else {
29     # are.owner and bre.owner can be null, so fall back to the consortial setting
30     $owner = $_TD->{new}{owner} || 1;
31 }
32
33 my $ous_rv = spi_exec_query("SELECT value FROM actor.org_unit_ancestor_setting('cat.marc_control_number_identifier', $owner)");
34 if ($ous_rv->{processed}) {
35     $ou_cni = $ous_rv->{rows}[0]->{value};
36     $ou_cni =~ s/"//g; # Stupid VIM syntax highlighting"
37 } else {
38     # Fall back to the shortname of the OU if there was no OU setting
39     $ous_rv = spi_exec_query("SELECT shortname FROM actor.org_unit WHERE id = $owner");
40     if ($ous_rv->{processed}) {
41         $ou_cni = $ous_rv->{rows}[0]->{shortname};
42     }
43 }
44
45 my ($create, $munge) = (0, 0);
46
47 # Incoming MARC records may have multiple 001s or 003s, despite the spec
48 my @control_ids = $record->field('003');
49 my @scns = $record->field('035');
50
51 foreach my $id_field ('001', '003') {
52     my $spec_value;
53     my @controls = $record->field($id_field);
54
55     if ($id_field eq '001') {
56         $spec_value = $rec_id;
57     } else {
58         $spec_value = $ou_cni;
59     }
60
61     # Create the 001/003 if none exist
62     if (scalar(@controls) == 0) {
63         $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value));
64         $create = 1;
65     } elsif (scalar(@controls) > 1) {
66         # Do we already have the right 001/003 value in the existing set?
67         unless (grep $_->data() eq $spec_value, @controls) {
68             $munge = 1;
69         }
70
71         # Delete the other fields, as with more than 1 001/003 we do not know which 003/001 to match
72         foreach my $control (@controls) {
73             unless ($control->data() eq $spec_value) {
74                 $record->delete_field($control);
75             }
76         }
77     } else {
78         # Only one field; check to see if we need to munge it
79         unless (grep $_->data() eq $spec_value, @controls) {
80             $munge = 1;
81         }
82     }
83 }
84
85 # Now, if we need to munge the 001, we will first push the existing 001/003 into the 035;
86 # but if the record did not have a 003 to begin with, skip this process
87 if ($munge && scalar(@control_ids) > 0) {
88     my $scn = "(" . $record->field('003')->data() . ")" . $record->field('001')->data();
89
90     # Do not create duplicate 035 fields
91     unless (grep $_->subfield('a') eq $scn, @scns) {
92         $record->insert_fields_ordered(MARC::Field->new('035', '', '', 'a' => $scn));
93     }
94 }
95
96 # Set the 001/003 and update the MARC
97 if ($create or $munge) {
98     $record->field('001')->data($rec_id);
99     $record->field('003')->data($ou_cni);
100
101     my $xml = $record->as_xml_record();
102     $xml =~ s/\n//sgo;
103     $xml =~ s/^<\?xml.+\?\s*>//go;
104     $xml =~ s/>\s+</></go;
105     $xml =~ s/\p{Cc}//go;
106
107     # Embed a version of OpenILS::Application::AppUtils->entityize()
108     # to avoid having to set PERL5LIB for PostgreSQL as well
109
110     # If we are going to convert non-ASCII characters to XML entities,
111     # we had better be dealing with a UTF8 string to begin with
112     $xml = decode_utf8($xml);
113
114     $xml = NFC($xml);
115
116     # Convert raw ampersands to entities
117     $xml =~ s/&(?!\S+;)/&amp;/gso;
118
119     # Convert Unicode characters to entities
120     $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
121
122     $xml =~ s/[\x00-\x1f]//go;
123     $_TD->{new}{marc} = $xml;
124
125     return "MODIFY";
126 }
127
128 return;
129 $func$ LANGUAGE PLPERLU;
130
131 COMMIT;