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

Last change on this file since 17209 was 17209, checked in by kjdon, 16 years ago

BasClas renamed to BaseClassifier, tidied up constructors

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