]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1006.function.trim_trailing_punctuation.sql
5a4004aae5f508a83c493015412f4bc92203b12f
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1006.function.trim_trailing_punctuation.sql
1 SELECT evergreen.upgrade_deps_block_check('1006', :eg_version);
2
3 BEGIN;
4
5 -- This function is used to help clean up facet labels. Due to quirks in
6 -- MARC parsing, some facet labels may be generated with periods or commas
7 -- at the end.  This will strip a trailing commas off all the time, and
8 -- periods when they don't look like they are part of initials.
9 --      Smith, John    =>  no change
10 --      Smith, John,   =>  Smith, John
11 --      Smith, John.   =>  Smith, John
12 --      Public, John Q. => no change
13 CREATE OR REPLACE FUNCTION metabib.trim_trailing_punctuation ( TEXT ) RETURNS TEXT AS $$
14 DECLARE
15     result    TEXT;
16     last_char TEXT;
17 BEGIN
18     result := $1;
19     last_char = substring(result from '.$');
20
21     IF last_char = ',' THEN
22         result := substring(result from '^(.*),$');
23
24     ELSIF last_char = '.' THEN
25         IF substring(result from ' \w\.$') IS NULL THEN
26             result := substring(result from '^(.*)\.$');
27         END IF;
28     END IF;
29
30     RETURN result;
31
32 END;
33 $$ language 'plpgsql';
34
35 INSERT INTO config.index_normalizer (name, description, func, param_count) VALUES (
36         'Trim Trailing Punctuation',
37         'Eliminate extraneous trailing commas and periods in text',
38         'metabib.trim_trailing_punctuation',
39         0
40 );
41
42 INSERT INTO config.metabib_field_index_norm_map (field,norm,pos)
43     SELECT  m.id,
44             i.id,
45             -1
46       FROM  config.metabib_field m,
47             config.index_normalizer i
48       WHERE i.func = 'metabib.trim_trailing_punctuation'
49             AND m.id IN (7,8,9,10);
50
51 COMMIT;
52