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

Last change on this file since 8728 was 7580, checked in by kjdon, 20 years ago

added a new function: generate_title_from_metadata to BasClas - takes a list of metadata and returns something that is more likely to have a corresponding button

  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 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
34sub BEGIN {
35 @ISA = ('BasClas');
36}
37
38
39my $arguments =
40 [ { 'name' => "metadata",
41 'desc' => "{AZList.metadata}",
42 'type' => "metadata",
43 'reqd' => "yes" } ,
44 { 'name' => "buttonname",
45 'desc' => "{BasClas.buttonname}",
46 'type' => "string",
47 'deft' => "",
48 'reqd' => "no" },
49 { 'name' => "removeprefix",
50 'desc' => "{AZList.removeprefix}",
51 'type' => "regexp",
52 'deft' => "",
53 'reqd' => "no" } ,
54 { 'name' => "removesuffix",
55 'desc' => "{AZList.removesuffix}",
56 'type' => "regexp",
57 'deft' => "",
58 'reqd' => "no" }
59 ];
60
61my $options = { 'name' => "AZList",
62 'desc' => "{AZList.desc}",
63 'abstract' => "no",
64 'inherits' => "yes",
65 'args' => $arguments };
66
67
68sub new {
69 my $class = shift (@_);
70 my $self = new BasClas($class, @_);
71
72 my $option_list = $self->{'option_list'};
73 push( @{$option_list}, $options );
74
75 if ($self->{'info_only'}) {
76 # created from classinfo.pl - don't need to parse the arguments
77 return bless $self, $class;
78 }
79 my ($metadata, $title, $removeprefix, $removesuffix);
80
81 if (!parsargv::parse(\@_,
82 q^metadata/.*/^, \$metadata,
83 q^buttonname/.*/^, \$title,
84 q^removeprefix/.*/^, \$removeprefix,
85 q^removesuffix/.*/^, \$removesuffix,
86 "allow_extra_options")) {
87
88 print STDERR "\nIncorrect options passed to $class, check your collect.cfg file\n";
89 $self->print_txt_usage(""); # Use default resource bundle
90 die "\n";
91 }
92
93 if (!$metadata) {
94 print STDERR "AZList Error: required option -metadata not supplied \n";
95 $self->print_txt_usage(""); # Use default resource bundle
96
97 die "AZList Error: required option -metadata not supplied\n";
98 }
99
100 my @meta_list = split(/,/, $metadata);
101 #my $meta1 = $meta_list[0];
102
103 #$title = $meta1 unless ($title);
104 $title = $self->generate_title_from_metadata($metadata) unless ($title);
105 $self->{'list'} = {};
106 $self->{'meta_list'} = \@meta_list;
107 $self->{'title'} = $title;
108
109 if (defined($removeprefix) && $removeprefix) {
110 $removeprefix =~ s/^\^//; # don't need a leading ^
111 $self->{'removeprefix'} = $removeprefix;
112 }
113 if (defined($removesuffix) && $removesuffix) {
114 $removesuffix =~ s/\$$//; # don't need a trailing $
115 $self->{'removesuffix'} = $removesuffix;
116 }
117
118 return bless $self, $class;
119}
120
121sub init {
122 my $self = shift (@_);
123
124 $self->{'list'} = {};
125}
126
127sub classify {
128 my $self = shift (@_);
129 my ($doc_obj) = @_;
130
131 my $doc_OID = $doc_obj->get_OID();
132 my $outhandle = $self->{'outhandle'};
133
134 my $metavalue;
135 my $metaname;
136 # should we extend this to use all available metadata not just the first?
137 if (!defined $self->{'meta_list'}) {
138 # just in case
139 return;
140 }
141
142 # find the first available metadata
143 foreach $m (@{$self->{'meta_list'}}) {
144 $metavalue = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $m);
145 $metaname = $m;
146 last if defined $metavalue;
147 }
148
149 #if we haven't found a metavalue here, then the doc shouldn't be included
150 if (!defined $metavalue || $metavalue eq "") {
151 print $outhandle "WARNING: AZList: $doc_OID metadata is empty - not classifying\n";
152 return;
153 }
154
155 if (defined($self->{'removeprefix'}) &&
156 length($self->{'removeprefix'})) {
157 $metavalue =~ s/^$self->{'removeprefix'}//;
158 }
159 if (defined($self->{'removesuffix'}) &&
160 length($self->{'removesuffix'})) {
161 $metavalue =~ s/$self->{'removesuffix'}$//;
162 }
163
164 $metavalue = $self->format_metadata_for_sorting($metaname, $metavalue, $doc_obj);
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->{'title'}, $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
268 # if there's a 0-9 section it will have been sorted to the beginning
269 # but we want it at the end
270 if ($tmparr[0] eq '0-9') {
271 shift @tmparr;
272 push (@tmparr, '0-9');
273 }
274
275 foreach my $subclass (@tmparr) {
276 my $tempclassify = $self->get_entry($subclass, "VList");
277 foreach my $subsubOID (@{$classhash->{$subclass}}) {
278 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
279 }
280 push (@{$classifyinfo->{'contains'}}, $tempclassify);
281 }
282
283 return $classifyinfo;
284}
285
286sub compactlist {
287 my $self = shift (@_);
288 my ($classhashref) = @_;
289 my $compactedhash = {};
290 my @currentOIDs = ();
291 my $currentfirstletter = ""; # start of working bin
292 my $currentlastletter = ""; # end of working bin
293 my $lastkey = ""; # the name of the last completed key
294
295 # minimum and maximum documents to be displayed per page.
296 # the actual maximum will be max + (min-1).
297 # the smallest sub-section is a single letter at present
298 # so in this case there may be many times max documents
299 # displayed on a page.
300 my $min = 10;
301 my $max = 30;
302
303 foreach my $subsection (sort keys %$classhashref) {
304 if ($subsection eq '0-9') {
305 # leave this bin as-is... copy it straight across
306 @{$compactedhash->{$subsection}} = @{$classhashref->{$subsection}};
307 next;
308 }
309 $currentfirstletter = $subsection if $currentfirstletter eq "";
310 if ((scalar (@currentOIDs) < $min) ||
311 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
312 # add this letter to the bin and continue
313 push (@currentOIDs, @{$classhashref->{$subsection}});
314 $currentlastletter = $subsection;
315 } else {
316 # too many or too few for a separate bin
317 if ($currentfirstletter eq $currentlastletter) {
318 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
319 $lastkey = $currentfirstletter;
320 } else {
321 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
322 $lastkey = "$currentfirstletter-$currentlastletter";
323 }
324 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
325 # this key is now complete. Start a new one
326 $compactedhash->{$subsection} = $classhashref->{$subsection};
327 @currentOIDs = ();
328 $currentfirstletter = "";
329 $lastkey = $subsection;
330 } else {
331 @currentOIDs = @{$classhashref->{$subsection}};
332 $currentfirstletter = $subsection;
333 $currentlastletter = $subsection;
334 }
335 }
336 }
337
338 # add final OIDs to last sub-classification if there aren't many otherwise
339 # add final sub-classification
340 # BUG FIX: don't add anything if there are no currentOIDs (thanks to Don Gourley)
341 if (! scalar(@currentOIDs)) {return $compactedhash;}
342
343 if (scalar (@currentOIDs) < $min) {
344 my ($newkey) = $lastkey =~ /^(.)/;
345 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
346 delete $compactedhash->{$lastkey};
347 @{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;
348 } else {
349 if ($currentfirstletter eq $currentlastletter) {
350 @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
351 }
352 else {
353 @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} =
354 @currentOIDs;
355 }
356 }
357
358 return $compactedhash;
359}
360
3611;
Note: See TracBrowser for help on using the repository browser.