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

Last change on this file since 4778 was 4759, checked in by mdewsnip, 21 years ago

Tidied up 'options' and 'arguments' structures (representing the options of the plugin) in preparation for removing the print_usage() routines.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 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# always sorts by 'Date' metadata
29
30# date is assumed to be in the form yyyymmdd
31
32# at present dates are split by year - this should change
33# jrm21 - added option "bymonth", which splits by year and month.
34
35# 12/05/02 Added usage datastructure - John Thompson
36
37package DateList;
38
39use BasClas;
40use sorttools;
41
42sub BEGIN {
43 @ISA = ('BasClas');
44}
45
46my $arguments =
47 [ { 'name' => "bymonth",
48 'desc' => "Classify by year and month.",
49 'type' => "flag",
50 'reqd' => "no" } ];
51
52my $options = { 'name' => "DateList",
53 'desc' => "Classifier plugin for sorting by date. Always sorts by 'Date' metadata. Date is assumed to be in the form yyyymmdd.",
54 'inherits' => "Yes",
55 'args' => $arguments };
56
57sub print_usage {
58 print STDERR "
59 usage: classify DateList [options]
60 options:
61 -bymonth [or bymonth=1] Classify by year and month
62
63 Classifier plugin for sorting by date.
64 Always sorts by 'Date' metadata.
65 Date is assumed to be in the form yyyymmdd (all digits).
66 By default dates are split by year - this should change.
67
68 Any errors are Dana's problem.
69";
70}
71
72sub new {
73 my $class = shift (@_);
74 my $self = new BasClas($class, @_);
75
76 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
77 my $option_list = $self->{'option_list'};
78 push( @{$option_list}, $options );
79
80 $self->{'list'} = {};
81 if (!parsargv::parse(\@_,
82 q^bymonth^, \$self->{'bymonth'},
83 "allow_extra_options")) {
84 &print_usage();
85 die "\n";
86 }
87 return bless $self, $class;
88}
89
90sub init {
91 my $self = shift (@_);
92
93 $self->{'list'} = {};
94}
95
96sub classify {
97 my $self = shift (@_);
98 my ($doc_obj) = @_;
99
100 my $doc_OID = $doc_obj->get_OID();
101 my $date = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Date');
102
103 # if this document doesn't contain Date element we won't
104 # include it in this classification
105 if (defined $date && $date =~ /\d/) {
106 if (defined $self->{'list'}->{$doc_OID}) {
107 my $outhandle = $self->{'outhandle'};
108 print $outhandle "WARNING: DateList::classify called multiple times for $doc_OID\n";
109 }
110 $self->{'list'}->{$doc_OID} = $date;
111 }
112}
113
114
115sub get_classify_info {
116 my $self = shift (@_);
117
118 my @classlist = sort {$self->{'list'}->{$a} cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
119
120 return $self->splitlist (\@classlist);
121}
122
123
124sub get_entry {
125 my $self = shift (@_);
126 my ($title, $childtype, $thistype) = @_;
127
128 # organise into classification structure
129 my %classifyinfo = ('childtype'=>$childtype,
130 'Title'=>$title,
131 'contains'=>[]);
132 $classifyinfo{'thistype'} = $thistype
133 if defined $thistype && $thistype =~ /\w/;
134
135 return \%classifyinfo;
136}
137
138# splitlist takes an ordered list of classifications (@$classlistref) and
139# splits it up into sub-sections by date
140sub splitlist {
141 my $self = shift (@_);
142 my ($classlistref) = @_;
143 my $classhash = {};
144
145 # top level
146 my $childtype = "HList";
147 if (scalar (@$classlistref) <= 39) {$childtype = "DateList";}
148 my $classifyinfo = $self->get_entry ("Date", $childtype, "Invisible");
149
150 # don't need to do any splitting if there are less than 39 (max + min -1) classifications
151 if ((scalar @$classlistref) <= 39) {
152 foreach $subOID (@$classlistref) {
153 push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
154 }
155 return $classifyinfo;
156 }
157
158
159 if ($self->{'bymonth'}) {
160 # first split up the list into separate year+month classifications
161 foreach $classification (@$classlistref) {
162 my $date = $self->{'list'}->{$classification};
163 $date =~ s/^(\d\d\d\d)(\d\d).*$/$1&nbsp;_textmonth$2_/;
164 # sanity check if month is zero
165 if ($date =~ /00_$/) {
166 $date =~ s/^(\d\d\d\d).*$/$1/g;
167 }
168 $classhash->{$date} = [] unless defined $classhash->{$date};
169 push (@{$classhash->{$date}}, $classification);
170 }
171 } else {
172 # first split up the list into separate year classifications
173 foreach $classification (@$classlistref) {
174 my $date = $self->{'list'}->{$classification};
175 $date =~ s/^(\d\d\d\d).*$/$1/;
176 $classhash->{$date} = [] unless defined $classhash->{$date};
177 push (@{$classhash->{$date}}, $classification);
178 }
179 }
180 $classhash = $self->compactlist ($classhash);
181
182 foreach $subclass (sort keys %$classhash) {
183 my $tempclassify = $self->get_entry($subclass, "DateList");
184 foreach $subsubOID (@{$classhash->{$subclass}}) {
185 push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
186 }
187 push (@{$classifyinfo->{'contains'}}, $tempclassify);
188 }
189
190 return $classifyinfo;
191}
192
193sub compactlist {
194 my $self = shift (@_);
195 my ($classhashref) = @_;
196 my $compactedhash = {};
197 my @currentOIDs = ();
198 my $currentfirstdate = "";
199 my $currentlastdate = "";
200 my $lastkey = "";
201
202 # minimum and maximum documents to be displayed per page.
203 # the actual maximum will be max + (min-1).
204 # the smallest sub-section is a single letter at present
205 # so in this case there may be many times max documents
206 # displayed on a page.
207 my $min = 10;
208 my $max = 30;
209 foreach my $subsection (sort keys %$classhashref) {
210 $currentfirstdate = $subsection if $currentfirstdate eq "";
211 if ((scalar (@currentOIDs) < $min) ||
212 ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
213 push (@currentOIDs, @{$classhashref->{$subsection}});
214 $currentlastdate = $subsection;
215 } else {
216
217 if ($currentfirstdate eq $currentlastdate) {
218 @{$compactedhash->{$currentfirstdate}} = @currentOIDs;
219 $lastkey = $currentfirstdate;
220 } else {
221 @{$compactedhash->{"$currentfirstdate-$currentlastdate"}} = @currentOIDs;
222 $lastkey = "$currentfirstdate-$currentlastdate";
223 }
224 if (scalar (@{$classhashref->{$subsection}}) >= $max) {
225 $compactedhash->{$subsection} = $classhashref->{$subsection};
226 @currentOIDs = ();
227 $currentfirstdate = "";
228 $lastkey = $subsection;
229 } else {
230 @currentOIDs = @{$classhashref->{$subsection}};
231 $currentfirstdate = $subsection;
232 $currentlastdate = $subsection;
233 }
234 }
235 }
236
237 # add final OIDs to last sub-classification if there aren't many otherwise
238 # add final sub-classification
239 if ((scalar (@currentOIDs) < $min) && (scalar (@currentOIDs) > 0)) {
240 # want every thing in previous up to the dash
241 my ($newkey) = $lastkey =~ /^([^\-]+)/;
242 @currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
243 delete $compactedhash->{$lastkey};
244 @{$compactedhash->{"$newkey-$currentlastdate"}} = @currentOIDs;
245 } else {
246 if ($currentfirstdate eq $currentlastdate) {
247 @{$compactedhash->{$currentfirstdate}} = @currentOIDs;
248 } else {
249 @{$compactedhash->{"$currentfirstdate-$currentlastdate"}} = @currentOIDs;
250 }
251 }
252
253 return $compactedhash;
254}
255
2561;
Note: See TracBrowser for help on using the repository browser.