1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # sorttools.pm --
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 | # various subroutines to format strings
|
---|
27 | # for sorting
|
---|
28 |
|
---|
29 | # To customise sort functions for a particular collection, create a customsorttools.pm inside
|
---|
30 | # collection's perllib folder, and implement any of the functions you want to change.
|
---|
31 |
|
---|
32 | package sorttools;
|
---|
33 |
|
---|
34 | use strict;
|
---|
35 |
|
---|
36 | my $has_custom_sort = 0;
|
---|
37 |
|
---|
38 | sub setup_custom_sort {
|
---|
39 |
|
---|
40 | my $collectdir = $ENV{'GSDLCOLLECTDIR'};
|
---|
41 | my $customperllibfolder = &FileUtils::filenameConcatenate($collectdir, 'perllib');
|
---|
42 | my $customsortfile = &FileUtils::filenameConcatenate($customperllibfolder, 'customsorttools.pm');
|
---|
43 | if (&FileUtils::fileExists($customsortfile)) {
|
---|
44 | # add perllib folder to INC, if its not already there
|
---|
45 | my $found_perllibfolder = 0;
|
---|
46 | foreach my $path (@INC)
|
---|
47 | {
|
---|
48 | if ($path eq $customperllibfolder)
|
---|
49 | {
|
---|
50 | $found_perllibfolder = 1;
|
---|
51 | last;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | if (!$found_perllibfolder)
|
---|
55 | {
|
---|
56 | unshift (@INC, $customperllibfolder);
|
---|
57 | }
|
---|
58 |
|
---|
59 | require customsorttools;
|
---|
60 | $has_custom_sort = 1;
|
---|
61 | }
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | # moved here from BasClas so import can share it
|
---|
66 | sub format_metadata_for_sorting {
|
---|
67 | my ($metaname, $metavalue, $doc_obj) = @_;
|
---|
68 |
|
---|
69 | if ($has_custom_sort && defined (&customsorttools::format_metadata_for_sorting)) {
|
---|
70 | return &customsorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj);
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (!defined $metaname || $metaname !~ /\S/ || ! defined $metavalue || $metavalue !~ /\S/) {
|
---|
74 | return "";
|
---|
75 | }
|
---|
76 |
|
---|
77 | if ($metaname eq "Language") {
|
---|
78 | $metavalue = $iso639::fromiso639{$metavalue};
|
---|
79 | return $metavalue;
|
---|
80 | }
|
---|
81 |
|
---|
82 | my $lang;
|
---|
83 | if (defined $doc_obj) {
|
---|
84 | $lang = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Language');
|
---|
85 | }
|
---|
86 | $lang = 'en' unless defined $lang;
|
---|
87 |
|
---|
88 | # is this metadata likely to be a name?
|
---|
89 | my $function_name="format_string_name_$lang";
|
---|
90 | if ($metaname =~ /^(?:\w+\.)?(?:Creators?|Authors?|Editors?)(?:[:,].*)?$/
|
---|
91 | && exists &$function_name) {
|
---|
92 | no strict 'refs';
|
---|
93 | &$function_name(\$metavalue);
|
---|
94 | } else {
|
---|
95 | $function_name="format_string_$lang";
|
---|
96 | if (exists &$function_name) {
|
---|
97 | no strict 'refs';
|
---|
98 | &$function_name(\$metavalue);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | return $metavalue;
|
---|
103 | }
|
---|
104 |
|
---|
105 | ### language-specific sorting functions (called by format_metadata_for_sorting)
|
---|
106 |
|
---|
107 | ## format_string_$lang() converts to lowercase (where appropriate), and
|
---|
108 | # removes punctuation, articles from the start of string, etc
|
---|
109 | ## format_string_name_$lang() converts to lowercase, puts the surname first,
|
---|
110 | # removes punctuation, etc
|
---|
111 |
|
---|
112 | sub format_string_en {
|
---|
113 | my $stringref = shift;
|
---|
114 |
|
---|
115 | if ($has_custom_sort && defined (&customsorttools::format_string_en)) {
|
---|
116 | return &customsorttools::format_string_en($stringref);
|
---|
117 | }
|
---|
118 |
|
---|
119 | $$stringref = lc($$stringref);
|
---|
120 | $$stringref =~ s/&[^\;]+\;//g; # html entities
|
---|
121 | $$stringref =~ s/^\s*(the|a|an)\b//; # articles
|
---|
122 | $$stringref =~ s/[^[:alnum:]]//g; # remove any non-alphanumeric chars from start of the string
|
---|
123 | $$stringref =~ s/\s+/ /g;
|
---|
124 | $$stringref =~ s/^\s+//;
|
---|
125 | $$stringref =~ s/\s+$//;
|
---|
126 | }
|
---|
127 |
|
---|
128 | sub format_string_name_en {
|
---|
129 | my ($stringref) = @_;
|
---|
130 |
|
---|
131 | if ($has_custom_sort && defined (&customsorttools::format_string_name_en)) {
|
---|
132 | return &customsorttools::format_string_name_en($stringref);
|
---|
133 | }
|
---|
134 |
|
---|
135 | $$stringref =~ tr/A-Z/a-z/;
|
---|
136 | $$stringref =~ s/&\S+;//g;
|
---|
137 |
|
---|
138 | my $comma_format = ($$stringref =~ m/^.+,.+$/);
|
---|
139 | $$stringref =~ s/[[:punct:]]//g;
|
---|
140 | $$stringref =~ s/\s+/ /g;
|
---|
141 | $$stringref =~ s/^\s+//;
|
---|
142 | $$stringref =~ s/\s+$//;
|
---|
143 |
|
---|
144 |
|
---|
145 | if (!$comma_format) {
|
---|
146 | # No commas in name => name in 'firstname surname' format
|
---|
147 | # need to sort by surname
|
---|
148 | my @names = split / /, $$stringref;
|
---|
149 | my $surname = pop @names;
|
---|
150 | while (scalar @names && $surname =~ /^(jnr|snr)$/i) {
|
---|
151 | $surname = pop @names;
|
---|
152 | }
|
---|
153 | $$stringref = $surname . " " . $$stringref;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | sub format_string_fr {
|
---|
159 | my $stringref = shift;
|
---|
160 |
|
---|
161 | if ($has_custom_sort && defined (&customsorttools::format_string_fr)) {
|
---|
162 | return &customsorttools::format_string_fr($stringref);
|
---|
163 | }
|
---|
164 |
|
---|
165 | $$stringref = lc($$stringref);
|
---|
166 | $$stringref =~ s/&[^\;]+\;//g; # html entities
|
---|
167 | $$stringref =~ s/^\s*(les?|la|une?)\b//; # articles
|
---|
168 | $$stringref =~ s/[^[:alnum:]]//g;
|
---|
169 | $$stringref =~ s/\s+/ /g;
|
---|
170 | $$stringref =~ s/^\s+//;
|
---|
171 | $$stringref =~ s/\s+$//;
|
---|
172 | }
|
---|
173 |
|
---|
174 | sub format_string_es {
|
---|
175 | my $stringref = shift;
|
---|
176 |
|
---|
177 | if ($has_custom_sort && defined (&customsorttools::format_string_es)) {
|
---|
178 | return &customsorttools::format_string_es($stringref);
|
---|
179 | }
|
---|
180 |
|
---|
181 | $$stringref = lc($$stringref);
|
---|
182 | $$stringref =~ s/&[^\;]+\;//g; # html entities
|
---|
183 | $$stringref =~ s/^\s*(la|el)\b//; # articles
|
---|
184 | $$stringref =~ s/[^[:alnum:]]//g;
|
---|
185 | $$stringref =~ s/\s+/ /g;
|
---|
186 | $$stringref =~ s/^\s+//;
|
---|
187 | $$stringref =~ s/\s+$//;
|
---|
188 | }
|
---|
189 |
|
---|
190 | ### end of language-specific functions
|
---|
191 |
|
---|
192 | # takes arguments of day, month, year and converts to
|
---|
193 | # date of form yyyymmdd. month may be full (e.g. "January"),
|
---|
194 | # abbreviated (e.g. "Jan"), or a number (1-12). Years like "86"
|
---|
195 | # will be assumed to be "1986".
|
---|
196 | sub format_date {
|
---|
197 | my ($day, $month, $year) = @_;
|
---|
198 |
|
---|
199 | if ($has_custom_sort && defined (&customsorttools::format_date)) {
|
---|
200 | return &customsorttools::format_date($day, $month, $year);
|
---|
201 | }
|
---|
202 |
|
---|
203 | my %months = ('january' => '01', 'jan' => '01', 'february' => '02', 'feb' => '02',
|
---|
204 | 'march' => '03', 'mar' => '03', 'april' => '04', 'apr' => '04',
|
---|
205 | 'may' => '05', 'june' => '06', 'jun' => '06', 'july' => '07',
|
---|
206 | 'jul' => '07', 'august' => '08', 'aug' => '08', 'september' => '09',
|
---|
207 | 'sep' => '09', 'october' => '10', 'oct' => '10', 'november' => '11',
|
---|
208 | 'nov' => '11', 'december' => '12', 'dec' => '12');
|
---|
209 |
|
---|
210 | $month =~ tr/A-Z/a-z/;
|
---|
211 |
|
---|
212 | if ($day < 1) {
|
---|
213 | print STDERR "sorttools::format_date WARNING day $day out of range\n";
|
---|
214 | $day = "01";
|
---|
215 | } elsif ($day > 31) {
|
---|
216 | print STDERR "sorttools::format_date WARNING day $day out of range\n";
|
---|
217 | $day = "31";
|
---|
218 | }
|
---|
219 |
|
---|
220 | $day = "0$day" if (length($day) == 1);
|
---|
221 |
|
---|
222 | if ($month =~ /^\d\d?$/) {
|
---|
223 | if ($month < 1) {
|
---|
224 | print STDERR "sorttools::format_date WARNING month $month out of range\n";
|
---|
225 | $month = "01";
|
---|
226 | } elsif ($month > 12) {
|
---|
227 | print STDERR "sorttools::format_date WARNING month $month out of range\n";
|
---|
228 | $month = "12";
|
---|
229 | }
|
---|
230 | if ($month =~ /^\d$/) {
|
---|
231 | $month = "0" . $month;
|
---|
232 | }
|
---|
233 | } elsif (!defined $months{$month}) {
|
---|
234 | print STDERR "sorttools::format_date WARNING month $month out of range\n";
|
---|
235 | $month = "01";
|
---|
236 | } else {
|
---|
237 | $month = $months{$month};
|
---|
238 | }
|
---|
239 |
|
---|
240 | if ($year !~ /^\d\d\d\d$/) {
|
---|
241 | if ($year !~ /^\d\d$/) {
|
---|
242 | my $newyear = 1900 + $year;
|
---|
243 | print STDERR "sorttools::format_date WARNING year $year assumed to be $newyear\n";
|
---|
244 | $year=$newyear;
|
---|
245 | } else {
|
---|
246 | print STDERR "sorttools::format_date WARNING year $year out of range - reset to 1900\n";
|
---|
247 | $year = "1900";
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | return "$year$month$day";
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | 1;
|
---|