From: Ben Shum <bshum@biblio.org> Date: Mon, 17 Aug 2015 16:59:21 +0000 (-0400) Subject: LP#1178377: Stamping upgrade script for unapi to include bib source X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=4662c52cc78e919c4f2ae611dfc1b378a0c83e11;p=evergreen%2Fmasslnc.git LP#1178377: Stamping upgrade script for unapi to include bib source Signed-off-by: Ben Shum <bshum@biblio.org> --- diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index 829b231d7e..6547616f04 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -91,7 +91,7 @@ CREATE TRIGGER no_overlapping_deps BEFORE INSERT OR UPDATE ON config.db_patch_dependencies FOR EACH ROW EXECUTE PROCEDURE evergreen.array_overlap_check ('deprecates'); -INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0922', :eg_version); -- jeffdavis/kmlussier/dyrcona +INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0923', :eg_version); -- jeffdavis/dkyle/bshum CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/upgrade/0923.schema.unapi_include_bib_source.sql b/Open-ILS/src/sql/Pg/upgrade/0923.schema.unapi_include_bib_source.sql new file mode 100644 index 0000000000..a3c6aa9595 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0923.schema.unapi_include_bib_source.sql @@ -0,0 +1,144 @@ +-- Include bib source in XML returned by unapi.bre. +BEGIN; + +-- check whether patch can be applied +SELECT evergreen.upgrade_deps_block_check('0923', :eg_version); + +CREATE OR REPLACE FUNCTION unapi.cbs ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ + SELECT XMLELEMENT( + name bib_source, + XMLATTRIBUTES( + NULL AS xmlns, -- TODO needs equivalent to http://open-ils.org/spec/holdings/v1 + id AS ident, + quality, + transcendant, + can_have_copies + ), + source + ) + FROM config.bib_source + WHERE id = $1; +$F$ LANGUAGE SQL STABLE; + +CREATE OR REPLACE FUNCTION unapi.bre ( + obj_id BIGINT, + format TEXT, + ename TEXT, + includes TEXT[], + org TEXT, + depth INT DEFAULT NULL, + slimit HSTORE DEFAULT NULL, + soffset HSTORE DEFAULT NULL, + include_xmlns BOOL DEFAULT TRUE, + pref_lib INT DEFAULT NULL +) +RETURNS XML AS $F$ +DECLARE + me biblio.record_entry%ROWTYPE; + layout unapi.bre_output_layout%ROWTYPE; + xfrm config.xml_transform%ROWTYPE; + ouid INT; + tmp_xml TEXT; + top_el TEXT; + output XML; + hxml XML; + axml XML; + source XML; +BEGIN + + IF org = '-' OR org IS NULL THEN + SELECT shortname INTO org FROM evergreen.org_top(); + END IF; + + SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org; + + IF ouid IS NULL THEN + RETURN NULL::XML; + END IF; + + IF format = 'holdings_xml' THEN -- the special case + output := unapi.holdings_xml( obj_id, ouid, org, depth, includes, slimit, soffset, include_xmlns); + RETURN output; + END IF; + + SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format; + + IF layout.name IS NULL THEN + RETURN NULL::XML; + END IF; + + SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform; + + SELECT * INTO me FROM biblio.record_entry WHERE id = obj_id; + + -- grab bib_source, if any + IF ('cbs' = ANY (includes) AND me.source IS NOT NULL) THEN + source := unapi.cbs(me.source,NULL,NULL,NULL,NULL); + ELSE + source := NULL::XML; + END IF; + + -- grab SVF if we need them + IF ('mra' = ANY (includes)) THEN + axml := unapi.mra(obj_id,NULL,NULL,NULL,NULL); + ELSE + axml := NULL::XML; + END IF; + + -- grab holdings if we need them + IF ('holdings_xml' = ANY (includes)) THEN + hxml := unapi.holdings_xml(obj_id, ouid, org, depth, evergreen.array_remove_item_by_value(includes,'holdings_xml'), slimit, soffset, include_xmlns, pref_lib); + ELSE + hxml := NULL::XML; + END IF; + + + -- generate our item node + + + IF format = 'marcxml' THEN + tmp_xml := me.marc; + IF tmp_xml !~ E'<marc:' THEN -- If we're not using the prefixed namespace in this record, then remove all declarations of it + tmp_xml := REGEXP_REPLACE(tmp_xml, ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g'); + END IF; + ELSE + tmp_xml := oils_xslt_process(me.marc, xfrm.xslt)::XML; + END IF; + + top_el := REGEXP_REPLACE(tmp_xml, E'^.*?<((?:\\S+:)?' || layout.holdings_element || ').*$', E'\\1'); + + IF source IS NOT NULL THEN + tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', source || '</' || top_el || E'>\\1'); + END IF; + + IF axml IS NOT NULL THEN + tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1'); + END IF; + + IF hxml IS NOT NULL THEN -- XXX how do we configure the holdings position? + tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1'); + END IF; + + IF ('bre.unapi' = ANY (includes)) THEN + output := REGEXP_REPLACE( + tmp_xml, + '</' || top_el || '>(.*?)', + XMLELEMENT( + name abbr, + XMLATTRIBUTES( + 'http://www.w3.org/1999/xhtml' AS xmlns, + 'unapi-id' AS class, + 'tag:open-ils.org:U2@bre/' || obj_id || '/' || org AS title + ) + )::TEXT || '</' || top_el || E'>\\1' + ); + ELSE + output := tmp_xml; + END IF; + + output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML; + RETURN output; +END; +$F$ LANGUAGE PLPGSQL STABLE; + +COMMIT; diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.unapi_include_bib_source.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.unapi_include_bib_source.sql deleted file mode 100644 index 17a4512cfd..0000000000 --- a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.unapi_include_bib_source.sql +++ /dev/null @@ -1,144 +0,0 @@ --- Include bib source in XML returned by unapi.bre. -BEGIN; - --- check whether patch can be applied -SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version); - -CREATE OR REPLACE FUNCTION unapi.cbs ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ - SELECT XMLELEMENT( - name bib_source, - XMLATTRIBUTES( - NULL AS xmlns, -- TODO needs equivalent to http://open-ils.org/spec/holdings/v1 - id AS ident, - quality, - transcendant, - can_have_copies - ), - source - ) - FROM config.bib_source - WHERE id = $1; -$F$ LANGUAGE SQL STABLE; - -CREATE OR REPLACE FUNCTION unapi.bre ( - obj_id BIGINT, - format TEXT, - ename TEXT, - includes TEXT[], - org TEXT, - depth INT DEFAULT NULL, - slimit HSTORE DEFAULT NULL, - soffset HSTORE DEFAULT NULL, - include_xmlns BOOL DEFAULT TRUE, - pref_lib INT DEFAULT NULL -) -RETURNS XML AS $F$ -DECLARE - me biblio.record_entry%ROWTYPE; - layout unapi.bre_output_layout%ROWTYPE; - xfrm config.xml_transform%ROWTYPE; - ouid INT; - tmp_xml TEXT; - top_el TEXT; - output XML; - hxml XML; - axml XML; - source XML; -BEGIN - - IF org = '-' OR org IS NULL THEN - SELECT shortname INTO org FROM evergreen.org_top(); - END IF; - - SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org; - - IF ouid IS NULL THEN - RETURN NULL::XML; - END IF; - - IF format = 'holdings_xml' THEN -- the special case - output := unapi.holdings_xml( obj_id, ouid, org, depth, includes, slimit, soffset, include_xmlns); - RETURN output; - END IF; - - SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format; - - IF layout.name IS NULL THEN - RETURN NULL::XML; - END IF; - - SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform; - - SELECT * INTO me FROM biblio.record_entry WHERE id = obj_id; - - -- grab bib_source, if any - IF ('cbs' = ANY (includes) AND me.source IS NOT NULL) THEN - source := unapi.cbs(me.source,NULL,NULL,NULL,NULL); - ELSE - source := NULL::XML; - END IF; - - -- grab SVF if we need them - IF ('mra' = ANY (includes)) THEN - axml := unapi.mra(obj_id,NULL,NULL,NULL,NULL); - ELSE - axml := NULL::XML; - END IF; - - -- grab holdings if we need them - IF ('holdings_xml' = ANY (includes)) THEN - hxml := unapi.holdings_xml(obj_id, ouid, org, depth, evergreen.array_remove_item_by_value(includes,'holdings_xml'), slimit, soffset, include_xmlns, pref_lib); - ELSE - hxml := NULL::XML; - END IF; - - - -- generate our item node - - - IF format = 'marcxml' THEN - tmp_xml := me.marc; - IF tmp_xml !~ E'<marc:' THEN -- If we're not using the prefixed namespace in this record, then remove all declarations of it - tmp_xml := REGEXP_REPLACE(tmp_xml, ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g'); - END IF; - ELSE - tmp_xml := oils_xslt_process(me.marc, xfrm.xslt)::XML; - END IF; - - top_el := REGEXP_REPLACE(tmp_xml, E'^.*?<((?:\\S+:)?' || layout.holdings_element || ').*$', E'\\1'); - - IF source IS NOT NULL THEN - tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', source || '</' || top_el || E'>\\1'); - END IF; - - IF axml IS NOT NULL THEN - tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1'); - END IF; - - IF hxml IS NOT NULL THEN -- XXX how do we configure the holdings position? - tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1'); - END IF; - - IF ('bre.unapi' = ANY (includes)) THEN - output := REGEXP_REPLACE( - tmp_xml, - '</' || top_el || '>(.*?)', - XMLELEMENT( - name abbr, - XMLATTRIBUTES( - 'http://www.w3.org/1999/xhtml' AS xmlns, - 'unapi-id' AS class, - 'tag:open-ils.org:U2@bre/' || obj_id || '/' || org AS title - ) - )::TEXT || '</' || top_el || E'>\\1' - ); - ELSE - output := tmp_xml; - END IF; - - output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML; - RETURN output; -END; -$F$ LANGUAGE PLPGSQL STABLE; - -COMMIT;