source: gsdl/trunk/perllib/classify/DateList.pm@ 20454

Last change on this file since 20454 was 20454, checked in by kjdon, 13 years ago

strip ex from metadata type args

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1###########################################################################
2#
3# DateList.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 by date
27
28# date is assumed to be in the form yyyymmdd
29
30# at present dates are split by year - this should change
31# jrm21 - added option "bymonth", which splits by year and month.
32
33# 23/09/03 Added some more options -kjdon.
34# these include:
35# -nogroup, which makes each year (or year+month) an individual entry in
36# the horizontal list and prevents compaction
37# -metadata, use a different metadata for the date (instead of Date), still expects yyyymmdd format. this affects display cos greenstone displays Date metadata as dd month yyyy, whereas any other date metadata is displayed as yyyymmdd - this needs fixing
38# -sort specifies an additional metadata to use in sorting, will take affect when two docs have the same date.
39
40package DateList;
41
42use BaseClassifier;
43use sorttools;
44
45use strict;
46no strict 'refs'; # allow filehandles to be variables and viceversa
47
48sub BEGIN {
49 @DateList::ISA = ('BaseClassifier');
50}
51
52my $arguments =
53 [ { 'name' => "metadata",
54 'desc' => "{DateList.metadata}",
55 'type' => "metadata",
56 'deft' => "Date",
57 'reqd' => "yes" } ,
58 { 'name' => "sort",
59 'desc' => "{DateList.sort}",
60 'type' => "metadata",
61 'reqd' => "no" } ,
62 { 'name' => "reverse_sort",
63 'desc' => "{DateList.reverse_sort}",
64 'type' => "flag",
65 'reqd' => "no" },
66 { 'name' => "bymonth",
67 'desc' => "{DateList.bymonth}",
68 'type' => "flag",
69 'reqd' => "no" },
70 { 'name' => "nogroup",
71 'desc' => "{DateList.nogroup}",
72 'type' => "flag",
73 'reqd' => "no" },
74 { 'name' => "no_special_formatting",
75 'desc' => "{DateList.no_special_formatting}",
76 'type' => "flag",
77 'reqd' => "no" }
78
79 ];
80
81my $options = { 'name' => "DateList",
82 'desc' => "{DateList.desc}",
83 'abstract' => "no",
84 'inherits' => "yes",
85 'args' => $arguments };
86
87
88sub new {
89 my ($class) = shift (@_);
90 my ($classifierslist,$inputargs,$hashArgOptLists) = @_;
91 push(@$classifierslist, $class);
92
93 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
94 push(@{$hashArgOptLists->{"OptList"}},$options);
95
96 my $self = new BaseClassifier($classifierslist, $inputargs, $hashArgOptLists);
97
98 if ($self->{'info_only'}) {
99 # don't worry about any options etc
100 return bless $self, $class;
101 }
102
103 # Manually set $self parameters.
104 $self->{'list'} = {};
105
106 if (!defined $self->{"metadata"} || $self->{"metadata"} eq "") {
107 $self->{'metadata'} = "Date";
108 }
109 # remove any ex.s
110 $self->{'metadata'} = $self->strip_ex_from_metadata($self->{'metadata'});
111 $self->{'sort'} = $self->strip_ex_from_metadata($self->{'sort'});
112
113 # now can have comma separated list of Dates - we just use the first one (for now)
114 my @meta_list = split(/,/, $self->{"metadata"});
115 $self->{'meta_list'} = \@meta_list;
116
117 $self->{'buttonname'} = $self->generate_title_from_metadata($self->{'metadata'}) unless ($self->{'buttonname'});
118
119 $self->{'childtype'} = "DateList";
120 if ($self->{'no_special_formatting'}) {
121 $self->{'childtype'} = "VList";
122 }
123
124 return bless $self, $class;
125}
126
127sub init {
128 my $self = shift (@_);
129
130 $self->{'list'} = {};
131}
132
133sub classify {
134 my $self = shift (@_);
135 my ($doc_obj, $edit_mode) = @_;
136
137 my $doc_OID = $doc_obj->get_OID();
138
139 # find the first available metadata
140 my $date;
141 foreach my $m (@{$self->{'meta_list'}}) {
142 $date = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $m);
143 last if defined $date;
144 }
145
146 #my $date = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'metadata'});
147 if (!defined $date || $date eq "") {
148 # if this document doesn't contain Date element we won't
149 # include it in this classification
150 return;
151 }
152
153 if (($edit_mode eq "delete") || ($edit_mode eq "update")) {
154 $self->oid_hash_delete($doc_OID,'list');
155 return if ($edit_mode eq "delete");
156 }
157
158 my $sort_other = "";
159 if (defined $self->{'sort'} && $self->{'sort'} ne "") {
160 $sort_other = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'sort'});
161 $sort_other = &sorttools::format_metadata_for_sorting($self->{'sort'}, $sort_other, $doc_obj) unless $self->{'no_metadata_formatting'};
162 }
163
164 if (defined $self->{'list'}->{$doc_OID}) {
165 my $outhandle = $self->{'outhandle'};
166 print $outhandle "WARNING: DateList::classify called multiple times for $doc_OID\n";
167 }
168
169
170 $self->{'list'}->{$doc_OID} = "$date$sort_other";
171
172}
173
174
175sub get_classify_info {
176 my $self = shift (@_);
177
178 my @classlist = sort {$self->{'list'}->{$a} cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
179
180 if ($self->{'reverse_sort'}) {
181 @classlist = reverse @classlist;
182 }
183
184
185 return $self->splitlist (\@classlist);
186}
187
188
189sub get_entry {
190 my $self = shift (@_);
191 my ($title, $childtype, $thistype) = @_;
192
193 # organise into classification structure
194 my %classifyinfo = ('childtype'=>$childtype,
195 'Title'=>$title,
196 'contains'=>[],
197 'mdtype'=>$self->{'metadata'});
198 $classifyinfo{'thistype'} = $thistype
199 if defined $thistype && $thistype =~ /\w/;
200
201 return \%classifyinfo;
202}
203
204# splitlist takes an ordered list of classifications (@$classlistref) and
205# splits it up into sub-sections by date
206sub splitlist {
207 my $self = shift (@_);
208 my ($classlistref) = @_;
209 my $classhash = {};
210
211 # top level
212 my $childtype = "HList";
213
214 if (scalar (@$classlistref) <= 39 &&
215 !$self->{'nogroup'}) {$childtype = $self->{'childtype'};}
216
217 my $classifyinfo = $self->get_entry ($self->{'buttonname'}, $childtype, "Invisible");
218 # don't need to do any splitting if there are less than 39 (max + min -1)
219 # classifications, unless nogroup is specified
220 if ((scalar @$classlistref) <= 39 && !$self->{'nogroup'}) {
221 foreach my $subOID (@$classlistref) {
222 push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
223 }
224 return $classifyinfo;
225 }
226
227
228 if ($self->{'bymonth'}) {
229 # first split up the list into separate year+month classifications
230
231 if (!$self->{'nogroup'}) { # hlist of year+month pairs
232 # single level of classifications
233 foreach my $classification (@$classlistref) {
234 my $date = $self->{'list'}->{$classification};
235 $date =~ s/^(\d\d\d\d)-?(\d\d).*$/$1&nbsp;_textmonth$2_/;
236 # sanity check if month is zero
237 if ($date =~ /00_$/) {
238 $date =~ s/^(\d\d\d\d).*$/$1/g;
239 }
240 $classhash->{$date} = [] unless defined $classhash->{$date};
241 push (@{$classhash->{$date}}, $classification);
242 }
243
244 } else { # don't group - individual years and months
245 foreach my $classification (@$classlistref) {
246 my $date = $self->{'list'}->{$classification};
247 $date =~ s/^(\d\d\d\d)-?(\d\d).*$/$1&nbsp;_textmonth$2_/;
248 my ($year, $month)=($1,$2);
249 # sanity check if month is zero
250 if ($date =~ /00_$/) {
251 $date =~ s/^(\d\d\d\d).*$/$1/g;
252 }
253 # create subclass if it doesn't already exist
254 $classhash->{$year} = () unless defined $classhash->{$year};
255
256 $classhash->{$year}->{$month} = []
257 unless defined $classhash->{$year}->{$month};
258 push (@{$classhash->{$year}->{$month}}, $classification);
259
260 }
261 # create hlist of years containing hlists of months
262
263
264 if ($self->{'reverse_sort'}){
265 foreach my $subclass (sort {$b <=> $a}
266 (keys %$classhash)){
267 my $yearclassify = $self->get_entry($subclass, "HList");
268 foreach my $subsubclass (sort {$b <=> $a}
269 (keys %{$classhash->{$subclass}})) {
270 my $monthname=$subsubclass;
271 if ($monthname >= 1 && $monthname <= 12) {
272 $monthname="_textmonth" . $monthname . "_";
273 }
274 my $monthclassify=$self->get_entry($monthname, $self->{'childtype'});
275 push (@{$yearclassify->{'contains'}}, $monthclassify);
276
277 foreach my $subsubOID
278 (@{$classhash->{$subclass}->{$subsubclass}}) {
279 push (@{$monthclassify->{'contains'}},
280 {'OID'=>$subsubOID});
281 }
282 }
283 push (@{$classifyinfo->{'contains'}}, $yearclassify);
284 }
285 }
286 else{
287 foreach my $subclass (sort {$a <=> $b}
288 (keys %$classhash)){
289 my $yearclassify = $self->get_entry($subclass, "HList");
290 foreach my $subsubclass (sort {$a <=> $b}
291 (keys %{$classhash->{$subclass}})) {
292 my $monthname=$subsubclass;
293 if ($monthname >= 1 && $monthname <= 12) {
294 $monthname="_textmonth" . $monthname . "_";
295 }
296 my $monthclassify=$self->get_entry($monthname, $self->{'childtype'});
297 push (@{$yearclassify->{'contains'}}, $monthclassify);
298
299 foreach my $subsubOID
300 (@{$classhash->{$subclass}->{$subsubclass}}) {
301 push (@{$monthclassify->{'contains'}},
302 {'OID'=>$subsubOID});
303 }
304 }
305 push (@{$classifyinfo->{'contains'}}, $yearclassify);
306 }
307
308
309 }
310
311 return $classifyinfo;
312 } # nogroup
313 }else {
314 # not by month
315 # first split up the list into separate year classifications
316 foreach my $classification (@$classlistref) {
317 my $date = $self->{'list'}->{$classification};
318 $date =~ s/^(\d\d\d\d).*$/$1/;
319 $classhash->{$date} = [] unless defined $classhash->{$date};
320 push (@{$classhash->{$date}}, $classification);
321 }
322
323 }
324
325 # only compact the list if nogroup not specified
326 if (!$self->{'nogroup'}) {
327 $classhash = $self->compactlist ($classhash);
328 }
329
330 if ($self->{'reverse_sort'} && $self->{'nogroup'} ) {
331 foreach my $subclass (reverse sort keys %$classhash) {
332 my $tempclassify = $self->get_entry($subclass, $self->{'childtype'});
333 foreach my $subsubOID (@{$classhash->{$subclass}}) {
334 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
335 }
336 push (@{$classifyinfo->{'contains'}}, $tempclassify);
337 }
338 }
339 else{
340 foreach my $subclass (sort keys %$classhash) {
341 my $tempclassify = $self->get_entry($subclass, $self->{'childtype'});
342 foreach my $subsubOID (@{$classhash->{$subclass}}) {
343 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
344 }
345 push (@{$classifyinfo->{'contains'}}, $tempclassify);
346 }
347
348 }
349
350
351 return $classifyinfo;
352}
353
354sub compactlist {
355 my $self = shift (@_);
356 my ($classhashref) = @_;
357 my $compactedhash = {};
358 my @currentOIDs = ();
359 my $currentfirstdate = "";
360 my $currentlastdate = "";
361 my $lastkey = "";
362
363 # minimum and maximum documents to be displayed per page.
364 # the actual maximum will be max + (min-1).
365 # the smallest sub-section is a single letter at present
366 # so in this case there may be many times max documents
367 # displayed on a page.
368 my $min = 10;
369 my $max = 30;
370 foreach my $subsection (sort keys %$classhashref) {
371 $currentfirstdate = $subsection if $currentfirstdate eq "";
372 if ((scalar (@currentOIDs) < $min) ||
373 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
374 push (@currentOIDs, @{$classhashref->{$subsection}});
375 $currentlastdate = $subsection;
376 } else {
377
378 if ($currentfirstdate eq $currentlastdate) {
379 @{$compactedhash->{$currentfirstdate}} = @currentOIDs;
380 $lastkey = $currentfirstdate;
381 } else {
382 @{$compactedhash->{"$currentfirstdate-$currentlastdate"}} = @currentOIDs;
383 $lastkey = "$currentfirstdate-$currentlastdate";
384 }
385 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
386 $compactedhash->{$subsection} = $classhashref->{$subsection};
387 @currentOIDs = ();
388 $currentfirstdate = "";
389 $lastkey = $subsection;
390 } else {
391 @currentOIDs = @{$classhashref->{$subsection}};
392 $currentfirstdate = $subsection;
393 $currentlastdate = $subsection;
394 }
395 }
396 }
397
398 # add final OIDs to last sub-classification if there aren't many otherwise
399 # add final sub-classification
400 if ((scalar (@currentOIDs) < $min) && (scalar (@currentOIDs) > 0)) {
401 # want every thing in previous up to the dash
402 my ($newkey) = $lastkey =~ /^([^\-]+)/;
403 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
404 delete $compactedhash->{$lastkey};
405 @{$compactedhash->{"$newkey-$currentlastdate"}} = @currentOIDs;
406 } else {
407 if ($currentfirstdate eq $currentlastdate) {
408 @{$compactedhash->{$currentfirstdate}} = @currentOIDs;
409 } else {
410 @{$compactedhash->{"$currentfirstdate-$currentlastdate"}} = @currentOIDs;
411 }
412 }
413
414 return $compactedhash;
415}
416
4171;
Note: See TracBrowser for help on using the repository browser.