3 INSERT INTO config.upgrade_log (version) VALUES ('0329'); -- dbs
5 ALTER TABLE authority.record_entry ADD COLUMN owner INT;
6 ALTER TABLE serial.record_entry ADD COLUMN owner INT;
8 INSERT INTO config.global_flag (name, label) -- defaults to enabled=FALSE
10 'cat.maintain_control_numbers',
12 'cat.maintain_control_numbers',
13 'Cat: Maintain 001/003/035 according to the MARC21 specification',
19 INSERT INTO config.org_unit_setting_type ( name, label, description, datatype ) VALUES (
20 'cat.marc_control_number_identifier',
22 'cat.marc_control_number_identifier',
23 'Cat: Defines the control number identifier used in 003 and 035 fields.',
27 'cat.marc_control_number_identifier',
28 'Cat: Defines the control number identifier used in 003 and 035 fields.',
34 CREATE OR REPLACE FUNCTION maintain_control_numbers() RETURNS TRIGGER AS $func$
39 use Unicode::Normalize;
41 my $record = MARC::Record->new_from_xml($_TD->{new}{marc});
42 my $schema = $_TD->{table_schema};
43 my $rec_id = $_TD->{new}{id};
45 # Short-circuit if maintaining control numbers per MARC21 spec is not enabled
46 my $enable = spi_exec_query("SELECT enabled FROM config.global_flag WHERE name = 'cat.maintain_control_numbers'");
47 if (!($enable->{processed}) or $enable->{rows}[0]->{enabled} eq 'f') {
51 # Get the control number identifier from an OU setting based on $_TD->{new}{owner}
52 my $ou_cni = 'EVRGRN';
54 # bre.owner can be null, so fall back to the consortial setting
55 my $owner = $_TD->{new}{owner} || 1;
57 my $ous_rv = spi_exec_query("SELECT value FROM actor.org_unit_ancestor_setting('cat.marc_control_number_identifier', $owner)");
58 if ($ous_rv->{processed}) {
59 $ou_cni = $ous_rv->{rows}[0]->{value};
60 $ou_cni =~ s/"//g; # Stupid VIM syntax highlighting"
62 # Fall back to the shortname of the OU if there was no OU setting
63 $ous_rv = spi_exec_query("SELECT shortname FROM actor.org_unit WHERE id = $owner");
64 if ($ous_rv->{processed}) {
65 $ou_cni = $ous_rv->{rows}[0]->{shortname};
69 my ($create, $munge) = (0, 0);
70 my ($orig_001, $orig_003) = ('', '');
72 # Incoming MARC records may have multiple 001s or 003s, despite the spec
73 my @control_ids = $record->field('003');
74 my @scns = $record->field('035');
76 foreach my $id_field ('001', '003') {
78 my @controls = $record->field($id_field);
80 if ($id_field eq '001') {
81 $spec_value = $rec_id;
83 $spec_value = $ou_cni;
86 # Create the 001/003 if none exist
87 if (scalar(@controls) == 0) {
88 $record->insert_fields_ordered(MARC::Field->new($id_field, $spec_value));
90 } elsif (scalar(@controls) > 1) {
91 # Do we already have the right 001/003 value in the existing set?
92 unless (grep $_->data() eq $spec_value, @controls) {
96 # Delete the other fields, as with more than 1 001/003 we do not know which 003/001 to match
97 foreach my $control (@controls) {
98 unless ($control->data() eq $spec_value) {
99 $record->delete_field($control);
103 # Only one field; check to see if we need to munge it
104 unless (grep $_->data() eq $spec_value, @controls) {
110 # Now, if we need to munge the 001, we will first push the existing 001/003 into the 035
112 my $scn = "(" . $record->field('003')->data() . ")" . $record->field('001')->data();
114 # Do not create duplicate 035 fields
115 unless (grep $_->subfield('a') eq $scn, @scns) {
116 $record->insert_fields_ordered(MARC::Field->new('035', '', '', 'a' => $scn));
120 # Set the 001/003 and update the MARC
121 if ($create or $munge) {
122 $record->field('001')->data($rec_id);
123 $record->field('003')->data($ou_cni);
125 my $xml = $record->as_xml_record();
127 $xml =~ s/^<\?xml.+\?\s*>//go;
128 $xml =~ s/>\s+</></go;
129 $xml =~ s/\p{Cc}//go;
131 # Embed a version of OpenILS::Application::AppUtils->entityize()
132 # to avoid having to set PERL5LIB for PostgreSQL as well
134 # If we are going to convert non-ASCII characters to XML entities,
135 # we had better be dealing with a UTF8 string to begin with
136 $xml = decode_utf8($xml);
140 # Convert raw ampersands to entities
141 $xml =~ s/&(?!\S+;)/&/gso;
143 # Convert Unicode characters to entities
144 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
146 $xml =~ s/[\x00-\x1f]//go;
147 $_TD->{new}{marc} = $xml;
153 $func$ LANGUAGE PLPERLU;
155 CREATE TRIGGER c_maintain_control_numbers BEFORE INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE maintain_control_numbers();
156 CREATE TRIGGER c_maintain_control_numbers BEFORE INSERT OR UPDATE ON biblio.record_entry FOR EACH ROW EXECUTE PROCEDURE maintain_control_numbers();
157 CREATE TRIGGER c_maintain_control_numbers BEFORE INSERT OR UPDATE ON serial.record_entry FOR EACH ROW EXECUTE PROCEDURE maintain_control_numbers();