From e0d62ba6f9cc6ab380bdb809780ef126f4b5b9cf Mon Sep 17 00:00:00 2001 From: miker Date: Fri, 25 Jun 2010 14:47:56 +0000 Subject: [PATCH] vandelay.add_field bug fix; add stored procs to 1) create an authority-based overlay template and 2) allow simple application of said template directly with marcxml git-svn-id: svn://svn.open-ils.org/ILS/trunk@16812 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/sql/Pg/002.schema.config.sql | 2 +- Open-ILS/src/sql/Pg/012.schema.vandelay.sql | 40 ++++- ...16.schema.authority-bib-overlay-stored-proc.sql | 186 +++++++++++++++++++++ 3 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0316.schema.authority-bib-overlay-stored-proc.sql diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index 42ad02f8da..da2204e3aa 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -68,7 +68,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0315'); -- miker +INSERT INTO config.upgrade_log (version) VALUES ('0316'); -- miker CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/012.schema.vandelay.sql b/Open-ILS/src/sql/Pg/012.schema.vandelay.sql index 16f6dbed46..b9f7bca962 100644 --- a/Open-ILS/src/sql/Pg/012.schema.vandelay.sql +++ b/Open-ILS/src/sql/Pg/012.schema.vandelay.sql @@ -342,7 +342,7 @@ CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT } for my $f ( keys %fields) { - if ( @{$fields{$f}} ) { + if ( @{$fields{$f}{sf}} ) { for my $from_field ($source_r->field( $f )) { for my $to_field ($target_r->field( $f )) { if (exists($fields{$f}{match})) { @@ -543,6 +543,44 @@ BEGIN END; $$ LANGUAGE PLPGSQL; +CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_marc TEXT, template_marc TEXT ) RETURNS TEXT AS $$ +DECLARE + dyn_profile vandelay.compile_profile%ROWTYPE; + replace_rule TEXT; + tmp_marc TEXT; + trgt_marc TEXT; + tmpl_marc TEXT; + match_count INT; +BEGIN + + IF target_marc IS NULL OR template_marc IS NULL THEN + -- RAISE NOTICE 'no marc for target or template record'; + RETURN NULL; + END IF; + + dyn_profile := vandelay.compile_profile( template_marc ); + + IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN + -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule; + RETURN NULL; + END IF; + + IF dyn_profile.replace_rule <> '' THEN + trgt_marc = target_marc; + tmpl_marc = template_marc; + replace_rule = dyn_profile.replace_rule; + ELSE + tmp_marc = target_marc; + trgt_marc = template_marc; + tmpl_marc = tmp_marc; + replace_rule = dyn_profile.preserve_rule; + END IF; + + RETURN vandelay.merge_record_xml( trgt_marc, tmpl_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule ); + +END; +$$ LANGUAGE PLPGSQL; + CREATE OR REPLACE FUNCTION vandelay.template_overlay_bib_record ( v_marc TEXT, eg_id BIGINT) RETURNS BOOL AS $$ SELECT vandelay.template_overlay_bib_record( $1, $2, NULL); $$ LANGUAGE SQL; diff --git a/Open-ILS/src/sql/Pg/upgrade/0316.schema.authority-bib-overlay-stored-proc.sql b/Open-ILS/src/sql/Pg/upgrade/0316.schema.authority-bib-overlay-stored-proc.sql new file mode 100644 index 0000000000..66a184a92f --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0316.schema.authority-bib-overlay-stored-proc.sql @@ -0,0 +1,186 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0316'); + +-- Function that takes, and returns, marcxml and compiles an embedded ruleset for you, and they applys it +CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_marc TEXT, template_marc TEXT ) RETURNS TEXT AS $$ +DECLARE + dyn_profile vandelay.compile_profile%ROWTYPE; + replace_rule TEXT; + tmp_marc TEXT; + trgt_marc TEXT; + tmpl_marc TEXT; + match_count INT; +BEGIN + + IF target_marc IS NULL OR template_marc IS NULL THEN + -- RAISE NOTICE 'no marc for target or template record'; + RETURN NULL; + END IF; + + dyn_profile := vandelay.compile_profile( template_marc ); + + IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN + -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule; + RETURN NULL; + END IF; + + IF dyn_profile.replace_rule <> '' THEN + trgt_marc = target_marc; + tmpl_marc = template_marc; + replace_rule = dyn_profile.replace_rule; + ELSE + tmp_marc = target_marc; + trgt_marc = template_marc; + tmpl_marc = tmp_marc; + replace_rule = dyn_profile.preserve_rule; + END IF; + + RETURN vandelay.merge_record_xml( trgt_marc, tmpl_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule ); + +END; +$$ LANGUAGE PLPGSQL; + +-- Fix a bug in this, for subfield-specific additions +CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$ + + use MARC::Record; + use MARC::File::XML; + + my $target_xml = shift; + my $source_xml = shift; + my $field_spec = shift; + + my $target_r = MARC::Record->new_from_xml( $target_xml ); + my $source_r = MARC::Record->new_from_xml( $source_xml ); + + return $target_xml unless ($target_r && $source_r); + + my @field_list = split(',', $field_spec); + + my %fields; + for my $f (@field_list) { + $f =~ s/^\s*//; $f =~ s/\s*$//; + if ($f =~ /^(.{3})(\w*)(?:\[([^]]*)\])?$/) { + my $field = $1; + $field =~ s/\s+//; + my $sf = $2; + $sf =~ s/\s+//; + my $match = $3; + $match =~ s/^\s*//; $match =~ s/\s*$//; + $fields{$field} = { sf => [ split('', $sf) ] }; + if ($match) { + my ($msf,$mre) = split('~', $match); + if (length($msf) > 0 and length($mre) > 0) { + $msf =~ s/^\s*//; $msf =~ s/\s*$//; + $mre =~ s/^\s*//; $mre =~ s/\s*$//; + $fields{$field}{match} = { sf => $msf, re => qr/$mre/ }; + } + } + } + } + + for my $f ( keys %fields) { + if ( @{$fields{$f}{sf}} ) { + for my $from_field ($source_r->field( $f )) { + for my $to_field ($target_r->field( $f )) { + if (exists($fields{$f}{match})) { + next unless (grep { $_ =~ $field{$f}{match}{re} } $to_field->subfield($field{$f}{match}{sf})); + } + my @new_sf = map { ($_ => $from_field->subfield($_)) } @{$fields{$f}}; + $to_field->add_subfields( @new_sf ); + } + } + } else { + my @new_fields = map { $_->clone } $source_r->field( $f ); + $target_r->insert_fields_ordered( @new_fields ); + } + } + + $target_xml = $target_r->as_xml_record; + $target_xml =~ s/^<\?.+?\?>$//mo; + $target_xml =~ s/\n//sgo; + $target_xml =~ s/>\s+new_from_xml( $xml ); + + return undef unless ($r); + + my $id = shift() || $r->subfield( '035' => 'a' ) || $r->subfield( '901' => 'c' ); + $id =~ s/^\s*(?:\([^)]+\))?\s*(.+)\s*?$/$1/; + return undef unless ($id); # We need an ID! + + my $tmpl = MARC::Record->new(); + + my @rule_fields; + for my $field ( $r->field( '1..' ) ) { # Get main entry fields from the authority record + + my $tag = $field->tag; + my $i1 = $field->indicator(1); + my $i2 = $field->indicator(2); + my $sf = join '', map { $_->[0] } $field->subfields; + my @data = map { @$_ } $field->subfields; + + my @replace_them; + + # Map the authority field to bib fields it can control. + if ($tag >= 100 and $tag <= 111) { # names + @replace_them = map { $tag + $_ } (0, 300, 500, 600, 700); + } elsif ($tag eq '130') { # uniform title + @replace_them = qw/130 240 440 730 830/; + } elsif ($tag >= 150 and $tag <= 155) { # subjects + @replace_them = ($tag + 500); + } elsif ($tag >= 180 and $tag <= 185) { # floating subdivisions + @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/; + } else { + next; + } + + # Dummy up the bib-side data + $tmpl->append_fields( + map { + MARC::Field->new( $_, $i1, $i2, @data ) + } @replace_them + ); + + # Construct some 'replace' rules + push @rule_fields, map { $_ . $sf . '[0~\)' .$id . '$]' } @replace_them; + } + + # Insert the replace rules into the template + $tmpl->append_fields( + MARC::Field->new( '905' => ' ' => ' ' => 'r' => join(',', @rule_fields ) ) + ); + + $xml = $tmpl->as_xml_record; + $xml =~ s/^<\?.+?\?>$//mo; + + # Leave formatting intact for now + #$xml =~ s/\n//sgo; + #$xml =~ s/>\s+