3 SELECT evergreen.upgrade_deps_block_check('0742', :eg_version);
5 -- Prepare for the July 2013 introduction of OCLC's "on" prefix
8 CREATE OR REPLACE FUNCTION maintain_control_numbers() RETURNS TRIGGER AS $func$
11 use MARC::File::XML (BinaryEncoding => 'UTF-8');
14 use Unicode::Normalize;
16 MARC::Charset->assume_unicode(1);
18 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
19 my $schema = $_TD->{table_schema};
20 my $rec_id = $_TD->{new}{id};
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') {
28 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
29 my $ou_cni = 'EVRGRN';
32 if ($schema eq 'serial') {
33 $owner = $_TD->{new}{owning_lib};
35 # are.owner and bre.owner can be null, so fall back to the consortial setting
36 $owner = $_TD->{new}{owner} || 1;
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"
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};
51 my ($create, $munge) = (0, 0);
53 my @scns = $record->field('035');
55 foreach my $id_field ('001', '003') {
57 my @controls = $record->field($id_field);
59 if ($id_field eq '001') {
60 $spec_value = $rec_id;
62 $spec_value = $ou_cni;
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) {
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);
76 $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value));
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');
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) {
94 my $scn = "(" . $record->field('003')->data() . ")" . $cn;
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));
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);
107 my $xml = $record->as_xml_record();
109 $xml =~ s/^<\?xml.+\?\s*>//go;
110 $xml =~ s/>\s+</></go;
111 $xml =~ s/\p{Cc}//go;
113 # Embed a version of OpenILS::Application::AppUtils->entityize()
114 # to avoid having to set PERL5LIB for PostgreSQL as well
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);
122 # Convert raw ampersands to entities
123 $xml =~ s/&(?!\S+;)/&/gso;
125 # Convert Unicode characters to entities
126 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
128 $xml =~ s/[\x00-\x1f]//go;
129 $_TD->{new}{marc} = $xml;
135 $func$ LANGUAGE PLPERLU;