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

Last change on this file since 11293 was 10993, checked in by kjdon, 19 years ago

fixed a spelling mistake

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