3 SELECT evergreen.upgrade_deps_block_check('1077', :eg_version); -- csharp/gmcharlt
5 -- if the "public" version of this function exists, drop it to prevent confusion/trouble
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;
12 DROP FUNCTION IF EXISTS public.maintain_control_numbers();
14 -- create the function within the "evergreen" schema
16 CREATE OR REPLACE FUNCTION evergreen.maintain_control_numbers() RETURNS TRIGGER AS $func$
19 use MARC::File::XML (BinaryEncoding => 'UTF-8');
22 use Unicode::Normalize;
24 MARC::Charset->assume_unicode(1);
26 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
27 my $schema = $_TD->{table_schema};
28 my $rec_id = $_TD->{new}{id};
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') {
36 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
37 my $ou_cni = 'EVRGRN';
40 if ($schema eq 'serial') {
41 $owner = $_TD->{new}{owning_lib};
43 # are.owner and bre.owner can be null, so fall back to the consortial setting
44 $owner = $_TD->{new}{owner} || 1;
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"
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};
59 my ($create, $munge) = (0, 0);
61 my @scns = $record->field('035');
63 foreach my $id_field ('001', '003') {
65 my @controls = $record->field($id_field);
67 if ($id_field eq '001') {
68 $spec_value = $rec_id;
70 $spec_value = $ou_cni;
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) {
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);
84 $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value));
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');
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) {
102 my $scn = "(" . $record->field('003')->data() . ")" . $cn;
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));
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);
115 my $xml = $record->as_xml_record();
117 $xml =~ s/^<\?xml.+\?\s*>//go;
118 $xml =~ s/>\s+</></go;
119 $xml =~ s/\p{Cc}//go;
121 # Embed a version of OpenILS::Application::AppUtils->entityize()
122 # to avoid having to set PERL5LIB for PostgreSQL as well
126 # Convert raw ampersands to entities
127 $xml =~ s/&(?!\S+;)/&/gso;
129 # Convert Unicode characters to entities
130 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
132 $xml =~ s/[\x00-\x1f]//go;
133 $_TD->{new}{marc} = $xml;
139 $func$ LANGUAGE PLPERLU;
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();