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

Last change on this file since 20828 was 20428, checked in by kjdon, 15 years ago

strip ex from metadata argument before using it

  • 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 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 $metadata = $self->{"metadata"};
92 $metadata = $self->strip_ex_from_metadata($metadata);
93 my @meta_list = split(/,/, $metadata);
94 $self->{'meta_list'} = \@meta_list;
95
96 $self->{'buttonname'} = $self->generate_title_from_metadata($metadata) unless ($self->{'buttonname'});
97
98 # Further setup
99 if (defined($self->{"removeprefix"}) && $self->{"removeprefix"}) {
100 $self->{"removeprefix"} =~ s/^\^//; # don't need a leading ^
101 }
102 if (defined($self->{"removesuffix"}) && $self->{"removesuffix"}) {
103 $self->{"removesuffix"} =~ s/\$$//; # don't need a trailing $
104 }
105
106 # Clean out the unused keys
107 delete $self->{"metadata"}; # Delete this key
108
109 if($self->{"removeprefix"} eq "") {delete $self->{"removeprefix"};}
110 if($self->{"removesuffix"} eq "") {delete $self->{"removesuffix"};}
111
112 return bless $self, $class;
113}
114
115sub init {
116 my $self = shift (@_);
117
118 $self->{'list'} = {};
119}
120
121sub classify {
122 my $self = shift (@_);
123 my ($doc_obj,$edit_mode) = @_;
124
125 my $doc_OID = $doc_obj->get_OID();
126 my $outhandle = $self->{'outhandle'};
127
128 if (($edit_mode eq "delete") || ($edit_mode eq "update")) {
129 $self->oid_hash_delete($doc_OID,'list');
130 return if ($edit_mode eq "delete");
131 }
132
133 my $metavalue;
134 my $metaname;
135 # should we extend this to use all available metadata not just the first?
136 if (!defined $self->{'meta_list'}) {
137 # just in case
138 return;
139 }
140
141 # find the first available metadata
142 foreach my $m (@{$self->{'meta_list'}}) {
143 $metavalue = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $m);
144 $metaname = $m;
145 last if defined $metavalue;
146 }
147
148 #if we haven't found a metavalue here, then the doc shouldn't be included
149 if (!defined $metavalue || $metavalue eq "") {
150 print $outhandle "WARNING: AZList: $doc_OID metadata is empty - not classifying\n";
151 return;
152 }
153
154 if (defined($self->{'removeprefix'}) &&
155 length($self->{'removeprefix'})) {
156 $metavalue =~ s/^$self->{'removeprefix'}//;
157 }
158 if (defined($self->{'removesuffix'}) &&
159 length($self->{'removesuffix'})) {
160 $metavalue =~ s/$self->{'removesuffix'}$//;
161 }
162
163
164 $metavalue = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj) unless $self->{'no_metadata_formatting'};
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->{'buttonname'}, $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.