]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0783.schema.enforce_use_id_for_tcn.sql
LP2042879 Shelving Location Groups Admin accessibility
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0783.schema.enforce_use_id_for_tcn.sql
1 -- Evergreen DB patch 0783.schema.enforce_use_id_for_tcn.sql
2 --
3 -- Sets the TCN value in the biblio.record_entry row to bib ID,
4 -- if the appropriate setting is in place
5 --
6 BEGIN;
7
8 -- check whether patch can be applied
9 SELECT evergreen.upgrade_deps_block_check('0783', :eg_version);
10
11 -- FIXME: add/check SQL statements to perform the upgrade
12 CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
13 use strict;
14 use MARC::Record;
15 use MARC::File::XML (BinaryEncoding => 'UTF-8');
16 use MARC::Charset;
17 use Encode;
18 use Unicode::Normalize;
19
20 MARC::Charset->assume_unicode(1);
21
22 my $schema = $_TD->{table_schema};
23 my $marc = MARC::Record->new_from_xml($_TD->{new}{marc});
24
25 my @old901s = $marc->field('901');
26 $marc->delete_fields(@old901s);
27
28 if ($schema eq 'biblio') {
29     my $tcn_value = $_TD->{new}{tcn_value};
30
31     # Set TCN value to record ID?
32     my $id_as_tcn = spi_exec_query("
33         SELECT enabled
34         FROM config.global_flag
35         WHERE name = 'cat.bib.use_id_for_tcn'
36     ");
37     if (($id_as_tcn->{processed}) && $id_as_tcn->{rows}[0]->{enabled} eq 't') {
38         $tcn_value = $_TD->{new}{id}; 
39         $_TD->{new}{tcn_value} = $tcn_value;
40     }
41
42     my $new_901 = MARC::Field->new("901", " ", " ",
43         "a" => $tcn_value,
44         "b" => $_TD->{new}{tcn_source},
45         "c" => $_TD->{new}{id},
46         "t" => $schema
47     );
48
49     if ($_TD->{new}{owner}) {
50         $new_901->add_subfields("o" => $_TD->{new}{owner});
51     }
52
53     if ($_TD->{new}{share_depth}) {
54         $new_901->add_subfields("d" => $_TD->{new}{share_depth});
55     }
56
57     $marc->append_fields($new_901);
58 } elsif ($schema eq 'authority') {
59     my $new_901 = MARC::Field->new("901", " ", " ",
60         "c" => $_TD->{new}{id},
61         "t" => $schema,
62     );
63     $marc->append_fields($new_901);
64 } elsif ($schema eq 'serial') {
65     my $new_901 = MARC::Field->new("901", " ", " ",
66         "c" => $_TD->{new}{id},
67         "t" => $schema,
68         "o" => $_TD->{new}{owning_lib},
69     );
70
71     if ($_TD->{new}{record}) {
72         $new_901->add_subfields("r" => $_TD->{new}{record});
73     }
74
75     $marc->append_fields($new_901);
76 } else {
77     my $new_901 = MARC::Field->new("901", " ", " ",
78         "c" => $_TD->{new}{id},
79         "t" => $schema,
80     );
81     $marc->append_fields($new_901);
82 }
83
84 my $xml = $marc->as_xml_record();
85 $xml =~ s/\n//sgo;
86 $xml =~ s/^<\?xml.+\?\s*>//go;
87 $xml =~ s/>\s+</></go;
88 $xml =~ s/\p{Cc}//go;
89
90 # Embed a version of OpenILS::Application::AppUtils->entityize()
91 # to avoid having to set PERL5LIB for PostgreSQL as well
92
93 # If we are going to convert non-ASCII characters to XML entities,
94 # we had better be dealing with a UTF8 string to begin with
95 $xml = decode_utf8($xml);
96
97 $xml = NFC($xml);
98
99 # Convert raw ampersands to entities
100 $xml =~ s/&(?!\S+;)/&amp;/gso;
101
102 # Convert Unicode characters to entities
103 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
104
105 $xml =~ s/[\x00-\x1f]//go;
106 $_TD->{new}{marc} = $xml;
107
108 return "MODIFY";
109 $func$ LANGUAGE PLPERLU;
110
111
112 COMMIT;