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

Last change on this file since 9322 was 9322, checked in by davidb, 19 years ago

Typo detected in code: mistakenly sets childtype of Vlist not VList. This
resulted in the failure of the right VList statement being applied to
classifier lists built using AZList. Line corrected to VList.

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