From b0c85c31df44e962c0ecc87e08e067513d6c4825 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Tue, 24 Jul 2012 11:51:52 -0400 Subject: [PATCH] lp1028514: fix regex replace in maintain_901() 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 Signed-off-by: Dan Scott --- Open-ILS/src/sql/Pg/002.functions.config.sql | 8 +-- .../upgrade/XXXX.schema.fix_maintain_901_regex.sql | 68 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_maintain_901_regex.sql diff --git a/Open-ILS/src/sql/Pg/002.functions.config.sql b/Open-ILS/src/sql/Pg/002.functions.config.sql index 3ea61ae289..b441ad8c2b 100644 --- a/Open-ILS/src/sql/Pg/002.functions.config.sql +++ b/Open-ILS/src/sql/Pg/002.functions.config.sql @@ -437,7 +437,7 @@ BEGIN NEW.marc := REGEXP_REPLACE( NEW.marc, - E'()', + E'(]*?:)?record>)', E'' || '' || REPLACE(evergreen.xml_escape(NEW.tcn_value), E'\\', E'\\\\') || E'' || '' || REPLACE(evergreen.xml_escape(NEW.tcn_source), E'\\', E'\\\\') || E'' || @@ -450,7 +450,7 @@ BEGIN ELSIF TG_TABLE_SCHEMA = 'authority' THEN NEW.marc := REGEXP_REPLACE( NEW.marc, - E'()', + E'(]*?:)?record>)', E'' || '' || NEW.id || E'' || '' || TG_TABLE_SCHEMA || E'' || @@ -459,7 +459,7 @@ BEGIN ELSIF TG_TABLE_SCHEMA = 'serial' THEN NEW.marc := REGEXP_REPLACE( NEW.marc, - E'()', + E'(]*?:)?record>)', E'' || '' || NEW.id || E'' || '' || TG_TABLE_SCHEMA || E'' || @@ -470,7 +470,7 @@ BEGIN ELSE NEW.marc := REGEXP_REPLACE( NEW.marc, - E'()', + E'(]*?:)?record>)', E'' || '' || NEW.id || E'' || '' || TG_TABLE_SCHEMA || E'' || 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 index 0000000000..607e3a7e25 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.fix_maintain_901_regex.sql @@ -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']*?tag="901".+?', '', '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'' || + '' || REPLACE(evergreen.xml_escape(NEW.tcn_value), E'\\', E'\\\\') || E'' || + '' || REPLACE(evergreen.xml_escape(NEW.tcn_source), E'\\', E'\\\\') || E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + CASE WHEN NEW.owner IS NOT NULL THEN '' || NEW.owner || E'' ELSE '' END || + CASE WHEN NEW.share_depth IS NOT NULL THEN '' || NEW.share_depth || E'' ELSE '' END || + E'\\1' + ); + ELSIF TG_TABLE_SCHEMA = 'authority' THEN + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'(]*?:)?record>)', + E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + E'\\1' + ); + ELSIF TG_TABLE_SCHEMA = 'serial' THEN + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'(]*?:)?record>)', + E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + '' || NEW.owning_lib || E'' || + CASE WHEN NEW.record IS NOT NULL THEN '' || NEW.record || E'' ELSE '' END || + E'\\1' + ); + ELSE + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'(]*?:)?record>)', + E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + E'\\1' + ); + END IF; + + RETURN NEW; +END; +$func$ LANGUAGE PLPGSQL; + +COMMIT; -- 2.11.0