]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0469.schema.authority-maint-trigger-funcs.sql
LP#1806968 Teach Vandelay to pass correct auth tracker type
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0469.schema.authority-maint-trigger-funcs.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0469'); -- miker
4
5 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT, BIGINT ) RETURNS TEXT AS $func$
6
7     use MARC::Record;
8     use MARC::File::XML (BinaryEncoding => 'UTF-8');
9
10     my $xml = shift;
11     my $r = MARC::Record->new_from_xml( $xml );
12
13     return undef unless ($r);
14
15     my $id = shift() || $r->subfield( '901' => 'c' );
16     $id =~ s/^\s*(?:\([^)]+\))?\s*(.+)\s*?$/$1/;
17     return undef unless ($id); # We need an ID!
18
19     my $tmpl = MARC::Record->new();
20     $tmpl->encoding( 'UTF-8' );
21
22     my @rule_fields;
23     for my $field ( $r->field( '1..' ) ) { # Get main entry fields from the authority record
24
25         my $tag = $field->tag;
26         my $i1 = $field->indicator(1);
27         my $i2 = $field->indicator(2);
28         my $sf = join '', map { $_->[0] } $field->subfields;
29         my @data = map { @$_ } $field->subfields;
30
31         my @replace_them;
32
33         # Map the authority field to bib fields it can control.
34         if ($tag >= 100 and $tag <= 111) {       # names
35             @replace_them = map { $tag + $_ } (0, 300, 500, 600, 700);
36         } elsif ($tag eq '130') {                # uniform title
37             @replace_them = qw/130 240 440 730 830/;
38         } elsif ($tag >= 150 and $tag <= 155) {  # subjects
39             @replace_them = ($tag + 500);
40         } elsif ($tag >= 180 and $tag <= 185) {  # floating subdivisions
41             @replace_them = qw/100 400 600 700 800 110 410 610 710 810 111 411 611 711 811 130 240 440 730 830 650 651 655/;
42         } else {
43             next;
44         }
45
46         # Dummy up the bib-side data
47         $tmpl->append_fields(
48             map {
49                 MARC::Field->new( $_, $i1, $i2, @data )
50             } @replace_them
51         );
52
53         # Construct some 'replace' rules
54         push @rule_fields, map { $_ . $sf . '[0~\)' .$id . '$]' } @replace_them;
55     }
56
57     # Insert the replace rules into the template
58     $tmpl->append_fields(
59         MARC::Field->new( '905' => ' ' => ' ' => 'r' => join(',', @rule_fields ) )
60     );
61
62     $xml = $tmpl->as_xml_record;
63     $xml =~ s/^<\?.+?\?>$//mo;
64     $xml =~ s/\n//sgo;
65     $xml =~ s/>\s+</></sgo;
66
67     return $xml;
68
69 $func$ LANGUAGE PLPERLU;
70
71 CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
72 DECLARE
73     xml_output TEXT;
74     parsed_target TEXT;
75 BEGIN
76     parsed_target := vandelay.strip_field( target_xml, ''); -- this dance normalized the format of the xml for the IF below
77     xml_output := vandelay.strip_field( parsed_target, field);
78
79     IF xml_output <> parsed_target  AND field ~ E'~' THEN
80         -- we removed something, and there was a regexp restriction in the field definition, so proceed
81         xml_output := vandelay.add_field( xml_output, source_xml, field, 1 );
82     ELSIF field !~ E'~' THEN
83         -- No regexp restriction, add the field
84         xml_output := vandelay.add_field( xml_output, source_xml, field, 0 );
85     END IF;
86
87     RETURN xml_output;
88 END;
89 $_$ LANGUAGE PLPGSQL;
90
91 COMMIT;
92