3 INSERT INTO config.upgrade_log (version) VALUES ('0321');
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$
12 my $r = MARC::Record->new_from_xml( $xml );
14 return undef unless ($r);
16 my $id = shift() || $r->subfield( '901' => 'c' );
17 $id =~ s/^\s*(?:\([^)]+\))?\s*(.+)\s*?$/$1/;
18 return undef unless ($id); # We need an ID!
20 my $tmpl = MARC::Record->new();
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 # Leave formatting intact for now
67 #$xml =~ s/>\s+</></sgo;
71 $func$ LANGUAGE PLPERLU;
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;
77 CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT ) RETURNS TEXT AS $func$
78 SELECT authority.generate_overlay_template( $1, NULL );