3 INSERT INTO config.upgrade_log (version) VALUES ('0469'); -- miker
5 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT, BIGINT ) RETURNS TEXT AS $func$
8 use MARC::File::XML (BinaryEncoding => 'UTF-8');
11 my $r = MARC::Record->new_from_xml( $xml );
13 return undef unless ($r);
15 my $id = shift() || $r->subfield( '901' => 'c' );
16 $id =~ s/^\s*(?:\([^)]+\))?\s*(.+)\s*?$/$1/;
17 return undef unless ($id); # We need an ID!
19 my $tmpl = MARC::Record->new();
20 $tmpl->encoding( 'UTF-8' );
23 for my $field ( $r->field( '1..' ) ) { # Get main entry fields from the authority record
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;
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/;
46 # Dummy up the bib-side data
49 MARC::Field->new( $_, $i1, $i2, @data )
53 # Construct some 'replace' rules
54 push @rule_fields, map { $_ . $sf . '[0~\)' .$id . '$]' } @replace_them;
57 # Insert the replace rules into the template
59 MARC::Field->new( '905' => ' ' => ' ' => 'r' => join(',', @rule_fields ) )
62 $xml = $tmpl->as_xml_record;
63 $xml =~ s/^<\?.+?\?>$//mo;
65 $xml =~ s/>\s+</></sgo;
69 $func$ LANGUAGE PLPERLU;
71 CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
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);
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 );