update authority.simple_heading_set() to use authority.extract_headings() where possible
authorGalen Charlton <gmc@equinoxinitiative.org>
Fri, 13 Jan 2017 20:36:15 +0000 (15:36 -0500)
committerGalen Charlton <gmc@equinoxinitiative.org>
Fri, 13 Jan 2017 20:36:15 +0000 (15:36 -0500)
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/sql/Pg/upgrade/YYYY.schema.authority.sql

index fd5a123..6310912 100644 (file)
@@ -415,3 +415,101 @@ AND ahf.heading_type = 'genre_form_term';
 -- SELECT * FROM authority.extract_headings(146);
 -- SELECT * FROM authority.extract_headings(174);
 -- SELECT * FROM authority.extract_headings(151);
+
+CREATE OR REPLACE FUNCTION authority.simple_heading_set( marcxml TEXT ) RETURNS SETOF authority.simple_heading AS $func$
+DECLARE
+    res             authority.simple_heading%ROWTYPE;
+    acsaf           authority.control_set_authority_field%ROWTYPE;
+    heading         authority.heading%ROWTYPE;
+    tag_used        TEXT;
+    nfi_used        TEXT;
+    sf              TEXT;
+    cset            INT;
+    heading_text    TEXT;
+    joiner_text     TEXT;
+    sort_text       TEXT;
+    tmp_text        TEXT;
+    tmp_xml         TEXT;
+    first_sf        BOOL;
+    auth_id         INT DEFAULT COALESCE(NULLIF(oils_xpath_string('//*[@tag="901"]/*[local-name()="subfield" and @code="c"]', marcxml), ''), '0')::INT; 
+BEGIN
+
+    SELECT control_set INTO cset FROM authority.record_entry WHERE id = auth_id;
+
+    IF cset IS NULL THEN
+        SELECT  control_set INTO cset
+          FROM  authority.control_set_authority_field
+          WHERE tag IN ( SELECT  UNNEST(XPATH('//*[starts-with(@tag,"1")]/@tag',marcxml::XML)::TEXT[]))
+          LIMIT 1;
+    END IF;
+
+    res.record := auth_id;
+    res.thesaurus := authority.extract_thesaurus(marcxml);
+
+    FOR acsaf IN SELECT * FROM authority.control_set_authority_field WHERE control_set = cset LOOP
+
+        res.atag := acsaf.id;
+        tag_used := acsaf.tag;
+        nfi_used := acsaf.nfi;
+        joiner_text := COALESCE(acsaf.joiner, ' ');
+
+        IF acsaf.heading_field IS NOT NULL THEN
+            FOR heading IN SELECT * FROM authority.extract_headings(marcxml) LOOP
+                IF heading.field = acsaf.heading_field THEN
+                    res.value := heading.heading;
+                    res.sort_value := heading.normalized_heading;
+                    res.index_vector = to_tsvector('keyword'::regconfig, res.sort_value);
+                    RETURN NEXT res;
+                END IF;
+            END LOOP;
+        ELSE
+            FOR tmp_xml IN SELECT UNNEST(XPATH('//*[@tag="'||tag_used||'"]', marcxml::XML)::TEXT[]) LOOP
+
+                heading_text := COALESCE(
+                    oils_xpath_string('./*[contains("'||acsaf.display_sf_list||'",@code)]', tmp_xml, joiner_text),
+                    ''
+                );
+
+                IF nfi_used IS NOT NULL THEN
+
+                    sort_text := SUBSTRING(
+                        heading_text FROM
+                        COALESCE(
+                            NULLIF(
+                                REGEXP_REPLACE(
+                                    oils_xpath_string('./@ind'||nfi_used, tmp_xml::TEXT),
+                                    $$\D+$$,
+                                    '',
+                                    'g'
+                                ),
+                                ''
+                            )::INT,
+                            0
+                        ) + 1
+                    );
+
+                ELSE
+                    sort_text := heading_text;
+                END IF;
+
+                IF heading_text IS NOT NULL AND heading_text <> '' THEN
+                    res.value := heading_text;
+                    res.sort_value := public.naco_normalize(sort_text);
+                    res.index_vector = to_tsvector('keyword'::regconfig, res.sort_value);
+                    RETURN NEXT res;
+                END IF;
+
+            END LOOP;
+        END IF;
+
+    END LOOP;
+
+    RETURN;
+END;
+$func$ LANGUAGE PLPGSQL STABLE STRICT;
+
+
+-- select value from metabib.browse_entry where value ~ '^Shakespeare, William' and value ~ '1564-1616$';
+-- UPDATE config.internal_flag SET enabled = TRUE where name = 'ingest.reingest.force_on_same_marc';
+-- UPDATE authority.record_entry SET id = id WHERE id = 149;
+-- select value from metabib.browse_entry where value ~ '^Shakespeare, William' and value ~ '1564-1616$';