]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0724.schema.fix_maintain_901_regex.sql
LP#1917826: tweaks to data update
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0724.schema.fix_maintain_901_regex.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0724', :eg_version); -- denials/gmcharlt
4
5 CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
6 use strict;
7 use MARC::Record;
8 use MARC::File::XML (BinaryEncoding => 'UTF-8');
9 use MARC::Charset;
10 use Encode;
11 use Unicode::Normalize;
12
13 MARC::Charset->assume_unicode(1);
14
15 my $schema = $_TD->{table_schema};
16 my $marc = MARC::Record->new_from_xml($_TD->{new}{marc});
17
18 my @old901s = $marc->field('901');
19 $marc->delete_fields(@old901s);
20
21 if ($schema eq 'biblio') {
22     my $tcn_value = $_TD->{new}{tcn_value};
23
24     # Set TCN value to record ID?
25     my $id_as_tcn = spi_exec_query("
26         SELECT enabled
27         FROM config.global_flag
28         WHERE name = 'cat.bib.use_id_for_tcn'
29     ");
30     if (($id_as_tcn->{processed}) && $id_as_tcn->{rows}[0]->{enabled} eq 't') {
31         $tcn_value = $_TD->{new}{id}; 
32     }
33
34     my $new_901 = MARC::Field->new("901", " ", " ",
35         "a" => $tcn_value,
36         "b" => $_TD->{new}{tcn_source},
37         "c" => $_TD->{new}{id},
38         "t" => $schema
39     );
40
41     if ($_TD->{new}{owner}) {
42         $new_901->add_subfields("o" => $_TD->{new}{owner});
43     }
44
45     if ($_TD->{new}{share_depth}) {
46         $new_901->add_subfields("d" => $_TD->{new}{share_depth});
47     }
48
49     $marc->append_fields($new_901);
50 } elsif ($schema = 'authority') {
51     $marc->append_fields(
52         ["901", " ", " ",
53             "c" => $_TD->{new}{id},
54             "t" => $schema,
55         ]
56     );
57 } elsif ($schema = 'serial') {
58     my $new_901 = MARC::Field->new("901", " ", " ",
59         "c" => $_TD->{new}{id},
60         "t" => $schema,
61         "o" => $_TD->{new}{owning_lib},
62     );
63
64     if ($_TD->{new}{record}) {
65         $new_901->add_subfields("r" => $_TD->{new}{record});
66     }
67
68     $marc->append_fields($new_901);
69 } else {
70     $marc->append_fields(
71         ["901", " ", " ",
72             "c" => $_TD->{new}{id},
73             "t" => $schema,
74         ]
75     );
76 }
77
78 my $xml = $marc->as_xml_record();
79 $xml =~ s/\n//sgo;
80 $xml =~ s/^<\?xml.+\?\s*>//go;
81 $xml =~ s/>\s+</></go;
82 $xml =~ s/\p{Cc}//go;
83
84 # Embed a version of OpenILS::Application::AppUtils->entityize()
85 # to avoid having to set PERL5LIB for PostgreSQL as well
86
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);
90
91 $xml = NFC($xml);
92
93 # Convert raw ampersands to entities
94 $xml =~ s/&(?!\S+;)/&amp;/gso;
95
96 # Convert Unicode characters to entities
97 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
98
99 $xml =~ s/[\x00-\x1f]//go;
100 $_TD->{new}{marc} = $xml;
101
102 return "MODIFY";
103 $func$ LANGUAGE PLPERLU;
104
105 COMMIT;