source: trunk/gsdl/perllib/classify/AZList.pm@ 6806

Last change on this file since 6806 was 6806, checked in by jrm21, 20 years ago

Bah. We were accidentally dropping the last classification because of a typo
(we were checking if @currentOIDS, when it should be currentOIDs).
Now we have "use strict;" to tell us when we use an undeclared variable!!

  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1###########################################################################
2#
3# AZList.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# classifier plugin for sorting alphabetically
27
28package AZList;
29
30use BasClas;
31use sorttools;
32use iso639;
33
34sub BEGIN {
35 @ISA = ('BasClas');
36}
37
38use strict;
39
40my $arguments =
41 [ { 'name' => "metadata",
42 'desc' => "{List.metadata}",
43 'type' => "metadata",
44 'reqd' => "yes" } ,
45 { 'name' => "buttonname",
46 'desc' => "{AZList.buttonname}",
47 'type' => "string",
48 'deft' => "{BasClas.metadata.deft}",
49 'reqd' => "no" },
50 { 'name' => "removeprefix",
51 'desc' => "{AZList.removeprefix}",
52 'type' => "regexp",
53 'deft' => "",
54 'reqd' => "no" } ,
55 { 'name' => "removesuffix",
56 'desc' => "{AZList.removesuffix}",
57 'type' => "regexp",
58 'deft' => "",
59 'reqd' => "no" }
60 ];
61
62my $options = { 'name' => "AZList",
63 'desc' => "{AZList.desc}",
64 'abstract' => "no",
65 'inherits' => "yes",
66 'args' => $arguments };
67
68# sub print_usage {
69# print STDERR "
70# usage: classify AZList [options]
71# options:
72
73# -metadata X (required) Metadata field used for classification.
74# List will be sorted by this element.
75
76# -buttonname X (optional) Button name for this classification.
77# defaults to metadata name.
78
79# -removeprefix regex (optional) A prefix to ignore in the Metadata values
80# for the field when sorting.
81# ";
82# }
83
84sub new {
85 my $class = shift (@_);
86 my $self = new BasClas($class, @_);
87
88 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
89 my $option_list = $self->{'option_list'};
90 push( @{$option_list}, $options );
91
92 my ($metaname, $title, $removeprefix, $removesuffix);
93
94 if (!parsargv::parse(\@_,
95 q^metadata/.*/^, \$metaname,
96 q^buttonname/.*/^, \$title,
97 q^removeprefix/.*/^, \$removeprefix,
98 q^removesuffix/.*/^, \$removesuffix,
99 "allow_extra_options")) {
100
101 print STDERR "\nIncorrect options passed to $class, check your collect.cfg file\n";
102 $self->print_txt_usage(""); # Use default resource bundle
103 die "\n";
104 }
105
106 if (!defined $metaname) {
107 $self->print_txt_usage(""); # Use default resource bundle
108 print STDERR "AZList used with no metadata name\n";
109 die "\n";
110 }
111
112 $title = $metaname unless ($title);
113
114 $self->{'list'} = {};
115 $self->{'metaname'} = $metaname;
116 $self->{'title'} = $title;
117 if (defined($removeprefix) && $removeprefix) {
118 $removeprefix =~ s/^\^//; # don't need a leading ^
119 $self->{'removeprefix'} = $removeprefix;
120 }
121 if (defined($removesuffix) && $removesuffix) {
122 $removesuffix =~ s/\$$//; # don't need a trailing $
123 $self->{'removesuffix'} = $removesuffix;
124 }
125
126 return bless $self, $class;
127}
128
129sub init {
130 my $self = shift (@_);
131
132 $self->{'list'} = {};
133}
134
135sub classify {
136 my $self = shift (@_);
137 my ($doc_obj) = @_;
138
139 my $doc_OID = $doc_obj->get_OID();
140 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'metaname'}, $self->{'ignore_namespace'});
141
142 # if this document doesn't contain the metadata element we're
143 # sorting by we won't include it in this classification
144 if (defined $metavalue && $metavalue ne "") {
145 if (defined($self->{'removeprefix'}) &&
146 length($self->{'removeprefix'})) {
147 $metavalue =~ s/^$self->{'removeprefix'}//;
148 }
149 if (defined($self->{'removesuffix'}) &&
150 length($self->{'removesuffix'})) {
151 $metavalue =~ s/$self->{'removesuffix'}$//;
152 }
153
154 if ($self->{'metaname'} eq 'Language') {
155 $metavalue = $iso639::fromiso639{$metavalue};
156 } elsif ($self->{'metaname'} eq 'Creator') {
157 &sorttools::format_string_name_english (\$metavalue);
158 } else {
159 &sorttools::format_string_english (\$metavalue);
160 }
161 if (defined $self->{'list'}->{$doc_OID}) {
162 my $outhandle = $self->{'outhandle'};
163 print $outhandle "WARNING: AZList::classify called multiple times for $doc_OID\n";
164 }
165 if ($metavalue) {
166 $self->{'list'}->{$doc_OID} = $metavalue;
167 } else {
168 my $outhandle = $self->{'outhandle'};
169 print $outhandle "WARNING: AZList: $doc_OID metadata is empty - not classifying\n";
170 }
171 }
172}
173
174sub alpha_numeric_cmp
175{
176 my ($self,$a,$b) = @_;
177
178 my $title_a = $self->{'list'}->{$a};
179 my $title_b = $self->{'list'}->{$b};
180
181 if ($title_a =~ m/^(\d+(\.\d+)?)/)
182 {
183 my $val_a = $1;
184 if ($title_b =~ m/^(\d+(\.\d+)?)/)
185 {
186 my $val_b = $1;
187 if ($val_a != $val_b)
188 {
189 return ($val_a <=> $val_b);
190 }
191 }
192 }
193
194 return ($title_a cmp $title_b);
195}
196
197sub get_classify_info {
198 my $self = shift (@_);
199
200 my @classlist
201 = sort { $self->alpha_numeric_cmp($a,$b) } keys %{$self->{'list'}};
202
203 return $self->splitlist (\@classlist);
204}
205
206sub get_entry {
207 my $self = shift (@_);
208 my ($title, $childtype, $thistype) = @_;
209
210 # organise into classification structure
211 my %classifyinfo = ('childtype'=>$childtype,
212 'Title'=>$title,
213 'contains'=>[]);
214 $classifyinfo{'thistype'} = $thistype
215 if defined $thistype && $thistype =~ /\w/;
216
217 return \%classifyinfo;
218}
219
220# splitlist takes an ordered list of classifications (@$classlistref) and splits it
221# up into alphabetical sub-sections.
222sub splitlist {
223 my $self = shift (@_);
224 my ($classlistref) = @_;
225 my $classhash = {};
226
227 # top level
228 my $childtype = "HList";
229 if (scalar (@$classlistref) <= 39) {$childtype = "VList";}
230 my $classifyinfo = $self->get_entry ($self->{'title'}, $childtype, "Invisible");
231
232 # don't need to do any splitting if there are less than 39 (max + min -1) classifications
233 if ((scalar @$classlistref) <= 39) {
234 foreach my $subOID (@$classlistref) {
235 push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
236 }
237 return $classifyinfo;
238 }
239
240 # first split up the list into separate A-Z and 0-9 classifications
241 foreach my $classification (@$classlistref) {
242 my $title = $self->{'list'}->{$classification};
243
244 $title =~ s/^(&.{1,6};|<[^>]>|[^a-zA-Z0-9])//g; # remove any unwanted stuff
245 # only need first char for classification
246 $title =~ m/^(.)/; $title=$1;
247 $title =~ tr/[a-z]/[A-Z]/;
248 if ($title =~ /^[0-9]$/) {$title = '0-9';}
249 elsif ($title !~ /^[A-Z]$/) {
250 my $outhandle = $self->{'outhandle'};
251 print $outhandle "AZList: WARNING $classification has badly formatted title ($title)\n";
252 }
253 $classhash->{$title} = [] unless defined $classhash->{$title};
254 push (@{$classhash->{$title}}, $classification);
255 }
256 $classhash = $self->compactlist ($classhash);
257
258 my @tmparr = ();
259 foreach my $subsection (sort keys (%$classhash)) {
260 push (@tmparr, $subsection);
261 }
262
263 # if there's a 0-9 section it will have been sorted to the beginning
264 # but we want it at the end
265 if ($tmparr[0] eq '0-9') {
266 shift @tmparr;
267 push (@tmparr, '0-9');
268 }
269
270 foreach my $subclass (@tmparr) {
271 my $tempclassify = $self->get_entry($subclass, "VList");
272 foreach my $subsubOID (@{$classhash->{$subclass}}) {
273 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
274 }
275 push (@{$classifyinfo->{'contains'}}, $tempclassify);
276 }
277
278 return $classifyinfo;
279}
280
281sub compactlist {
282 my $self = shift (@_);
283 my ($classhashref) = @_;
284 my $compactedhash = {};
285 my @currentOIDs = ();
286 my $currentfirstletter = ""; # start of working bin
287 my $currentlastletter = ""; # end of working bin
288 my $lastkey = ""; # the name of the last completed key
289
290 # minimum and maximum documents to be displayed per page.
291 # the actual maximum will be max + (min-1).
292 # the smallest sub-section is a single letter at present
293 # so in this case there may be many times max documents
294 # displayed on a page.
295 my $min = 10;
296 my $max = 30;
297
298 foreach my $subsection (sort keys %$classhashref) {
299 if ($subsection eq '0-9') {
300 # leave this bin as-is... copy it straight across
301 @{$compactedhash->{$subsection}} = @{$classhashref->{$subsection}};
302 next;
303 }
304 $currentfirstletter = $subsection if $currentfirstletter eq "";
305 if ((scalar (@currentOIDs) < $min) ||
306 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
307 # add this letter to the bin and continue
308 push (@currentOIDs, @{$classhashref->{$subsection}});
309 $currentlastletter = $subsection;
310 } else {
311 # too many or too few for a separate bin
312 if ($currentfirstletter eq $currentlastletter) {
313 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
314 $lastkey = $currentfirstletter;
315 } else {
316 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
317 $lastkey = "$currentfirstletter-$currentlastletter";
318 }
319 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
320 # this key is now complete. Start a new one
321 $compactedhash->{$subsection} = $classhashref->{$subsection};
322 @currentOIDs = ();
323 $currentfirstletter = "";
324 $lastkey = $subsection;
325 } else {
326 @currentOIDs = @{$classhashref->{$subsection}};
327 $currentfirstletter = $subsection;
328 $currentlastletter = $subsection;
329 }
330 }
331 }
332
333 # add final OIDs to last sub-classification if there aren't many otherwise
334 # add final sub-classification
335 # BUG FIX: don't add anything if there are no currentOIDs (thanks to Don Gourley)
336 if (! scalar(@currentOIDs)) {return $compactedhash;}
337
338 if (scalar (@currentOIDs) < $min) {
339 my ($newkey) = $lastkey =~ /^(.)/;
340 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
341 delete $compactedhash->{$lastkey};
342 @{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;
343 } else {
344 if ($currentfirstletter eq $currentlastletter) {
345 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
346 }
347 else {
348 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} =
349 @currentOIDs;
350 }
351 }
352
353 return $compactedhash;
354}
355
3561;
Note: See TracBrowser for help on using the repository browser.