1 DROP SCHEMA IF EXISTS vandelay CASCADE;
5 CREATE SCHEMA vandelay;
7 CREATE TABLE vandelay.match_set (
10 owner INT NOT NULL REFERENCES actor.org_unit (id) ON DELETE CASCADE,
11 mtype TEXT NOT NULL DEFAULT 'biblio', -- 'biblio','authority','mfhd'?, others?
12 CONSTRAINT name_once_per_owner_mtype UNIQUE (name, owner, mtype)
15 -- Table to define match points, either FF via SVF or tag+subfield
16 CREATE TABLE vandelay.match_set_point (
17 id SERIAL PRIMARY KEY,
18 match_set INT REFERENCES vandelay.match_set (id) ON DELETE CASCADE,
19 parent INT REFERENCES vandelay.match_set_point (id),
20 bool_op TEXT CHECK (bool_op IS NULL OR (bool_op IN ('AND','OR','NOT'))),
21 svf TEXT REFERENCES config.record_attr_definition (name),
24 negate BOOL DEFAULT FALSE,
25 quality INT NOT NULL DEFAULT 1, -- higher is better
26 heading BOOLEAN NOT NULL DEFAULT FALSE, -- match on authority heading
27 CONSTRAINT vmsp_need_a_subfield_with_a_tag CHECK ((tag IS NOT NULL AND subfield IS NOT NULL) OR tag IS NULL),
28 CONSTRAINT vmsp_need_a_tag_or_a_ff_or_a_bo CHECK (
29 (tag IS NOT NULL AND svf IS NULL AND heading IS FALSE AND bool_op IS NULL) OR
30 (tag IS NULL AND svf IS NOT NULL AND heading IS FALSE AND bool_op IS NULL) OR
31 (tag IS NULL AND svf IS NULL AND heading IS TRUE AND bool_op IS NULL) OR
32 (tag IS NULL AND svf IS NULL AND heading IS FALSE AND bool_op IS NOT NULL)
36 CREATE TABLE vandelay.match_set_quality (
37 id SERIAL PRIMARY KEY,
38 match_set INT NOT NULL REFERENCES vandelay.match_set (id) ON DELETE CASCADE,
39 svf TEXT REFERENCES config.record_attr_definition,
43 quality INT NOT NULL DEFAULT 1, -- higher is better
44 CONSTRAINT vmsq_need_a_subfield_with_a_tag CHECK ((tag IS NOT NULL AND subfield IS NOT NULL) OR tag IS NULL),
45 CONSTRAINT vmsq_need_a_tag_or_a_ff CHECK ((tag IS NOT NULL AND svf IS NULL) OR (tag IS NULL AND svf IS NOT NULL))
47 CREATE UNIQUE INDEX vmsq_def_once_per_set ON vandelay.match_set_quality (match_set, COALESCE(tag,''), COALESCE(subfield,''), COALESCE(svf,''), value);
50 CREATE TABLE vandelay.queue (
51 id BIGSERIAL PRIMARY KEY,
52 owner INT NOT NULL REFERENCES actor.usr (id) DEFERRABLE INITIALLY DEFERRED,
54 complete BOOL NOT NULL DEFAULT FALSE,
55 match_set INT REFERENCES vandelay.match_set (id) ON UPDATE CASCADE ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED
58 CREATE TABLE vandelay.queued_record (
59 id BIGSERIAL PRIMARY KEY,
60 create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
61 import_time TIMESTAMP WITH TIME ZONE,
62 purpose TEXT NOT NULL DEFAULT 'import' CHECK (purpose IN ('import','overlay')),
64 quality INT NOT NULL DEFAULT 0
69 /* Bib stuff at the top */
70 ----------------------------------------------------
72 CREATE TABLE vandelay.bib_attr_definition (
73 id SERIAL PRIMARY KEY,
74 code TEXT UNIQUE NOT NULL,
77 remove TEXT NOT NULL DEFAULT ''
80 -- Each TEXT field (other than 'name') should hold an XPath predicate for pulling the data needed
81 -- DROP TABLE vandelay.import_item_attr_definition CASCADE;
82 CREATE TABLE vandelay.import_item_attr_definition (
83 id BIGSERIAL PRIMARY KEY,
84 owner INT NOT NULL REFERENCES actor.org_unit (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
87 keep BOOL NOT NULL DEFAULT FALSE,
107 priv_note_title TEXT,
111 CONSTRAINT vand_import_item_attr_def_idx UNIQUE (owner,name)
114 CREATE TABLE vandelay.import_error (
115 code TEXT PRIMARY KEY,
116 description TEXT NOT NULL -- i18n
119 CREATE TYPE vandelay.bib_queue_queue_type AS ENUM ('bib', 'acq');
121 CREATE TABLE vandelay.bib_queue (
122 queue_type vandelay.bib_queue_queue_type NOT NULL DEFAULT 'bib',
123 item_attr_def BIGINT REFERENCES vandelay.import_item_attr_definition (id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
124 match_bucket INTEGER, -- REFERENCES container.biblio_record_entry_bucket(id);
125 CONSTRAINT vand_bib_queue_name_once_per_owner_const UNIQUE (owner,name,queue_type)
126 ) INHERITS (vandelay.queue);
127 ALTER TABLE vandelay.bib_queue ADD PRIMARY KEY (id);
129 CREATE TABLE vandelay.queued_bib_record (
130 queue INT NOT NULL REFERENCES vandelay.bib_queue (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
131 bib_source INT REFERENCES config.bib_source (id) DEFERRABLE INITIALLY DEFERRED,
132 imported_as BIGINT REFERENCES biblio.record_entry (id) DEFERRABLE INITIALLY DEFERRED,
133 import_error TEXT REFERENCES vandelay.import_error (code) ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
135 ) INHERITS (vandelay.queued_record);
136 ALTER TABLE vandelay.queued_bib_record ADD PRIMARY KEY (id);
137 CREATE INDEX queued_bib_record_queue_idx ON vandelay.queued_bib_record (queue);
139 CREATE TABLE vandelay.queued_bib_record_attr (
140 id BIGSERIAL PRIMARY KEY,
141 record BIGINT NOT NULL REFERENCES vandelay.queued_bib_record (id) DEFERRABLE INITIALLY DEFERRED,
142 field INT NOT NULL REFERENCES vandelay.bib_attr_definition (id) DEFERRABLE INITIALLY DEFERRED,
143 attr_value TEXT NOT NULL
145 CREATE INDEX queued_bib_record_attr_record_idx ON vandelay.queued_bib_record_attr (record);
147 CREATE TABLE vandelay.bib_match (
148 id BIGSERIAL PRIMARY KEY,
149 queued_record BIGINT REFERENCES vandelay.queued_bib_record (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
150 eg_record BIGINT REFERENCES biblio.record_entry (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
151 quality INT NOT NULL DEFAULT 1,
152 match_score INT NOT NULL DEFAULT 0
154 CREATE INDEX bib_match_queued_record_idx ON vandelay.bib_match (queued_record);
156 CREATE TABLE vandelay.import_item (
157 id BIGSERIAL PRIMARY KEY,
158 record BIGINT NOT NULL REFERENCES vandelay.queued_bib_record (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
159 definition BIGINT NOT NULL REFERENCES vandelay.import_item_attr_definition (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
160 import_error TEXT REFERENCES vandelay.import_error (code) ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
163 import_time TIMESTAMP WITH TIME ZONE,
172 deposit_amount NUMERIC(8,2),
184 internal_id BIGINT -- queue_type == 'acq' ? acq.lineitem_detail.id : asset.copy.id
186 CREATE INDEX import_item_record_idx ON vandelay.import_item (record);
188 CREATE TABLE vandelay.import_bib_trash_group(
189 id SERIAL PRIMARY KEY,
190 owner INTEGER NOT NULL REFERENCES actor.org_unit(id),
191 label TEXT NOT NULL, --i18n
192 always_apply BOOLEAN NOT NULL DEFAULT FALSE,
193 CONSTRAINT vand_import_bib_trash_grp_owner_label UNIQUE (owner, label)
196 CREATE TABLE vandelay.import_bib_trash_fields (
197 id BIGSERIAL PRIMARY KEY,
198 grp INTEGER NOT NULL REFERENCES vandelay.import_bib_trash_group,
200 CONSTRAINT vand_import_bib_trash_fields_once_per UNIQUE (grp, field)
203 CREATE TABLE vandelay.merge_profile (
204 id BIGSERIAL PRIMARY KEY,
205 owner INT NOT NULL REFERENCES actor.org_unit (id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
212 CONSTRAINT vand_merge_prof_owner_name_idx UNIQUE (owner,name),
213 CONSTRAINT add_replace_strip_or_preserve CHECK ((preserve_spec IS NOT NULL OR replace_spec IS NOT NULL) OR (preserve_spec IS NULL AND replace_spec IS NULL))
216 CREATE OR REPLACE FUNCTION vandelay.marc21_record_type( marc TEXT ) RETURNS config.marc21_rec_type_map AS $func$
223 retval config.marc21_rec_type_map%ROWTYPE;
225 ldr := oils_xpath_string( '//*[local-name()="leader"]', marc );
227 IF ldr IS NULL OR ldr = '' THEN
228 SELECT * INTO retval FROM config.marc21_rec_type_map WHERE code = 'BKS';
232 SELECT * INTO tval_rec FROM config.marc21_ff_pos_map WHERE fixed_field = 'Type' LIMIT 1; -- They're all the same
233 SELECT * INTO bval_rec FROM config.marc21_ff_pos_map WHERE fixed_field = 'BLvl' LIMIT 1; -- They're all the same
236 tval := SUBSTRING( ldr, tval_rec.start_pos + 1, tval_rec.length );
237 bval := SUBSTRING( ldr, bval_rec.start_pos + 1, bval_rec.length );
239 -- RAISE NOTICE 'type %, blvl %, ldr %', tval, bval, ldr;
241 SELECT * INTO retval FROM config.marc21_rec_type_map WHERE type_val LIKE '%' || tval || '%' AND blvl_val LIKE '%' || bval || '%';
244 IF retval.code IS NULL THEN
245 SELECT * INTO retval FROM config.marc21_rec_type_map WHERE code = 'BKS';
250 $func$ LANGUAGE PLPGSQL;
252 CREATE TYPE biblio.record_ff_map AS (record BIGINT, ff_name TEXT, ff_value TEXT);
253 CREATE OR REPLACE FUNCTION vandelay.marc21_extract_all_fixed_fields( marc TEXT, use_default BOOL DEFAULT FALSE ) RETURNS SETOF biblio.record_ff_map AS $func$
258 output biblio.record_ff_map%ROWTYPE;
260 rtype := (vandelay.marc21_record_type( marc )).code;
262 FOR ff_pos IN SELECT * FROM config.marc21_ff_pos_map WHERE rec_type = rtype ORDER BY tag DESC LOOP
263 output.ff_name := ff_pos.fixed_field;
264 output.ff_value := NULL;
266 IF ff_pos.tag = 'ldr' THEN
267 output.ff_value := oils_xpath_string('//*[local-name()="leader"]', marc);
268 IF output.ff_value IS NOT NULL THEN
269 output.ff_value := SUBSTRING( output.ff_value, ff_pos.start_pos + 1, ff_pos.length );
271 output.ff_value := NULL;
274 FOR tag_data IN SELECT value FROM UNNEST( oils_xpath( '//*[@tag="' || UPPER(ff_pos.tag) || '"]/text()', marc ) ) x(value) LOOP
275 output.ff_value := SUBSTRING( tag_data, ff_pos.start_pos + 1, ff_pos.length );
276 CONTINUE WHEN output.ff_value IS NULL AND NOT use_default;
277 IF output.ff_value IS NULL THEN output.ff_value := REPEAT( ff_pos.default_val, ff_pos.length ); END IF;
279 output.ff_value := NULL;
287 $func$ LANGUAGE PLPGSQL;
289 CREATE OR REPLACE FUNCTION vandelay.marc21_extract_fixed_field_list( marc TEXT, ff TEXT, use_default BOOL DEFAULT FALSE ) RETURNS TEXT[] AS $func$
295 collection TEXT[] := '{}'::TEXT[];
297 rtype := (vandelay.marc21_record_type( marc )).code;
298 FOR ff_pos IN SELECT * FROM config.marc21_ff_pos_map WHERE fixed_field = ff AND rec_type = rtype ORDER BY tag DESC LOOP
299 IF ff_pos.tag = 'ldr' THEN
300 val := oils_xpath_string('//*[local-name()="leader"]', marc);
301 IF val IS NOT NULL THEN
302 val := SUBSTRING( val, ff_pos.start_pos + 1, ff_pos.length );
303 collection := collection || val;
306 FOR tag_data IN SELECT value FROM UNNEST( oils_xpath( '//*[@tag="' || UPPER(ff_pos.tag) || '"]/text()', marc ) ) x(value) LOOP
307 val := SUBSTRING( tag_data.value, ff_pos.start_pos + 1, ff_pos.length );
308 collection := collection || val;
311 CONTINUE WHEN NOT use_default;
312 CONTINUE WHEN ARRAY_UPPER(collection, 1) > 0;
313 val := REPEAT( ff_pos.default_val, ff_pos.length );
314 collection := collection || val;
319 $func$ LANGUAGE PLPGSQL;
321 CREATE OR REPLACE FUNCTION vandelay.marc21_extract_fixed_field( marc TEXT, ff TEXT, use_default BOOL DEFAULT FALSE ) RETURNS TEXT AS $func$
328 rtype := (vandelay.marc21_record_type( marc )).code;
329 FOR ff_pos IN SELECT * FROM config.marc21_ff_pos_map WHERE fixed_field = ff AND rec_type = rtype ORDER BY tag DESC LOOP
330 IF ff_pos.tag = 'ldr' THEN
331 val := oils_xpath_string('//*[local-name()="leader"]', marc);
332 IF val IS NOT NULL THEN
333 val := SUBSTRING( val, ff_pos.start_pos + 1, ff_pos.length );
337 FOR tag_data IN SELECT value FROM UNNEST( oils_xpath( '//*[@tag="' || UPPER(ff_pos.tag) || '"]/text()', marc ) ) x(value) LOOP
338 val := SUBSTRING( tag_data.value, ff_pos.start_pos + 1, ff_pos.length );
342 CONTINUE WHEN NOT use_default;
343 val := REPEAT( ff_pos.default_val, ff_pos.length );
349 $func$ LANGUAGE PLPGSQL;
351 CREATE TYPE biblio.marc21_physical_characteristics AS ( id INT, record BIGINT, ptype TEXT, subfield INT, value INT );
352 CREATE OR REPLACE FUNCTION vandelay.marc21_physical_characteristics( marc TEXT) RETURNS SETOF biblio.marc21_physical_characteristics AS $func$
356 ptype config.marc21_physical_characteristic_type_map%ROWTYPE;
357 psf config.marc21_physical_characteristic_subfield_map%ROWTYPE;
358 pval config.marc21_physical_characteristic_value_map%ROWTYPE;
359 retval biblio.marc21_physical_characteristics%ROWTYPE;
362 FOR _007 IN SELECT oils_xpath_string('//*', value) FROM UNNEST(oils_xpath('//*[@tag="007"]', marc)) x(value) LOOP
363 IF _007 IS NOT NULL AND _007 <> '' THEN
364 SELECT * INTO ptype FROM config.marc21_physical_characteristic_type_map WHERE ptype_key = SUBSTRING( _007, 1, 1 );
366 IF ptype.ptype_key IS NOT NULL THEN
367 FOR psf IN SELECT * FROM config.marc21_physical_characteristic_subfield_map WHERE ptype_key = ptype.ptype_key LOOP
368 SELECT * INTO pval FROM config.marc21_physical_characteristic_value_map WHERE ptype_subfield = psf.id AND value = SUBSTRING( _007, psf.start_pos + 1, psf.length );
370 IF pval.id IS NOT NULL THEN
373 retval.ptype := ptype.ptype_key;
374 retval.subfield := psf.id;
375 retval.value := pval.id;
386 $func$ LANGUAGE PLPGSQL;
388 CREATE TYPE vandelay.flat_marc AS ( tag CHAR(3), ind1 TEXT, ind2 TEXT, subfield TEXT, value TEXT );
389 CREATE OR REPLACE FUNCTION vandelay.flay_marc ( TEXT ) RETURNS SETOF vandelay.flat_marc AS $func$
392 use MARC::File::XML (BinaryEncoding => 'UTF-8');
396 MARC::Charset->assume_unicode(1);
399 my $r = MARC::Record->new_from_xml( $xml );
401 return_next( { tag => 'LDR', value => $r->leader } );
403 for my $f ( $r->fields ) {
404 if ($f->is_control_field) {
405 return_next({ tag => $f->tag, value => $f->data });
407 for my $s ($f->subfields) {
410 ind1 => $f->indicator(1),
411 ind2 => $f->indicator(2),
416 if ( $f->tag eq '245' and $s->[0] eq 'a' ) {
417 my $trim = $f->indicator(2) || 0;
420 ind1 => $f->indicator(1),
421 ind2 => $f->indicator(2),
423 value => substr( $s->[1], $trim )
432 $func$ LANGUAGE PLPERLU;
434 CREATE OR REPLACE FUNCTION vandelay.flatten_marc ( marc TEXT ) RETURNS SETOF vandelay.flat_marc AS $func$
436 output vandelay.flat_marc%ROWTYPE;
439 FOR field IN SELECT * FROM vandelay.flay_marc( marc ) LOOP
440 output.ind1 := field.ind1;
441 output.ind2 := field.ind2;
442 output.tag := field.tag;
443 output.subfield := field.subfield;
444 IF field.subfield IS NOT NULL AND field.tag NOT IN ('020','022','024') THEN -- exclude standard numbers and control fields
445 output.value := naco_normalize(field.value, field.subfield);
447 output.value := field.value;
450 CONTINUE WHEN output.value IS NULL;
455 $func$ LANGUAGE PLPGSQL;
457 CREATE OR REPLACE FUNCTION vandelay.extract_rec_attrs ( xml TEXT, attr_defs TEXT[]) RETURNS hstore AS $_$
459 transformed_xml TEXT;
462 xfrm config.xml_transform%ROWTYPE;
464 new_attrs HSTORE := ''::HSTORE;
465 attr_def config.record_attr_definition%ROWTYPE;
468 FOR attr_def IN SELECT * FROM config.record_attr_definition WHERE name IN (SELECT * FROM UNNEST(attr_defs)) ORDER BY format LOOP
470 IF attr_def.tag IS NOT NULL THEN -- tag (and optional subfield list) selection
471 SELECT STRING_AGG(x.value, COALESCE(attr_def.joiner,' ')) INTO attr_value
472 FROM vandelay.flatten_marc(xml) AS x
473 WHERE x.tag LIKE attr_def.tag
475 WHEN attr_def.sf_list IS NOT NULL
476 THEN POSITION(x.subfield IN attr_def.sf_list) > 0
483 ELSIF attr_def.fixed_field IS NOT NULL THEN -- a named fixed field, see config.marc21_ff_pos_map.fixed_field
484 attr_value := vandelay.marc21_extract_fixed_field(xml, attr_def.fixed_field);
486 ELSIF attr_def.xpath IS NOT NULL THEN -- and xpath expression
488 SELECT INTO xfrm * FROM config.xml_transform WHERE name = attr_def.format;
490 -- See if we can skip the XSLT ... it's expensive
491 IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
492 -- Can't skip the transform
493 IF xfrm.xslt <> '---' THEN
494 transformed_xml := oils_xslt_process(xml,xfrm.xslt);
496 transformed_xml := xml;
499 prev_xfrm := xfrm.name;
502 IF xfrm.name IS NULL THEN
503 -- just grab the marcxml (empty) transform
504 SELECT INTO xfrm * FROM config.xml_transform WHERE xslt = '---' LIMIT 1;
505 prev_xfrm := xfrm.name;
508 attr_value := oils_xpath_string(attr_def.xpath, transformed_xml, COALESCE(attr_def.joiner,' '), ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]);
510 ELSIF attr_def.phys_char_sf IS NOT NULL THEN -- a named Physical Characteristic, see config.marc21_physical_characteristic_*_map
511 SELECT m.value::TEXT INTO attr_value
512 FROM vandelay.marc21_physical_characteristics(xml) v
513 JOIN config.marc21_physical_characteristic_value_map m ON (m.id = v.value)
514 WHERE v.subfield = attr_def.phys_char_sf
515 LIMIT 1; -- Just in case ...
519 -- apply index normalizers to attr_value
521 SELECT n.func AS func,
522 n.param_count AS param_count,
524 FROM config.index_normalizer n
525 JOIN config.record_attr_index_norm_map m ON (m.norm = n.id)
526 WHERE attr = attr_def.name
528 EXECUTE 'SELECT ' || normalizer.func || '(' ||
529 quote_nullable( attr_value ) ||
531 WHEN normalizer.param_count > 0
532 THEN ',' || REPLACE(REPLACE(BTRIM(normalizer.params,'[]'),E'\'',E'\\\''),E'"',E'\'')
539 -- Add the new value to the hstore
540 new_attrs := new_attrs || hstore( attr_def.name, attr_value );
546 $_$ LANGUAGE PLPGSQL;
548 CREATE OR REPLACE FUNCTION vandelay.extract_rec_attrs ( xml TEXT ) RETURNS hstore AS $_$
549 SELECT vandelay.extract_rec_attrs( $1, (SELECT ARRAY_AGG(name) FROM config.record_attr_definition));
552 -- Everything between this comment and the beginning of the definition of
553 -- vandelay.match_bib_record() is strictly in service of that function.
554 CREATE TYPE vandelay.match_set_test_result AS (record BIGINT, quality INTEGER);
556 CREATE OR REPLACE FUNCTION vandelay.match_set_test_marcxml(
557 match_set_id INTEGER, record_xml TEXT, bucket_id INTEGER
558 ) RETURNS SETOF vandelay.match_set_test_result AS $$
569 tags_rstore := vandelay.flatten_marc_hstore(record_xml);
570 svf_rstore := vandelay.extract_rec_attrs(record_xml);
572 CREATE TEMPORARY TABLE _vandelay_tmp_qrows (q INTEGER);
573 CREATE TEMPORARY TABLE _vandelay_tmp_jrows (j TEXT);
575 -- generate the where clause and return that directly (into wq), and as
576 -- a side-effect, populate the _vandelay_tmp_[qj]rows tables.
577 wq := vandelay.get_expr_from_match_set(match_set_id, tags_rstore);
579 query_ := 'SELECT DISTINCT(record), ';
581 -- qrows table is for the quality bits we add to the SELECT clause
583 'COALESCE(n' || q::TEXT || '.quality, 0)', ' + '
584 ) INTO coal FROM _vandelay_tmp_qrows;
586 -- our query string so far is the SELECT clause and the inital FROM.
587 -- no JOINs yet nor the WHERE clause
588 query_ := query_ || coal || ' AS quality ' || E'\n';
590 -- jrows table is for the joins we must make (and the real text conditions)
591 SELECT STRING_AGG(j, E'\n') INTO joins
592 FROM _vandelay_tmp_jrows;
594 -- add those joins and the where clause to our query.
595 query_ := query_ || joins || E'\n';
597 -- join the record bucket
598 IF bucket_id IS NOT NULL THEN
599 query_ := query_ || 'JOIN container.biblio_record_entry_bucket_item ' ||
600 'brebi ON (brebi.target_biblio_record_entry = record ' ||
601 'AND brebi.bucket = ' || bucket_id || E')\n';
604 query_ := query_ || 'JOIN biblio.record_entry bre ON (bre.id = record) ' || 'WHERE ' || wq || ' AND not bre.deleted';
606 -- this will return rows of record,quality
607 FOR rec IN EXECUTE query_ USING tags_rstore, svf_rstore LOOP
611 DROP TABLE _vandelay_tmp_qrows;
612 DROP TABLE _vandelay_tmp_jrows;
618 CREATE OR REPLACE FUNCTION vandelay.flatten_marc_hstore(
620 ) RETURNS HSTORE AS $func$
624 ARRAY_AGG(tag || (COALESCE(subfield, ''))),
628 SELECT tag, subfield, ARRAY_AGG(value)::TEXT AS value
631 CASE WHEN tag = '020' THEN -- caseless -- isbn
632 LOWER((REGEXP_MATCHES(value,$$^(\S{10,17})$$))[1] || '%')
633 WHEN tag = '022' THEN -- caseless -- issn
634 LOWER((REGEXP_MATCHES(value,$$^(\S{4}[- ]?\S{4})$$))[1] || '%')
635 WHEN tag = '024' THEN -- caseless -- upc (other)
640 FROM vandelay.flatten_marc(record_xml)) x
641 GROUP BY tag, subfield ORDER BY tag, subfield
645 $func$ LANGUAGE PLPGSQL;
647 -- backwards compat version so we don't have
648 -- to modify vandelay.match_set_test_marcxml()
649 CREATE OR REPLACE FUNCTION vandelay.get_expr_from_match_set(
650 match_set_id INTEGER,
654 RETURN vandelay.get_expr_from_match_set(
655 match_set_id, tags_rstore, NULL);
659 CREATE OR REPLACE FUNCTION vandelay.get_expr_from_match_set(
660 match_set_id INTEGER,
665 root vandelay.match_set_point;
667 SELECT * INTO root FROM vandelay.match_set_point
668 WHERE parent IS NULL AND match_set = match_set_id;
670 RETURN vandelay.get_expr_from_match_set_point(
671 root, tags_rstore, auth_heading);
675 CREATE OR REPLACE FUNCTION vandelay.get_expr_from_match_set_point(
676 node vandelay.match_set_point,
685 child vandelay.match_set_point;
687 SELECT ARRAY_AGG(id) INTO children FROM vandelay.match_set_point
688 WHERE parent = node.id;
690 IF ARRAY_LENGTH(children, 1) > 0 THEN
691 this_op := vandelay._get_expr_render_one(node);
694 WHILE children[i] IS NOT NULL LOOP
695 SELECT * INTO child FROM vandelay.match_set_point
696 WHERE id = children[i];
698 q := q || ' ' || this_op || ' ';
701 q := q || vandelay.get_expr_from_match_set_point(
702 child, tags_rstore, auth_heading);
706 ELSIF node.bool_op IS NULL THEN
707 PERFORM vandelay._get_expr_push_qrow(node);
708 PERFORM vandelay._get_expr_push_jrow(node, tags_rstore, auth_heading);
709 RETURN vandelay._get_expr_render_one(node);
716 CREATE OR REPLACE FUNCTION vandelay._get_expr_push_qrow(
717 node vandelay.match_set_point
721 INSERT INTO _vandelay_tmp_qrows (q) VALUES (node.id);
725 CREATE OR REPLACE FUNCTION vandelay._get_expr_push_jrow(
726 node vandelay.match_set_point,
741 -- remember $1 is tags_rstore, and $2 is svf_rstore
742 -- a non-NULL auth_heading means we're matching authority records
744 IF auth_heading IS NOT NULL THEN
745 rec_table := 'authority.full_rec';
747 rec_table := 'metabib.full_rec';
751 SELECT COUNT(*) INTO jrow_count FROM _vandelay_tmp_jrows;
752 IF jrow_count > 0 THEN
753 my_using := ' USING (record)';
754 my_join := 'FULL OUTER JOIN';
760 IF node.tag IS NOT NULL THEN
761 caseless := (node.tag IN ('020', '022', '024'));
763 IF node.subfield IS NOT NULL THEN
764 tagkey := tagkey || node.subfield;
782 my_alias := 'n' || node.id::TEXT;
784 jrow := my_join || ' (SELECT *, ';
785 IF node.tag IS NOT NULL THEN
786 jrow := jrow || node.quality ||
787 ' AS quality FROM ' || rec_table || ' mfr WHERE mfr.tag = ''' ||
789 IF node.subfield IS NOT NULL THEN
790 jrow := jrow || ' AND mfr.subfield = ''' ||
791 node.subfield || '''';
793 jrow := jrow || ' AND (';
794 jrow := jrow || vandelay._node_tag_comparisons(caseless, op, tags_rstore, tagkey);
795 jrow := jrow || ')) ' || my_alias || my_using || E'\n';
797 IF auth_heading IS NOT NULL THEN -- authority record
798 IF node.heading AND auth_heading <> '' THEN
799 jrow := jrow || 'id AS record, ' || node.quality ||
800 ' AS quality FROM authority.record_entry are ' ||
801 ' WHERE are.heading = ''' || auth_heading || '''';
802 jrow := jrow || ') ' || my_alias || my_using || E'\n';
805 jrow := jrow || 'id AS record, ' || node.quality ||
806 ' AS quality FROM metabib.record_attr_flat mraf WHERE mraf.attr = ''' ||
807 node.svf || ''' AND mraf.value ' || op || ' $2->''' || node.svf || ''') ' ||
808 my_alias || my_using || E'\n';
811 INSERT INTO _vandelay_tmp_jrows (j) VALUES (jrow);
815 CREATE OR REPLACE FUNCTION vandelay._node_tag_comparisons(
827 vals := tags_rstore->tagkey;
832 IF vals[i] IS NULL THEN
835 result := result || ' OR ';
840 result := result || 'LOWER(mfr.value) ' || op;
842 result := result || 'mfr.value ' || op;
845 result := result || ' ' || COALESCE('''' || vals[i] || '''', 'NULL');
847 IF vals[i] IS NULL THEN
858 CREATE OR REPLACE FUNCTION vandelay._get_expr_render_one(
859 node vandelay.match_set_point
864 IF node.bool_op IS NOT NULL THEN
867 RETURN '(n' || node.id::TEXT || '.id IS NOT NULL)';
872 CREATE OR REPLACE FUNCTION vandelay.match_bib_record() RETURNS TRIGGER AS $func$
874 incoming_existing_id TEXT;
875 test_result vandelay.match_set_test_result%ROWTYPE;
880 IF TG_OP IN ('INSERT','UPDATE') AND NEW.imported_as IS NOT NULL THEN
884 DELETE FROM vandelay.bib_match WHERE queued_record = NEW.id;
886 SELECT q.match_set INTO match_set FROM vandelay.bib_queue q WHERE q.id = NEW.queue;
888 IF match_set IS NOT NULL THEN
889 NEW.quality := vandelay.measure_record_quality( NEW.marc, match_set );
892 -- Perfect matches on 901$c exit early with a match with high quality.
893 incoming_existing_id :=
894 oils_xpath_string('//*[@tag="901"]/*[@code="c"][1]', NEW.marc);
896 IF incoming_existing_id IS NOT NULL AND incoming_existing_id != '' THEN
897 SELECT id INTO tmp_rec FROM biblio.record_entry WHERE id = incoming_existing_id::bigint;
898 IF tmp_rec IS NOT NULL THEN
899 INSERT INTO vandelay.bib_match (queued_record, eg_record, match_score, quality)
904 -- note: no match_set means quality==0
905 vandelay.measure_record_quality( b.marc, match_set )
906 FROM biblio.record_entry b
907 WHERE id = incoming_existing_id::bigint;
911 IF match_set IS NULL THEN
915 SELECT q.match_bucket INTO match_bucket FROM vandelay.bib_queue q WHERE q.id = NEW.queue;
917 FOR test_result IN SELECT * FROM
918 vandelay.match_set_test_marcxml(match_set, NEW.marc, match_bucket) LOOP
920 INSERT INTO vandelay.bib_match ( queued_record, eg_record, match_score, quality )
925 vandelay.measure_record_quality( b.marc, match_set )
926 FROM biblio.record_entry b
927 WHERE id = test_result.record;
933 $func$ LANGUAGE PLPGSQL;
935 CREATE OR REPLACE FUNCTION vandelay.measure_record_quality ( xml TEXT, match_set_id INT ) RETURNS INT AS $_$
939 test vandelay.match_set_quality%ROWTYPE;
942 FOR test IN SELECT * FROM vandelay.match_set_quality WHERE match_set = match_set_id LOOP
943 IF test.tag IS NOT NULL THEN
944 FOR rvalue IN SELECT value FROM vandelay.flatten_marc( xml ) WHERE tag = test.tag AND subfield = test.subfield LOOP
945 IF test.value = rvalue THEN
946 out_q := out_q + test.quality;
950 IF test.value = vandelay.extract_rec_attrs(xml, ARRAY[test.svf]) -> test.svf THEN
951 out_q := out_q + test.quality;
958 $_$ LANGUAGE PLPGSQL;
960 CREATE TYPE vandelay.tcn_data AS (tcn TEXT, tcn_source TEXT, used BOOL);
961 CREATE OR REPLACE FUNCTION vandelay.find_bib_tcn_data ( xml TEXT ) RETURNS SETOF vandelay.tcn_data AS $_$
965 output vandelay.tcn_data%ROWTYPE;
969 eg_tcn := BTRIM((oils_xpath('//*[@tag="001"]/text()',xml))[1]);
970 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
972 eg_tcn_source := BTRIM((oils_xpath('//*[@tag="003"]/text()',xml))[1]);
973 IF eg_tcn_source IS NULL OR eg_tcn_source = '' THEN
974 eg_tcn_source := 'System Local';
977 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
980 output.used := FALSE;
985 output.tcn := eg_tcn;
986 output.tcn_source := eg_tcn_source;
992 eg_tcn := BTRIM((oils_xpath('//*[@tag="901"]/*[@code="a"]/text()',xml))[1]);
993 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
995 eg_tcn_source := BTRIM((oils_xpath('//*[@tag="901"]/*[@code="b"]/text()',xml))[1]);
996 IF eg_tcn_source IS NULL OR eg_tcn_source = '' THEN
997 eg_tcn_source := 'System Local';
1000 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
1003 output.used := FALSE;
1005 output.used := TRUE;
1008 output.tcn := eg_tcn;
1009 output.tcn_source := eg_tcn_source;
1015 eg_tcn := BTRIM((oils_xpath('//*[@tag="039"]/*[@code="a"]/text()',xml))[1]);
1016 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
1018 eg_tcn_source := BTRIM((oils_xpath('//*[@tag="039"]/*[@code="b"]/text()',xml))[1]);
1019 IF eg_tcn_source IS NULL OR eg_tcn_source = '' THEN
1020 eg_tcn_source := 'System Local';
1023 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
1026 output.used := FALSE;
1028 output.used := TRUE;
1031 output.tcn := eg_tcn;
1032 output.tcn_source := eg_tcn_source;
1038 eg_tcn := REGEXP_REPLACE((oils_xpath('//*[@tag="020"]/*[@code="a"]/text()',xml))[1], $re$^(\w+).*?$$re$, $re$\1$re$);
1039 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
1041 eg_tcn_source := 'ISBN';
1043 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
1046 output.used := FALSE;
1048 output.used := TRUE;
1051 output.tcn := eg_tcn;
1052 output.tcn_source := eg_tcn_source;
1058 eg_tcn := REGEXP_REPLACE((oils_xpath('//*[@tag="022"]/*[@code="a"]/text()',xml))[1], $re$^(\w+).*?$$re$, $re$\1$re$);
1059 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
1061 eg_tcn_source := 'ISSN';
1063 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
1066 output.used := FALSE;
1068 output.used := TRUE;
1071 output.tcn := eg_tcn;
1072 output.tcn_source := eg_tcn_source;
1078 eg_tcn := REGEXP_REPLACE((oils_xpath('//*[@tag="010"]/*[@code="a"]/text()',xml))[1], $re$^(\w+).*?$$re$, $re$\1$re$);
1079 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
1081 eg_tcn_source := 'LCCN';
1083 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
1086 output.used := FALSE;
1088 output.used := TRUE;
1091 output.tcn := eg_tcn;
1092 output.tcn_source := eg_tcn_source;
1098 eg_tcn := REGEXP_REPLACE((oils_xpath('//*[@tag="035"]/*[@code="a"]/text()',xml))[1], $re$^.*?(\w+)$$re$, $re$\1$re$);
1099 IF eg_tcn IS NOT NULL AND eg_tcn <> '' THEN
1101 eg_tcn_source := 'System Legacy';
1103 PERFORM id FROM biblio.record_entry WHERE tcn_value = eg_tcn AND NOT deleted;
1106 output.used := FALSE;
1108 output.used := TRUE;
1111 output.tcn := eg_tcn;
1112 output.tcn_source := eg_tcn_source;
1119 $_$ LANGUAGE PLPGSQL;
1121 CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT, force_add INT ) RETURNS TEXT AS $_$
1124 use MARC::File::XML (BinaryEncoding => 'UTF-8');
1128 MARC::Charset->assume_unicode(1);
1130 my $target_xml = shift;
1131 my $source_xml = shift;
1132 my $field_spec = shift;
1133 my $force_add = shift || 0;
1135 my $target_r = MARC::Record->new_from_xml( $target_xml );
1136 my $source_r = MARC::Record->new_from_xml( $source_xml );
1138 return $target_xml unless ($target_r && $source_r);
1140 my @field_list = split(',', $field_spec);
1143 for my $f (@field_list) {
1144 $f =~ s/^\s*//; $f =~ s/\s*$//;
1145 if ($f =~ /^(.{3})(\w*)(?:\[([^]]*)\])?$/) {
1151 $match =~ s/^\s*//; $match =~ s/\s*$//;
1152 $fields{$field} = { sf => [ split('', $sf) ] };
1154 my ($msf,$mre) = split('~', $match);
1155 if (length($msf) > 0 and length($mre) > 0) {
1156 $msf =~ s/^\s*//; $msf =~ s/\s*$//;
1157 $mre =~ s/^\s*//; $mre =~ s/\s*$//;
1158 $fields{$field}{match} = { sf => $msf, re => qr/$mre/ };
1164 for my $f ( keys %fields) {
1165 if ( @{$fields{$f}{sf}} ) {
1166 for my $from_field ($source_r->field( $f )) {
1167 my @tos = $target_r->field( $f );
1169 next if (exists($fields{$f}{match}) and !$force_add);
1170 my @new_fields = map { $_->clone } $source_r->field( $f );
1171 $target_r->insert_fields_ordered( @new_fields );
1173 for my $to_field (@tos) {
1174 if (exists($fields{$f}{match})) {
1175 next unless (grep { $_ =~ $fields{$f}{match}{re} } $to_field->subfield($fields{$f}{match}{sf}));
1177 for my $old_sf ($from_field->subfields) {
1178 $to_field->add_subfields( @$old_sf ) if grep(/$$old_sf[0]/,@{$fields{$f}{sf}});
1184 my @new_fields = map { $_->clone } $source_r->field( $f );
1185 $target_r->insert_fields_ordered( @new_fields );
1189 $target_xml = $target_r->as_xml_record;
1190 $target_xml =~ s/^<\?.+?\?>$//mo;
1191 $target_xml =~ s/\n//sgo;
1192 $target_xml =~ s/>\s+</></sgo;
1196 $_$ LANGUAGE PLPERLU;
1198 CREATE OR REPLACE FUNCTION vandelay.add_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
1199 SELECT vandelay.add_field( $1, $2, $3, 0 );
1202 CREATE OR REPLACE FUNCTION vandelay.strip_field ( xml TEXT, field TEXT ) RETURNS TEXT AS $_$
1205 use MARC::File::XML (BinaryEncoding => 'UTF-8');
1209 MARC::Charset->assume_unicode(1);
1212 my $r = MARC::Record->new_from_xml( $xml );
1214 return $xml unless ($r);
1216 my $field_spec = shift;
1217 my @field_list = split(',', $field_spec);
1220 for my $f (@field_list) {
1221 $f =~ s/^\s*//; $f =~ s/\s*$//;
1222 if ($f =~ /^(.{3})(\w*)(?:\[([^]]*)\])?$/) {
1228 $match =~ s/^\s*//; $match =~ s/\s*$//;
1229 $fields{$field} = { sf => [ split('', $sf) ] };
1231 my ($msf,$mre) = split('~', $match);
1232 if (length($msf) > 0 and length($mre) > 0) {
1233 $msf =~ s/^\s*//; $msf =~ s/\s*$//;
1234 $mre =~ s/^\s*//; $mre =~ s/\s*$//;
1235 $fields{$field}{match} = { sf => $msf, re => qr/$mre/ };
1241 for my $f ( keys %fields) {
1242 for my $to_field ($r->field( $f )) {
1243 if (exists($fields{$f}{match})) {
1244 next unless (grep { $_ =~ $fields{$f}{match}{re} } $to_field->subfield($fields{$f}{match}{sf}));
1247 if ( @{$fields{$f}{sf}} ) {
1248 $to_field->delete_subfield(code => $fields{$f}{sf});
1250 $r->delete_field( $to_field );
1255 $xml = $r->as_xml_record;
1256 $xml =~ s/^<\?.+?\?>$//mo;
1258 $xml =~ s/>\s+</></sgo;
1262 $_$ LANGUAGE PLPERLU;
1264 CREATE OR REPLACE FUNCTION vandelay.replace_field ( target_xml TEXT, source_xml TEXT, field TEXT ) RETURNS TEXT AS $_$
1271 parsed_target := vandelay.strip_field( target_xml, ''); -- this dance normalizes the format of the xml for the IF below
1272 xml_output := parsed_target; -- if there are no replace rules, just return the input
1274 FOR curr_field IN SELECT UNNEST( STRING_TO_ARRAY(field, ',') ) LOOP -- naive split, but it's the same we use in the perl
1276 xml_output := vandelay.strip_field( parsed_target, curr_field);
1278 IF xml_output <> parsed_target AND curr_field ~ E'~' THEN
1279 -- we removed something, and there was a regexp restriction in the curr_field definition, so proceed
1280 xml_output := vandelay.add_field( xml_output, source_xml, curr_field, 1 );
1281 ELSIF curr_field !~ E'~' THEN
1282 -- No regexp restriction, add the curr_field
1283 xml_output := vandelay.add_field( xml_output, source_xml, curr_field, 0 );
1286 parsed_target := xml_output; -- in prep for any following loop iterations
1292 $_$ LANGUAGE PLPGSQL;
1294 CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_xml TEXT, source_xml TEXT, add_rule TEXT, replace_preserve_rule TEXT, strip_rule TEXT ) RETURNS TEXT AS $_$
1295 SELECT vandelay.replace_field( vandelay.add_field( vandelay.strip_field( $1, $5) , $2, $3 ), $2, $4);
1298 CREATE TYPE vandelay.compile_profile AS (add_rule TEXT, replace_rule TEXT, preserve_rule TEXT, strip_rule TEXT);
1299 CREATE OR REPLACE FUNCTION vandelay.compile_profile ( incoming_xml TEXT ) RETURNS vandelay.compile_profile AS $_$
1301 output vandelay.compile_profile%ROWTYPE;
1302 profile vandelay.merge_profile%ROWTYPE;
1304 profile_tmpl_owner TEXT;
1305 add_rule TEXT := '';
1306 strip_rule TEXT := '';
1307 replace_rule TEXT := '';
1308 preserve_rule TEXT := '';
1312 profile_tmpl := (oils_xpath('//*[@tag="905"]/*[@code="t"]/text()',incoming_xml))[1];
1313 profile_tmpl_owner := (oils_xpath('//*[@tag="905"]/*[@code="o"]/text()',incoming_xml))[1];
1315 IF profile_tmpl IS NOT NULL AND profile_tmpl <> '' AND profile_tmpl_owner IS NOT NULL AND profile_tmpl_owner <> '' THEN
1316 SELECT p.* INTO profile
1317 FROM vandelay.merge_profile p
1318 JOIN actor.org_unit u ON (u.id = p.owner)
1319 WHERE p.name = profile_tmpl
1320 AND u.shortname = profile_tmpl_owner;
1322 IF profile.id IS NOT NULL THEN
1323 add_rule := COALESCE(profile.add_spec,'');
1324 strip_rule := COALESCE(profile.strip_spec,'');
1325 replace_rule := COALESCE(profile.replace_spec,'');
1326 preserve_rule := COALESCE(profile.preserve_spec,'');
1330 add_rule := add_rule || ',' || COALESCE(ARRAY_TO_STRING(oils_xpath('//*[@tag="905"]/*[@code="a"]/text()',incoming_xml),','),'');
1331 strip_rule := strip_rule || ',' || COALESCE(ARRAY_TO_STRING(oils_xpath('//*[@tag="905"]/*[@code="d"]/text()',incoming_xml),','),'');
1332 replace_rule := replace_rule || ',' || COALESCE(ARRAY_TO_STRING(oils_xpath('//*[@tag="905"]/*[@code="r"]/text()',incoming_xml),','),'');
1333 preserve_rule := preserve_rule || ',' || COALESCE(ARRAY_TO_STRING(oils_xpath('//*[@tag="905"]/*[@code="p"]/text()',incoming_xml),','),'');
1335 output.add_rule := BTRIM(add_rule,',');
1336 output.replace_rule := BTRIM(replace_rule,',');
1337 output.strip_rule := BTRIM(strip_rule,',');
1338 output.preserve_rule := BTRIM(preserve_rule,',');
1342 $_$ LANGUAGE PLPGSQL;
1344 CREATE OR REPLACE FUNCTION vandelay.template_overlay_bib_record ( v_marc TEXT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1346 merge_profile vandelay.merge_profile%ROWTYPE;
1347 dyn_profile vandelay.compile_profile%ROWTYPE;
1357 SELECT b.marc INTO eg_marc
1358 FROM biblio.record_entry b
1362 IF eg_marc IS NULL OR v_marc IS NULL THEN
1363 -- RAISE NOTICE 'no marc for template or bib record';
1367 dyn_profile := vandelay.compile_profile( v_marc );
1369 IF merge_profile_id IS NOT NULL THEN
1370 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
1372 dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
1373 dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
1374 dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
1375 dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
1379 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
1380 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
1384 IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
1385 --Since we have nothing to do, just return a NOOP "we did it"
1387 ELSIF dyn_profile.replace_rule <> '' THEN
1388 source_marc = v_marc;
1389 target_marc = eg_marc;
1390 replace_rule = dyn_profile.replace_rule;
1392 source_marc = eg_marc;
1393 target_marc = v_marc;
1394 replace_rule = dyn_profile.preserve_rule;
1397 UPDATE biblio.record_entry
1398 SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
1402 -- RAISE NOTICE 'update of biblio.record_entry failed';
1409 $$ LANGUAGE PLPGSQL;
1411 CREATE OR REPLACE FUNCTION vandelay.merge_record_xml ( target_marc TEXT, template_marc TEXT ) RETURNS TEXT AS $$
1413 dyn_profile vandelay.compile_profile%ROWTYPE;
1421 IF target_marc IS NULL OR template_marc IS NULL THEN
1422 -- RAISE NOTICE 'no marc for target or template record';
1426 dyn_profile := vandelay.compile_profile( template_marc );
1428 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
1429 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
1433 IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
1434 --Since we have nothing to do, just return what we were given.
1436 ELSIF dyn_profile.replace_rule <> '' THEN
1437 trgt_marc = target_marc;
1438 tmpl_marc = template_marc;
1439 replace_rule = dyn_profile.replace_rule;
1441 tmp_marc = target_marc;
1442 trgt_marc = template_marc;
1443 tmpl_marc = tmp_marc;
1444 replace_rule = dyn_profile.preserve_rule;
1447 RETURN vandelay.merge_record_xml( trgt_marc, tmpl_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule );
1450 $$ LANGUAGE PLPGSQL;
1452 CREATE OR REPLACE FUNCTION vandelay.template_overlay_bib_record ( v_marc TEXT, eg_id BIGINT) RETURNS BOOL AS $$
1453 SELECT vandelay.template_overlay_bib_record( $1, $2, NULL);
1456 CREATE OR REPLACE FUNCTION vandelay.overlay_bib_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1462 update_fields TEXT[];
1466 SELECT q.marc, q.bib_source INTO v_marc, v_bib_source
1467 FROM vandelay.queued_bib_record q
1468 JOIN vandelay.bib_match m ON (m.queued_record = q.id AND q.id = import_id)
1471 IF v_marc IS NULL THEN
1472 -- RAISE NOTICE 'no marc for vandelay or bib record';
1476 IF vandelay.template_overlay_bib_record( v_marc, eg_id, merge_profile_id) THEN
1477 UPDATE vandelay.queued_bib_record
1478 SET imported_as = eg_id,
1480 WHERE id = import_id;
1482 editor_string := (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1];
1484 IF editor_string IS NOT NULL AND editor_string <> '' THEN
1485 SELECT usr INTO editor_id FROM actor.card WHERE barcode = editor_string;
1487 IF editor_id IS NULL THEN
1488 SELECT id INTO editor_id FROM actor.usr WHERE usrname = editor_string;
1491 IF editor_id IS NOT NULL THEN
1492 --only update the edit date if we have a valid editor
1493 update_fields := ARRAY_APPEND(update_fields, 'editor = ' || editor_id || ', edit_date = NOW()');
1497 IF v_bib_source IS NOT NULL THEN
1498 update_fields := ARRAY_APPEND(update_fields, 'source = ' || v_bib_source);
1501 IF ARRAY_LENGTH(update_fields, 1) > 0 THEN
1502 update_query := 'UPDATE biblio.record_entry SET ' || ARRAY_TO_STRING(update_fields, ',') || ' WHERE id = ' || eg_id || ';';
1503 --RAISE NOTICE 'query: %', update_query;
1504 EXECUTE update_query;
1510 -- RAISE NOTICE 'update of biblio.record_entry failed';
1515 $$ LANGUAGE PLPGSQL;
1517 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_record_with_best ( import_id BIGINT, merge_profile_id INT, lwm_ratio_value_p NUMERIC ) RETURNS BOOL AS $$
1520 lwm_ratio_value NUMERIC;
1523 lwm_ratio_value := COALESCE(lwm_ratio_value_p, 0.0);
1525 PERFORM * FROM vandelay.queued_bib_record WHERE import_time IS NOT NULL AND id = import_id;
1528 -- RAISE NOTICE 'already imported, cannot auto-overlay'
1532 SELECT m.eg_record INTO eg_id
1533 FROM vandelay.bib_match m
1534 JOIN vandelay.queued_bib_record qr ON (m.queued_record = qr.id)
1535 JOIN vandelay.bib_queue q ON (qr.queue = q.id)
1536 JOIN biblio.record_entry r ON (r.id = m.eg_record)
1537 WHERE m.queued_record = import_id
1538 AND qr.quality::NUMERIC / COALESCE(NULLIF(m.quality,0),1)::NUMERIC >= lwm_ratio_value
1539 ORDER BY m.match_score DESC, -- required match score
1540 qr.quality::NUMERIC / COALESCE(NULLIF(m.quality,0),1)::NUMERIC DESC, -- quality tie breaker
1541 m.id -- when in doubt, use the first match
1544 IF eg_id IS NULL THEN
1545 -- RAISE NOTICE 'incoming record is not of high enough quality';
1549 RETURN vandelay.overlay_bib_record( import_id, eg_id, merge_profile_id );
1551 $$ LANGUAGE PLPGSQL;
1553 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_record_with_best ( import_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1554 SELECT vandelay.auto_overlay_bib_record_with_best( $1, $2, p.lwm_ratio ) FROM vandelay.merge_profile p WHERE id = $2;
1557 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_record ( import_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1563 PERFORM * FROM vandelay.queued_bib_record WHERE import_time IS NOT NULL AND id = import_id;
1566 -- RAISE NOTICE 'already imported, cannot auto-overlay'
1570 SELECT COUNT(*) INTO match_count FROM vandelay.bib_match WHERE queued_record = import_id;
1572 IF match_count <> 1 THEN
1573 -- RAISE NOTICE 'not an exact match';
1577 -- Check that the one match is on the first 901c
1578 SELECT m.eg_record INTO eg_id
1579 FROM vandelay.queued_bib_record q
1580 JOIN vandelay.bib_match m ON (m.queued_record = q.id)
1581 WHERE q.id = import_id
1582 AND m.eg_record = oils_xpath_string('//*[@tag="901"]/*[@code="c"][1]',marc)::BIGINT;
1585 -- RAISE NOTICE 'not a 901c match';
1589 RETURN vandelay.overlay_bib_record( import_id, eg_id, merge_profile_id );
1591 $$ LANGUAGE PLPGSQL;
1593 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_queue ( queue_id BIGINT, merge_profile_id INT ) RETURNS SETOF BIGINT AS $$
1595 queued_record vandelay.queued_bib_record%ROWTYPE;
1598 FOR queued_record IN SELECT * FROM vandelay.queued_bib_record WHERE queue = queue_id AND import_time IS NULL LOOP
1600 IF vandelay.auto_overlay_bib_record( queued_record.id, merge_profile_id ) THEN
1601 RETURN NEXT queued_record.id;
1609 $$ LANGUAGE PLPGSQL;
1611 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_queue_with_best ( queue_id BIGINT, merge_profile_id INT, lwm_ratio_value NUMERIC ) RETURNS SETOF BIGINT AS $$
1613 queued_record vandelay.queued_bib_record%ROWTYPE;
1616 FOR queued_record IN SELECT * FROM vandelay.queued_bib_record WHERE queue = queue_id AND import_time IS NULL LOOP
1618 IF vandelay.auto_overlay_bib_record_with_best( queued_record.id, merge_profile_id, lwm_ratio_value ) THEN
1619 RETURN NEXT queued_record.id;
1627 $$ LANGUAGE PLPGSQL;
1629 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_queue_with_best ( import_id BIGINT, merge_profile_id INT ) RETURNS SETOF BIGINT AS $$
1630 SELECT vandelay.auto_overlay_bib_queue_with_best( $1, $2, p.lwm_ratio ) FROM vandelay.merge_profile p WHERE id = $2;
1633 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_bib_queue ( queue_id BIGINT ) RETURNS SETOF BIGINT AS $$
1634 SELECT * FROM vandelay.auto_overlay_bib_queue( $1, NULL );
1637 CREATE OR REPLACE FUNCTION vandelay.ingest_bib_marc ( ) RETURNS TRIGGER AS $$
1643 IF TG_OP IN ('INSERT','UPDATE') AND NEW.imported_as IS NOT NULL THEN
1647 FOR adef IN SELECT * FROM vandelay.bib_attr_definition LOOP
1649 SELECT extract_marc_field('vandelay.queued_bib_record', id, adef.xpath, adef.remove) INTO value FROM vandelay.queued_bib_record WHERE id = NEW.id;
1650 IF (value IS NOT NULL AND value <> '') THEN
1651 INSERT INTO vandelay.queued_bib_record_attr (record, field, attr_value) VALUES (NEW.id, adef.id, value);
1658 $$ LANGUAGE PLPGSQL;
1660 CREATE OR REPLACE FUNCTION vandelay.cleanup_bib_marc ( ) RETURNS TRIGGER AS $$
1662 IF TG_OP IN ('INSERT','UPDATE') AND NEW.imported_as IS NOT NULL THEN
1666 DELETE FROM vandelay.queued_bib_record_attr WHERE record = OLD.id;
1667 DELETE FROM vandelay.import_item WHERE record = OLD.id;
1669 IF TG_OP = 'UPDATE' THEN
1674 $$ LANGUAGE PLPGSQL;
1676 CREATE TRIGGER cleanup_bib_trigger
1677 BEFORE UPDATE OR DELETE ON vandelay.queued_bib_record
1678 FOR EACH ROW EXECUTE PROCEDURE vandelay.cleanup_bib_marc();
1680 CREATE TRIGGER ingest_bib_trigger
1681 AFTER INSERT OR UPDATE ON vandelay.queued_bib_record
1682 FOR EACH ROW EXECUTE PROCEDURE vandelay.ingest_bib_marc();
1684 CREATE TRIGGER zz_match_bibs_trigger
1685 BEFORE INSERT OR UPDATE ON vandelay.queued_bib_record
1686 FOR EACH ROW EXECUTE PROCEDURE vandelay.match_bib_record();
1689 /* Authority stuff down here */
1690 ---------------------------------------
1691 CREATE TABLE vandelay.authority_attr_definition (
1692 id SERIAL PRIMARY KEY,
1693 code TEXT UNIQUE NOT NULL,
1695 xpath TEXT NOT NULL,
1696 remove TEXT NOT NULL DEFAULT ''
1699 CREATE TYPE vandelay.authority_queue_queue_type AS ENUM ('authority');
1700 CREATE TABLE vandelay.authority_queue (
1701 queue_type vandelay.authority_queue_queue_type NOT NULL DEFAULT 'authority',
1702 CONSTRAINT vand_authority_queue_name_once_per_owner_const UNIQUE (owner,name,queue_type)
1703 ) INHERITS (vandelay.queue);
1704 ALTER TABLE vandelay.authority_queue ADD PRIMARY KEY (id);
1706 CREATE TABLE vandelay.queued_authority_record (
1707 queue INT NOT NULL REFERENCES vandelay.authority_queue (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
1708 imported_as INT REFERENCES authority.record_entry (id) DEFERRABLE INITIALLY DEFERRED,
1709 import_error TEXT REFERENCES vandelay.import_error (code) ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
1711 ) INHERITS (vandelay.queued_record);
1712 ALTER TABLE vandelay.queued_authority_record ADD PRIMARY KEY (id);
1713 CREATE INDEX queued_authority_record_queue_idx ON vandelay.queued_authority_record (queue);
1715 CREATE TABLE vandelay.queued_authority_record_attr (
1716 id BIGSERIAL PRIMARY KEY,
1717 record BIGINT NOT NULL REFERENCES vandelay.queued_authority_record (id) DEFERRABLE INITIALLY DEFERRED,
1718 field INT NOT NULL REFERENCES vandelay.authority_attr_definition (id) DEFERRABLE INITIALLY DEFERRED,
1719 attr_value TEXT NOT NULL
1721 CREATE INDEX queued_authority_record_attr_record_idx ON vandelay.queued_authority_record_attr (record);
1723 CREATE TABLE vandelay.authority_match (
1724 id BIGSERIAL PRIMARY KEY,
1725 queued_record BIGINT REFERENCES vandelay.queued_authority_record (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
1726 eg_record BIGINT REFERENCES authority.record_entry (id) DEFERRABLE INITIALLY DEFERRED,
1727 quality INT NOT NULL DEFAULT 0,
1728 match_score INT NOT NULL DEFAULT 0
1731 CREATE OR REPLACE FUNCTION vandelay.ingest_authority_marc ( ) RETURNS TRIGGER AS $$
1737 IF TG_OP IN ('INSERT','UPDATE') AND NEW.imported_as IS NOT NULL THEN
1741 FOR adef IN SELECT * FROM vandelay.authority_attr_definition LOOP
1743 SELECT extract_marc_field('vandelay.queued_authority_record', id, adef.xpath, adef.remove) INTO value FROM vandelay.queued_authority_record WHERE id = NEW.id;
1744 IF (value IS NOT NULL AND value <> '') THEN
1745 INSERT INTO vandelay.queued_authority_record_attr (record, field, attr_value) VALUES (NEW.id, adef.id, value);
1752 $$ LANGUAGE PLPGSQL;
1754 CREATE OR REPLACE FUNCTION vandelay.cleanup_authority_marc ( ) RETURNS TRIGGER AS $$
1756 IF TG_OP IN ('INSERT','UPDATE') AND NEW.imported_as IS NOT NULL THEN
1760 DELETE FROM vandelay.queued_authority_record_attr WHERE record = OLD.id;
1761 IF TG_OP = 'UPDATE' THEN
1766 $$ LANGUAGE PLPGSQL;
1768 CREATE TRIGGER cleanup_authority_trigger
1769 BEFORE UPDATE OR DELETE ON vandelay.queued_authority_record
1770 FOR EACH ROW EXECUTE PROCEDURE vandelay.cleanup_authority_marc();
1772 CREATE TRIGGER ingest_authority_trigger
1773 AFTER INSERT OR UPDATE ON vandelay.queued_authority_record
1774 FOR EACH ROW EXECUTE PROCEDURE vandelay.ingest_authority_marc();
1776 CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1778 merge_profile vandelay.merge_profile%ROWTYPE;
1779 dyn_profile vandelay.compile_profile%ROWTYPE;
1788 SELECT b.marc INTO eg_marc
1789 FROM authority.record_entry b
1790 JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
1793 SELECT q.marc INTO v_marc
1794 FROM vandelay.queued_record q
1795 JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id)
1798 IF eg_marc IS NULL OR v_marc IS NULL THEN
1799 -- RAISE NOTICE 'no marc for vandelay or authority record';
1803 dyn_profile := vandelay.compile_profile( v_marc );
1805 IF merge_profile_id IS NOT NULL THEN
1806 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
1808 dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
1809 dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
1810 dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
1811 dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
1815 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
1816 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
1820 IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
1821 --Since we have nothing to do, just return a NOOP "we did it"
1823 ELSIF dyn_profile.replace_rule <> '' THEN
1824 source_marc = v_marc;
1825 target_marc = eg_marc;
1826 replace_rule = dyn_profile.replace_rule;
1828 source_marc = eg_marc;
1829 target_marc = v_marc;
1830 replace_rule = dyn_profile.preserve_rule;
1833 UPDATE authority.record_entry
1834 SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
1838 UPDATE vandelay.queued_authority_record
1839 SET imported_as = eg_id,
1841 WHERE id = import_id;
1845 -- RAISE NOTICE 'update of authority.record_entry failed';
1850 $$ LANGUAGE PLPGSQL;
1852 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_authority_record ( import_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
1857 SELECT COUNT(*) INTO match_count FROM vandelay.authority_match WHERE queued_record = import_id;
1859 IF match_count <> 1 THEN
1860 -- RAISE NOTICE 'not an exact match';
1864 SELECT m.eg_record INTO eg_id
1865 FROM vandelay.authority_match m
1866 WHERE m.queued_record = import_id
1869 IF eg_id IS NULL THEN
1873 RETURN vandelay.overlay_authority_record( import_id, eg_id, merge_profile_id );
1875 $$ LANGUAGE PLPGSQL;
1877 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_authority_queue ( queue_id BIGINT, merge_profile_id INT ) RETURNS SETOF BIGINT AS $$
1879 queued_record vandelay.queued_authority_record%ROWTYPE;
1882 FOR queued_record IN SELECT * FROM vandelay.queued_authority_record WHERE queue = queue_id AND import_time IS NULL LOOP
1884 IF vandelay.auto_overlay_authority_record( queued_record.id, merge_profile_id ) THEN
1885 RETURN NEXT queued_record.id;
1893 $$ LANGUAGE PLPGSQL;
1895 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_authority_queue ( queue_id BIGINT ) RETURNS SETOF BIGINT AS $$
1896 SELECT * FROM vandelay.auto_overlay_authority_queue( $1, NULL );
1899 CREATE OR REPLACE FUNCTION vandelay.match_set_test_authxml(
1900 match_set_id INTEGER, record_xml TEXT
1901 ) RETURNS SETOF vandelay.match_set_test_result AS $$
1912 tags_rstore := vandelay.flatten_marc_hstore(record_xml);
1914 SELECT normalize_heading INTO heading
1915 FROM authority.normalize_heading(record_xml);
1917 CREATE TEMPORARY TABLE _vandelay_tmp_qrows (q INTEGER);
1918 CREATE TEMPORARY TABLE _vandelay_tmp_jrows (j TEXT);
1920 -- generate the where clause and return that directly (into wq), and as
1921 -- a side-effect, populate the _vandelay_tmp_[qj]rows tables.
1922 wq := vandelay.get_expr_from_match_set(
1923 match_set_id, tags_rstore, heading);
1925 query_ := 'SELECT DISTINCT(record), ';
1927 -- qrows table is for the quality bits we add to the SELECT clause
1929 'COALESCE(n' || q::TEXT || '.quality, 0)', ' + '
1930 ) INTO coal FROM _vandelay_tmp_qrows;
1932 -- our query string so far is the SELECT clause and the inital FROM.
1933 -- no JOINs yet nor the WHERE clause
1934 query_ := query_ || coal || ' AS quality ' || E'\n';
1936 -- jrows table is for the joins we must make (and the real text conditions)
1937 SELECT STRING_AGG(j, E'\n') INTO joins
1938 FROM _vandelay_tmp_jrows;
1940 -- add those joins and the where clause to our query.
1941 query_ := query_ || joins || E'\n';
1943 query_ := query_ || 'JOIN authority.record_entry are ON (are.id = record) '
1944 || 'WHERE ' || wq || ' AND not are.deleted';
1946 -- this will return rows of record,quality
1947 FOR rec IN EXECUTE query_ USING tags_rstore LOOP
1951 DROP TABLE _vandelay_tmp_qrows;
1952 DROP TABLE _vandelay_tmp_jrows;
1955 $$ LANGUAGE PLPGSQL;
1957 CREATE OR REPLACE FUNCTION vandelay.measure_auth_record_quality
1958 ( xml TEXT, match_set_id INT ) RETURNS INT AS $_$
1962 test vandelay.match_set_quality%ROWTYPE;
1965 FOR test IN SELECT * FROM vandelay.match_set_quality
1966 WHERE match_set = match_set_id LOOP
1967 IF test.tag IS NOT NULL THEN
1968 FOR rvalue IN SELECT value FROM vandelay.flatten_marc( xml )
1969 WHERE tag = test.tag AND subfield = test.subfield LOOP
1970 IF test.value = rvalue THEN
1971 out_q := out_q + test.quality;
1979 $_$ LANGUAGE PLPGSQL;
1983 CREATE OR REPLACE FUNCTION vandelay.match_authority_record() RETURNS TRIGGER AS $func$
1985 incoming_existing_id TEXT;
1986 test_result vandelay.match_set_test_result%ROWTYPE;
1990 IF TG_OP IN ('INSERT','UPDATE') AND NEW.imported_as IS NOT NULL THEN
1994 DELETE FROM vandelay.authority_match WHERE queued_record = NEW.id;
1996 SELECT q.match_set INTO match_set FROM vandelay.authority_queue q WHERE q.id = NEW.queue;
1998 IF match_set IS NOT NULL THEN
1999 NEW.quality := vandelay.measure_auth_record_quality( NEW.marc, match_set );
2002 -- Perfect matches on 901$c exit early with a match with high quality.
2003 incoming_existing_id :=
2004 oils_xpath_string('//*[@tag="901"]/*[@code="c"][1]', NEW.marc);
2006 IF incoming_existing_id IS NOT NULL AND incoming_existing_id != '' THEN
2007 SELECT id INTO tmp_rec FROM authority.record_entry WHERE id = incoming_existing_id::bigint;
2008 IF tmp_rec IS NOT NULL THEN
2009 INSERT INTO vandelay.authority_match (queued_record, eg_record, match_score, quality)
2014 -- note: no match_set means quality==0
2015 vandelay.measure_auth_record_quality( b.marc, match_set )
2016 FROM authority.record_entry b
2017 WHERE id = incoming_existing_id::bigint;
2021 IF match_set IS NULL THEN
2025 FOR test_result IN SELECT * FROM
2026 vandelay.match_set_test_authxml(match_set, NEW.marc) LOOP
2028 INSERT INTO vandelay.authority_match ( queued_record, eg_record, match_score, quality )
2032 test_result.quality,
2033 vandelay.measure_auth_record_quality( b.marc, match_set )
2034 FROM authority.record_entry b
2035 WHERE id = test_result.record;
2041 $func$ LANGUAGE PLPGSQL;
2043 CREATE TRIGGER zz_match_auths_trigger
2044 BEFORE INSERT OR UPDATE ON vandelay.queued_authority_record
2045 FOR EACH ROW EXECUTE PROCEDURE vandelay.match_authority_record();
2047 CREATE OR REPLACE FUNCTION vandelay.auto_overlay_authority_record_with_best ( import_id BIGINT, merge_profile_id INT, lwm_ratio_value_p NUMERIC ) RETURNS BOOL AS $$
2050 lwm_ratio_value NUMERIC;
2053 lwm_ratio_value := COALESCE(lwm_ratio_value_p, 0.0);
2055 PERFORM * FROM vandelay.queued_authority_record WHERE import_time IS NOT NULL AND id = import_id;
2058 -- RAISE NOTICE 'already imported, cannot auto-overlay'
2062 SELECT m.eg_record INTO eg_id
2063 FROM vandelay.authority_match m
2064 JOIN vandelay.queued_authority_record qr ON (m.queued_record = qr.id)
2065 JOIN vandelay.authority_queue q ON (qr.queue = q.id)
2066 JOIN authority.record_entry r ON (r.id = m.eg_record)
2067 WHERE m.queued_record = import_id
2068 AND qr.quality::NUMERIC / COALESCE(NULLIF(m.quality,0),1)::NUMERIC >= lwm_ratio_value
2069 ORDER BY m.match_score DESC, -- required match score
2070 qr.quality::NUMERIC / COALESCE(NULLIF(m.quality,0),1)::NUMERIC DESC, -- quality tie breaker
2071 m.id -- when in doubt, use the first match
2074 IF eg_id IS NULL THEN
2075 -- RAISE NOTICE 'incoming record is not of high enough quality';
2079 RETURN vandelay.overlay_authority_record( import_id, eg_id, merge_profile_id );
2081 $$ LANGUAGE PLPGSQL;
2086 -- Vandelay (for importing and exporting records) 012.schema.vandelay.sql
2087 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (1, 'title', oils_i18n_gettext(1, 'vqbrad', 'Title of work', 'description'),'//*[@tag="245"]/*[contains("abcmnopr",@code)]');
2088 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (2, 'author', oils_i18n_gettext(1, 'vqbrad', 'Author of work', 'description'),'//*[@tag="100" or @tag="110" or @tag="113"]/*[contains("ad",@code)]');
2089 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (3, 'language', oils_i18n_gettext(3, 'vqbrad', 'Language of work', 'description'),'//*[@tag="240"]/*[@code="l"][1]');
2090 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (4, 'pagination', oils_i18n_gettext(4, 'vqbrad', 'Pagination', 'description'),'//*[@tag="300"]/*[@code="a"][1]');
2091 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident, remove ) VALUES (5, 'isbn',oils_i18n_gettext(5, 'vqbrad', 'ISBN', 'description'),'//*[@tag="020"]/*[@code="a"]', TRUE, $r$(?:-|\s.+$)$r$);
2092 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident, remove ) VALUES (6, 'issn',oils_i18n_gettext(6, 'vqbrad', 'ISSN', 'description'),'//*[@tag="022"]/*[@code="a"]', TRUE, $r$(?:-|\s.+$)$r$);
2093 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (7, 'price',oils_i18n_gettext(7, 'vqbrad', 'Price', 'description'),'//*[@tag="020" or @tag="022"]/*[@code="c"][1]');
2094 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident ) VALUES (8, 'rec_identifier',oils_i18n_gettext(8, 'vqbrad', 'Accession Number', 'description'),'//*[@tag="001"]', TRUE);
2095 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident ) VALUES (9, 'eg_tcn',oils_i18n_gettext(9, 'vqbrad', 'TCN Value', 'description'),'//*[@tag="901"]/*[@code="a"]', TRUE);
2096 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident ) VALUES (10, 'eg_tcn_source',oils_i18n_gettext(10, 'vqbrad', 'TCN Source', 'description'),'//*[@tag="901"]/*[@code="b"]', TRUE);
2097 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, ident ) VALUES (11, 'eg_identifier',oils_i18n_gettext(11, 'vqbrad', 'Internal ID', 'description'),'//*[@tag="901"]/*[@code="c"]', TRUE);
2098 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (12, 'publisher',oils_i18n_gettext(12, 'vqbrad', 'Publisher', 'description'),'//*[@tag="260"]/*[@code="b"][1]');
2099 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath, remove ) VALUES (13, 'pubdate',oils_i18n_gettext(13, 'vqbrad', 'Publication Date', 'description'),'//*[@tag="260"]/*[@code="c"][1]',$r$\D$r$);
2100 --INSERT INTO vandelay.bib_attr_definition ( id, code, description, xpath ) VALUES (14, 'edition',oils_i18n_gettext(14, 'vqbrad', 'Edition', 'description'),'//*[@tag="250"]/*[@code="a"][1]');
2102 --INSERT INTO vandelay.import_item_attr_definition (
2103 -- owner, name, tag, owning_lib, circ_lib, location,
2104 -- call_number, circ_modifier, barcode, price, copy_number,
2105 -- circulate, ref, holdable, opac_visible, status
2108 -- 'Evergreen 852 export format',
2110 -- '[@code = "b"][1]',
2111 -- '[@code = "b"][2]',
2118 -- '[@code = "x" and text() = "circulating"]',
2119 -- '[@code = "x" and text() = "reference"]',
2120 -- '[@code = "x" and text() = "holdable"]',
2121 -- '[@code = "x" and text() = "visible"]',
2125 --INSERT INTO vandelay.import_item_attr_definition (
2138 -- 'Unicorn Import format -- 999',
2149 --INSERT INTO vandelay.authority_attr_definition ( code, description, xpath, ident ) VALUES ('rec_identifier','Identifier','//*[@tag="001"]', TRUE);