4 CREATE OR REPLACE FUNCTION authority.propagate_changes
5 (aid BIGINT, bid BIGINT) RETURNS BIGINT AS $func$
7 bib_rec biblio.record_entry%ROWTYPE;
11 SELECT INTO bib_rec * FROM biblio.record_entry WHERE id = bid;
13 new_marc := vandelay.merge_record_xml(
14 bib_rec.marc, authority.generate_overlay_template(aid));
16 IF new_marc = bib_rec.marc THEN
17 -- Authority record change had no impact on this bib record.
18 -- Nothing left to do.
22 PERFORM 1 FROM config.global_flag
23 WHERE name = 'ingest.disable_authority_auto_update_bib_meta'
27 -- update the bib record editor and edit_date
29 SELECT editor FROM authority.record_entry WHERE id = aid);
30 bib_rec.edit_date = NOW();
33 UPDATE biblio.record_entry SET
35 editor = bib_rec.editor,
36 edit_date = bib_rec.edit_date
42 $func$ LANGUAGE PLPGSQL;
46 -- Disabled by default
47 INSERT INTO config.global_flag (name, enabled, label) VALUES (
48 'ingest.disable_authority_auto_update_bib_meta', FALSE,
50 'ingest.disable_authority_auto_update_bib_meta',
51 'Authority Automation: Disable automatic authority updates ' ||
52 'from modifying bib record editor and edit_date',
59 CREATE OR REPLACE FUNCTION authority.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
61 ashs authority.simple_heading%ROWTYPE;
62 mbe_row metabib.browse_entry%ROWTYPE;
67 IF NEW.deleted IS TRUE THEN -- If this authority is deleted
68 DELETE FROM authority.bib_linking WHERE authority = NEW.id; -- Avoid updating fields in bibs that are no longer visible
69 DELETE FROM authority.full_rec WHERE record = NEW.id; -- Avoid validating fields against deleted authority records
70 DELETE FROM authority.simple_heading WHERE record = NEW.id;
71 -- Should remove matching $0 from controlled fields at the same time?
73 -- XXX What do we about the actual linking subfields present in
74 -- authority records that target this one when this happens?
75 DELETE FROM authority.authority_linking
76 WHERE source = NEW.id OR target = NEW.id;
78 RETURN NEW; -- and we're done
81 IF TG_OP = 'UPDATE' THEN -- re-ingest?
82 PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled;
84 IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change
88 -- Unless there's a setting stopping us, propagate these updates to any linked bib records when the heading changes
89 PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_auto_update' AND enabled;
91 IF NOT FOUND AND NEW.heading <> OLD.heading THEN
92 PERFORM authority.propagate_changes(NEW.id);
95 DELETE FROM authority.simple_heading WHERE record = NEW.id;
96 DELETE FROM authority.authority_linking WHERE source = NEW.id;
99 INSERT INTO authority.authority_linking (source, target, field)
100 SELECT source, target, field FROM authority.calculate_authority_linking(
101 NEW.id, NEW.control_set, NEW.marc::XML
104 FOR ashs IN SELECT * FROM authority.simple_heading_set(NEW.marc) LOOP
106 INSERT INTO authority.simple_heading (record,atag,value,sort_value,thesaurus)
107 VALUES (ashs.record, ashs.atag, ashs.value, ashs.sort_value, ashs.thesaurus);
108 ash_id := CURRVAL('authority.simple_heading_id_seq'::REGCLASS);
110 SELECT INTO mbe_row * FROM metabib.browse_entry
111 WHERE value = ashs.value AND sort_value = ashs.sort_value;
114 mbe_id := mbe_row.id;
116 INSERT INTO metabib.browse_entry
117 ( value, sort_value ) VALUES
118 ( ashs.value, ashs.sort_value );
120 mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
123 INSERT INTO metabib.browse_entry_simple_heading_map (entry,simple_heading) VALUES (mbe_id,ash_id);
127 -- Flatten and insert the afr data
128 PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_full_rec' AND enabled;
130 PERFORM authority.reingest_authority_full_rec(NEW.id);
131 PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_rec_descriptor' AND enabled;
133 PERFORM authority.reingest_authority_rec_descriptor(NEW.id);
139 $func$ LANGUAGE PLPGSQL;