]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/2.1.1-2.1.2-upgrade-db.sql
LP918020: Fix regression in biblio.extract_located_uris
[working/Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 2.1.1-2.1.2-upgrade-db.sql
1 -- Fix sorting by pubdate by ensuring migrated records
2 -- have a pubdate attribute in metabib.record_attr.attrs
3 UPDATE metabib.record_attr
4    SET attrs = attrs || ('pubdate' => (attrs->'date1'))
5    WHERE defined(attrs, 'pubdate') IS FALSE
6    AND defined(attrs, 'date1') IS TRUE;
7
8 CREATE OR REPLACE FUNCTION biblio.extract_located_uris( bib_id BIGINT, marcxml TEXT, editor_id INT ) RETURNS VOID AS $func$
9 DECLARE
10     uris            TEXT[];
11     uri_xml         TEXT;
12     uri_label       TEXT;
13     uri_href        TEXT;
14     uri_use         TEXT;
15     uri_owner_list  TEXT[];
16     uri_owner       TEXT;
17     uri_owner_id    INT;
18     uri_id          INT;
19     uri_cn_id       INT;
20     uri_map_id      INT;
21 BEGIN
22
23     -- Clear any URI mappings and call numbers for this bib.
24     -- This leads to acn / auricnm inflation, but also enables
25     -- old acn/auricnm's to go away and for bibs to be deleted.
26     FOR uri_cn_id IN SELECT id FROM asset.call_number WHERE record = bib_id AND label = '##URI##' AND NOT deleted LOOP
27         DELETE FROM asset.uri_call_number_map WHERE call_number = uri_cn_id;
28         DELETE FROM asset.call_number WHERE id = uri_cn_id;
29     END LOOP;
30
31     uris := oils_xpath('//*[@tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]',marcxml);
32     IF ARRAY_UPPER(uris,1) > 0 THEN
33         FOR i IN 1 .. ARRAY_UPPER(uris, 1) LOOP
34             -- First we pull info out of the 856
35             uri_xml     := uris[i];
36
37             uri_href    := (oils_xpath('//*[@code="u"]/text()',uri_xml))[1];
38             uri_label   := (oils_xpath('//*[@code="y"]/text()|//*[@code="3"]/text()',uri_xml))[1];
39             uri_use     := (oils_xpath('//*[@code="z"]/text()|//*[@code="2"]/text()|//*[@code="n"]/text()',uri_xml))[1];
40
41             IF uri_label IS NULL THEN
42                 uri_label := uri_href;
43             END IF;
44             CONTINUE WHEN uri_href IS NULL;
45
46             -- Get the distinct list of libraries wanting to use 
47             SELECT  ARRAY_ACCUM(
48                         DISTINCT REGEXP_REPLACE(
49                             x,
50                             $re$^.*?\((\w+)\).*$$re$,
51                             E'\\1'
52                         )
53                     ) INTO uri_owner_list
54               FROM  UNNEST(
55                         oils_xpath(
56                             '//*[@code="9"]/text()|//*[@code="w"]/text()|//*[@code="n"]/text()',
57                             uri_xml
58                         )
59                     )x;
60
61             IF ARRAY_UPPER(uri_owner_list,1) > 0 THEN
62
63                 -- look for a matching uri
64                 IF uri_use IS NULL THEN
65                     SELECT id INTO uri_id
66                         FROM asset.uri
67                         WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active
68                         ORDER BY id LIMIT 1;
69                     IF NOT FOUND THEN -- create one
70                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
71                         SELECT id INTO uri_id
72                             FROM asset.uri
73                             WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
74                     END IF;
75                 ELSE
76                     SELECT id INTO uri_id
77                         FROM asset.uri
78                         WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active
79                         ORDER BY id LIMIT 1;
80                     IF NOT FOUND THEN -- create one
81                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
82                         SELECT id INTO uri_id
83                             FROM asset.uri
84                             WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
85                     END IF;
86                 END IF;
87
88                 FOR j IN 1 .. ARRAY_UPPER(uri_owner_list, 1) LOOP
89                     uri_owner := uri_owner_list[j];
90
91                     SELECT id INTO uri_owner_id FROM actor.org_unit WHERE shortname = uri_owner;
92                     CONTINUE WHEN NOT FOUND;
93
94                     -- we need a call number to link through
95                     SELECT id INTO uri_cn_id FROM asset.call_number WHERE owning_lib = uri_owner_id AND record = bib_id AND label = '##URI##' AND NOT deleted;
96                     IF NOT FOUND THEN
97                         INSERT INTO asset.call_number (owning_lib, record, create_date, edit_date, creator, editor, label)
98                             VALUES (uri_owner_id, bib_id, 'now', 'now', editor_id, editor_id, '##URI##');
99                         SELECT id INTO uri_cn_id FROM asset.call_number WHERE owning_lib = uri_owner_id AND record = bib_id AND label = '##URI##' AND NOT deleted;
100                     END IF;
101
102                     -- now, link them if they're not already
103                     SELECT id INTO uri_map_id FROM asset.uri_call_number_map WHERE call_number = uri_cn_id AND uri = uri_id;
104                     IF NOT FOUND THEN
105                         INSERT INTO asset.uri_call_number_map (call_number, uri) VALUES (uri_cn_id, uri_id);
106                     END IF;
107
108                 END LOOP;
109
110             END IF;
111
112         END LOOP;
113     END IF;
114
115     RETURN;
116 END;
117 $func$ LANGUAGE PLPGSQL;
118