3 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0724', :eg_version); -- denials/gmcharlt
5 CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
8 use MARC::File::XML (BinaryEncoding => 'UTF-8');
11 use Unicode::Normalize;
13 MARC::Charset->assume_unicode(1);
15 my $schema = $_TD->{table_schema};
16 my $marc = MARC::Record->new_from_xml($_TD->{new}{marc});
18 my @old901s = $marc->field('901');
19 $marc->delete_fields(@old901s);
21 if ($schema eq 'biblio') {
22 my $tcn_value = $_TD->{new}{tcn_value};
24 # Set TCN value to record ID?
25 my $id_as_tcn = spi_exec_query("
27 FROM config.global_flag
28 WHERE name = 'cat.bib.use_id_for_tcn'
30 if (($id_as_tcn->{processed}) && $id_as_tcn->{rows}[0]->{enabled} eq 't') {
31 $tcn_value = $_TD->{new}{id};
34 my $new_901 = MARC::Field->new("901", " ", " ",
36 "b" => $_TD->{new}{tcn_source},
37 "c" => $_TD->{new}{id},
41 if ($_TD->{new}{owner}) {
42 $new_901->add_subfields("o" => $_TD->{new}{owner});
45 if ($_TD->{new}{share_depth}) {
46 $new_901->add_subfields("d" => $_TD->{new}{share_depth});
49 $marc->append_fields($new_901);
50 } elsif ($schema = 'authority') {
53 "c" => $_TD->{new}{id},
57 } elsif ($schema = 'serial') {
58 my $new_901 = MARC::Field->new("901", " ", " ",
59 "c" => $_TD->{new}{id},
61 "o" => $_TD->{new}{owning_lib},
64 if ($_TD->{new}{record}) {
65 $new_901->add_subfields("r" => $_TD->{new}{record});
68 $marc->append_fields($new_901);
72 "c" => $_TD->{new}{id},
78 my $xml = $marc->as_xml_record();
80 $xml =~ s/^<\?xml.+\?\s*>//go;
81 $xml =~ s/>\s+</></go;
84 # Embed a version of OpenILS::Application::AppUtils->entityize()
85 # to avoid having to set PERL5LIB for PostgreSQL as well
87 # If we are going to convert non-ASCII characters to XML entities,
88 # we had better be dealing with a UTF8 string to begin with
89 $xml = decode_utf8($xml);
93 # Convert raw ampersands to entities
94 $xml =~ s/&(?!\S+;)/&/gso;
96 # Convert Unicode characters to entities
97 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
99 $xml =~ s/[\x00-\x1f]//go;
100 $_TD->{new}{marc} = $xml;
103 $func$ LANGUAGE PLPERLU;