]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/ZZZZ.UNDO.metabib-display-field.sql
60c4cd1ae7f28741fa349a6b35df46286b7253f6
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / ZZZZ.UNDO.metabib-display-field.sql
1 -- XXX REVERT FILE -- DELETE THIS BEFORE MERGING XXX --
2
3 DELETE FROM metabib.display_entry;
4 DELETE FROM config.display_field_map;
5 DELETE FROM config.metabib_field WHERE display_field; -- ASSUMES ALL NEW FIELDS
6
7 DELETE FROM config.internal_flag WHERE name = 'ingest.skip_display_indexing';
8
9 BEGIN;
10
11 DROP FUNCTION metabib.reingest_metabib_field_entries(BIGINT, BOOL, BOOL, BOOL, BOOL);
12
13 CREATE OR REPLACE FUNCTION metabib.reingest_metabib_field_entries( bib_id BIGINT, skip_facet BOOL DEFAULT FALSE, skip_browse BOOL DEFAULT FALSE, skip_search BOOL DEFAULT FALSE ) RETURNS VOID AS $func$
14 DECLARE
15     fclass          RECORD;
16     ind_data        metabib.field_entry_template%ROWTYPE;
17     mbe_row         metabib.browse_entry%ROWTYPE;
18     mbe_id          BIGINT;
19     b_skip_facet    BOOL;
20     b_skip_browse   BOOL;
21     b_skip_search   BOOL;
22     value_prepped   TEXT;
23 BEGIN
24
25     SELECT COALESCE(NULLIF(skip_facet, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_facet_indexing' AND enabled)) INTO b_skip_facet;
26     SELECT COALESCE(NULLIF(skip_browse, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_browse_indexing' AND enabled)) INTO b_skip_browse;
27     SELECT COALESCE(NULLIF(skip_search, FALSE), EXISTS (SELECT enabled FROM config.internal_flag WHERE name =  'ingest.skip_search_indexing' AND enabled)) INTO b_skip_search;
28
29     PERFORM * FROM config.internal_flag WHERE name = 'ingest.assume_inserts_only' AND enabled;
30     IF NOT FOUND THEN
31         IF NOT b_skip_search THEN
32             FOR fclass IN SELECT * FROM config.metabib_class LOOP
33                 -- RAISE NOTICE 'Emptying out %', fclass.name;
34                 EXECUTE $$DELETE FROM metabib.$$ || fclass.name || $$_field_entry WHERE source = $$ || bib_id;
35             END LOOP;
36         END IF;
37         IF NOT b_skip_facet THEN
38             DELETE FROM metabib.facet_entry WHERE source = bib_id;
39         END IF;
40         IF NOT b_skip_browse THEN
41             DELETE FROM metabib.browse_entry_def_map WHERE source = bib_id;
42         END IF;
43     END IF;
44
45     FOR ind_data IN SELECT * FROM biblio.extract_metabib_field_entry( bib_id ) LOOP
46
47         -- don't store what has been normalized away
48         CONTINUE WHEN ind_data.value IS NULL;
49
50         IF ind_data.field < 0 THEN
51             ind_data.field = -1 * ind_data.field;
52         END IF;
53
54         IF ind_data.facet_field AND NOT b_skip_facet THEN
55             INSERT INTO metabib.facet_entry (field, source, value)
56                 VALUES (ind_data.field, ind_data.source, ind_data.value);
57         END IF;
58
59         IF ind_data.browse_field AND NOT b_skip_browse THEN
60             -- A caveat about this SELECT: this should take care of replacing
61             -- old mbe rows when data changes, but not if normalization (by
62             -- which I mean specifically the output of
63             -- evergreen.oils_tsearch2()) changes.  It may or may not be
64             -- expensive to add a comparison of index_vector to index_vector
65             -- to the WHERE clause below.
66
67             CONTINUE WHEN ind_data.sort_value IS NULL;
68
69             value_prepped := metabib.browse_normalize(ind_data.value, ind_data.field);
70             SELECT INTO mbe_row * FROM metabib.browse_entry
71                 WHERE value = value_prepped AND sort_value = ind_data.sort_value;
72
73             IF FOUND THEN
74                 mbe_id := mbe_row.id;
75             ELSE
76                 INSERT INTO metabib.browse_entry
77                     ( value, sort_value ) VALUES
78                     ( value_prepped, ind_data.sort_value );
79
80                 mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
81             END IF;
82
83             INSERT INTO metabib.browse_entry_def_map (entry, def, source, authority)
84                 VALUES (mbe_id, ind_data.field, ind_data.source, ind_data.authority);
85         END IF;
86
87         IF ind_data.search_field AND NOT b_skip_search THEN
88             -- Avoid inserting duplicate rows
89             EXECUTE 'SELECT 1 FROM metabib.' || ind_data.field_class ||
90                 '_field_entry WHERE field = $1 AND source = $2 AND value = $3'
91                 INTO mbe_id USING ind_data.field, ind_data.source, ind_data.value;
92                 -- RAISE NOTICE 'Search for an already matching row returned %', mbe_id;
93             IF mbe_id IS NULL THEN
94                 EXECUTE $$
95                 INSERT INTO metabib.$$ || ind_data.field_class || $$_field_entry (field, source, value)
96                     VALUES ($$ ||
97                         quote_literal(ind_data.field) || $$, $$ ||
98                         quote_literal(ind_data.source) || $$, $$ ||
99                         quote_literal(ind_data.value) ||
100                     $$);$$;
101             END IF;
102         END IF;
103
104     END LOOP;
105
106     IF NOT b_skip_search THEN
107         PERFORM metabib.update_combined_index_vectors(bib_id);
108     END IF;
109
110     RETURN;
111 END;
112 $func$ LANGUAGE PLPGSQL;
113
114
115 CREATE OR REPLACE FUNCTION biblio.extract_metabib_field_entry ( rid BIGINT, default_joiner TEXT ) RETURNS SETOF metabib.field_entry_template AS $func$
116 DECLARE
117     bib     biblio.record_entry%ROWTYPE;
118     idx     config.metabib_field%ROWTYPE;
119     xfrm        config.xml_transform%ROWTYPE;
120     prev_xfrm   TEXT;
121     transformed_xml TEXT;
122     xml_node    TEXT;
123     xml_node_list   TEXT[];
124     facet_text  TEXT;
125     browse_text TEXT;
126     sort_value  TEXT;
127     raw_text    TEXT;
128     curr_text   TEXT;
129     joiner      TEXT := default_joiner; -- XXX will index defs supply a joiner?
130     authority_text TEXT;
131     authority_link BIGINT;
132     output_row  metabib.field_entry_template%ROWTYPE;
133 BEGIN
134
135     -- Start out with no field-use bools set
136     output_row.browse_field = FALSE;
137     output_row.facet_field = FALSE;
138     output_row.search_field = FALSE;
139
140     -- Get the record
141     SELECT INTO bib * FROM biblio.record_entry WHERE id = rid;
142
143     -- Loop over the indexing entries
144     FOR idx IN SELECT * FROM config.metabib_field ORDER BY format LOOP
145
146         joiner := COALESCE(idx.joiner, default_joiner);
147
148         SELECT INTO xfrm * from config.xml_transform WHERE name = idx.format;
149
150         -- See if we can skip the XSLT ... it's expensive
151         IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
152             -- Can't skip the transform
153             IF xfrm.xslt <> '---' THEN
154                 transformed_xml := oils_xslt_process(bib.marc,xfrm.xslt);
155             ELSE
156                 transformed_xml := bib.marc;
157             END IF;
158
159             prev_xfrm := xfrm.name;
160         END IF;
161
162         xml_node_list := oils_xpath( idx.xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
163
164         raw_text := NULL;
165         FOR xml_node IN SELECT x FROM unnest(xml_node_list) AS x LOOP
166             CONTINUE WHEN xml_node !~ E'^\\s*<';
167
168             -- XXX much of this should be moved into oils_xpath_string...
169             curr_text := ARRAY_TO_STRING(evergreen.array_remove_item_by_value(evergreen.array_remove_item_by_value(
170                 oils_xpath( '//text()', -- get the content of all the nodes within the main selected node
171                     REGEXP_REPLACE( xml_node, E'\\s+', ' ', 'g' ) -- Translate adjacent whitespace to a single space
172                 ), ' '), ''),  -- throw away morally empty (bankrupt?) strings
173                 joiner
174             );
175
176             CONTINUE WHEN curr_text IS NULL OR curr_text = '';
177
178             IF raw_text IS NOT NULL THEN
179                 raw_text := raw_text || joiner;
180             END IF;
181
182             raw_text := COALESCE(raw_text,'') || curr_text;
183
184             -- autosuggest/metabib.browse_entry
185             IF idx.browse_field THEN
186
187                 IF idx.browse_xpath IS NOT NULL AND idx.browse_xpath <> '' THEN
188                     browse_text := oils_xpath_string( idx.browse_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
189                 ELSE
190                     browse_text := curr_text;
191                 END IF;
192
193                 IF idx.browse_sort_xpath IS NOT NULL AND
194                     idx.browse_sort_xpath <> '' THEN
195
196                     sort_value := oils_xpath_string(
197                         idx.browse_sort_xpath, xml_node, joiner,
198                         ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]
199                     );
200                 ELSE
201                     sort_value := browse_text;
202                 END IF;
203
204                 output_row.field_class = idx.field_class;
205                 output_row.field = idx.id;
206                 output_row.source = rid;
207                 output_row.value = BTRIM(REGEXP_REPLACE(browse_text, E'\\s+', ' ', 'g'));
208                 output_row.sort_value :=
209                     public.naco_normalize(sort_value);
210
211                 output_row.authority := NULL;
212
213                 IF idx.authority_xpath IS NOT NULL AND idx.authority_xpath <> '' THEN
214                     authority_text := oils_xpath_string(
215                         idx.authority_xpath, xml_node, joiner,
216                         ARRAY[
217                             ARRAY[xfrm.prefix, xfrm.namespace_uri],
218                             ARRAY['xlink','http://www.w3.org/1999/xlink']
219                         ]
220                     );
221
222                     IF authority_text ~ '^\d+$' THEN
223                         authority_link := authority_text::BIGINT;
224                         PERFORM * FROM authority.record_entry WHERE id = authority_link;
225                         IF FOUND THEN
226                             output_row.authority := authority_link;
227                         END IF;
228                     END IF;
229
230                 END IF;
231
232                 output_row.browse_field = TRUE;
233                 -- Returning browse rows with search_field = true for search+browse
234                 -- configs allows us to retain granularity of being able to search
235                 -- browse fields with "starts with" type operators (for example, for
236                 -- titles of songs in music albums)
237                 IF idx.search_field THEN
238                     output_row.search_field = TRUE;
239                 END IF;
240                 RETURN NEXT output_row;
241                 output_row.browse_field = FALSE;
242                 output_row.search_field = FALSE;
243                 output_row.sort_value := NULL;
244             END IF;
245
246             -- insert raw node text for faceting
247             IF idx.facet_field THEN
248
249                 IF idx.facet_xpath IS NOT NULL AND idx.facet_xpath <> '' THEN
250                     facet_text := oils_xpath_string( idx.facet_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
251                 ELSE
252                     facet_text := curr_text;
253                 END IF;
254
255                 output_row.field_class = idx.field_class;
256                 output_row.field = -1 * idx.id;
257                 output_row.source = rid;
258                 output_row.value = BTRIM(REGEXP_REPLACE(facet_text, E'\\s+', ' ', 'g'));
259
260                 output_row.facet_field = TRUE;
261                 RETURN NEXT output_row;
262                 output_row.facet_field = FALSE;
263             END IF;
264
265         END LOOP;
266
267         CONTINUE WHEN raw_text IS NULL OR raw_text = '';
268
269         -- insert combined node text for searching
270         IF idx.search_field THEN
271             output_row.field_class = idx.field_class;
272             output_row.field = idx.id;
273             output_row.source = rid;
274             output_row.value = BTRIM(REGEXP_REPLACE(raw_text, E'\\s+', ' ', 'g'));
275
276             output_row.search_field = TRUE;
277             RETURN NEXT output_row;
278             output_row.search_field = FALSE;
279         END IF;
280
281     END LOOP;
282
283 END;
284
285 $func$ LANGUAGE PLPGSQL;
286
287
288 ALTER TYPE metabib.field_entry_template DROP ATTRIBUTE display_field;
289 DROP TRIGGER display_field_force_nfc_tgr ON metabib.display_entry;
290 DROP FUNCTION evergreen.display_field_force_nfc();
291 DROP TRIGGER display_field_normalize_tgr ON metabib.display_entry;
292 DROP FUNCTION metabib.display_field_normalize_trigger();
293 DROP INDEX metabib.metabib_display_entry_source_idx;
294 DROP INDEX metabib.metabib_display_entry_field_idx;
295 DROP VIEW metabib.flat_display_entry;
296 DROP TABLE config.display_field_map;
297 DROP TABLE metabib.display_entry;
298
299 ALTER TABLE config.metabib_field 
300     DROP COLUMN display_xpath,
301     DROP COLUMN display_field;
302
303 COMMIT;
304
305
306