]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0988.schema.bib_source_in_901.sql
LP#1643709: Stamping upgrade scripts
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0988.schema.bib_source_in_901.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('0988', :eg_version);
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         $_TD->{new}{tcn_value} = $tcn_value;
33     }
34
35     my $new_901 = MARC::Field->new("901", " ", " ",
36         "a" => $tcn_value,
37         "b" => $_TD->{new}{tcn_source},
38         "c" => $_TD->{new}{id},
39         "t" => $schema
40     );
41
42     if ($_TD->{new}{owner}) {
43         $new_901->add_subfields("o" => $_TD->{new}{owner});
44     }
45
46     if ($_TD->{new}{share_depth}) {
47         $new_901->add_subfields("d" => $_TD->{new}{share_depth});
48     }
49
50     if ($_TD->{new}{source}) {
51         my $plan = spi_prepare('
52             SELECT source
53             FROM config.bib_source
54             WHERE id = $1
55         ', 'INTEGER');
56         my $source_name =
57             spi_exec_prepared($plan, {limit => 1}, $_TD->{new}{source})->{rows}[0]{source};
58         spi_freeplan($plan);
59         $new_901->add_subfields("s" => $source_name) if $source_name;
60     }
61
62     $marc->append_fields($new_901);
63 } elsif ($schema eq 'authority') {
64     my $new_901 = MARC::Field->new("901", " ", " ",
65         "c" => $_TD->{new}{id},
66         "t" => $schema,
67     );
68     $marc->append_fields($new_901);
69 } elsif ($schema eq 'serial') {
70     my $new_901 = MARC::Field->new("901", " ", " ",
71         "c" => $_TD->{new}{id},
72         "t" => $schema,
73         "o" => $_TD->{new}{owning_lib},
74     );
75
76     if ($_TD->{new}{record}) {
77         $new_901->add_subfields("r" => $_TD->{new}{record});
78     }
79
80     $marc->append_fields($new_901);
81 } else {
82     my $new_901 = MARC::Field->new("901", " ", " ",
83         "c" => $_TD->{new}{id},
84         "t" => $schema,
85     );
86     $marc->append_fields($new_901);
87 }
88
89 my $xml = $marc->as_xml_record();
90 $xml =~ s/\n//sgo;
91 $xml =~ s/^<\?xml.+\?\s*>//go;
92 $xml =~ s/>\s+</></go;
93 $xml =~ s/\p{Cc}//go;
94
95 # Embed a version of OpenILS::Application::AppUtils->entityize()
96 # to avoid having to set PERL5LIB for PostgreSQL as well
97
98 $xml = NFC($xml);
99
100 # Convert raw ampersands to entities
101 $xml =~ s/&(?!\S+;)/&amp;/gso;
102
103 # Convert Unicode characters to entities
104 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
105
106 $xml =~ s/[\x00-\x1f]//go;
107 $_TD->{new}{marc} = $xml;
108
109 return "MODIFY";
110 $func$ LANGUAGE PLPERLU;
111
112 COMMIT;
113
114 \qecho Now running an update to set the 901$s for bibliographic
115 \qecho records that have a source set. This may take a while.
116 \qecho
117 \qecho The update can be cancelled now \qecho and run later
118 \qecho using the following SQL statement:
119 \qecho
120 \qecho UPDATE biblio.record_entry SET id = id WHERE source IS NOT NULL;
121 \qecho
122 UPDATE biblio.record_entry SET id = id WHERE source IS NOT NULL;