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

Last change on this file since 8728 was 8716, checked in by kjdon, 19 years ago

added some changes made by Emanuel Dejanu (Simple Words)

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 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 BasClas;
43use sorttools;
44
45sub BEGIN {
46 @ISA = ('BasClas');
47}
48
49my $arguments =
50 [ { 'name' => "metadata",
51 'desc' => "{DateList.metadata}",
52 'type' => "metadata",
53 'deft' => "Date",
54 'reqd' => "no" } ,
55 { 'name' => "sort",
56 'desc' => "{DateList.sort}",
57 'type' => "metadata",
58 'reqd' => "no" } ,
59 { 'name' => "reverse_sort",
60 'desc' => "{DateList.reverse_sort}",
61 'type' => "flag",
62 'reqd' => "no" },
63 { 'name' => "bymonth",
64 'desc' => "{DateList.bymonth}",
65 'type' => "flag",
66 'reqd' => "no" },
67 { 'name' => "nogroup",
68 'desc' => "{DateList.nogroup}",
69 'type' => "flag",
70 'reqd' => "no" }
71 ];
72
73my $options = { 'name' => "DateList",
74 'desc' => "{DateList.desc}",
75 'abstract' => "no",
76 'inherits' => "yes",
77 'args' => $arguments };
78
79
80sub new {
81 my $class = shift (@_);
82 my $self = new BasClas($class, @_);
83
84 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
85 my $option_list = $self->{'option_list'};
86 push( @{$option_list}, $options );
87
88 if ($self->{'info_only'}) {
89 # created from classinfo.pl - don't need to parse the arguments
90 return bless $self, $class;
91 }
92
93 $self->{'list'} = {};
94
95 my ($datemeta, $sortmeta);
96 $self->{'nogroup'}=0;
97 if (!parsargv::parse(\@_,
98 q^bymonth^, \$self->{'bymonth'},
99 q^nogroup^, \$self->{'nogroup'},
100 q^metadata/.*/^, \$datemeta,
101 q^sort/.*/^, \$sortmeta,
102 q^newest_first^, \$self->{'newest_first'},
103 "allow_extra_options")) {
104 $self->print_txt_usage(""); # Use default resource bundle
105 die "\n";
106 }
107
108 if (!defined $datemeta || $datemeta eq "") {
109 $datemeta = "Date";
110 }
111 $self->{'datemeta'} = $datemeta;
112
113 if (defined $sortmeta && $sortmeta ne "") {
114 $self->{'sortmeta'} = $sortmeta;
115 }
116 return bless $self, $class;
117}
118
119sub init {
120 my $self = shift (@_);
121
122 $self->{'list'} = {};
123}
124
125sub classify {
126 my $self = shift (@_);
127 my ($doc_obj) = @_;
128
129 my $doc_OID = $doc_obj->get_OID();
130 my $date = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'datemeta'});
131
132 my $sort_other = "";
133 if (defined $self->{'sortmeta'} && $self->{'sortmeta'} ne "") {
134 $sort_other = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'sortmeta'});
135 }
136 # if this document doesn't contain Date element we won't
137 # include it in this classification
138 if (defined $date && $date =~ /\d/) {
139 if (defined $self->{'list'}->{$doc_OID}) {
140 my $outhandle = $self->{'outhandle'};
141 print $outhandle "WARNING: DateList::classify called multiple times for $doc_OID\n";
142 }
143
144
145 $self->{'list'}->{$doc_OID} = "$date$sort_other";
146 }
147}
148
149
150sub get_classify_info {
151 my $self = shift (@_);
152
153 my @classlist;
154 if ($self->{'newest_first'}) {
155 @classlist = sort {$self->{'list'}->{$b} cmp $self->{'list'}->{$a};} keys %{$self->{'list'}};
156 }
157 else {
158 @classlist = sort {$self->{'list'}->{$a} cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
159 }
160
161 return $self->splitlist (\@classlist);
162}
163
164
165sub get_entry {
166 my $self = shift (@_);
167 my ($title, $childtype, $thistype) = @_;
168
169 # organise into classification structure
170 my %classifyinfo = ('childtype'=>$childtype,
171 'Title'=>$title,
172 'contains'=>[],
173 'mdtype'=>$self->{'datemeta'});
174 $classifyinfo{'thistype'} = $thistype
175 if defined $thistype && $thistype =~ /\w/;
176
177 return \%classifyinfo;
178}
179
180# splitlist takes an ordered list of classifications (@$classlistref) and
181# splits it up into sub-sections by date
182sub splitlist {
183 my $self = shift (@_);
184 my ($classlistref) = @_;
185 my $classhash = {};
186
187 # top level
188 my $childtype = "HList";
189
190 if (scalar (@$classlistref) <= 39 &&
191 !$self->{'nogroup'}) {$childtype = "DateList";}
192 my $classifyinfo = $self->get_entry ("Date", $childtype, "Invisible");
193 # don't need to do any splitting if there are less than 39 (max + min -1)
194 # classifications, unless nogroup is specified
195 if ((scalar @$classlistref) <= 39 && !$self->{'nogroup'}) {
196 foreach $subOID (@$classlistref) {
197 push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
198 }
199 return $classifyinfo;
200 }
201
202
203 if ($self->{'bymonth'}) {
204 # first split up the list into separate year+month classifications
205
206 if (!$self->{'nogroup'}) { # hlist of year+month pairs
207 # single level of classifications
208 foreach $classification (@$classlistref) {
209 my $date = $self->{'list'}->{$classification};
210 $date =~ s/^(\d\d\d\d)(\d\d).*$/$1&nbsp;_textmonth$2_/;
211 # sanity check if month is zero
212 if ($date =~ /00_$/) {
213 $date =~ s/^(\d\d\d\d).*$/$1/g;
214 }
215 $classhash->{$date} = [] unless defined $classhash->{$date};
216 push (@{$classhash->{$date}}, $classification);
217 }
218
219 } else { # don't group - individual years and months
220 foreach $classification (@$classlistref) {
221 my $date = $self->{'list'}->{$classification};
222 $date =~ s/^(\d\d\d\d)(\d\d).*$/$1&nbsp;_textmonth$2_/;
223 my ($year, $month)=($1,$2);
224 # sanity check if month is zero
225 if ($date =~ /00_$/) {
226 $date =~ s/^(\d\d\d\d).*$/$1/g;
227 }
228 # create subclass if it doesn't already exist
229 $classhash->{$year} = () unless defined $classhash->{$year};
230 $classhash->{$year}->{$month} = []
231 unless defined $classhash->{$year}->{$month};
232 push (@{$classhash->{$year}->{$month}}, $classification);
233 }
234 # create hlist of years containing hlists of months
235
236 foreach my $subclass (sort {$a <=> $b} keys %$classhash) {
237 my $yearclassify = $self->get_entry($subclass, "HList");
238 foreach my $subsubclass (sort {$a <=> $b}
239 (keys %{$classhash->{$subclass}})) {
240 my $monthname=$subsubclass;
241 if ($monthname >= 1 && $monthname <= 12) {
242 $monthname="_textmonth" . $monthname . "_";
243 }
244 my $monthclassify=$self->get_entry($monthname, "DateList");
245 push (@{$yearclassify->{'contains'}}, $monthclassify);
246
247 foreach $subsubOID
248 (@{$classhash->{$subclass}->{$subsubclass}}) {
249 push (@{$monthclassify->{'contains'}},
250 {'OID'=>$subsubOID});
251 }
252 }
253 push (@{$classifyinfo->{'contains'}}, $yearclassify);
254 }
255 return $classifyinfo;
256 } # nogroup
257 } else {
258 # not by month
259 # first split up the list into separate year classifications
260 foreach $classification (@$classlistref) {
261 my $date = $self->{'list'}->{$classification};
262 $date =~ s/^(\d\d\d\d).*$/$1/;
263 $classhash->{$date} = [] unless defined $classhash->{$date};
264 push (@{$classhash->{$date}}, $classification);
265 }
266 }
267
268 # only compact the list if nogroup not specified
269 if (!$self->{'nogroup'}) {
270 $classhash = $self->compactlist ($classhash);
271 }
272 foreach $subclass (sort keys %$classhash) {
273 my $tempclassify = $self->get_entry($subclass, "DateList");
274 foreach $subsubOID (@{$classhash->{$subclass}}) {
275 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
276 }
277 push (@{$classifyinfo->{'contains'}}, $tempclassify);
278 }
279
280 return $classifyinfo;
281}
282
283sub compactlist {
284 my $self = shift (@_);
285 my ($classhashref) = @_;
286 my $compactedhash = {};
287 my @currentOIDs = ();
288 my $currentfirstdate = "";
289 my $currentlastdate = "";
290 my $lastkey = "";
291
292 # minimum and maximum documents to be displayed per page.
293 # the actual maximum will be max + (min-1).
294 # the smallest sub-section is a single letter at present
295 # so in this case there may be many times max documents
296 # displayed on a page.
297 my $min = 10;
298 my $max = 30;
299 foreach my $subsection (sort keys %$classhashref) {
300 $currentfirstdate = $subsection if $currentfirstdate eq "";
301 if ((scalar (@currentOIDs) < $min) ||
302 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
303 push (@currentOIDs, @{$classhashref->{$subsection}});
304 $currentlastdate = $subsection;
305 } else {
306
307 if ($currentfirstdate eq $currentlastdate) {
308 @{$compactedhash->{$currentfirstdate}} = @currentOIDs;
309 $lastkey = $currentfirstdate;
310 } else {
311 @{$compactedhash->{"$currentfirstdate-$currentlastdate"}} = @currentOIDs;
312 $lastkey = "$currentfirstdate-$currentlastdate";
313 }
314 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
315 $compactedhash->{$subsection} = $classhashref->{$subsection};
316 @currentOIDs = ();
317 $currentfirstdate = "";
318 $lastkey = $subsection;
319 } else {
320 @currentOIDs = @{$classhashref->{$subsection}};
321 $currentfirstdate = $subsection;
322 $currentlastdate = $subsection;
323 }
324 }
325 }
326
327 # add final OIDs to last sub-classification if there aren't many otherwise
328 # add final sub-classification
329 if ((scalar (@currentOIDs) < $min) && (scalar (@currentOIDs) > 0)) {
330 # want every thing in previous up to the dash
331 my ($newkey) = $lastkey =~ /^([^\-]+)/;
332 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
333 delete $compactedhash->{$lastkey};
334 @{$compactedhash->{"$newkey-$currentlastdate"}} = @currentOIDs;
335 } else {
336 if ($currentfirstdate eq $currentlastdate) {
337 @{$compactedhash->{$currentfirstdate}} = @currentOIDs;
338 } else {
339 @{$compactedhash->{"$currentfirstdate-$currentlastdate"}} = @currentOIDs;
340 }
341 }
342
343 return $compactedhash;
344}
345
3461;
Note: See TracBrowser for help on using the repository browser.