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