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

Last change on this file since 15891 was 15891, checked in by mdewsnip, 16 years ago

Removing unnecessary include.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
RevLine 
[537]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
[256]26# classifier plugin for sorting alphabetically
27
28package AZList;
29
[1483]30use BasClas;
[385]31use sorttools;
32
[10253]33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
[1483]36sub BEGIN {
[10253]37 @AZList::ISA = ('BasClas');
[1483]38}
39
[6806]40
[4759]41my $arguments =
42 [ { 'name' => "metadata",
[6940]43 'desc' => "{AZList.metadata}",
[4759]44 'type' => "metadata",
45 'reqd' => "yes" } ,
46 { 'name' => "removeprefix",
[9064]47 'desc' => "{BasClas.removeprefix}",
[6408]48 'type' => "regexp",
[4759]49 'deft' => "",
[4840]50 'reqd' => "no" } ,
51 { 'name' => "removesuffix",
[9064]52 'desc' => "{BasClas.removesuffix}",
[6408]53 'type' => "regexp",
[4840]54 'deft' => "",
55 'reqd' => "no" }
56 ];
[3540]57
58my $options = { 'name' => "AZList",
[5645]59 'desc' => "{AZList.desc}",
[6408]60 'abstract' => "no",
61 'inherits' => "yes",
[4759]62 'args' => $arguments };
[3540]63
[1839]64
[256]65sub new {
[10218]66 my ($class) = shift (@_);
67 my ($classifierslist,$inputargs,$hashArgOptLists) = @_;
68 push(@$classifierslist, $class);
[256]69
[10218]70 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
71 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
72
[12891]73 my $self = new BasClas($classifierslist, $inputargs, $hashArgOptLists);
[10218]74
[10253]75 if ($self->{'info_only'}) {
76 # don't worry about any options etc
77 return bless $self, $class;
78 }
79
[10218]80 if (!$self->{"metadata"}) {
[6984]81 print STDERR "AZList Error: required option -metadata not supplied \n";
[4873]82 $self->print_txt_usage(""); # Use default resource bundle
[6961]83
[6984]84 die "AZList Error: required option -metadata not supplied\n";
[1483]85 }
[10218]86
87 # Manually set $self parameters.
88 $self->{'list'} = {};
[6961]89
[10993]90 # Transfer value from Auto Parsing to the variable name that used in previous GreenStone.
[10218]91 my @meta_list = split(/,/, $self->{"metadata"});
[6961]92 $self->{'meta_list'} = \@meta_list;
[10218]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 ^
[2954]99 }
[10218]100 if (defined($self->{"removesuffix"}) && $self->{"removesuffix"}) {
101 $self->{"removesuffix"} =~ s/\$$//; # don't need a trailing $
[4840]102 }
[1483]103
[10218]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
[1483]110 return bless $self, $class;
[256]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();
[6961]124 my $outhandle = $self->{'outhandle'};
[256]125
[6961]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 }
[2954]133
[6961]134 # find the first available metadata
[10253]135 foreach my $m (@{$self->{'meta_list'}}) {
[6961]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;
[256]145 }
[6961]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
[10630]156
157 $metavalue = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj) unless $self->{'no_metadata_formatting'};
[6961]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
[256]170}
171
[837]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
[316]195sub get_classify_info {
[256]196 my $self = shift (@_);
197
[837]198 my @classlist
199 = sort { $self->alpha_numeric_cmp($a,$b) } keys %{$self->{'list'}};
[256]200
[409]201 return $self->splitlist (\@classlist);
[256]202}
203
[316]204sub get_entry {
205 my $self = shift (@_);
[677]206 my ($title, $childtype, $thistype) = @_;
[316]207
208 # organise into classification structure
[677]209 my %classifyinfo = ('childtype'=>$childtype,
[316]210 'Title'=>$title,
211 'contains'=>[]);
[677]212 $classifyinfo{'thistype'} = $thistype
213 if defined $thistype && $thistype =~ /\w/;
[316]214
215 return \%classifyinfo;
216}
217
[256]218# splitlist takes an ordered list of classifications (@$classlistref) and splits it
[409]219# up into alphabetical sub-sections.
[256]220sub splitlist {
221 my $self = shift (@_);
222 my ($classlistref) = @_;
223 my $classhash = {};
224
[409]225 # top level
[677]226 my $childtype = "HList";
[741]227 if (scalar (@$classlistref) <= 39) {$childtype = "VList";}
[10218]228 my $classifyinfo = $self->get_entry ($self->{'buttonname'}, $childtype, "Invisible");
[409]229
[741]230 # don't need to do any splitting if there are less than 39 (max + min -1) classifications
231 if ((scalar @$classlistref) <= 39) {
[6806]232 foreach my $subOID (@$classlistref) {
[409]233 push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
234 }
235 return $classifyinfo;
236 }
237
[256]238 # first split up the list into separate A-Z and 0-9 classifications
[6806]239 foreach my $classification (@$classlistref) {
[256]240 my $title = $self->{'list'}->{$classification};
[837]241
[708]242 $title =~ s/^(&.{1,6};|<[^>]>|[^a-zA-Z0-9])//g; # remove any unwanted stuff
[3109]243 # only need first char for classification
244 $title =~ m/^(.)/; $title=$1;
[256]245 $title =~ tr/[a-z]/[A-Z]/;
246 if ($title =~ /^[0-9]$/) {$title = '0-9';}
247 elsif ($title !~ /^[A-Z]$/) {
[1483]248 my $outhandle = $self->{'outhandle'};
249 print $outhandle "AZList: WARNING $classification has badly formatted title ($title)\n";
[256]250 }
251 $classhash->{$title} = [] unless defined $classhash->{$title};
252 push (@{$classhash->{$title}}, $classification);
253 }
254 $classhash = $self->compactlist ($classhash);
255
256 my @tmparr = ();
[6806]257 foreach my $subsection (sort keys (%$classhash)) {
[256]258 push (@tmparr, $subsection);
259 }
[8866]260 #if there is only one entry here, we suppress the buckets
261 if ((scalar @tmparr) == 1) {
[9322]262 $classifyinfo->{'childtype'} = "VList";
[9204]263 foreach my $OID (@{$classhash->{$tmparr[0]}}) {
[8866]264 push (@{$classifyinfo->{'contains'}}, {'OID'=>$OID});
265 }
266 return $classifyinfo;
267 }
[256]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
[6806]276 foreach my $subclass (@tmparr) {
[677]277 my $tempclassify = $self->get_entry($subclass, "VList");
[6806]278 foreach my $subsubOID (@{$classhash->{$subclass}}) {
[409]279 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
280 }
281 push (@{$classifyinfo->{'contains'}}, $tempclassify);
[256]282 }
283
[409]284 return $classifyinfo;
[256]285}
286
287sub compactlist {
288 my $self = shift (@_);
289 my ($classhashref) = @_;
290 my $compactedhash = {};
291 my @currentOIDs = ();
[6806]292 my $currentfirstletter = ""; # start of working bin
293 my $currentlastletter = ""; # end of working bin
294 my $lastkey = ""; # the name of the last completed key
[256]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
[6806]304 foreach my $subsection (sort keys %$classhashref) {
[256]305 if ($subsection eq '0-9') {
[6806]306 # leave this bin as-is... copy it straight across
[256]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)) {
[6806]313 # add this letter to the bin and continue
[256]314 push (@currentOIDs, @{$classhashref->{$subsection}});
315 $currentlastletter = $subsection;
316 } else {
[6806]317 # too many or too few for a separate bin
[256]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) {
[6806]326 # this key is now complete. Start a new one
[256]327 $compactedhash->{$subsection} = $classhashref->{$subsection};
328 @currentOIDs = ();
329 $currentfirstletter = "";
[677]330 $lastkey = $subsection;
[256]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
[5646]341 # BUG FIX: don't add anything if there are no currentOIDs (thanks to Don Gourley)
[6806]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;
[5646]352 }
353 else {
[6806]354 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} =
355 @currentOIDs;
[5646]356 }
[256]357 }
358
359 return $compactedhash;
360}
361
3621;
Note: See TracBrowser for help on using the repository browser.