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