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

Last change on this file since 14119 was 12891, checked in by mdewsnip, 18 years ago

Tidied up that horrible long line in the new() function of every classifier.

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