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