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

Last change on this file since 8890 was 8852, checked in by kjdon, 19 years ago

shifted format_metadata_for_sorting from BasCLas to sorttools, so other things can use it

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 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
35 if ($metaname eq "Language") {
36 $metavalue = $iso639::fromiso639{$metavalue};
37 return $metavalue;
38 }
39
40 my $lang;
41 if (defined $doc_obj) {
42 $lang = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Language');
43 }
44 $lang = 'en' unless defined $lang;
45 if ($lang eq 'en') {
46 if ($metaname =~ /^(\w+\.)?Creator(:.*)?/) {
47 &format_string_name_english (\$metavalue);
48 } else {
49 &format_string_english (\$metavalue);
50 }
51 }
52 return $metavalue;
53}
54
55# format an english string for sorting
56# i.e. convert to lowercase, remove the, a or an
57# from beginning of string etc.
58sub format_string_english {
59 my ($stringref) = @_;
60
61 $$stringref =~ tr/A-Z/a-z/;
62 $$stringref =~ s/&[^\;]+\;//g;
63 $$stringref =~ s/[^a-z0-9 ]//g;
64 $$stringref =~ s/^\s*(the|a|an)\b//;
65 $$stringref =~ s/\s+/ /g;
66 $$stringref =~ s/^\s+//;
67 $$stringref =~ s/\s+$//;
68}
69
70# format an english name for sorting
71# i.e. convert to lowercase, put surname before
72# first names etc.
73sub format_string_name_english {
74 my ($stringref) = @_;
75
76 $$stringref =~ tr/A-Z/a-z/;
77 $$stringref =~ s/&\S+;//g;
78
79 my $comma_format = ($$stringref =~ m/^.+,.+$/);
80
81 $$stringref =~ s/[^a-z0-9 ]//g;
82 $$stringref =~ s/\s+/ /g;
83 $$stringref =~ s/^\s+//;
84 $$stringref =~ s/\s+$//;
85
86
87 if (!$comma_format) {
88 # No commas in name => name in 'firstname surname' format
89 # need to sort by surname
90 my @names = split / /, $$stringref;
91 my $surname = pop @names;
92 while (scalar @names && $surname =~ /^(jnr|snr)$/i) {
93 $surname = pop @names;
94 }
95 $$stringref = $surname . " " . $$stringref;
96 }
97}
98
99# takes arguments of day, month, year and converts to
100# date of form yyyymmdd. month may be full (e.g. "January"),
101# abbreviated (e.g. "Jan"), or a number (1-12). Years like "86"
102# will be assumed to be "1986".
103sub format_date {
104 my ($day, $month, $year) = @_;
105
106 my %months = ('january' => '01', 'jan' => '01', 'february' => '02', 'feb' => '02',
107 'march' => '03', 'mar' => '03', 'april' => '04', 'apr' => '04',
108 'may' => '05', 'june' => '06', 'jun' => '06', 'july' => '07',
109 'jul' => '07', 'august' => '08', 'aug' => '08', 'september' => '09',
110 'sep' => '09', 'october' => '10', 'oct' => '10', 'november' => '11',
111 'nov' => '11', 'december' => '12', 'dec' => '12');
112
113 $month =~ tr/A-Z/a-z/;
114
115 if ($day < 1) {
116 print STDERR "sorttools::format_date WARNING day $day out of range\n";
117 $day = "01";
118 } elsif ($day > 31) {
119 print STDERR "sorttools::format_date WARNING day $day out of range\n";
120 $day = "31";
121 }
122
123 $day = "0$day" if (length($day) == 1);
124
125 if ($month =~ /^\d\d?$/) {
126 if ($month < 1) {
127 print STDERR "sorttools::format_date WARNING month $month out of range\n";
128 $month = "01";
129 } elsif ($month > 12) {
130 print STDERR "sorttools::format_date WARNING month $month out of range\n";
131 $month = "12";
132 }
133 if ($month =~ /^\d$/) {
134 $month = "0" . $month;
135 }
136 } elsif (!defined $months{$month}) {
137 print STDERR "sorttools::format_date WARNING month $month out of range\n";
138 $month = "01";
139 } else {
140 $month = $months{$month};
141 }
142
143 if ($year !~ /^\d\d\d\d$/) {
144 if ($year !~ /^\d\d$/) {
145 my $newyear = 1900 + $year;
146 print STDERR "sorttools::format_date WARNING year $year assumed to be $newyear\n";
147 $year=$newyear;
148 } else {
149 print STDERR "sorttools::format_date WARNING year $year out of range - reset to 1900\n";
150 $year = "1900";
151 }
152 }
153
154 return "$year$month$day";
155}
156
157
1581;
Note: See TracBrowser for help on using the repository browser.