1 -- Only consider main entry headings for bib overlay
5 -- check whether patch can be applied
6 SELECT evergreen.upgrade_deps_block_check('0750', :eg_version);
9 -- Function to generate an ephemeral overlay template from an authority record
10 CREATE OR REPLACE FUNCTION authority.generate_overlay_template (source_xml TEXT) RETURNS TEXT AS $f$
13 main_entry authority.control_set_authority_field%ROWTYPE;
14 bib_field authority.control_set_bib_field%ROWTYPE;
15 auth_id INT DEFAULT oils_xpath_string('//*[@tag="901"]/*[local-name()="subfield" and @code="c"]', source_xml)::INT;
16 replace_data XML[] DEFAULT '{}'::XML[];
17 replace_rules TEXT[] DEFAULT '{}'::TEXT[];
20 IF auth_id IS NULL THEN
24 -- Default to the LoC controll set
25 SELECT control_set INTO cset FROM authority.record_entry WHERE id = auth_id;
27 -- if none, make a best guess
29 SELECT control_set INTO cset
30 FROM authority.control_set_authority_field
32 SELECT UNNEST(XPATH('//*[starts-with(@tag,"1")]/@tag',marc::XML)::TEXT[])
33 FROM authority.record_entry
39 -- if STILL none, no-op change
43 XMLATTRIBUTES('http://www.loc.gov/MARC21/slim' AS xmlns),
44 XMLELEMENT( name leader, '00881nam a2200193 4500'),
47 XMLATTRIBUTES( '905' AS tag, ' ' AS ind1, ' ' AS ind2),
50 XMLATTRIBUTES('d' AS code),
57 FOR main_entry IN SELECT * FROM authority.control_set_authority_field acsaf WHERE acsaf.control_set = cset AND acsaf.main_entry IS NULL LOOP
58 auth_field := XPATH('//*[@tag="'||main_entry.tag||'"][1]',source_xml::XML);
59 IF ARRAY_LENGTH(auth_field,1) > 0 THEN
60 FOR bib_field IN SELECT * FROM authority.control_set_bib_field WHERE authority_field = main_entry.id LOOP
61 replace_data := replace_data || XMLELEMENT( name datafield, XMLATTRIBUTES(bib_field.tag AS tag), XPATH('//*[local-name()="subfield"]',auth_field[1])::XML[]);
62 replace_rules := replace_rules || ( bib_field.tag || main_entry.sf_list || E'[0~\\)' || auth_id || '$]' );
70 XMLATTRIBUTES('http://www.loc.gov/MARC21/slim' AS xmlns),
71 XMLELEMENT( name leader, '00881nam a2200193 4500'),
75 XMLATTRIBUTES( '905' AS tag, ' ' AS ind1, ' ' AS ind2),
78 XMLATTRIBUTES('r' AS code),
79 ARRAY_TO_STRING(replace_rules,',')
84 $f$ STABLE LANGUAGE PLPGSQL;
86 -- Change the two argument form of vandelay.merge_record_xml to
87 -- prevent bibliographic record destruction when there is nothing to
91 CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_marc TEXT, template_marc TEXT ) RETURNS TEXT AS $$
93 dyn_profile vandelay.compile_profile%ROWTYPE;
101 IF target_marc IS NULL OR template_marc IS NULL THEN
102 -- RAISE NOTICE 'no marc for target or template record';
106 dyn_profile := vandelay.compile_profile( template_marc );
108 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
109 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
113 IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' THEN
114 --Since we have nothing to do, just return what we were given.
116 ELSIF dyn_profile.replace_rule <> '' THEN
117 trgt_marc = target_marc;
118 tmpl_marc = template_marc;
119 replace_rule = dyn_profile.replace_rule;
121 tmp_marc = target_marc;
122 trgt_marc = template_marc;
123 tmpl_marc = tmp_marc;
124 replace_rule = dyn_profile.preserve_rule;
127 RETURN vandelay.merge_record_xml( trgt_marc, tmpl_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule );
132 CREATE OR REPLACE FUNCTION vandelay.template_overlay_bib_record ( v_marc TEXT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
134 merge_profile vandelay.merge_profile%ROWTYPE;
135 dyn_profile vandelay.compile_profile%ROWTYPE;
145 SELECT b.marc INTO eg_marc
146 FROM biblio.record_entry b
150 IF eg_marc IS NULL OR v_marc IS NULL THEN
151 -- RAISE NOTICE 'no marc for template or bib record';
155 dyn_profile := vandelay.compile_profile( v_marc );
157 IF merge_profile_id IS NOT NULL THEN
158 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
160 dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
161 dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
162 dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
163 dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
167 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
168 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
172 IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
173 --Since we have nothing to do, just return a NOOP "we did it"
175 ELSIF dyn_profile.replace_rule <> '' THEN
176 source_marc = v_marc;
177 target_marc = eg_marc;
178 replace_rule = dyn_profile.replace_rule;
180 source_marc = eg_marc;
181 target_marc = v_marc;
182 replace_rule = dyn_profile.preserve_rule;
185 UPDATE biblio.record_entry
186 SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
190 -- RAISE NOTICE 'update of biblio.record_entry failed';
199 CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
201 merge_profile vandelay.merge_profile%ROWTYPE;
202 dyn_profile vandelay.compile_profile%ROWTYPE;
211 SELECT b.marc INTO eg_marc
212 FROM authority.record_entry b
213 JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
216 SELECT q.marc INTO v_marc
217 FROM vandelay.queued_record q
218 JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id)
221 IF eg_marc IS NULL OR v_marc IS NULL THEN
222 -- RAISE NOTICE 'no marc for vandelay or authority record';
226 dyn_profile := vandelay.compile_profile( v_marc );
228 IF merge_profile_id IS NOT NULL THEN
229 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
231 dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
232 dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
233 dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
234 dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
238 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
239 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
243 IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
244 --Since we have nothing to do, just return a NOOP "we did it"
246 ELSIF dyn_profile.replace_rule <> '' THEN
247 source_marc = v_marc;
248 target_marc = eg_marc;
249 replace_rule = dyn_profile.replace_rule;
251 source_marc = eg_marc;
252 target_marc = v_marc;
253 replace_rule = dyn_profile.preserve_rule;
256 UPDATE authority.record_entry
257 SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
261 UPDATE vandelay.queued_authority_record
262 SET imported_as = eg_id,
264 WHERE id = import_id;
268 -- RAISE NOTICE 'update of authority.record_entry failed';