From 64e028a2a2102cca49b65755a580ef4be10f0ec0 Mon Sep 17 00:00:00 2001 From: gmc Date: Wed, 6 Apr 2011 20:51:38 +0000 Subject: [PATCH] fix crash when importing/saving bib with '&' in TCN value Also fix similar glitch if ampersand is present in the TCN source. Signed-off-by: Galen Charlton git-svn-id: svn://svn.open-ils.org/ILS/trunk@20003 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/sql/Pg/002.functions.config.sql | 13 +++- Open-ILS/src/sql/Pg/002.schema.config.sql | 2 +- .../upgrade/0508.schema.maintain_901_amp_fix.sql | 77 ++++++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0508.schema.maintain_901_amp_fix.sql diff --git a/Open-ILS/src/sql/Pg/002.functions.config.sql b/Open-ILS/src/sql/Pg/002.functions.config.sql index afd2b2b1fc..99f98d81d9 100644 --- a/Open-ILS/src/sql/Pg/002.functions.config.sql +++ b/Open-ILS/src/sql/Pg/002.functions.config.sql @@ -422,6 +422,8 @@ $f$ LANGUAGE PLPERLU; CREATE OR REPLACE FUNCTION maintain_901 () RETURNS TRIGGER AS $func$ DECLARE use_id_for_tcn BOOLEAN; + norm_tcn_value TEXT; + norm_tcn_source TEXT; BEGIN -- Remove any existing 901 fields before we insert the authoritative one NEW.marc := REGEXP_REPLACE(NEW.marc, E']*?tag="901".+?', '', 'g'); @@ -433,14 +435,21 @@ BEGIN IF use_id_for_tcn = 't' THEN NEW.tcn_value := NEW.id; + norm_tcn_value := NEW.tcn_value; + ELSE + -- yes, ampersands can show up in tcn_values ... + norm_tcn_value := REGEXP_REPLACE(NEW.tcn_value, E'&(?!\\S+;)', '&', 'g'); END IF; + -- ... and TCN sources + -- FIXME we have here yet another (stub) version of entityize + norm_tcn_source := REGEXP_REPLACE(NEW.tcn_source, E'&(?!\\S+;)', '&', 'g'); NEW.marc := REGEXP_REPLACE( NEW.marc, E'()', E'' || - '' || NEW.tcn_value || E'' || - '' || NEW.tcn_source || E'' || + '' || norm_tcn_value || E'' || + '' || norm_tcn_source || E'' || '' || NEW.id || E'' || '' || TG_TABLE_SCHEMA || E'' || CASE WHEN NEW.owner IS NOT NULL THEN '' || NEW.owner || E'' ELSE '' END || diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index fdc77be937..2f4138cd17 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -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 ('0507'); -- miker +INSERT INTO config.upgrade_log (version) VALUES ('0508'); -- gmc CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/upgrade/0508.schema.maintain_901_amp_fix.sql b/Open-ILS/src/sql/Pg/upgrade/0508.schema.maintain_901_amp_fix.sql new file mode 100644 index 0000000000..a29a10b72e --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0508.schema.maintain_901_amp_fix.sql @@ -0,0 +1,77 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0508'); -- gmc + +CREATE OR REPLACE FUNCTION maintain_901 () RETURNS TRIGGER AS $func$ +DECLARE + use_id_for_tcn BOOLEAN; + norm_tcn_value TEXT; + norm_tcn_source TEXT; +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; + norm_tcn_value := NEW.tcn_value; + ELSE + -- yes, ampersands can show up in tcn_values ... + norm_tcn_value := REGEXP_REPLACE(NEW.tcn_value, E'&(?!\\S+;)', '&', 'g'); + END IF; + -- ... and TCN sources + -- FIXME we have here yet another (stub) version of entityize + norm_tcn_source := REGEXP_REPLACE(NEW.tcn_source, E'&(?!\\S+;)', '&', 'g'); + + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'()', + E'' || + '' || norm_tcn_value || E'' || + '' || norm_tcn_source || 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'()', + E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + E'\\1' + ); + ELSIF TG_TABLE_SCHEMA = 'serial' THEN + NEW.marc := REGEXP_REPLACE( + NEW.marc, + E'()', + 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'()', + E'' || + '' || NEW.id || E'' || + '' || TG_TABLE_SCHEMA || E'' || + E'\\1' + ); + END IF; + + RETURN NEW; +END; +$func$ LANGUAGE PLPGSQL; + +COMMIT; -- 2.11.0