]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0321.schema.only-use-901-for-authority-linking.sql
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0321.schema.only-use-901-for-authority-linking.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0321');
4
5 -- Function to generate an ephemeral overlay template from an authority record
6 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT, BIGINT ) RETURNS TEXT AS $func$
7
8     use MARC::Record;
9     use MARC::File::XML;
10
11     my $xml = shift;
12     my $r = MARC::Record->new_from_xml( $xml );
13
14     return undef unless ($r);
15
16     my $id = shift() || $r->subfield( '901' => 'c' );
17     $id =~ s/^\s*(?:\([^)]+\))?\s*(.+)\s*?$/$1/;
18     return undef unless ($id); # We need an ID!
19
20     my $tmpl = MARC::Record->new();
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
65     # Leave formatting intact for now
66     #$xml =~ s/\n//sgo;
67     #$xml =~ s/>\s+</></sgo;
68
69     return $xml;
70
71 $func$ LANGUAGE PLPERLU;
72
73 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( BIGINT ) RETURNS TEXT AS $func$
74     SELECT authority.generate_overlay_template( marc, id ) FROM authority.record_entry WHERE id = $1;
75 $func$ LANGUAGE SQL;
76
77 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT ) RETURNS TEXT AS $func$
78     SELECT authority.generate_overlay_template( $1, NULL );
79 $func$ LANGUAGE SQL;
80
81
82 COMMIT;
83