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

Last change on this file since 18455 was 18455, checked in by davidb, 15 years ago

Addition of 'edit_mode' parameter to classify(). This can be either 'add' 'delete' or 'reindex' (should think about renaming the last one to something more appropriate, e.g. update).

  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 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,$edit_mode) = @_;
122
123 my $doc_OID = $doc_obj->get_OID();
124 my $outhandle = $self->{'outhandle'};
125
126 if ($edit_mode eq "delete") {
127 $self->oid_hash_delete($doc_OID,'list');
128 return;
129 }
130
131 my $metavalue;
132 my $metaname;
133 # should we extend this to use all available metadata not just the first?
134 if (!defined $self->{'meta_list'}) {
135 # just in case
136 return;
137 }
138
139 # find the first available metadata
140 foreach my $m (@{$self->{'meta_list'}}) {
141 $metavalue = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $m);
142 $metaname = $m;
143 last if defined $metavalue;
144 }
145
146 #if we haven't found a metavalue here, then the doc shouldn't be included
147 if (!defined $metavalue || $metavalue eq "") {
148 print $outhandle "WARNING: AZList: $doc_OID metadata is empty - not classifying\n";
149 return;
150 }
151
152 if (defined($self->{'removeprefix'}) &&
153 length($self->{'removeprefix'})) {
154 $metavalue =~ s/^$self->{'removeprefix'}//;
155 }
156 if (defined($self->{'removesuffix'}) &&
157 length($self->{'removesuffix'})) {
158 $metavalue =~ s/$self->{'removesuffix'}$//;
159 }
160
161
162 $metavalue = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj) unless $self->{'no_metadata_formatting'};
163
164 if (defined $self->{'list'}->{$doc_OID}) {
165 print $outhandle "WARNING: AZList::classify called multiple times for $doc_OID\n";
166 }
167 if ($metavalue) {
168 $self->{'list'}->{$doc_OID} = $metavalue;
169 } else {
170 # the formatting has made it empty
171 my $outhandle = $self->{'outhandle'};
172 print $outhandle "WARNING: AZList: $doc_OID metadata has become empty - not classifying\n";
173 }
174
175}
176
177sub alpha_numeric_cmp
178{
179 my ($self,$a,$b) = @_;
180
181 my $title_a = $self->{'list'}->{$a};
182 my $title_b = $self->{'list'}->{$b};
183
184 if ($title_a =~ m/^(\d+(\.\d+)?)/)
185 {
186 my $val_a = $1;
187 if ($title_b =~ m/^(\d+(\.\d+)?)/)
188 {
189 my $val_b = $1;
190 if ($val_a != $val_b)
191 {
192 return ($val_a <=> $val_b);
193 }
194 }
195 }
196
197 return ($title_a cmp $title_b);
198}
199
200sub get_classify_info {
201 my $self = shift (@_);
202
203 my @classlist
204 = sort { $self->alpha_numeric_cmp($a,$b) } keys %{$self->{'list'}};
205
206 return $self->splitlist (\@classlist);
207}
208
209sub get_entry {
210 my $self = shift (@_);
211 my ($title, $childtype, $thistype) = @_;
212
213 # organise into classification structure
214 my %classifyinfo = ('childtype'=>$childtype,
215 'Title'=>$title,
216 'contains'=>[]);
217 $classifyinfo{'thistype'} = $thistype
218 if defined $thistype && $thistype =~ /\w/;
219
220 return \%classifyinfo;
221}
222
223# splitlist takes an ordered list of classifications (@$classlistref) and splits it
224# up into alphabetical sub-sections.
225sub splitlist {
226 my $self = shift (@_);
227 my ($classlistref) = @_;
228 my $classhash = {};
229
230 # top level
231 my $childtype = "HList";
232 if (scalar (@$classlistref) <= 39) {$childtype = "VList";}
233 my $classifyinfo = $self->get_entry ($self->{'buttonname'}, $childtype, "Invisible");
234
235 # don't need to do any splitting if there are less than 39 (max + min -1) classifications
236 if ((scalar @$classlistref) <= 39) {
237 foreach my $subOID (@$classlistref) {
238 push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
239 }
240 return $classifyinfo;
241 }
242
243 # first split up the list into separate A-Z and 0-9 classifications
244 foreach my $classification (@$classlistref) {
245 my $title = $self->{'list'}->{$classification};
246
247 $title =~ s/^(&.{1,6};|<[^>]>|[^a-zA-Z0-9])//g; # remove any unwanted stuff
248 # only need first char for classification
249 $title =~ m/^(.)/; $title=$1;
250 $title =~ tr/[a-z]/[A-Z]/;
251 if ($title =~ /^[0-9]$/) {$title = '0-9';}
252 elsif ($title !~ /^[A-Z]$/) {
253 my $outhandle = $self->{'outhandle'};
254 print $outhandle "AZList: WARNING $classification has badly formatted title ($title)\n";
255 }
256 $classhash->{$title} = [] unless defined $classhash->{$title};
257 push (@{$classhash->{$title}}, $classification);
258 }
259 $classhash = $self->compactlist ($classhash);
260
261 my @tmparr = ();
262 foreach my $subsection (sort keys (%$classhash)) {
263 push (@tmparr, $subsection);
264 }
265 #if there is only one entry here, we suppress the buckets
266 if ((scalar @tmparr) == 1) {
267 $classifyinfo->{'childtype'} = "VList";
268 foreach my $OID (@{$classhash->{$tmparr[0]}}) {
269 push (@{$classifyinfo->{'contains'}}, {'OID'=>$OID});
270 }
271 return $classifyinfo;
272 }
273
274 # if there's a 0-9 section it will have been sorted to the beginning
275 # but we want it at the end
276 if ($tmparr[0] eq '0-9') {
277 shift @tmparr;
278 push (@tmparr, '0-9');
279 }
280
281 foreach my $subclass (@tmparr) {
282 my $tempclassify = $self->get_entry($subclass, "VList");
283 foreach my $subsubOID (@{$classhash->{$subclass}}) {
284 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
285 }
286 push (@{$classifyinfo->{'contains'}}, $tempclassify);
287 }
288
289 return $classifyinfo;
290}
291
292sub compactlist {
293 my $self = shift (@_);
294 my ($classhashref) = @_;
295 my $compactedhash = {};
296 my @currentOIDs = ();
297 my $currentfirstletter = ""; # start of working bin
298 my $currentlastletter = ""; # end of working bin
299 my $lastkey = ""; # the name of the last completed key
300
301 # minimum and maximum documents to be displayed per page.
302 # the actual maximum will be max + (min-1).
303 # the smallest sub-section is a single letter at present
304 # so in this case there may be many times max documents
305 # displayed on a page.
306 my $min = 10;
307 my $max = 30;
308
309 foreach my $subsection (sort keys %$classhashref) {
310 if ($subsection eq '0-9') {
311 # leave this bin as-is... copy it straight across
312 @{$compactedhash->{$subsection}} = @{$classhashref->{$subsection}};
313 next;
314 }
315 $currentfirstletter = $subsection if $currentfirstletter eq "";
316 if ((scalar (@currentOIDs) < $min) ||
317 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
318 # add this letter to the bin and continue
319 push (@currentOIDs, @{$classhashref->{$subsection}});
320 $currentlastletter = $subsection;
321 } else {
322 # too many or too few for a separate bin
323 if ($currentfirstletter eq $currentlastletter) {
324 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
325 $lastkey = $currentfirstletter;
326 } else {
327 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
328 $lastkey = "$currentfirstletter-$currentlastletter";
329 }
330 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
331 # this key is now complete. Start a new one
332 $compactedhash->{$subsection} = $classhashref->{$subsection};
333 @currentOIDs = ();
334 $currentfirstletter = "";
335 $lastkey = $subsection;
336 } else {
337 @currentOIDs = @{$classhashref->{$subsection}};
338 $currentfirstletter = $subsection;
339 $currentlastletter = $subsection;
340 }
341 }
342 }
343
344 # add final OIDs to last sub-classification if there aren't many otherwise
345 # add final sub-classification
346 # BUG FIX: don't add anything if there are no currentOIDs (thanks to Don Gourley)
347 if (! scalar(@currentOIDs)) {return $compactedhash;}
348
349 if (scalar (@currentOIDs) < $min) {
350 my ($newkey) = $lastkey =~ /^(.)/;
351 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
352 delete $compactedhash->{$lastkey};
353 @{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;
354 } else {
355 if ($currentfirstletter eq $currentlastletter) {
356 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
357 }
358 else {
359 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} =
360 @currentOIDs;
361 }
362 }
363
364 return $compactedhash;
365}
366
3671;
Note: See TracBrowser for help on using the repository browser.