3 SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
5 CREATE OR REPLACE FUNCTION unapi.bre (
11 depth INT DEFAULT NULL,
12 slimit HSTORE DEFAULT NULL,
13 soffset HSTORE DEFAULT NULL,
14 include_xmlns BOOL DEFAULT TRUE,
15 pref_lib INT DEFAULT NULL
19 me biblio.record_entry%ROWTYPE;
20 layout unapi.bre_output_layout%ROWTYPE;
21 xfrm config.xml_transform%ROWTYPE;
31 IF org = '-' OR org IS NULL THEN
32 SELECT shortname INTO org FROM evergreen.org_top();
35 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
41 IF format = 'holdings_xml' THEN -- the special case
42 output := unapi.holdings_xml( obj_id, ouid, org, depth, includes, slimit, soffset, include_xmlns);
46 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
48 IF layout.name IS NULL THEN
52 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
54 SELECT * INTO me FROM biblio.record_entry WHERE id = obj_id;
56 -- grab bib_source, if any
57 IF ('cbs' = ANY (includes) AND me.source IS NOT NULL) THEN
58 source := unapi.cbs(me.source,NULL,NULL,NULL,NULL);
63 -- grab SVF if we need them
64 IF ('mra' = ANY (includes)) THEN
65 axml := unapi.mra(obj_id,NULL,NULL,NULL,NULL);
70 -- grab holdings if we need them
71 IF ('holdings_xml' = ANY (includes)) THEN
72 hxml := unapi.holdings_xml(obj_id, ouid, org, depth, array_remove(includes,'holdings_xml'), slimit, soffset, include_xmlns, pref_lib);
78 -- generate our item node
81 IF format = 'marcxml' THEN
83 IF tmp_xml !~ E'<marc:' THEN -- If we're not using the prefixed namespace in this record, then remove all declarations of it
84 tmp_xml := REGEXP_REPLACE(tmp_xml, ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
87 tmp_xml := oils_xslt_process(me.marc, xfrm.xslt)::XML;
90 top_el := REGEXP_REPLACE(tmp_xml, E'^.*?<((?:\\S+:)?' || layout.holdings_element || ').*$', E'\\1');
92 IF source IS NOT NULL THEN
93 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', source || '</' || top_el || E'>\\1');
96 IF axml IS NOT NULL THEN
97 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
100 IF hxml IS NOT NULL THEN -- XXX how do we configure the holdings position?
101 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
104 IF ('bre.unapi' = ANY (includes)) THEN
105 output := REGEXP_REPLACE(
107 '</' || top_el || '>(.*?)',
111 'http://www.w3.org/1999/xhtml' AS xmlns,
113 'tag:open-ils.org:U2@bre/' || obj_id || '/' || org AS title
115 )::TEXT || '</' || top_el || E'>\\1'
121 IF ('bre.extern' = ANY (includes)) THEN
122 output := REGEXP_REPLACE(
124 '</' || top_el || '>(.*?)',
128 'http://open-ils.org/spec/biblio/v1' AS xmlns,
129 me.creator AS creator,
131 me.create_date AS create_date,
132 me.edit_date AS edit_date,
133 me.quality AS quality,
134 me.fingerprint AS fingerprint,
135 me.tcn_source AS tcn_source,
136 me.tcn_value AS tcn_value,
138 me.share_depth AS share_depth,
140 me.deleted AS deleted
142 )::TEXT || '</' || top_el || E'>\\1'
148 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
151 $F$ LANGUAGE PLPGSQL STABLE;
153 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
157 depth INT DEFAULT NULL,
158 includes TEXT[] DEFAULT NULL::TEXT[],
159 slimit HSTORE DEFAULT NULL,
160 soffset HSTORE DEFAULT NULL,
161 include_xmlns BOOL DEFAULT TRUE,
162 pref_lib INT DEFAULT NULL
168 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
169 CASE WHEN ('bre' = ANY ($5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id,
170 (SELECT record_has_holdable_copy FROM asset.record_has_holdable_copy($1)) AS has_holdable
174 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
177 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
179 FROM asset.opac_ou_record_copy_count($2, $1)
183 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
185 FROM asset.staff_ou_record_copy_count($2, $1)
189 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
191 FROM asset.opac_ou_record_copy_count($9, $1)
196 WHEN ('bmp' = ANY ($5)) THEN
198 name monograph_parts,
199 (SELECT XMLAGG(bmp) FROM (
200 SELECT unapi.bmp( id, 'xml', 'monograph_part', array_remove( array_remove($5,'bre'), 'holdings_xml'), $3, $4, $6, $7, FALSE)
201 FROM biblio.monograph_part
202 WHERE NOT deleted AND record = $1
209 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
211 SELECT unapi.acn(y.id,'xml','volume',array_remove( array_remove($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), y.rank, name, label_sortkey
212 FROM evergreen.ranked_volumes($1, $2, $4, $6, $7, $9, $5) AS y
215 SELECT unapi.acn(uris.id,'xml','volume',array_remove( array_remove($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), uris.rank, name, label_sortkey
216 FROM evergreen.located_uris($1, $2, $9) AS uris
219 CASE WHEN ('ssub' = ANY ($5)) THEN
222 (SELECT XMLAGG(ssub) FROM (
223 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
224 FROM serial.subscription
225 WHERE record_entry = $1
229 CASE WHEN ('acp' = ANY ($5)) THEN
232 (SELECT XMLAGG(acp) FROM (
233 SELECT unapi.acp(p.target_copy,'xml','copy',array_remove($5,'acp'), $3, $4, $6, $7, FALSE)
234 FROM biblio.peer_bib_copy_map p
235 JOIN asset.copy c ON (p.target_copy = c.id)
236 WHERE NOT c.deleted AND p.peer_record = $1
237 LIMIT ($6 -> 'acp')::INT
238 OFFSET ($7 -> 'acp')::INT
243 $F$ LANGUAGE SQL STABLE;
245 CREATE OR REPLACE FUNCTION unapi.ssub ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
249 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
250 'tag:open-ils.org:U2@ssub/' || id AS id,
251 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
252 start_date AS start, end_date AS end, expected_date_offset
255 WHEN ('sdist' = ANY ($4)) THEN
256 XMLELEMENT( name distributions,
257 (SELECT XMLAGG(sdist) FROM (
258 SELECT unapi.sdist( id, 'xml', 'distribution', array_remove($4,'ssub'), $5, $6, $7, $8, FALSE)
259 FROM serial.distribution
260 WHERE subscription = ssub.id
266 FROM serial.subscription ssub
268 GROUP BY id, start_date, end_date, expected_date_offset, owning_lib;
269 $F$ LANGUAGE SQL STABLE;
271 CREATE OR REPLACE FUNCTION unapi.sdist ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
275 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
276 'tag:open-ils.org:U2@sdist/' || id AS id,
277 'tag:open-ils.org:U2@acn/' || receive_call_number AS receive_call_number,
278 'tag:open-ils.org:U2@acn/' || bind_call_number AS bind_call_number,
279 unit_label_prefix, label, unit_label_suffix, summary_method
281 unapi.aou( holding_lib, $2, 'holding_lib', array_remove($4,'sdist'), $5, $6, $7, $8),
282 CASE WHEN subscription IS NOT NULL AND ('ssub' = ANY ($4)) THEN unapi.ssub( subscription, 'xml', 'subscription', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE) ELSE NULL END,
284 WHEN ('sstr' = ANY ($4)) THEN
285 XMLELEMENT( name streams,
286 (SELECT XMLAGG(sstr) FROM (
287 SELECT unapi.sstr( id, 'xml', 'stream', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE)
289 WHERE distribution = sdist.id
294 XMLELEMENT( name summaries,
296 WHEN ('sbsum' = ANY ($4)) THEN
297 (SELECT XMLAGG(sbsum) FROM (
298 SELECT unapi.sbsum( id, 'xml', 'serial_summary', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE)
299 FROM serial.basic_summary
300 WHERE distribution = sdist.id
305 WHEN ('sisum' = ANY ($4)) THEN
306 (SELECT XMLAGG(sisum) FROM (
307 SELECT unapi.sisum( id, 'xml', 'serial_summary', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE)
308 FROM serial.index_summary
309 WHERE distribution = sdist.id
314 WHEN ('sssum' = ANY ($4)) THEN
315 (SELECT XMLAGG(sssum) FROM (
316 SELECT unapi.sssum( id, 'xml', 'serial_summary', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE)
317 FROM serial.supplement_summary
318 WHERE distribution = sdist.id
324 FROM serial.distribution sdist
326 GROUP BY id, label, unit_label_prefix, unit_label_suffix, holding_lib, summary_method, subscription, receive_call_number, bind_call_number;
327 $F$ LANGUAGE SQL STABLE;
329 CREATE OR REPLACE FUNCTION unapi.sstr ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
333 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
334 'tag:open-ils.org:U2@sstr/' || id AS id,
337 CASE WHEN distribution IS NOT NULL AND ('sdist' = ANY ($4)) THEN unapi.sssum( distribution, 'xml', 'distribtion', array_remove($4,'sstr'), $5, $6, $7, $8, FALSE) ELSE NULL END,
339 WHEN ('sitem' = ANY ($4)) THEN
340 XMLELEMENT( name items,
341 (SELECT XMLAGG(sitem) FROM (
342 SELECT unapi.sitem( id, 'xml', 'serial_item', array_remove($4,'sstr'), $5, $6, $7, $8, FALSE)
344 WHERE stream = sstr.id
350 FROM serial.stream sstr
352 GROUP BY id, routing_label, distribution;
353 $F$ LANGUAGE SQL STABLE;
355 CREATE OR REPLACE FUNCTION unapi.siss ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
359 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
360 'tag:open-ils.org:U2@siss/' || id AS id,
361 create_date, edit_date, label, date_published,
362 holding_code, holding_type, holding_link_id
364 CASE WHEN subscription IS NOT NULL AND ('ssub' = ANY ($4)) THEN unapi.ssub( subscription, 'xml', 'subscription', array_remove($4,'siss'), $5, $6, $7, $8, FALSE) ELSE NULL END,
366 WHEN ('sitem' = ANY ($4)) THEN
367 XMLELEMENT( name items,
368 (SELECT XMLAGG(sitem) FROM (
369 SELECT unapi.sitem( id, 'xml', 'serial_item', array_remove($4,'siss'), $5, $6, $7, $8, FALSE)
371 WHERE issuance = sstr.id
377 FROM serial.issuance sstr
379 GROUP BY id, create_date, edit_date, label, date_published, holding_code, holding_type, holding_link_id, subscription;
380 $F$ LANGUAGE SQL STABLE;
382 CREATE OR REPLACE FUNCTION unapi.sitem ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
386 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
387 'tag:open-ils.org:U2@sitem/' || id AS id,
388 'tag:open-ils.org:U2@siss/' || issuance AS issuance,
389 date_expected, date_received
391 CASE WHEN issuance IS NOT NULL AND ('siss' = ANY ($4)) THEN unapi.siss( issuance, $2, 'issuance', array_remove($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
392 CASE WHEN stream IS NOT NULL AND ('sstr' = ANY ($4)) THEN unapi.sstr( stream, $2, 'stream', array_remove($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
393 CASE WHEN unit IS NOT NULL AND ('sunit' = ANY ($4)) THEN unapi.sunit( unit, $2, 'serial_unit', array_remove($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
394 CASE WHEN uri IS NOT NULL AND ('auri' = ANY ($4)) THEN unapi.auri( uri, $2, 'uri', array_remove($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END
395 -- XMLELEMENT( name notes,
397 -- WHEN ('acpn' = ANY ($4)) THEN
398 -- (SELECT XMLAGG(acpn) FROM (
399 -- SELECT unapi.acpn( id, 'xml', 'copy_note', array_remove($4,'acp'), $5, $6, $7, $8)
400 -- FROM asset.copy_note
401 -- WHERE owning_copy = cp.id AND pub
407 FROM serial.item sitem
409 $F$ LANGUAGE SQL STABLE;
412 CREATE OR REPLACE FUNCTION unapi.sssum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
416 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
417 'tag:open-ils.org:U2@sbsum/' || id AS id,
418 'sssum' AS type, generated_coverage, textual_holdings, show_generated
420 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', array_remove($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
422 FROM serial.supplement_summary ssum
424 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
425 $F$ LANGUAGE SQL STABLE;
427 CREATE OR REPLACE FUNCTION unapi.sbsum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
431 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
432 'tag:open-ils.org:U2@sbsum/' || id AS id,
433 'sbsum' AS type, generated_coverage, textual_holdings, show_generated
435 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', array_remove($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
437 FROM serial.basic_summary ssum
439 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
440 $F$ LANGUAGE SQL STABLE;
442 CREATE OR REPLACE FUNCTION unapi.sisum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
446 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
447 'tag:open-ils.org:U2@sbsum/' || id AS id,
448 'sisum' AS type, generated_coverage, textual_holdings, show_generated
450 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', array_remove($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
452 FROM serial.index_summary ssum
454 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
455 $F$ LANGUAGE SQL STABLE;
457 CREATE OR REPLACE FUNCTION unapi.bmp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
461 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
462 'tag:open-ils.org:U2@bmp/' || id AS id,
466 'tag:open-ils.org:U2@bre/' || record AS record
469 WHEN ('acp' = ANY ($4)) THEN
470 XMLELEMENT( name copies,
471 (SELECT XMLAGG(acp) FROM (
472 SELECT unapi.acp( cp.id, 'xml', 'copy', array_remove($4,'bmp'), $5, $6, $7, $8, FALSE)
474 JOIN asset.copy_part_map cpm ON (cpm.target_copy = cp.id)
476 AND cp.deleted IS FALSE
477 ORDER BY COALESCE(cp.copy_number,0), cp.barcode
478 LIMIT ($7 -> 'acp')::INT
479 OFFSET ($8 -> 'acp')::INT
485 CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( record, 'marcxml', 'record', array_remove($4,'bmp'), $5, $6, $7, $8, FALSE) ELSE NULL END
487 FROM biblio.monograph_part
488 WHERE NOT deleted AND id = $1
489 GROUP BY id, label, label_sortkey, record;
490 $F$ LANGUAGE SQL STABLE;
492 CREATE OR REPLACE FUNCTION unapi.acp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
496 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
497 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
498 create_date, edit_date, copy_number, circulate, deposit,
499 ref, holdable, deleted, deposit_amount, price, barcode,
500 circ_modifier, circ_as_type, opac_visible, age_protect
502 unapi.ccs( status, $2, 'status', array_remove($4,'acp'), $5, $6, $7, $8, FALSE),
503 unapi.acl( location, $2, 'location', array_remove($4,'acp'), $5, $6, $7, $8, FALSE),
504 unapi.aou( circ_lib, $2, 'circ_lib', array_remove($4,'acp'), $5, $6, $7, $8),
505 unapi.aou( circ_lib, $2, 'circlib', array_remove($4,'acp'), $5, $6, $7, $8),
506 CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', array_remove($4,'acp'), $5, $6, $7, $8, FALSE) ELSE NULL END,
508 WHEN ('acpn' = ANY ($4)) THEN
509 XMLELEMENT( name copy_notes,
510 (SELECT XMLAGG(acpn) FROM (
511 SELECT unapi.acpn( id, 'xml', 'copy_note', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
513 WHERE owning_copy = cp.id AND pub
519 WHEN ('ascecm' = ANY ($4)) THEN
520 XMLELEMENT( name statcats,
521 (SELECT XMLAGG(ascecm) FROM (
522 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
523 FROM asset.stat_cat_entry_copy_map
524 WHERE owning_copy = cp.id
530 WHEN ('bre' = ANY ($4)) THEN
531 XMLELEMENT( name foreign_records,
532 (SELECT XMLAGG(bre) FROM (
533 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
534 FROM biblio.peer_bib_copy_map
535 WHERE target_copy = cp.id
542 WHEN ('bmp' = ANY ($4)) THEN
543 XMLELEMENT( name monograph_parts,
544 (SELECT XMLAGG(bmp) FROM (
545 SELECT unapi.bmp( part, 'xml', 'monograph_part', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
546 FROM asset.copy_part_map
547 WHERE target_copy = cp.id
553 WHEN ('circ' = ANY ($4)) THEN
554 XMLELEMENT( name current_circulation,
555 (SELECT XMLAGG(circ) FROM (
556 SELECT unapi.circ( id, 'xml', 'circ', array_remove($4,'circ'), $5, $6, $7, $8, FALSE)
557 FROM action.circulation
558 WHERE target_copy = cp.id
559 AND checkin_time IS NULL
567 AND cp.deleted IS FALSE
568 GROUP BY id, status, location, circ_lib, call_number, create_date,
569 edit_date, copy_number, circulate, deposit, ref, holdable,
570 deleted, deposit_amount, price, barcode, circ_modifier,
571 circ_as_type, opac_visible, age_protect;
572 $F$ LANGUAGE SQL STABLE;
574 CREATE OR REPLACE FUNCTION unapi.sunit ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
578 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
579 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
580 create_date, edit_date, copy_number, circulate, deposit,
581 ref, holdable, deleted, deposit_amount, price, barcode,
582 circ_modifier, circ_as_type, opac_visible, age_protect,
583 status_changed_time, floating, mint_condition,
584 detailed_contents, sort_key, summary_contents, cost
586 unapi.ccs( status, $2, 'status', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE),
587 unapi.acl( location, $2, 'location', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE),
588 unapi.aou( circ_lib, $2, 'circ_lib', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8),
589 unapi.aou( circ_lib, $2, 'circlib', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8),
590 CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', array_remove($4,'acp'), $5, $6, $7, $8, FALSE) ELSE NULL END,
591 XMLELEMENT( name copy_notes,
593 WHEN ('acpn' = ANY ($4)) THEN
594 (SELECT XMLAGG(acpn) FROM (
595 SELECT unapi.acpn( id, 'xml', 'copy_note', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE)
597 WHERE owning_copy = cp.id AND pub
602 XMLELEMENT( name statcats,
604 WHEN ('ascecm' = ANY ($4)) THEN
605 (SELECT XMLAGG(ascecm) FROM (
606 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
607 FROM asset.stat_cat_entry_copy_map
608 WHERE owning_copy = cp.id
613 XMLELEMENT( name foreign_records,
615 WHEN ('bre' = ANY ($4)) THEN
616 (SELECT XMLAGG(bre) FROM (
617 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
618 FROM biblio.peer_bib_copy_map
619 WHERE target_copy = cp.id
625 WHEN ('bmp' = ANY ($4)) THEN
626 XMLELEMENT( name monograph_parts,
627 (SELECT XMLAGG(bmp) FROM (
628 SELECT unapi.bmp( part, 'xml', 'monograph_part', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
629 FROM asset.copy_part_map
630 WHERE target_copy = cp.id
636 WHEN ('circ' = ANY ($4)) THEN
637 XMLELEMENT( name current_circulation,
638 (SELECT XMLAGG(circ) FROM (
639 SELECT unapi.circ( id, 'xml', 'circ', array_remove($4,'circ'), $5, $6, $7, $8, FALSE)
640 FROM action.circulation
641 WHERE target_copy = cp.id
642 AND checkin_time IS NULL
650 AND cp.deleted IS FALSE
651 GROUP BY id, status, location, circ_lib, call_number, create_date,
652 edit_date, copy_number, circulate, floating, mint_condition,
653 deposit, ref, holdable, deleted, deposit_amount, price,
654 barcode, circ_modifier, circ_as_type, opac_visible,
655 status_changed_time, detailed_contents, sort_key,
656 summary_contents, cost, age_protect;
657 $F$ LANGUAGE SQL STABLE;
659 CREATE OR REPLACE FUNCTION unapi.acn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
663 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
664 'tag:open-ils.org:U2@acn/' || acn.id AS id,
665 acn.id AS vol_id, o.shortname AS lib,
666 o.opac_visible AS opac_visible,
667 deleted, label, label_sortkey, label_class, record
669 unapi.aou( owning_lib, $2, 'owning_lib', array_remove($4,'acn'), $5, $6, $7, $8),
671 WHEN ('acp' = ANY ($4)) THEN
672 CASE WHEN $6 IS NOT NULL THEN
673 XMLELEMENT( name copies,
674 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
675 SELECT unapi.acp( cp.id, 'xml', 'copy', array_remove($4,'acn'), $5, $6, $7, $8, FALSE),
676 evergreen.rank_cp(cp) AS rank_avail
678 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5), $6) aoud ON (cp.circ_lib = aoud.id)
679 WHERE cp.call_number = acn.id
680 AND cp.deleted IS FALSE
681 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
682 LIMIT ($7 -> 'acp')::INT
683 OFFSET ($8 -> 'acp')::INT
687 XMLELEMENT( name copies,
688 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
689 SELECT unapi.acp( cp.id, 'xml', 'copy', array_remove($4,'acn'), $5, $6, $7, $8, FALSE),
690 evergreen.rank_cp(cp) AS rank_avail
692 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5) ) aoud ON (cp.circ_lib = aoud.id)
693 WHERE cp.call_number = acn.id
694 AND cp.deleted IS FALSE
695 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
696 LIMIT ($7 -> 'acp')::INT
697 OFFSET ($8 -> 'acp')::INT
705 (SELECT XMLAGG(auri) FROM (SELECT unapi.auri(uri,'xml','uri', array_remove($4,'acn'), $5, $6, $7, $8, FALSE) FROM asset.uri_call_number_map WHERE call_number = acn.id)x)
707 unapi.acnp( acn.prefix, 'marcxml', 'prefix', array_remove($4,'acn'), $5, $6, $7, $8, FALSE),
708 unapi.acns( acn.suffix, 'marcxml', 'suffix', array_remove($4,'acn'), $5, $6, $7, $8, FALSE),
709 CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( acn.record, 'marcxml', 'record', array_remove($4,'acn'), $5, $6, $7, $8, FALSE) ELSE NULL END
711 FROM asset.call_number acn
712 JOIN actor.org_unit o ON (o.id = acn.owning_lib)
714 AND acn.deleted IS FALSE
715 GROUP BY acn.id, o.shortname, o.opac_visible, deleted, label, label_sortkey, label_class, owning_lib, record, acn.prefix, acn.suffix;
716 $F$ LANGUAGE SQL STABLE;
718 CREATE OR REPLACE FUNCTION unapi.auri ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
722 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
723 'tag:open-ils.org:U2@auri/' || uri.id AS id,
729 WHEN ('acn' = ANY ($4)) THEN
730 XMLELEMENT( name copies,
731 (SELECT XMLAGG(acn) FROM (SELECT unapi.acn( call_number, 'xml', 'copy', array_remove($4,'auri'), $5, $6, $7, $8, FALSE) FROM asset.uri_call_number_map WHERE uri = uri.id)x)
738 GROUP BY uri.id, use_restriction, href, label;
739 $F$ LANGUAGE SQL STABLE;
741 CREATE OR REPLACE FUNCTION unapi.circ (obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT DEFAULT '-', depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
745 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
746 'tag:open-ils.org:U2@circ/' || id AS id,
750 CASE WHEN ('aou' = ANY ($4)) THEN unapi.aou( circ_lib, $2, 'circ_lib', array_remove($4,'circ'), $5, $6, $7, $8, FALSE) ELSE NULL END,
751 CASE WHEN ('acp' = ANY ($4)) THEN unapi.acp( circ_lib, $2, 'target_copy', array_remove($4,'circ'), $5, $6, $7, $8, FALSE) ELSE NULL END
753 FROM action.circulation
755 $F$ LANGUAGE SQL STABLE;
757 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
761 depth INT DEFAULT NULL,
762 includes TEXT[] DEFAULT NULL::TEXT[],
763 slimit HSTORE DEFAULT NULL,
764 soffset HSTORE DEFAULT NULL,
765 include_xmlns BOOL DEFAULT TRUE,
766 pref_lib INT DEFAULT NULL
772 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
773 CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
774 (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
778 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
781 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
783 FROM asset.opac_ou_metarecord_copy_count($2, $1)
787 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
789 FROM asset.staff_ou_metarecord_copy_count($2, $1)
793 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
795 FROM asset.opac_ou_metarecord_copy_count($9, $1)
799 -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
802 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
804 SELECT unapi.acn(y.id,'xml','volume',array_remove( array_remove($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), y.rank, name, label_sortkey
805 FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
808 SELECT unapi.acn(uris.id,'xml','volume',array_remove( array_remove($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), uris.rank, name, label_sortkey
809 FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
812 CASE WHEN ('ssub' = ANY ($5)) THEN
815 (SELECT XMLAGG(ssub) FROM (
816 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
817 FROM serial.subscription
818 WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
823 $F$ LANGUAGE SQL STABLE;
825 CREATE OR REPLACE FUNCTION unapi.mmr (
831 depth INT DEFAULT NULL,
832 slimit HSTORE DEFAULT NULL,
833 soffset HSTORE DEFAULT NULL,
834 include_xmlns BOOL DEFAULT TRUE,
835 pref_lib INT DEFAULT NULL
839 mmrec metabib.metarecord%ROWTYPE;
840 leadrec biblio.record_entry%ROWTYPE;
841 subrec biblio.record_entry%ROWTYPE;
842 layout unapi.bre_output_layout%ROWTYPE;
843 xfrm config.xml_transform%ROWTYPE;
845 xml_buf TEXT; -- growing XML document
846 tmp_xml TEXT; -- single-use XML string
847 xml_frag TEXT; -- single-use XML fragment
852 subxml XML; -- subordinate records elements
857 -- xpath for extracting bre.marc values from subordinate records
858 -- so they may be appended to the MARC of the master record prior
859 -- to XSLT processing.
860 -- subjects, isbn, issn, upc -- anything else?
862 '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
864 IF org = '-' OR org IS NULL THEN
865 SELECT shortname INTO org FROM evergreen.org_top();
868 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
874 SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
879 -- TODO: aggregate holdings from constituent records
880 IF format = 'holdings_xml' THEN -- the special case
881 output := unapi.mmr_holdings_xml(
882 obj_id, ouid, org, depth,
883 array_remove(includes,'holdings_xml'),
884 slimit, soffset, include_xmlns, pref_lib);
888 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
890 IF layout.name IS NULL THEN
894 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
896 SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
898 -- Grab distinct MVF for all records if requested
899 IF ('mra' = ANY (includes)) THEN
900 axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,org,depth,NULL,NULL,TRUE,pref_lib);
905 xml_buf = leadrec.marc;
908 IF ('holdings_xml' = ANY (includes)) THEN
909 hxml := unapi.mmr_holdings_xml(
910 obj_id, ouid, org, depth,
911 array_remove(includes,'holdings_xml'),
912 slimit, soffset, include_xmlns, pref_lib);
916 parts := '{}'::TEXT[];
917 FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
918 JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
919 JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
920 WHERE mmr.id = obj_id AND NOT bre.deleted
921 ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
922 LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
924 IF subrec.id = leadrec.id THEN CONTINUE; END IF;
925 -- Append choice data from the the non-lead records to the
926 -- the lead record document
928 parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
931 SELECT ARRAY_TO_STRING( ARRAY_AGG( DISTINCT p ), '' )::XML INTO subxml FROM UNNEST(parts) p;
933 -- append data from the subordinate records to the
934 -- main record document before applying the XSLT
936 IF subxml IS NOT NULL THEN
937 xml_buf := REGEXP_REPLACE(xml_buf,
938 '</record>(.*?)$', subxml || '</record>' || E'\\1');
941 IF format = 'marcxml' THEN
942 -- If we're not using the prefixed namespace in
943 -- this record, then remove all declarations of it
944 IF xml_buf !~ E'<marc:' THEN
945 xml_buf := REGEXP_REPLACE(xml_buf,
946 ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
949 xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
952 -- update top_el to reflect the change in xml_buf, which may
953 -- now be a different type of document (e.g. record -> mods)
954 top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' ||
955 layout.holdings_element || ').*$', E'\\1');
957 IF axml IS NOT NULL THEN
958 xml_buf := REGEXP_REPLACE(xml_buf,
959 '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
962 IF hxml IS NOT NULL THEN
963 xml_buf := REGEXP_REPLACE(xml_buf,
964 '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
967 IF ('mmr.unapi' = ANY (includes)) THEN
968 output := REGEXP_REPLACE(
970 '</' || top_el || '>(.*?)',
974 'http://www.w3.org/1999/xhtml' AS xmlns,
976 'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
978 )::TEXT || '</' || top_el || E'>\\1'
984 -- remove ignorable whitesace
985 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
988 $F$ LANGUAGE PLPGSQL STABLE;
990 CREATE OR REPLACE FUNCTION authority.extract_headings(marc TEXT, restrict INT[] DEFAULT NULL) RETURNS SETOF authority.heading AS $func$
992 idx authority.heading_field%ROWTYPE;
993 xfrm config.xml_transform%ROWTYPE;
995 transformed_xml TEXT;
997 heading_node_list TEXT[];
999 component_node_list TEXT[];
1001 normalized_text TEXT;
1006 base_thesaurus TEXT := NULL;
1007 output_row authority.heading;
1010 -- Loop over the indexing entries
1011 FOR idx IN SELECT * FROM authority.heading_field WHERE restrict IS NULL OR id = ANY (restrict) ORDER BY format LOOP
1013 output_row.field := idx.id;
1014 output_row.type := idx.heading_type;
1015 output_row.purpose := idx.heading_purpose;
1017 joiner := COALESCE(idx.joiner, ' ');
1019 SELECT INTO xfrm * from config.xml_transform WHERE name = idx.format;
1021 -- See if we can skip the XSLT ... it's expensive
1022 IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
1023 -- Can't skip the transform
1024 IF xfrm.xslt <> '---' THEN
1025 transformed_xml := oils_xslt_process(marc, xfrm.xslt);
1027 transformed_xml := marc;
1030 prev_xfrm := xfrm.name;
1033 IF idx.thesaurus_xpath IS NOT NULL THEN
1034 base_thesaurus := ARRAY_TO_STRING(oils_xpath(idx.thesaurus_xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]), '');
1037 heading_node_list := oils_xpath( idx.heading_xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1039 FOR heading_node IN SELECT x FROM unnest(heading_node_list) AS x LOOP
1041 CONTINUE WHEN heading_node !~ E'^\\s*<';
1043 output_row.variant_type := NULL;
1044 output_row.related_type := NULL;
1045 output_row.thesaurus := NULL;
1046 output_row.heading := NULL;
1048 IF idx.heading_purpose = 'variant' AND idx.type_xpath IS NOT NULL THEN
1049 type_value := ARRAY_TO_STRING(oils_xpath(idx.type_xpath, heading_node, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]), '');
1051 output_row.variant_type := type_value;
1052 EXCEPTION WHEN invalid_text_representation THEN
1053 RAISE NOTICE 'Do not recognize variant heading type %', type_value;
1056 IF idx.heading_purpose = 'related' AND idx.type_xpath IS NOT NULL THEN
1057 type_value := ARRAY_TO_STRING(oils_xpath(idx.type_xpath, heading_node, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]), '');
1059 output_row.related_type := type_value;
1060 EXCEPTION WHEN invalid_text_representation THEN
1061 RAISE NOTICE 'Do not recognize related heading type %', type_value;
1065 IF idx.thesaurus_override_xpath IS NOT NULL THEN
1066 output_row.thesaurus := ARRAY_TO_STRING(oils_xpath(idx.thesaurus_override_xpath, heading_node, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]), '');
1068 IF output_row.thesaurus IS NULL THEN
1069 output_row.thesaurus := base_thesaurus;
1074 -- now iterate over components of heading
1075 component_node_list := oils_xpath( idx.component_xpath, heading_node, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1076 FOR component_node IN SELECT x FROM unnest(component_node_list) AS x LOOP
1077 -- XXX much of this should be moved into oils_xpath_string...
1078 curr_text := ARRAY_TO_STRING(array_remove(array_remove(
1079 oils_xpath( '//text()', -- get the content of all the nodes within the main selected node
1080 REGEXP_REPLACE( component_node, E'\\s+', ' ', 'g' ) -- Translate adjacent whitespace to a single space
1081 ), ' '), ''), -- throw away morally empty (bankrupt?) strings
1085 CONTINUE WHEN curr_text IS NULL OR curr_text = '';
1087 IF raw_text IS NOT NULL THEN
1088 raw_text := raw_text || joiner;
1091 raw_text := COALESCE(raw_text,'') || curr_text;
1094 IF raw_text IS NOT NULL THEN
1095 output_row.heading := raw_text;
1096 normalized_text := raw_text;
1099 SELECT n.func AS func,
1100 n.param_count AS param_count,
1102 FROM config.index_normalizer n
1103 JOIN authority.heading_field_norm_map m ON (m.norm = n.id)
1104 WHERE m.field = idx.id
1107 EXECUTE 'SELECT ' || normalizer.func || '(' ||
1108 quote_literal( normalized_text ) ||
1110 WHEN normalizer.param_count > 0
1111 THEN ',' || REPLACE(REPLACE(BTRIM(normalizer.params,'[]'),E'\'',E'\\\''),E'"',E'\'')
1114 ')' INTO normalized_text;
1118 output_row.normalized_heading := normalized_text;
1120 RETURN NEXT output_row;
1126 $func$ LANGUAGE PLPGSQL;
1128 CREATE OR REPLACE FUNCTION authority.extract_headings(rid BIGINT, restrict INT[] DEFAULT NULL) RETURNS SETOF authority.heading AS $func$
1130 auth authority.record_entry%ROWTYPE;
1131 output_row authority.heading;
1134 SELECT INTO auth * FROM authority.record_entry WHERE id = rid;
1136 RETURN QUERY SELECT * FROM authority.extract_headings(auth.marc, restrict);
1138 $func$ LANGUAGE PLPGSQL;
1145 CREATE OR REPLACE FUNCTION biblio.extract_metabib_field_entry (
1147 default_joiner TEXT,
1150 ) RETURNS SETOF metabib.field_entry_template AS $func$
1152 bib biblio.record_entry%ROWTYPE;
1153 idx config.metabib_field%ROWTYPE;
1154 xfrm config.xml_transform%ROWTYPE;
1156 transformed_xml TEXT;
1158 xml_node_list TEXT[];
1165 joiner TEXT := default_joiner; -- XXX will index defs supply a joiner?
1166 authority_text TEXT;
1167 authority_link BIGINT;
1168 output_row metabib.field_entry_template%ROWTYPE;
1172 -- Start out with no field-use bools set
1173 output_row.browse_field = FALSE;
1174 output_row.facet_field = FALSE;
1175 output_row.display_field = FALSE;
1176 output_row.search_field = FALSE;
1179 SELECT INTO bib * FROM biblio.record_entry WHERE id = rid;
1181 -- Loop over the indexing entries
1182 FOR idx IN SELECT * FROM config.metabib_field WHERE id = ANY (only_fields) ORDER BY format LOOP
1183 CONTINUE WHEN idx.xpath IS NULL OR idx.xpath = ''; -- pure virtual field
1185 process_idx := FALSE;
1186 IF idx.display_field AND 'display' = ANY (field_types) THEN process_idx = TRUE; END IF;
1187 IF idx.browse_field AND 'browse' = ANY (field_types) THEN process_idx = TRUE; END IF;
1188 IF idx.search_field AND 'search' = ANY (field_types) THEN process_idx = TRUE; END IF;
1189 IF idx.facet_field AND 'facet' = ANY (field_types) THEN process_idx = TRUE; END IF;
1190 CONTINUE WHEN process_idx = FALSE; -- disabled for all types
1192 joiner := COALESCE(idx.joiner, default_joiner);
1194 SELECT INTO xfrm * from config.xml_transform WHERE name = idx.format;
1196 -- See if we can skip the XSLT ... it's expensive
1197 IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
1198 -- Can't skip the transform
1199 IF xfrm.xslt <> '---' THEN
1200 transformed_xml := oils_xslt_process(bib.marc,xfrm.xslt);
1202 transformed_xml := bib.marc;
1205 prev_xfrm := xfrm.name;
1208 xml_node_list := oils_xpath( idx.xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1211 FOR xml_node IN SELECT x FROM unnest(xml_node_list) AS x LOOP
1212 CONTINUE WHEN xml_node !~ E'^\\s*<';
1214 -- XXX much of this should be moved into oils_xpath_string...
1215 curr_text := ARRAY_TO_STRING(array_remove(array_remove(
1216 oils_xpath( '//text()', -- get the content of all the nodes within the main selected node
1217 REGEXP_REPLACE( xml_node, E'\\s+', ' ', 'g' ) -- Translate adjacent whitespace to a single space
1218 ), ' '), ''), -- throw away morally empty (bankrupt?) strings
1222 CONTINUE WHEN curr_text IS NULL OR curr_text = '';
1224 IF raw_text IS NOT NULL THEN
1225 raw_text := raw_text || joiner;
1228 raw_text := COALESCE(raw_text,'') || curr_text;
1230 -- autosuggest/metabib.browse_entry
1231 IF idx.browse_field THEN
1233 IF idx.browse_xpath IS NOT NULL AND idx.browse_xpath <> '' THEN
1234 browse_text := oils_xpath_string( idx.browse_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1236 browse_text := curr_text;
1239 IF idx.browse_sort_xpath IS NOT NULL AND
1240 idx.browse_sort_xpath <> '' THEN
1242 sort_value := oils_xpath_string(
1243 idx.browse_sort_xpath, xml_node, joiner,
1244 ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]
1247 sort_value := browse_text;
1250 output_row.field_class = idx.field_class;
1251 output_row.field = idx.id;
1252 output_row.source = rid;
1253 output_row.value = BTRIM(REGEXP_REPLACE(browse_text, E'\\s+', ' ', 'g'));
1254 output_row.sort_value :=
1255 public.naco_normalize(sort_value);
1257 output_row.authority := NULL;
1259 IF idx.authority_xpath IS NOT NULL AND idx.authority_xpath <> '' THEN
1260 authority_text := oils_xpath_string(
1261 idx.authority_xpath, xml_node, joiner,
1263 ARRAY[xfrm.prefix, xfrm.namespace_uri],
1264 ARRAY['xlink','http://www.w3.org/1999/xlink']
1268 IF authority_text ~ '^\d+$' THEN
1269 authority_link := authority_text::BIGINT;
1270 PERFORM * FROM authority.record_entry WHERE id = authority_link;
1272 output_row.authority := authority_link;
1278 output_row.browse_field = TRUE;
1279 -- Returning browse rows with search_field = true for search+browse
1280 -- configs allows us to retain granularity of being able to search
1281 -- browse fields with "starts with" type operators (for example, for
1282 -- titles of songs in music albums)
1283 IF idx.search_field THEN
1284 output_row.search_field = TRUE;
1286 RETURN NEXT output_row;
1287 output_row.browse_field = FALSE;
1288 output_row.search_field = FALSE;
1289 output_row.sort_value := NULL;
1292 -- insert raw node text for faceting
1293 IF idx.facet_field THEN
1295 IF idx.facet_xpath IS NOT NULL AND idx.facet_xpath <> '' THEN
1296 facet_text := oils_xpath_string( idx.facet_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1298 facet_text := curr_text;
1301 output_row.field_class = idx.field_class;
1302 output_row.field = -1 * idx.id;
1303 output_row.source = rid;
1304 output_row.value = BTRIM(REGEXP_REPLACE(facet_text, E'\\s+', ' ', 'g'));
1306 output_row.facet_field = TRUE;
1307 RETURN NEXT output_row;
1308 output_row.facet_field = FALSE;
1311 -- insert raw node text for display
1312 IF idx.display_field THEN
1314 IF idx.display_xpath IS NOT NULL AND idx.display_xpath <> '' THEN
1315 display_text := oils_xpath_string( idx.display_xpath, xml_node, joiner, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]] );
1317 display_text := curr_text;
1320 output_row.field_class = idx.field_class;
1321 output_row.field = -1 * idx.id;
1322 output_row.source = rid;
1323 output_row.value = BTRIM(REGEXP_REPLACE(display_text, E'\\s+', ' ', 'g'));
1325 output_row.display_field = TRUE;
1326 RETURN NEXT output_row;
1327 output_row.display_field = FALSE;
1332 CONTINUE WHEN raw_text IS NULL OR raw_text = '';
1334 -- insert combined node text for searching
1335 IF idx.search_field THEN
1336 output_row.field_class = idx.field_class;
1337 output_row.field = idx.id;
1338 output_row.source = rid;
1339 output_row.value = BTRIM(REGEXP_REPLACE(raw_text, E'\\s+', ' ', 'g'));
1341 output_row.search_field = TRUE;
1342 RETURN NEXT output_row;
1343 output_row.search_field = FALSE;
1349 $func$ LANGUAGE PLPGSQL;
1351 -- Shall we drop it?
1352 -- DROP FUNCTION evergreen.array_remove_item_by_value(ANYARRAY, ANYELEMENT)