Fix two bugs:
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 10 Dec 2010 05:03:25 +0000 (05:03 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 10 Dec 2010 05:03:25 +0000 (05:03 +0000)
 * Wide Character warning in authority.generate_overlay_template due to the generated template not being UTF-8 encoded internally
 * Correctly test the same space-normalization form of the pre- and post-strip records during the application of a replace rule in vandelay.replace_field

NOTE: to be backported to rel_2_0 and rel_1_6

git-svn-id: svn://svn.open-ils.org/ILS/trunk@18957 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/sql/Pg/002.schema.config.sql
Open-ILS/src/sql/Pg/011.schema.authority.sql
Open-ILS/src/sql/Pg/012.schema.vandelay.sql
Open-ILS/src/sql/Pg/1.6.1-2.0-upgrade-db.sql
Open-ILS/src/sql/Pg/upgrade/0469.schema.authority-maint-trigger-funcs.sql [new file with mode: 0644]

index 22e3e08..a5dcd7d 100644 (file)
@@ -70,7 +70,7 @@ CREATE TABLE config.upgrade_log (
     install_date    TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
 );
 
-INSERT INTO config.upgrade_log (version) VALUES ('0468'); -- gmc
+INSERT INTO config.upgrade_log (version) VALUES ('0469'); -- miker
 
 CREATE TABLE config.bib_source (
        id              SERIAL  PRIMARY KEY,
index b4691f2..bc11a44 100644 (file)
@@ -132,6 +132,7 @@ CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT, BIGINT )
     return undef unless ($id); # We need an ID!
 
     my $tmpl = MARC::Record->new();
+    $tmpl->encoding( 'UTF-8' );
 
     my @rule_fields;
     for my $field ( $r->field( '1..' ) ) { # Get main entry fields from the authority record
index 9ca7680..769e790 100644 (file)
@@ -444,10 +444,12 @@ $_$ LANGUAGE PLPERLU;
 CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
 DECLARE
     xml_output TEXT;
+    parsed_target TEXT;
 BEGIN
-    xml_output := vandelay.strip_field( target_xml, field);
+    parsed_target := vandelay.strip_field( target_xml, ''); -- this dance normalized the format of the xml for the IF below
+    xml_output := vandelay.strip_field( parsed_target, field);
 
-    IF xml_output <> target_xml  AND field ~ E'~' THEN
+    IF xml_output <> parsed_target  AND field ~ E'~' THEN
         -- we removed something, and there was a regexp restriction in the field definition, so proceed
         xml_output := vandelay.add_field( xml_output, source_xml, field, 1 );
     ELSIF field !~ E'~' THEN
index 8beae96..ed2f588 100644 (file)
@@ -31,7 +31,7 @@ UPDATE biblio.record_entry SET marc = '<record xmlns="http://www.loc.gov/MARC21/
 
 -- Highest-numbered individual upgrade script incorporated herein:
 
-INSERT INTO config.upgrade_log (version) VALUES ('0461');
+INSERT INTO config.upgrade_log (version) VALUES ('0469');
 
 -- Push the auri sequence in case it's out of date
 -- Add 2 as the sequence value must be 1 or higher, and seed is -1
@@ -14217,10 +14217,12 @@ $_$ LANGUAGE PLPERLU;
 CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
 DECLARE
     xml_output TEXT;
+    parsed_target TEXT;
 BEGIN
-    xml_output := vandelay.strip_field( target_xml, field);
+    parsed_target := vandelay.strip_field( target_xml, ''); -- this dance normalized the format of the xml for the IF below
+    xml_output := vandelay.strip_field( parsed_target, field);
 
-    IF xml_output <> target_xml  AND field ~ E'~' THEN
+    IF xml_output <> parsed_target  AND field ~ E'~' THEN
         -- we removed something, and there was a regexp restriction in the field definition, so proceed
         xml_output := vandelay.add_field( xml_output, source_xml, field, 1 );
     ELSIF field !~ E'~' THEN
@@ -16594,6 +16596,7 @@ CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT, BIGINT )
     return undef unless ($id); # We need an ID!
 
     my $tmpl = MARC::Record->new();
+    $tmpl->encoding( 'UTF-8' );
 
     my @rule_fields;
     for my $field ( $r->field( '1..' ) ) { # Get main entry fields from the authority record
diff --git a/Open-ILS/src/sql/Pg/upgrade/0469.schema.authority-maint-trigger-funcs.sql b/Open-ILS/src/sql/Pg/upgrade/0469.schema.authority-maint-trigger-funcs.sql
new file mode 100644 (file)
index 0000000..61864ff
--- /dev/null
@@ -0,0 +1,92 @@
+BEGIN;
+
+INSERT INTO config.upgrade_log (version) VALUES ('0469'); -- miker
+
+CREATE OR REPLACE FUNCTION authority.generate_overlay_template ( TEXT, BIGINT ) RETURNS TEXT AS $func$
+
+    use MARC::Record;
+    use MARC::File::XML (BinaryEncoding => 'UTF-8');
+
+    my $xml = shift;
+    my $r = MARC::Record->new_from_xml( $xml );
+
+    return undef unless ($r);
+
+    my $id = shift() || $r->subfield( '901' => 'c' );
+    $id =~ s/^\s*(?:\([^)]+\))?\s*(.+)\s*?$/$1/;
+    return undef unless ($id); # We need an ID!
+
+    my $tmpl = MARC::Record->new();
+    $tmpl->encoding( 'UTF-8' );
+
+    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;
+    $xml =~ s/\n//sgo;
+    $xml =~ s/>\s+</></sgo;
+
+    return $xml;
+
+$func$ LANGUAGE PLPERLU;
+
+CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
+DECLARE
+    xml_output TEXT;
+    parsed_target TEXT;
+BEGIN
+    parsed_target := vandelay.strip_field( target_xml, ''); -- this dance normalized the format of the xml for the IF below
+    xml_output := vandelay.strip_field( parsed_target, field);
+
+    IF xml_output <> parsed_target  AND field ~ E'~' THEN
+        -- we removed something, and there was a regexp restriction in the field definition, so proceed
+        xml_output := vandelay.add_field( xml_output, source_xml, field, 1 );
+    ELSIF field !~ E'~' THEN
+        -- No regexp restriction, add the field
+        xml_output := vandelay.add_field( xml_output, source_xml, field, 0 );
+    END IF;
+
+    RETURN xml_output;
+END;
+$_$ LANGUAGE PLPGSQL;
+
+COMMIT;
+