source: trunk/gsdl/perllib/classify/List.pm@ 8852

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

shifted format_metadata_for_sorting from BasCLas to sorttools, so other things can use it

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1###########################################################################
2#
3# List.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# simple list classifier plugin
27# to see the options, run "perl -S classinfo.pl List"
28
29use BasClas;
30package List;
31
32use sorttools;
33
34sub BEGIN {
35 @ISA = ('BasClas');
36}
37
38my $arguments =
39 [ { 'name' => "metadata",
40 'desc' => "{List.metadata}",
41 'type' => "metadata",
42 'reqd' => "no" },
43 { 'name' => "buttonname",
44 'desc' => "{BasClas.buttonname}",
45 'type' => "string",
46 'deft' => "{BasClas.metadata.deft}",
47 'reqd' => "no" },
48 { 'name' => "sort",
49 'desc' => "{List.sort}",
50 'type' => "string",
51 'deft' => "{BasClas.metadata.deft}",
52 'reqd' => "no" } ];
53
54my $options = { 'name' => "List",
55 'desc' => "{List.desc}",
56 'abstract' => "no",
57 'inherits' => "yes",
58 'args' => $arguments };
59
60
61sub new {
62 my $class = shift (@_);
63 my $self = new BasClas($class, @_);
64
65 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
66 my $option_list = $self->{'option_list'};
67 push( @{$option_list}, $options );
68
69 if ($self->{'info_only'}) {
70 # created from classinfo.pl - don't need to parse the arguments
71 return bless $self, $class;
72 }
73
74 my ($metadata, $title, $sortname, $list);
75
76 if (!parsargv::parse(\@_,
77 q^metadata/.*/^, \$metadata,
78 q^buttonname/.*/^, \$title,
79 q^sort/.*/^, \$sortname,
80 "allow_extra_options")) {
81
82 print STDERR "\nIncorrect options passed to $class, check your collect.cfg file\n";
83 $self->print_txt_usage(""); # Use default resource bundle
84 die "\n";
85 }
86 my @meta_list;
87 my $meta1;
88
89 if ($metadata) {
90 @meta_list = split(/,/, $metadata);
91 $meta1 = $meta_list[0];
92 } else {
93 $metadata = undef;
94 $meta1=undef;
95 @meta_list = undef;
96 }
97
98 if (!$title) {
99 if (defined ($meta1)) {
100 $title = $meta1;
101 } else {
102 $title = 'List';
103 }
104 }
105
106 # sortname is handled a bit differently - kjdon
107 # undef means to sort, but use the metadata value from -metadata
108 # because there is no one metadata value to get for sorting when
109 # we have a list of possible metadata
110 # to get no sorting, set sortname = 'nosort'
111 if (!$sortname) {
112 if (defined ($metadata)) {
113 $sortname = undef;
114 } else {
115 $sortname = "nosort";
116 }
117 }
118
119 if (defined $sortname && $sortname eq "nosort") {
120 $list = [];
121 } else {
122 $list = {};
123 }
124
125 $self->{'list'} = $list;
126 if (defined $metadata) {
127 $self->{'meta_list'} = \@meta_list;
128 }
129 $self->{'title'} = $title;
130 $self->{'sortname'} = $sortname;
131
132 return bless $self, $class;
133}
134
135sub init {
136 my $self = shift (@_);
137
138}
139
140sub classify {
141 my $self = shift (@_);
142 my ($doc_obj) = @_;
143
144 my $doc_OID = $doc_obj->get_OID();
145
146 # are we sorting the list??
147 my $nosort = 0;
148 if (defined $self->{'sortname'} && $self->{'sortname'} eq "nosort") {
149 $nosort = 1;
150 }
151
152 my $metavalue;
153 my $metaname;
154 if (defined $self->{'meta_list'}) {
155 my $topsection=$doc_obj->get_top_section();
156
157 # find the correct bit of metadata, if multi-valued metadata field
158 if (exists $doc_obj->{'mdoffset'}) { # set by AZCompactList
159
160 my $mdoffset=$doc_obj->{'mdoffset'} - 1;
161
162 foreach my $m (@{$self->{'meta_list'}}) {
163 my $values_listref=
164 $doc_obj->get_metadata($topsection, $m);
165 $array_size = scalar(@{$values_listref});
166 if ($array_size==0 || $array_size < $mdoffset+1) {
167 $mdoffset = $mdoffset - $array_size;
168 } else {
169 $metaname = $m;
170 # get the correct value using the offset
171 $metavalue=@$values_listref[$mdoffset];
172 # use a special format for docOID...
173 $doc_OID .= ".offset$mdoffset";
174 last;
175 }
176 }
177 } else {
178 # use the first available metadata
179 foreach my $m (@{$self->{'meta_list'}}) {
180 $metavalue = $doc_obj->
181 get_metadata_element($topsection, $m);
182 $metaname = $m;
183 last if defined $metavalue;
184 }
185 }
186 # if we haven't found a metavalue, then the doc shouldn't be included
187 return unless defined $metavalue;
188 }
189
190 # we know the doc should be included, add it now if we are not sorting
191 if ($nosort) {
192 push (@{$self->{'list'}}, $doc_OID);
193 return;
194 }
195
196 #check for a sort element other than our metadata
197 if (defined $self->{'sortname'}) {
198 my $sortmeta;
199 if ($self->{'sortname'} =~ /^filename$/i) {
200 $sortmeta = $doc_obj->get_source_filename();
201 } else {
202 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sortname'});
203 if (defined $sortmeta) {
204 $sortmeta = &sorttools::format_metadata_for_sorting($self->{'sortname'}, $sortmeta, $doc_obj);
205 }
206 }
207 $sortmeta = "" unless defined $sortmeta;
208 $self->{'list'}->{$doc_OID} = $sortmeta;
209 } else {
210 # we add to the list based on metadata value
211 # but we need to do the same formatting as for sort value
212 ($metavalue) = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj);
213 $self->{'list'}->{$doc_OID} = $metavalue;
214 }
215 my $id = $self->get_number();
216 $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$id");
217}
218
219
220sub get_classify_info {
221 my $self = shift (@_);
222 my ($no_thistype) = @_;
223 $no_thistype = 0 unless defined $no_thistype;
224 my $memberof = &supports_memberof();
225
226 my @list = ();
227 if (defined $self->{'sortname'} && $self->{'sortname'} eq "nosort") {
228 @list = @{$self->{'list'}};
229 } else {
230 if (keys %{$self->{'list'}}) {
231 @list = sort {$self->{'list'}->{$a}
232 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
233 }
234 }
235
236 # organise into classification structure
237 my %classifyinfo = ('childtype'=>'VList',
238 'Title'=>$self->{'title'},
239 'contains'=>[]);
240 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
241 # always supports memberof
242 $classifyinfo{'supportsmemberof'} = $memberof;
243
244 foreach $OID (@list) {
245 my $hashref={};
246 # special oid format, if using offsets (from AZCompactList)
247 if ($OID =~ s/\.offset(\d+)$//) {
248 $hashref->{'offset'}=$1;
249 }
250 $hashref->{'OID'}=$OID;
251
252 push (@{$classifyinfo{'contains'}}, $hashref);
253 }
254
255 return \%classifyinfo;
256}
257
258sub supports_memberof {
259 my $self = shift(@_);
260
261 return "true";
262}
263
2641;
265
266
267
268
Note: See TracBrowser for help on using the repository browser.