]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0994.schema.authority-propage-edit-date.sql
LP#1917826: add release notes entry
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0994.schema.authority-propage-edit-date.sql
1
2 BEGIN;
3
4 SELECT evergreen.upgrade_deps_block_check('0994', :eg_version);
5
6 CREATE OR REPLACE FUNCTION authority.propagate_changes 
7     (aid BIGINT, bid BIGINT) RETURNS BIGINT AS $func$
8 DECLARE
9     bib_rec biblio.record_entry%ROWTYPE;
10     new_marc TEXT;
11 BEGIN
12
13     SELECT INTO bib_rec * FROM biblio.record_entry WHERE id = bid;
14
15     new_marc := vandelay.merge_record_xml(
16         bib_rec.marc, authority.generate_overlay_template(aid));
17
18     IF new_marc = bib_rec.marc THEN
19         -- Authority record change had no impact on this bib record.
20         -- Nothing left to do.
21         RETURN aid;
22     END IF;
23
24     PERFORM 1 FROM config.global_flag 
25         WHERE name = 'ingest.disable_authority_auto_update_bib_meta' 
26             AND enabled;
27
28     IF NOT FOUND THEN 
29         -- update the bib record editor and edit_date
30         bib_rec.editor := (
31             SELECT editor FROM authority.record_entry WHERE id = aid);
32         bib_rec.edit_date = NOW();
33     END IF;
34
35     UPDATE biblio.record_entry SET
36         marc = new_marc,
37         editor = bib_rec.editor,
38         edit_date = bib_rec.edit_date
39     WHERE id = bid;
40
41     RETURN aid;
42
43 END;
44 $func$ LANGUAGE PLPGSQL;
45
46
47 -- DATA
48 -- Disabled by default
49 INSERT INTO config.global_flag (name, enabled, label) VALUES (
50     'ingest.disable_authority_auto_update_bib_meta',  FALSE, 
51     oils_i18n_gettext(
52         'ingest.disable_authority_auto_update_bib_meta',
53         'Authority Automation: Disable automatic authority updates ' ||
54             'from modifying bib record editor and edit_date',
55         'cgf',
56         'label'
57     )
58 );
59
60
61 CREATE OR REPLACE FUNCTION authority.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
62 DECLARE
63     ashs    authority.simple_heading%ROWTYPE;
64     mbe_row metabib.browse_entry%ROWTYPE;
65     mbe_id  BIGINT;
66     ash_id  BIGINT;
67 BEGIN
68
69     IF NEW.deleted IS TRUE THEN -- If this authority is deleted
70         DELETE FROM authority.bib_linking WHERE authority = NEW.id; -- Avoid updating fields in bibs that are no longer visible
71         DELETE FROM authority.full_rec WHERE record = NEW.id; -- Avoid validating fields against deleted authority records
72         DELETE FROM authority.simple_heading WHERE record = NEW.id;
73           -- Should remove matching $0 from controlled fields at the same time?
74
75         -- XXX What do we about the actual linking subfields present in
76         -- authority records that target this one when this happens?
77         DELETE FROM authority.authority_linking
78             WHERE source = NEW.id OR target = NEW.id;
79
80         RETURN NEW; -- and we're done
81     END IF;
82
83     IF TG_OP = 'UPDATE' THEN -- re-ingest?
84         PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled;
85
86         IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change
87             RETURN NEW;
88         END IF;
89
90         -- Unless there's a setting stopping us, propagate these updates to any linked bib records when the heading changes
91         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_auto_update' AND enabled;
92
93         IF NOT FOUND AND NEW.heading <> OLD.heading THEN
94             PERFORM authority.propagate_changes(NEW.id);
95         END IF;
96         
97         DELETE FROM authority.simple_heading WHERE record = NEW.id;
98         DELETE FROM authority.authority_linking WHERE source = NEW.id;
99     END IF;
100
101     INSERT INTO authority.authority_linking (source, target, field)
102         SELECT source, target, field FROM authority.calculate_authority_linking(
103             NEW.id, NEW.control_set, NEW.marc::XML
104         );
105
106     FOR ashs IN SELECT * FROM authority.simple_heading_set(NEW.marc) LOOP
107
108         INSERT INTO authority.simple_heading (record,atag,value,sort_value,thesaurus)
109             VALUES (ashs.record, ashs.atag, ashs.value, ashs.sort_value, ashs.thesaurus);
110             ash_id := CURRVAL('authority.simple_heading_id_seq'::REGCLASS);
111
112         SELECT INTO mbe_row * FROM metabib.browse_entry
113             WHERE value = ashs.value AND sort_value = ashs.sort_value;
114
115         IF FOUND THEN
116             mbe_id := mbe_row.id;
117         ELSE
118             INSERT INTO metabib.browse_entry
119                 ( value, sort_value ) VALUES
120                 ( ashs.value, ashs.sort_value );
121
122             mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
123         END IF;
124
125         INSERT INTO metabib.browse_entry_simple_heading_map (entry,simple_heading) VALUES (mbe_id,ash_id);
126
127     END LOOP;
128
129     -- Flatten and insert the afr data
130     PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_full_rec' AND enabled;
131     IF NOT FOUND THEN
132         PERFORM authority.reingest_authority_full_rec(NEW.id);
133         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_rec_descriptor' AND enabled;
134         IF NOT FOUND THEN
135             PERFORM authority.reingest_authority_rec_descriptor(NEW.id);
136         END IF;
137     END IF;
138
139     RETURN NEW;
140 END;
141 $func$ LANGUAGE PLPGSQL;
142
143 COMMIT;
144