[Koha-patches] [PATCH 2/6] bug 6281: introduce LC::CallNumber::LC for sorting LC call numbers

Galen Charlton gmc at esilibrary.com
Wed Jan 9 04:36:23 CET 2013


This replaces the previous hand-coded normalizer.  Because
LC::CallNumber::LC appears to reject strings that aren't valid
LC call numbers, significant changes to the test cases were
made as well -- however, the one that really counts is the
last one which verifies the sorting.

To recalculate the call number sort key for each item, it is necessary
to run misc/maintenance/touch_all_items.pl

To test, create item records with the following call numbers, setting
the classification sort to 'lccl:

QC100 .U57 NO. 555 1986
QC145 .A57 V.12 1980
QC145.45 .H4 D65 1998
QC995 .E29 1997

Next, make a report of them in the inventory tool.  The items should be sorted
in the above order.

Signed-off-by: Galen Charlton <gmc at esilibrary.com>
---
 C4/ClassSortRoutine/LCC.pm |   19 ++++++-------------
 t/ClassSortRoutine_LCC.t   |   12 ++++++------
 2 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/C4/ClassSortRoutine/LCC.pm b/C4/ClassSortRoutine/LCC.pm
index 6ec3846..f5f4a1c 100644
--- a/C4/ClassSortRoutine/LCC.pm
+++ b/C4/ClassSortRoutine/LCC.pm
@@ -1,6 +1,7 @@
 package C4::ClassSortRoutine::LCC;
 
 # Copyright (C) 2007 LibLime
+# Copyright (C) 2012 Equinox Software, Inc.
 # 
 # This file is part of Koha.
 #
@@ -19,6 +20,7 @@ package C4::ClassSortRoutine::LCC;
 
 use strict;
 use warnings;
+use Library::CallNumber::LC;
 
 use vars qw($VERSION);
 
@@ -50,19 +52,10 @@ sub get_class_sort_key {
 
     $cn_class = '' unless defined $cn_class;
     $cn_item  = '' unless defined $cn_item;
-    my $key = uc "$cn_class $cn_item";
-    $key =~ s/^\s+//;
-    $key =~ s/\s+$//;
-    $key =~ s/^[^\p{IsAlnum}\s.]//g;
-    $key =~ s/^([A-Z]+)/$1 /;
-    $key =~ s/(\.[A-Z])/ $1/g;
-    # handle first digit group
-    $key =~ s/(\d+)/sprintf("%-05.5d", $1)/xe;
-    $key =~ s/\s+/_/g;
-    $key =~ s/\./_/g;
-    $key =~ s/__/_/g;
-    $key =~ s/[^\p{IsAlnum}_]//g;
-
+    my $call_number = Library::CallNumber::LC->new(uc "$cn_class $cn_item");
+    return '' unless defined $call_number;
+    my $key = $call_number->normalize();
+    $key = '' unless defined $key;
     return $key;
 
 }
diff --git a/t/ClassSortRoutine_LCC.t b/t/ClassSortRoutine_LCC.t
index 7454351..9ec37cf 100755
--- a/t/ClassSortRoutine_LCC.t
+++ b/t/ClassSortRoutine_LCC.t
@@ -14,17 +14,17 @@ BEGIN {
 
 #Obvious cases
 is(C4::ClassSortRoutine::LCC::get_class_sort_key(), "", "No arguments returns an empty string");
-is(C4::ClassSortRoutine::LCC::get_class_sort_key('a','b'), "A_B", "Arguments 'a','b' return 'A_B'");
+is(C4::ClassSortRoutine::LCC::get_class_sort_key('a','b'), "A B", "Arguments 'a','b' return 'A B'");
 
 #spaces in arguements
-is(C4::ClassSortRoutine::LCC::get_class_sort_key(' ','b'), "B_", "Arguments ' ','b' return 'B_'");
-is(C4::ClassSortRoutine::LCC::get_class_sort_key('a',' '), "A_", "Arguments 'a',' ' return 'A_'");
+is(C4::ClassSortRoutine::LCC::get_class_sort_key(' ','b'), "B", "Arguments ' ','b' return 'B'");
+is(C4::ClassSortRoutine::LCC::get_class_sort_key('a',' '), "A", "Arguments 'a',' ' return 'A'");
 is(C4::ClassSortRoutine::LCC::get_class_sort_key(' ','    '), "", "Arguments ' ','    ' return ''");
 
 #'funky cases' based on regex in code
-is(C4::ClassSortRoutine::LCC::get_class_sort_key('.','b'), "_B", "Arguments '.','b' return '_B'");
-is(C4::ClassSortRoutine::LCC::get_class_sort_key('....','........'), "_______", "Arguments '....','........' return '_______'");
-is(C4::ClassSortRoutine::LCC::get_class_sort_key('.','.'), "__", "Arguments '.','.' return '__'");
+is(C4::ClassSortRoutine::LCC::get_class_sort_key('.','b'), "", "Arguments '.','b' return ''");
+is(C4::ClassSortRoutine::LCC::get_class_sort_key('....','........'), "", "Arguments '....','........' return ''");
+is(C4::ClassSortRoutine::LCC::get_class_sort_key('.','.'), "", "Arguments '.','.' return ''");
 
 # list of example call numbers -- these
 # are intentionally in the _reverse_ of
-- 
1.7.2.5



More information about the Koha-patches mailing list