]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/ZZZZ.schema.unapi-mmr.sql
2aaa075f560c2422b54c1c857945e2f5848b8f8a
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / ZZZZ.schema.unapi-mmr.sql
1 BEGIN;
2
3 CREATE OR REPLACE FUNCTION asset.opac_ou_metarecord_copy_count (org INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
4 DECLARE
5     ans RECORD;
6     trans INT;
7 BEGIN
8     SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) JOIN metabib.metarecord_source_map m ON (m.source = b.id) WHERE src.transcendant AND m.metarecord = rid;
9
10     FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
11         RETURN QUERY
12         SELECT  ans.depth,
13                 ans.id,
14                 COUNT( av.id ),
15                 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
16                 COUNT( av.id ),
17                 trans
18           FROM  
19                 actor.org_unit_descendants(ans.id) d
20                 JOIN asset.opac_visible_copies av ON (av.circ_lib = d.id)
21                 JOIN asset.copy cp ON (cp.id = av.copy_id)
22                 JOIN metabib.metarecord_source_map m ON (m.metarecord = rid AND m.source = av.record)
23           GROUP BY 1,2,6;
24
25         IF NOT FOUND THEN
26             RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
27         END IF;
28
29     END LOOP;
30
31     RETURN;
32 END;
33 $f$ LANGUAGE PLPGSQL;
34
35 CREATE OR REPLACE FUNCTION asset.opac_lasso_metarecord_copy_count (i_lasso INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
36 DECLARE
37     ans RECORD;
38     trans INT;
39 BEGIN
40     SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) JOIN metabib.metarecord_source_map m ON (m.source = b.id) WHERE src.transcendant AND m.metarecord = rid;
41
42     FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
43         RETURN QUERY
44         SELECT  -1,
45                 ans.id,
46                 COUNT( av.id ),
47                 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
48                 COUNT( av.id ),
49                 trans
50           FROM
51                 actor.org_unit_descendants(ans.id) d
52                 JOIN asset.opac_visible_copies av ON (av.circ_lib = d.id)
53                 JOIN asset.copy cp ON (cp.id = av.copy_id)
54                 JOIN metabib.metarecord_source_map m ON (m.metarecord = rid AND m.source = av.record)
55           GROUP BY 1,2,6;
56
57         IF NOT FOUND THEN
58             RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
59         END IF;
60
61     END LOOP;   
62                 
63     RETURN;     
64 END;            
65 $f$ LANGUAGE PLPGSQL;
66
67 CREATE OR REPLACE FUNCTION asset.staff_ou_metarecord_copy_count (org INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
68 DECLARE         
69     ans RECORD; 
70     trans INT;
71 BEGIN
72     SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) JOIN metabib.metarecord_source_map m ON (m.source = b.id) WHERE src.transcendant AND m.metarecord = rid;
73
74     FOR ans IN SELECT u.id, t.depth FROM actor.org_unit_ancestors(org) AS u JOIN actor.org_unit_type t ON (u.ou_type = t.id) LOOP
75         RETURN QUERY
76         SELECT  ans.depth,
77                 ans.id,
78                 COUNT( cp.id ),
79                 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
80                 COUNT( cp.id ),
81                 trans
82           FROM
83                 actor.org_unit_descendants(ans.id) d
84                 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
85                 JOIN asset.call_number cn ON (cn.id = cp.call_number AND NOT cn.deleted)
86                 JOIN metabib.metarecord_source_map m ON (m.metarecord = rid AND m.source = cn.record)
87           GROUP BY 1,2,6;
88
89         IF NOT FOUND THEN
90             RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
91         END IF;
92
93     END LOOP;
94
95     RETURN;
96 END;
97 $f$ LANGUAGE PLPGSQL;
98
99 CREATE OR REPLACE FUNCTION asset.staff_lasso_metarecord_copy_count (i_lasso INT, rid BIGINT) RETURNS TABLE (depth INT, org_unit INT, visible BIGINT, available BIGINT, unshadow BIGINT, transcendant INT) AS $f$
100 DECLARE
101     ans RECORD;
102     trans INT;
103 BEGIN
104     SELECT 1 INTO trans FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) JOIN metabib.metarecord_source_map m ON (m.source = b.id) WHERE src.transcendant AND m.metarecord = rid;
105
106     FOR ans IN SELECT u.org_unit AS id FROM actor.org_lasso_map AS u WHERE lasso = i_lasso LOOP
107         RETURN QUERY
108         SELECT  -1,
109                 ans.id,
110                 COUNT( cp.id ),
111                 SUM( CASE WHEN cp.status IN (0,7,12) THEN 1 ELSE 0 END ),
112                 COUNT( cp.id ),
113                 trans
114           FROM
115                 actor.org_unit_descendants(ans.id) d
116                 JOIN asset.copy cp ON (cp.circ_lib = d.id AND NOT cp.deleted)
117                 JOIN asset.call_number cn ON (cn.id = cp.call_number AND NOT cn.deleted)
118                 JOIN metabib.metarecord_source_map m ON (m.metarecord = rid AND m.source = cn.record)
119           GROUP BY 1,2,6;
120
121         IF NOT FOUND THEN
122             RETURN QUERY SELECT ans.depth, ans.id, 0::BIGINT, 0::BIGINT, 0::BIGINT, trans;
123         END IF;
124
125     END LOOP;
126
127     RETURN;
128 END;
129 $f$ LANGUAGE PLPGSQL;
130
131 CREATE OR REPLACE FUNCTION unapi.mmr_mra (
132     obj_id BIGINT,
133     format TEXT,
134     ename TEXT,
135     includes TEXT[],
136     org TEXT,
137     depth INT DEFAULT NULL,
138     slimit HSTORE DEFAULT NULL,
139     soffset HSTORE DEFAULT NULL,
140     include_xmlns BOOL DEFAULT TRUE
141 ) RETURNS XML AS $F$
142     SELECT  XMLELEMENT(
143         name attributes,
144         XMLATTRIBUTES(
145             CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
146             'tag:open-ils.org:U2@mmr/' || $1 AS metarecord
147         ),
148         (SELECT XMLAGG(foo.y)
149           FROM (
150             SELECT  DISTINCT ON (COALESCE(cvm.id,uvm.id))
151                     COALESCE(cvm.id,uvm.id),
152                     XMLELEMENT(
153                         name field,
154                         XMLATTRIBUTES(
155                             mra.attr AS name,
156                             cvm.value AS "coded-value",
157                             cvm.id AS "cvmid",
158                             rad.composite,
159                             rad.multi,
160                             rad.filter,
161                             rad.sorter
162                         ),
163                         mra.value
164                     )
165               FROM  metabib.record_attr_flat mra
166                     JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
167                     LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
168                     LEFT JOIN metabib.uncontrolled_record_attr_value uvm ON (uvm.attr = mra.attr AND uvm.value = mra.value)
169               WHERE mra.id IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
170               ORDER BY 1
171             )foo(id,y)
172         )
173     )
174 $F$ LANGUAGE SQL STABLE;
175
176 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
177     bibid BIGINT[],
178     ouid INT,
179     depth INT DEFAULT NULL,
180     slimit HSTORE DEFAULT NULL,
181     soffset HSTORE DEFAULT NULL,
182     pref_lib INT DEFAULT NULL,
183     includes TEXT[] DEFAULT NULL::TEXT[]
184 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
185     SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
186         SELECT acn.id, aou.name, acn.label_sortkey,
187             evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status),
188             RANK() OVER w
189         FROM asset.call_number acn
190             JOIN asset.copy acp ON (acn.id = acp.call_number)
191             JOIN actor.org_unit_descendants( $2, COALESCE(
192                 $3, (
193                     SELECT depth
194                     FROM actor.org_unit_type aout
195                         INNER JOIN actor.org_unit ou ON ou_type = aout.id
196                     WHERE ou.id = $2
197                 ), $6)
198             ) AS aou ON (acp.circ_lib = aou.id)
199         WHERE acn.record = ANY ($1)
200             AND acn.deleted IS FALSE
201             AND acp.deleted IS FALSE
202             AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN
203                 EXISTS (
204                     SELECT 1
205                     FROM asset.opac_visible_copies
206                     WHERE copy_id = acp.id AND record = acn.record
207                 ) ELSE TRUE END
208         GROUP BY acn.id, acp.status, aou.name, acn.label_sortkey, aou.id
209         WINDOW w AS (
210             ORDER BY evergreen.rank_ou(aou.id, $2, $6), evergreen.rank_cp_status(acp.status)
211         )
212     ) AS ua
213     GROUP BY ua.id, ua.name, ua.label_sortkey
214     ORDER BY rank, ua.name, ua.label_sortkey
215     LIMIT ($4 -> 'acn')::INT
216     OFFSET ($5 -> 'acn')::INT;
217 $$
218 LANGUAGE SQL STABLE;
219
220 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes
221     ( bibid BIGINT, ouid INT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, pref_lib INT DEFAULT NULL, includes TEXT[] DEFAULT NULL::TEXT[] )
222     RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT)
223     AS $$ SELECT * FROM evergreen.ranked_volumes(ARRAY[$1],$2,$3,$4,$5,$6,$7) $$ LANGUAGE SQL STABLE;
224
225
226 CREATE OR REPLACE FUNCTION evergreen.located_uris (
227     bibid BIGINT[],
228     ouid INT,
229     pref_lib INT DEFAULT NULL
230 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT) AS $$
231     WITH all_orgs AS (SELECT COALESCE( enabled, FALSE ) AS flag FROM config.global_flag WHERE name = 'opac.located_uri.act_as_copy')
232     SELECT DISTINCT ON (id) * FROM (
233     SELECT acn.id, COALESCE(aou.name,aoud.name), acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
234       FROM asset.call_number acn
235            INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
236            INNER JOIN asset.uri auri ON auri.id = auricnm.uri
237            LEFT JOIN actor.org_unit_ancestors( COALESCE($3, $2) ) aou ON (acn.owning_lib = aou.id)
238            LEFT JOIN actor.org_unit_descendants( COALESCE($3, $2) ) aoud ON (acn.owning_lib = aoud.id),
239            all_orgs
240       WHERE acn.record = ANY ($1)
241           AND acn.deleted IS FALSE
242           AND auri.active IS TRUE
243           AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR COALESCE(aou.id,aoud.id) IS NOT NULL)
244     UNION
245     SELECT acn.id, COALESCE(aou.name,aoud.name) AS name, acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
246       FROM asset.call_number acn
247            INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
248            INNER JOIN asset.uri auri ON auri.id = auricnm.uri
249            LEFT JOIN actor.org_unit_ancestors( $2 ) aou ON (acn.owning_lib = aou.id)
250            LEFT JOIN actor.org_unit_descendants( $2 ) aoud ON (acn.owning_lib = aoud.id),
251            all_orgs
252       WHERE acn.record = ANY ($1)
253           AND acn.deleted IS FALSE
254           AND auri.active IS TRUE
255           AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR COALESCE(aou.id,aoud.id) IS NOT NULL))x
256     ORDER BY id, pref_ou DESC;
257 $$
258 LANGUAGE SQL STABLE;
259
260 CREATE OR REPLACE FUNCTION evergreen.located_uris ( bibid BIGINT, ouid INT, pref_lib INT DEFAULT NULL)
261     RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT)
262     AS $$ SELECT * FROM evergreen.located_uris(ARRAY[$1],$2,$3) $$ LANGUAGE SQL STABLE;
263
264
265 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
266     mid BIGINT,
267     ouid INT,
268     org TEXT,
269     depth INT DEFAULT NULL,
270     includes TEXT[] DEFAULT NULL::TEXT[],
271     slimit HSTORE DEFAULT NULL,
272     soffset HSTORE DEFAULT NULL,
273     include_xmlns BOOL DEFAULT TRUE,
274     pref_lib INT DEFAULT NULL
275 )
276 RETURNS XML AS $F$
277      SELECT  XMLELEMENT(
278                  name holdings,
279                  XMLATTRIBUTES(
280                     CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
281                     CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
282                     (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
283                  ),
284                  XMLELEMENT(
285                      name counts,
286                      (SELECT  XMLAGG(XMLELEMENT::XML) FROM (
287                          SELECT  XMLELEMENT(
288                                      name count,
289                                      XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
290                                  )::text
291                            FROM  asset.opac_ou_metarecord_copy_count($2,  $1)
292                                      UNION
293                          SELECT  XMLELEMENT(
294                                      name count,
295                                      XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
296                                  )::text
297                            FROM  asset.staff_ou_metarecord_copy_count($2, $1)
298                                      UNION
299                          SELECT  XMLELEMENT(
300                                      name count,
301                                      XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
302                                  )::text
303                            FROM  asset.opac_ou_metarecord_copy_count($9,  $1)
304                                      ORDER BY 1
305                      )x)
306                  ),
307                  -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
308                  XMLELEMENT(
309                      name volumes,
310                      (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
311                         -- Physical copies
312                         SELECT  unapi.acn(y.id,'xml','volume',evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), y.rank, name, label_sortkey
313                         FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
314                         UNION ALL
315                         -- Located URIs
316                         SELECT unapi.acn(uris.id,'xml','volume',evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), uris.rank, name, label_sortkey
317                         FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
318                      )x)
319                  ),
320                  CASE WHEN ('ssub' = ANY ($5)) THEN
321                      XMLELEMENT(
322                          name subscriptions,
323                          (SELECT XMLAGG(ssub) FROM (
324                             SELECT  unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
325                               FROM  serial.subscription
326                               WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
327                         )x)
328                      )
329                  ELSE NULL END
330              );
331 $F$ LANGUAGE SQL STABLE;
332
333 CREATE OR REPLACE FUNCTION unapi.mmr (
334     obj_id BIGINT,
335     format TEXT,
336     ename TEXT,
337     includes TEXT[],
338     org TEXT,
339     depth INT DEFAULT NULL,
340     slimit HSTORE DEFAULT NULL,
341     soffset HSTORE DEFAULT NULL,
342     include_xmlns BOOL DEFAULT TRUE,
343     pref_lib INT DEFAULT NULL
344 )
345 RETURNS XML AS $F$
346 DECLARE
347     mmrec   metabib.metarecord%ROWTYPE;
348     leadrec biblio.record_entry%ROWTYPE;
349     subrec biblio.record_entry%ROWTYPE;
350     layout  unapi.bre_output_layout%ROWTYPE;
351     xfrm    config.xml_transform%ROWTYPE;
352     ouid    INT;
353     xml_buf TEXT; -- growing XML document
354     tmp_xml TEXT; -- single-use XML string
355     xml_frag TEXT; -- single-use XML fragment
356     top_el  TEXT;
357     output  XML;
358     hxml    XML;
359     axml    XML;
360     subxml  XML; -- subordinate records elements
361     sub_xpath TEXT; 
362     parts   TEXT[]; 
363 BEGIN
364
365     -- xpath for extracting bre.marc values from subordinate records 
366     -- so they may be appended to the MARC of the master record prior
367     -- to XSLT processing.
368     -- subjects, isbn, issn, upc -- anything else?
369     sub_xpath := 
370       '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
371
372     IF org = '-' OR org IS NULL THEN
373         SELECT shortname INTO org FROM evergreen.org_top();
374     END IF;
375
376     SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
377
378     IF ouid IS NULL THEN
379         RETURN NULL::XML;
380     END IF;
381
382     SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
383     IF NOT FOUND THEN
384         RETURN NULL::XML;
385     END IF;
386
387     -- TODO: aggregate holdings from constituent records
388     IF format = 'holdings_xml' THEN -- the special case
389         output := unapi.mmr_holdings_xml(
390             obj_id, ouid, org, depth,
391             evergreen.array_remove_item_by_value(includes,'holdings_xml'),
392             slimit, soffset, include_xmlns);
393         RETURN output;
394     END IF;
395
396     SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
397
398     IF layout.name IS NULL THEN
399         RETURN NULL::XML;
400     END IF;
401
402     SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
403
404     SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
405
406     -- Grab distinct MVF for all records if requested
407     IF ('mra' = ANY (includes)) THEN 
408         axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,NULL,NULL,NULL,NULL,TRUE);
409     ELSE
410         axml := NULL::XML;
411     END IF;
412
413     xml_buf = leadrec.marc;
414
415     hxml := NULL::XML;
416     IF ('holdings_xml' = ANY (includes)) THEN
417         hxml := unapi.mmr_holdings_xml(
418                     obj_id, ouid, org, depth,
419                     evergreen.array_remove_item_by_value(includes,'holdings_xml'),
420                     slimit, soffset, include_xmlns, pref_lib);
421     END IF;
422
423     subxml := NULL::XML;
424     parts := '{}'::TEXT[];
425     FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
426          JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
427          JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
428          WHERE mmr.id = obj_id
429          ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
430          LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
431
432         IF subrec.id = leadrec.id THEN CONTINUE; END IF;
433         -- Append choice data from the the non-lead records to the 
434         -- the lead record document
435
436         parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
437     END LOOP;
438
439     SELECT ARRAY_TO_STRING( ARRAY_AGG( DISTINCT p ), '' )::XML INTO subxml FROM UNNEST(parts) p;
440
441     -- append data from the subordinate records to the 
442     -- main record document before applying the XSLT
443
444     IF subxml IS NOT NULL THEN 
445         xml_buf := REGEXP_REPLACE(xml_buf, 
446             '</record>(.*?)$', subxml || '</record>' || E'\\1');
447     END IF;
448
449     IF format = 'marcxml' THEN
450          -- If we're not using the prefixed namespace in 
451          -- this record, then remove all declarations of it
452         IF xml_buf !~ E'<marc:' THEN
453            xml_buf := REGEXP_REPLACE(xml_buf, 
454             ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
455         END IF; 
456     ELSE
457         xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
458     END IF;
459
460     -- update top_el to reflect the change in xml_buf, which may
461     -- now be a different type of document (e.g. record -> mods)
462     top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' || 
463         layout.holdings_element || ').*$', E'\\1');
464
465     IF axml IS NOT NULL THEN 
466         xml_buf := REGEXP_REPLACE(xml_buf, 
467             '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
468     END IF;
469
470     IF hxml IS NOT NULL THEN
471         xml_buf := REGEXP_REPLACE(xml_buf, 
472             '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
473     END IF;
474
475     IF ('mmr.unapi' = ANY (includes)) THEN 
476         output := REGEXP_REPLACE(
477             xml_buf,
478             '</' || top_el || '>(.*?)',
479             XMLELEMENT(
480                 name abbr,
481                 XMLATTRIBUTES(
482                     'http://www.w3.org/1999/xhtml' AS xmlns,
483                     'unapi-id' AS class,
484                     'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
485                 )
486             )::TEXT || '</' || top_el || E'>\\1'
487         );
488     ELSE
489         output := xml_buf;
490     END IF;
491
492     -- remove ignorable whitesace
493     output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
494     RETURN output;
495 END;
496 $F$ LANGUAGE PLPGSQL STABLE;
497
498
499 COMMIT;
500