1 -- Evergreen DB patch 0819.schema.acn_dewey_normalizer.sql
3 -- Fixes Dewey call number sorting (per LP# 1150939)
7 -- check whether patch can be applied
8 SELECT evergreen.upgrade_deps_block_check('0819', :eg_version);
10 CREATE OR REPLACE FUNCTION asset.label_normalizer_dewey(TEXT) RETURNS TEXT AS $func$
11 # Derived from the Koha C4::ClassSortRoutine::Dewey module
12 # Copyright (C) 2007 LibLime
13 # Licensed under the GPL v2 or later
22 $init =~ s/^([\p{IsAlpha}]+)/$1 /;
23 my @tokens = split /\.|\s+/, $init;
24 my $digit_group_count = 0;
25 my $first_digit_group_idx;
26 for (my $i = 0; $i <= $#tokens; $i++) {
27 if ($tokens[$i] =~ /^\d+$/) {
29 if ($digit_group_count == 1) {
30 $first_digit_group_idx = $i;
32 if (2 == $digit_group_count) {
33 $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
34 $tokens[$i] =~ tr/ /0/;
38 # Pad the first digit_group if there was only one
39 if (1 == $digit_group_count) {
40 $tokens[$first_digit_group_idx] .= '_000000000000000'
42 my $key = join("_", @tokens);
43 $key =~ s/[^\p{IsAlnum}_]//g;
47 $func$ LANGUAGE PLPERLU;
49 -- regenerate sort keys for any dewey call numbers
50 UPDATE asset.call_number SET id = id WHERE label_class = 2;