3 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0726', :eg_version); -- denials
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 eq 'authority') {
51 my $new_901 = MARC::Field->new("901", " ", " ",
52 "c" => $_TD->{new}{id},
55 $marc->append_fields($new_901);
56 } elsif ($schema eq 'serial') {
57 my $new_901 = MARC::Field->new("901", " ", " ",
58 "c" => $_TD->{new}{id},
60 "o" => $_TD->{new}{owning_lib},
63 if ($_TD->{new}{record}) {
64 $new_901->add_subfields("r" => $_TD->{new}{record});
67 $marc->append_fields($new_901);
69 my $new_901 = MARC::Field->new("901", " ", " ",
70 "c" => $_TD->{new}{id},
73 $marc->append_fields($new_901);
76 my $xml = $marc->as_xml_record();
78 $xml =~ s/^<\?xml.+\?\s*>//go;
79 $xml =~ s/>\s+</></go;
82 # Embed a version of OpenILS::Application::AppUtils->entityize()
83 # to avoid having to set PERL5LIB for PostgreSQL as well
85 # If we are going to convert non-ASCII characters to XML entities,
86 # we had better be dealing with a UTF8 string to begin with
87 $xml = decode_utf8($xml);
91 # Convert raw ampersands to entities
92 $xml =~ s/&(?!\S+;)/&/gso;
94 # Convert Unicode characters to entities
95 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
97 $xml =~ s/[\x00-\x1f]//go;
98 $_TD->{new}{marc} = $xml;
101 $func$ LANGUAGE PLPERLU;