From 48ab13cd66c0badfd90b6d3387122968991d006a Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Thu, 7 Mar 2013 11:59:09 -0500 Subject: [PATCH] LP#1150939 fix dewey sort There's a bit in the code where it tries to pad the first digit group, if it's the only digit group, but it assumed the digit group was the first token. Signed-off-by: Jason Etheridge Signed-off-by: Ben Shum --- Open-ILS/src/sql/Pg/040.schema.asset.sql | 7 ++- .../XXXX.schema.acn_dewey_normalizer.sql | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/XXXX.schema.acn_dewey_normalizer.sql diff --git a/Open-ILS/src/sql/Pg/040.schema.asset.sql b/Open-ILS/src/sql/Pg/040.schema.asset.sql index ea10156d2e..dea51fd9c7 100644 --- a/Open-ILS/src/sql/Pg/040.schema.asset.sql +++ b/Open-ILS/src/sql/Pg/040.schema.asset.sql @@ -340,9 +340,13 @@ CREATE OR REPLACE FUNCTION asset.label_normalizer_dewey(TEXT) RETURNS TEXT AS $f $init =~ s/^([\p{IsAlpha}]+)/$1 /; my @tokens = split /\.|\s+/, $init; my $digit_group_count = 0; + my $first_digit_group_idx; for (my $i = 0; $i <= $#tokens; $i++) { if ($tokens[$i] =~ /^\d+$/) { $digit_group_count++; + if ($digit_group_count == 1) { + $first_digit_group_idx = $i; + } if (2 == $digit_group_count) { $tokens[$i] = sprintf("%-15.15s", $tokens[$i]); $tokens[$i] =~ tr/ /0/; @@ -351,7 +355,7 @@ CREATE OR REPLACE FUNCTION asset.label_normalizer_dewey(TEXT) RETURNS TEXT AS $f } # Pad the first digit_group if there was only one if (1 == $digit_group_count) { - $tokens[0] .= '_000000000000000' + $tokens[$first_digit_group_idx] .= '_000000000000000' } my $key = join("_", @tokens); $key =~ s/[^\p{IsAlnum}_]//g; @@ -360,6 +364,7 @@ CREATE OR REPLACE FUNCTION asset.label_normalizer_dewey(TEXT) RETURNS TEXT AS $f $func$ LANGUAGE PLPERLU; + CREATE OR REPLACE FUNCTION asset.label_normalizer_lc(TEXT) RETURNS TEXT AS $func$ use strict; use warnings; diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.acn_dewey_normalizer.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.acn_dewey_normalizer.sql new file mode 100644 index 0000000000..fa4d5015f3 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.acn_dewey_normalizer.sql @@ -0,0 +1,52 @@ +-- Evergreen DB patch XXXX.schema.acn_dewey_normalizer.sql +-- +-- Fixes Dewey call number sorting (per LP# 1150939) +-- +BEGIN; + +-- check whether patch can be applied +SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version); + +CREATE OR REPLACE FUNCTION asset.label_normalizer_dewey(TEXT) RETURNS TEXT AS $func$ + # Derived from the Koha C4::ClassSortRoutine::Dewey module + # Copyright (C) 2007 LibLime + # Licensed under the GPL v2 or later + + use strict; + use warnings; + + my $init = uc(shift); + $init =~ s/^\s+//; + $init =~ s/\s+$//; + $init =~ s!/!!g; + $init =~ s/^([\p{IsAlpha}]+)/$1 /; + my @tokens = split /\.|\s+/, $init; + my $digit_group_count = 0; + my $first_digit_group_idx; + for (my $i = 0; $i <= $#tokens; $i++) { + if ($tokens[$i] =~ /^\d+$/) { + $digit_group_count++; + if ($digit_group_count == 1) { + $first_digit_group_idx = $i; + } + if (2 == $digit_group_count) { + $tokens[$i] = sprintf("%-15.15s", $tokens[$i]); + $tokens[$i] =~ tr/ /0/; + } + } + } + # Pad the first digit_group if there was only one + if (1 == $digit_group_count) { + $tokens[$first_digit_group_idx] .= '_000000000000000' + } + my $key = join("_", @tokens); + $key =~ s/[^\p{IsAlnum}_]//g; + + return $key; + +$func$ LANGUAGE PLPERLU; + +-- regenerate sort keys for any dewey call numbers +UPDATE asset.call_number SET id = id WHERE label_class = 2; + +COMMIT; -- 2.43.2