lp1028514: fix regex replace in maintain_901()
authorGalen Charlton <gmc@esilibrary.com>
Tue, 24 Jul 2012 15:51:52 +0000 (11:51 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Fri, 27 Jul 2012 13:14:27 +0000 (09:14 -0400)
The regex used to insert the 901 field can put the new
field in the wrong location if the MARCXML record being
modified uses namespace prefixes.  This patch tightens
the regexp to avoid this problem.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/src/sql/Pg/002.functions.config.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_maintain_901_regex.sql [new file with mode: 0644]

index 3ea61ae..b441ad8 100644 (file)
@@ -437,7 +437,7 @@ BEGIN
 
         NEW.marc := REGEXP_REPLACE(
             NEW.marc,
-            E'(</(?:[^:]*?:)?record>)',
+            E'(</(?:[^:>]*?:)?record>)',
             E'<datafield tag="901" ind1=" " ind2=" ">' ||
                 '<subfield code="a">' || REPLACE(evergreen.xml_escape(NEW.tcn_value), E'\\', E'\\\\') || E'</subfield>' ||
                 '<subfield code="b">' || REPLACE(evergreen.xml_escape(NEW.tcn_source), E'\\', E'\\\\') || E'</subfield>' ||
@@ -450,7 +450,7 @@ BEGIN
     ELSIF TG_TABLE_SCHEMA = 'authority' THEN
         NEW.marc := REGEXP_REPLACE(
             NEW.marc,
-            E'(</(?:[^:]*?:)?record>)',
+            E'(</(?:[^:>]*?:)?record>)',
             E'<datafield tag="901" ind1=" " ind2=" ">' ||
                 '<subfield code="c">' || NEW.id || E'</subfield>' ||
                 '<subfield code="t">' || TG_TABLE_SCHEMA || E'</subfield>' ||
@@ -459,7 +459,7 @@ BEGIN
     ELSIF TG_TABLE_SCHEMA = 'serial' THEN
         NEW.marc := REGEXP_REPLACE(
             NEW.marc,
-            E'(</(?:[^:]*?:)?record>)',
+            E'(</(?:[^:>]*?:)?record>)',
             E'<datafield tag="901" ind1=" " ind2=" ">' ||
                 '<subfield code="c">' || NEW.id || E'</subfield>' ||
                 '<subfield code="t">' || TG_TABLE_SCHEMA || E'</subfield>' ||
@@ -470,7 +470,7 @@ BEGIN
     ELSE
         NEW.marc := REGEXP_REPLACE(
             NEW.marc,
-            E'(</(?:[^:]*?:)?record>)',
+            E'(</(?:[^:>]*?:)?record>)',
             E'<datafield tag="901" ind1=" " ind2=" ">' ||
                 '<subfield code="c">' || NEW.id || E'</subfield>' ||
                 '<subfield code="t">' || TG_TABLE_SCHEMA || E'</subfield>' ||
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_maintain_901_regex.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_maintain_901_regex.sql
new file mode 100644 (file)
index 0000000..607e3a7
--- /dev/null
@@ -0,0 +1,68 @@
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
+DECLARE
+    use_id_for_tcn BOOLEAN;
+BEGIN
+    -- Remove any existing 901 fields before we insert the authoritative one
+    NEW.marc := REGEXP_REPLACE(NEW.marc, E'<datafield[^>]*?tag="901".+?</datafield>', '', 'g');
+
+    IF TG_TABLE_SCHEMA = 'biblio' THEN
+        -- Set TCN value to record ID?
+        SELECT enabled FROM config.global_flag INTO use_id_for_tcn
+            WHERE name = 'cat.bib.use_id_for_tcn';
+
+        IF use_id_for_tcn = 't' THEN
+            NEW.tcn_value := NEW.id;
+        END IF;
+
+        NEW.marc := REGEXP_REPLACE(
+            NEW.marc,
+            E'(</(?:[^:>]*?:)?record>)',
+            E'<datafield tag="901" ind1=" " ind2=" ">' ||
+                '<subfield code="a">' || REPLACE(evergreen.xml_escape(NEW.tcn_value), E'\\', E'\\\\') || E'</subfield>' ||
+                '<subfield code="b">' || REPLACE(evergreen.xml_escape(NEW.tcn_source), E'\\', E'\\\\') || E'</subfield>' ||
+                '<subfield code="c">' || NEW.id || E'</subfield>' ||
+                '<subfield code="t">' || TG_TABLE_SCHEMA || E'</subfield>' ||
+                CASE WHEN NEW.owner IS NOT NULL THEN '<subfield code="o">' || NEW.owner || E'</subfield>' ELSE '' END ||
+                CASE WHEN NEW.share_depth IS NOT NULL THEN '<subfield code="d">' || NEW.share_depth || E'</subfield>' ELSE '' END ||
+             E'</datafield>\\1'
+        );
+    ELSIF TG_TABLE_SCHEMA = 'authority' THEN
+        NEW.marc := REGEXP_REPLACE(
+            NEW.marc,
+            E'(</(?:[^:>]*?:)?record>)',
+            E'<datafield tag="901" ind1=" " ind2=" ">' ||
+                '<subfield code="c">' || NEW.id || E'</subfield>' ||
+                '<subfield code="t">' || TG_TABLE_SCHEMA || E'</subfield>' ||
+             E'</datafield>\\1'
+        );
+    ELSIF TG_TABLE_SCHEMA = 'serial' THEN
+        NEW.marc := REGEXP_REPLACE(
+            NEW.marc,
+            E'(</(?:[^:>]*?:)?record>)',
+            E'<datafield tag="901" ind1=" " ind2=" ">' ||
+                '<subfield code="c">' || NEW.id || E'</subfield>' ||
+                '<subfield code="t">' || TG_TABLE_SCHEMA || E'</subfield>' ||
+                '<subfield code="o">' || NEW.owning_lib || E'</subfield>' ||
+                CASE WHEN NEW.record IS NOT NULL THEN '<subfield code="r">' || NEW.record || E'</subfield>' ELSE '' END ||
+             E'</datafield>\\1'
+        );
+    ELSE
+        NEW.marc := REGEXP_REPLACE(
+            NEW.marc,
+            E'(</(?:[^:>]*?:)?record>)',
+            E'<datafield tag="901" ind1=" " ind2=" ">' ||
+                '<subfield code="c">' || NEW.id || E'</subfield>' ||
+                '<subfield code="t">' || TG_TABLE_SCHEMA || E'</subfield>' ||
+             E'</datafield>\\1'
+        );
+    END IF;
+
+    RETURN NEW;
+END;
+$func$ LANGUAGE PLPGSQL;
+
+COMMIT;