source: trunk/gsdl/perllib/sorttools.pm@ 10304

Last change on this file since 10304 was 10257, checked in by kjdon, 19 years ago

added a check for undefined/empty values passed into format_metadata_for_sorting

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
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
29package sorttools;
30
31# moved here from BasClas so import can share it
32sub format_metadata_for_sorting {
33 my ($metaname, $metavalue, $doc_obj) = @_;
34 if (!defined $metaname || $metaname !~ /\S/ || ! defined $metavalue || $metavalue !~ /\S/) {
35 return "";
36 }
37
38 if ($metaname eq "Language") {
39 $metavalue = $iso639::fromiso639{$metavalue};
40 return $metavalue;
41 }
42
43 my $lang;
44 if (defined $doc_obj) {
45 $lang = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Language');
46 }
47 $lang = 'en' unless defined $lang;
48
49 # is this metadata likely to be a name?
50 my $function_name="format_string_name_$lang";
51 if ($metaname =~ /^(?:\w+\.)?(?:Creator|Author|Editor)(?::.*)?$/
52 && exists &$function_name) {
53 no strict 'refs';
54 &$function_name(\$metavalue);
55 } else {
56 $function_name="format_string_$lang";
57 if (exists &$function_name) {
58 no strict 'refs';
59 &$function_name(\$metavalue);
60 }
61 }
62
63 return $metavalue;
64}
65
66### language-specific sorting functions (called by format_metadata_for_sorting)
67
68## format_string_$lang() converts to lowercase (where appropriate), and
69# removes punctuation, articles from the start of string, etc
70## format_string_name_$lang() converts to lowercase, puts the surname first,
71# removes punctuation, etc
72
73sub format_string_en {
74 my $stringref = shift;
75 $$stringref = lc($$stringref);
76 $$stringref =~ s/&[^\;]+\;//g; # html entities
77 $$stringref =~ s/^\s*(the|a|an)\b//; # articles
78 $$stringref =~ s/[^[:alnum:]]//g;
79 $$stringref =~ s/\s+/ /g;
80 $$stringref =~ s/^\s+//;
81 $$stringref =~ s/\s+$//;
82}
83
84sub format_string_name_en {
85 my ($stringref) = @_;
86
87 $$stringref =~ tr/A-Z/a-z/;
88 $$stringref =~ s/&\S+;//g;
89
90 my $comma_format = ($$stringref =~ m/^.+,.+$/);
91 $$stringref =~ s/[[:punct:]]//g;
92 $$stringref =~ s/\s+/ /g;
93 $$stringref =~ s/^\s+//;
94 $$stringref =~ s/\s+$//;
95
96
97 if (!$comma_format) {
98 # No commas in name => name in 'firstname surname' format
99 # need to sort by surname
100 my @names = split / /, $$stringref;
101 my $surname = pop @names;
102 while (scalar @names && $surname =~ /^(jnr|snr)$/i) {
103 $surname = pop @names;
104 }
105 $$stringref = $surname . " " . $$stringref;
106 }
107}
108
109
110sub format_string_fr {
111 my $stringref = shift;
112
113 $$stringref = lc($$stringref);
114 $$stringref =~ s/&[^\;]+\;//g; # html entities
115 $$stringref =~ s/[^[:alpha:]]//g;
116 $$stringref =~ s/^\s*(les?|la|une?)\b//; # articles
117 $$stringref =~ s/\s+/ /g;
118 $$stringref =~ s/^\s+//;
119 $$stringref =~ s/\s+$//;
120}
121
122sub format_string_es {
123 my $stringref = shift;
124
125 $$stringref = lc($$stringref);
126 $$stringref =~ s/&[^\;]+\;//g; # html entities
127 $$stringref =~ s/[^[:alpha:]]//g;
128 $$stringref =~ s/^\s*(la|el)\b//; # articles
129 $$stringref =~ s/\s+/ /g;
130 $$stringref =~ s/^\s+//;
131 $$stringref =~ s/\s+$//;
132}
133
134### end of language-specific functions
135
136# takes arguments of day, month, year and converts to
137# date of form yyyymmdd. month may be full (e.g. "January"),
138# abbreviated (e.g. "Jan"), or a number (1-12). Years like "86"
139# will be assumed to be "1986".
140sub format_date {
141 my ($day, $month, $year) = @_;
142
143 my %months = ('january' => '01', 'jan' => '01', 'february' => '02', 'feb' => '02',
144 'march' => '03', 'mar' => '03', 'april' => '04', 'apr' => '04',
145 'may' => '05', 'june' => '06', 'jun' => '06', 'july' => '07',
146 'jul' => '07', 'august' => '08', 'aug' => '08', 'september' => '09',
147 'sep' => '09', 'october' => '10', 'oct' => '10', 'november' => '11',
148 'nov' => '11', 'december' => '12', 'dec' => '12');
149
150 $month =~ tr/A-Z/a-z/;
151
152 if ($day < 1) {
153 print STDERR "sorttools::format_date WARNING day $day out of range\n";
154 $day = "01";
155 } elsif ($day > 31) {
156 print STDERR "sorttools::format_date WARNING day $day out of range\n";
157 $day = "31";
158 }
159
160 $day = "0$day" if (length($day) == 1);
161
162 if ($month =~ /^\d\d?$/) {
163 if ($month < 1) {
164 print STDERR "sorttools::format_date WARNING month $month out of range\n";
165 $month = "01";
166 } elsif ($month > 12) {
167 print STDERR "sorttools::format_date WARNING month $month out of range\n";
168 $month = "12";
169 }
170 if ($month =~ /^\d$/) {
171 $month = "0" . $month;
172 }
173 } elsif (!defined $months{$month}) {
174 print STDERR "sorttools::format_date WARNING month $month out of range\n";
175 $month = "01";
176 } else {
177 $month = $months{$month};
178 }
179
180 if ($year !~ /^\d\d\d\d$/) {
181 if ($year !~ /^\d\d$/) {
182 my $newyear = 1900 + $year;
183 print STDERR "sorttools::format_date WARNING year $year assumed to be $newyear\n";
184 $year=$newyear;
185 } else {
186 print STDERR "sorttools::format_date WARNING year $year out of range - reset to 1900\n";
187 $year = "1900";
188 }
189 }
190
191 return "$year$month$day";
192}
193
194
1951;
Note: See TracBrowser for help on using the repository browser.