From f11a210fef018712c70f27a3c260f275470fc457 Mon Sep 17 00:00:00 2001 From: miker Date: Fri, 25 Jun 2010 17:31:21 +0000 Subject: [PATCH] add bib-authority linking table; add stored proc to extract linking; add hook to ingest (controlled by internal_flag) to implement authority link extraction git-svn-id: svn://svn.open-ils.org/ILS/trunk@16816 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/examples/fm_IDL.xml | 19 ++++- Open-ILS/src/sql/Pg/002.schema.config.sql | 3 +- Open-ILS/src/sql/Pg/011.schema.authority.sql | 8 ++ Open-ILS/src/sql/Pg/030.schema.metabib.sql | 18 +++++ .../0318.schema.authority-linking-ingest.sql | 93 ++++++++++++++++++++++ 5 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0318.schema.authority-linking-ingest.sql diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml index 7d6ffa836..fcc6ffb9e 100644 --- a/Open-ILS/examples/fm_IDL.xml +++ b/Open-ILS/examples/fm_IDL.xml @@ -1408,13 +1408,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - - + + + + @@ -1429,6 +1431,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + + + + + + + + + + + @@ -1740,6 +1753,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + @@ -1758,6 +1772,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index 119cecbed..12c11c17d 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -57,6 +57,7 @@ INSERT INTO config.internal_flag (name) VALUES ('ingest.disable_located_uri'); INSERT INTO config.internal_flag (name) VALUES ('ingest.disable_metabib_full_rec'); INSERT INTO config.internal_flag (name) VALUES ('ingest.disable_metabib_rec_descriptor'); INSERT INTO config.internal_flag (name) VALUES ('ingest.disable_metabib_field_entry'); +INSERT INTO config.internal_flag (name) VALUES ('ingest.disable_authority_linking'); CREATE TABLE config.global_flag ( label TEXT NOT NULL @@ -68,7 +69,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0317'); -- miker +INSERT INTO config.upgrade_log (version) VALUES ('0318'); -- miker CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/011.schema.authority.sql b/Open-ILS/src/sql/Pg/011.schema.authority.sql index a362cec45..085103f65 100644 --- a/Open-ILS/src/sql/Pg/011.schema.authority.sql +++ b/Open-ILS/src/sql/Pg/011.schema.authority.sql @@ -39,6 +39,14 @@ CREATE INDEX authority_record_entry_editor_idx ON authority.record_entry ( edito CREATE UNIQUE INDEX authority_record_unique_tcn ON authority.record_entry (arn_source,arn_value) WHERE deleted = FALSE OR deleted IS FALSE; CREATE TRIGGER a_marcxml_is_well_formed BEFORE INSERT OR UPDATE ON authority.record_entry FOR EACH ROW EXECUTE PROCEDURE biblio.check_marcxml_well_formed(); +CREATE TABLE authority.bib_linking ( + id BIGSERIAL PRIMARY KEY, + bib BIGINT NOT NULL REFERENCES biblio.record_entry (id), + authority BIGINT NOT NULL REFERENCES authority.record_entry (id) +); +CREATE INDEX authority_bl_bib_idx ON authority.bib_linking ( bib ); +CREATE UNIQUE INDEX authority_bl_bib_authority_once_idx ON authority.bib_linking ( authority, bib ); + CREATE TABLE authority.record_note ( id BIGSERIAL PRIMARY KEY, record BIGINT NOT NULL REFERENCES authority.record_entry (id) DEFERRABLE INITIALLY DEFERRED, diff --git a/Open-ILS/src/sql/Pg/030.schema.metabib.sql b/Open-ILS/src/sql/Pg/030.schema.metabib.sql index 92a49c189..621a038b7 100644 --- a/Open-ILS/src/sql/Pg/030.schema.metabib.sql +++ b/Open-ILS/src/sql/Pg/030.schema.metabib.sql @@ -883,6 +883,18 @@ BEGIN END; $func$ LANGUAGE PLPGSQL; +CREATE OR REPLACE FUNCTION biblio.map_authority_linking (bibid BIGINT, marc TEXT) RETURNS BIGINT AS $func$ + DELETE FROM authority.bib_linking WHERE bib = $1; + INSERT INTO authority.bib_linking (bib, authority) + SELECT y.bib, + y.authority + FROM ( SELECT DISTINCT $1 AS bib, + BTRIM(remove_paren_substring(x))::BIGINT AS authority + FROM explode_array(oils_xpath('//*[@code="0"]/text()',$2)) x + ) y JOIN authority.record_entry r ON r.id = y.authority; + SELECT $1; +$func$ LANGUAGE SQL; + -- AFTER UPDATE OR INSERT trigger for biblio.record_entry CREATE OR REPLACE FUNCTION biblio.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$ BEGIN @@ -902,6 +914,12 @@ BEGIN END IF; + -- Record authority linking + PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_linking' AND enabled; + IF NOT FOUND THEN + PERFORM biblio.map_authority_linking( NEW.id, NEW.marc ); + END IF; + -- Flatten and insert the mfr data PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_metabib_full_rec' AND enabled; IF NOT FOUND THEN diff --git a/Open-ILS/src/sql/Pg/upgrade/0318.schema.authority-linking-ingest.sql b/Open-ILS/src/sql/Pg/upgrade/0318.schema.authority-linking-ingest.sql new file mode 100644 index 000000000..f325316b4 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0318.schema.authority-linking-ingest.sql @@ -0,0 +1,93 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0318'); --miker + +INSERT INTO config.internal_flag (name) VALUES ('ingest.disable_authority_linking'); + +CREATE TABLE authority.bib_linking ( + id BIGSERIAL PRIMARY KEY, + bib BIGINT NOT NULL REFERENCES biblio.record_entry (id), + authority BIGINT NOT NULL REFERENCES authority.record_entry (id) +); +CREATE INDEX authority_bl_bib_idx ON authority.bib_linking ( bib ); +CREATE UNIQUE INDEX authority_bl_bib_authority_once_idx ON authority.bib_linking ( authority, bib ); + +CREATE OR REPLACE FUNCTION biblio.map_authority_linking (bibid BIGINT, marc TEXT) RETURNS BIGINT AS $func$ + DELETE FROM authority.bib_linking WHERE bib = $1; + INSERT INTO authority.bib_linking (bib, authority) + SELECT y.bib, + y.authority + FROM ( SELECT DISTINCT $1 AS bib, + BTRIM(remove_paren_substring(x))::BIGINT AS authority + FROM explode_array(oils_xpath('//*[@code="0"]/text()',$2)) x + ) y JOIN authority.record_entry r ON r.id = y.authority; + SELECT $1; +$func$ LANGUAGE SQL; + +-- AFTER UPDATE OR INSERT trigger for biblio.record_entry +CREATE OR REPLACE FUNCTION biblio.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$ +BEGIN + + IF NEW.deleted IS TRUE THEN -- If this bib is deleted + DELETE FROM metabib.metarecord_source_map WHERE source = NEW.id; -- Rid ourselves of the search-estimate-killing linkage + RETURN NEW; -- and we're done + END IF; + + IF TG_OP = 'UPDATE' THEN -- re-ingest? + PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled; + + IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change + RETURN NEW; + END IF; + + + END IF; + + -- Record authority linking + PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_linking' AND enabled; + IF NOT FOUND THEN + PERFORM biblio.map_authority_linking( NEW.id, NEW.marc ); + END IF; + + -- Flatten and insert the mfr data + PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_metabib_full_rec' AND enabled; + IF NOT FOUND THEN + PERFORM metabib.reingest_metabib_full_rec(NEW.id); + PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_metabib_rec_descriptor' AND enabled; + IF NOT FOUND THEN + PERFORM metabib.reingest_metabib_rec_descriptor(NEW.id); + END IF; + END IF; + + -- Gather and insert the field entry data + PERFORM metabib.reingest_metabib_field_entries(NEW.id); + + -- Located URI magic + IF TG_OP = 'INSERT' THEN + PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_located_uri' AND enabled; + IF NOT FOUND THEN + PERFORM biblio.extract_located_uris( NEW.id, NEW.marc, NEW.editor ); + END IF; + ELSE + PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_located_uri' AND enabled; + IF NOT FOUND THEN + PERFORM biblio.extract_located_uris( NEW.id, NEW.marc, NEW.editor ); + END IF; + END IF; + + -- (re)map metarecord-bib linking + IF TG_OP = 'INSERT' THEN -- if not deleted and performing an insert, check for the flag + PERFORM * FROM config.internal_flag WHERE name = 'ingest.metarecord_mapping.skip_on_insert' AND enabled; + IF NOT FOUND THEN + PERFORM metabib.remap_metarecord_for_bib( NEW.id, NEW.fingerprint ); + END IF; + ELSE -- we're doing an update, and we're not deleted, remap + PERFORM metabib.remap_metarecord_for_bib( NEW.id, NEW.fingerprint ); + END IF; + + RETURN NEW; +END; +$func$ LANGUAGE PLPGSQL; + +COMMIT; + -- 2.11.0