]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0357.schema.isbn1013-translation.sql
LP#1758426: Disable triggers before recalculating bib visibility in 1085
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0357.schema.isbn1013-translation.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0357'); -- dbs
4
5 DELETE FROM config.metabib_field_index_norm_map
6     WHERE norm IN (
7         SELECT id 
8             FROM config.index_normalizer
9             WHERE func IN ('first_word', 'naco_normalize', 'split_date_range')
10     )
11     AND field = 18
12 ;
13
14 CREATE OR REPLACE FUNCTION public.translate_isbn1013( TEXT ) RETURNS TEXT AS $func$
15     use Business::ISBN;
16     use strict;
17     use warnings;
18
19     # For each ISBN found in a single string containing a set of ISBNs:
20     #   * Normalize an incoming ISBN to have the correct checksum and no hyphens
21     #   * Convert an incoming ISBN10 or ISBN13 to its counterpart and return
22
23     my $input = shift;
24     my $output = '';
25
26     foreach my $word (split(/\s/, $input)) {
27         my $isbn = Business::ISBN->new($word);
28
29         # First check the checksum; if it is not valid, fix it and add the original
30         # bad-checksum ISBN to the output
31         if ($isbn && $isbn->is_valid_checksum() == Business::ISBN::BAD_CHECKSUM) {
32             $output .= $isbn->isbn() . " ";
33             $isbn->fix_checksum();
34         }
35
36         # If we now have a valid ISBN, convert it to its counterpart ISBN10/ISBN13
37         # and add the normalized original ISBN to the output
38         if ($isbn && $isbn->is_valid()) {
39             my $isbn_xlated = ($isbn->type eq "ISBN13") ? $isbn->as_isbn10 : $isbn->as_isbn13;
40             $output .= $isbn->isbn . " ";
41
42             # If we successfully converted the ISBN to its counterpart, add the
43             # converted ISBN to the output as well
44             $output .= ($isbn_xlated->isbn . " ") if ($isbn_xlated);
45         }
46     }
47     return $output if $output;
48
49     # If there were no valid ISBNs, just return the raw input
50     return $input;
51 $func$ LANGUAGE PLPERLU;
52
53 COMMENT ON FUNCTION public.translate_isbn1013(TEXT) IS $$
54 /*
55  * Copyright (C) 2010 Merrimack Valley Library Consortium
56  * Jason Stephenson <jstephenson@mvlc.org>
57  * Copyright (C) 2010 Laurentian University
58  * Dan Scott <dscott@laurentian.ca>
59  *
60  * The translate_isbn1013 function takes an input ISBN and returns the
61  * following in a single space-delimited string if the input ISBN is valid:
62  *   - The normalized input ISBN (hyphens stripped)
63  *   - The normalized input ISBN with a fixed checksum if the checksum was bad
64  *   - The ISBN converted to its ISBN10 or ISBN13 counterpart, if possible
65  */
66 $$;
67
68 COMMIT;