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

Last change on this file since 8728 was 8221, checked in by cs025, 20 years ago

Added AllList to provide a universal list of all documents, which
supports OAI in particular at this early date. Also, changes to
Hierarchy and List to support 'memberof' function, and BasClas
ditto.

  • 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 my $mdoffset=$doc_obj->{'mdoffset'} - 1;
160 # if this List is used by AZCompactList, then $metavalue is
161 # only really needed for sorting.
162 # assume (!!) there is only one fieldname in the meta_list
163 $metaname=(@{$self->{'meta_list'}})[0];
164 # get all metadata values for this field
165 my $values_listref=
166 $doc_obj->get_metadata($topsection, $metaname);
167 # get the correct one (from the offset)
168 $metavalue=@$values_listref[$mdoffset];
169 # use a special format for docOID...
170 $doc_OID .= ".offset$mdoffset";
171
172 } else {
173 # use the first available metadata
174 foreach my $m (@{$self->{'meta_list'}}) {
175 $metavalue = $doc_obj->
176 get_metadata_element($topsection, $m);
177 $metaname = $m;
178 last if defined $metavalue;
179 }
180 }
181 # if we haven't found a metavalue, then the doc shouldn't be included
182 return unless defined $metavalue;
183 }
184
185 # we know the doc should be included, add it now if we are not sorting
186 if ($nosort) {
187 push (@{$self->{'list'}}, $doc_OID);
188 return;
189 }
190
191 #check for a sort element other than our metadata
192 if (defined $self->{'sortname'}) {
193 my $sortmeta;
194 if ($self->{'sortname'} =~ /^filename$/i) {
195 $sortmeta = $doc_obj->get_source_filename();
196 } else {
197 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sortname'});
198 if (defined $sortmeta) {
199 $sortmeta = $self->format_metadata_for_sorting($self->{'sortname'}, $sortmeta, $doc_obj);
200 }
201 }
202 $sortmeta = "" unless defined $sortmeta;
203 $self->{'list'}->{$doc_OID} = $sortmeta;
204 } else {
205 # we add to the list based on metadata value
206 # but we need to do the same formatting as for sort value
207 ($metavalue) = $self->format_metadata_for_sorting($metaname, $metavalue, $doc_obj);
208 $self->{'list'}->{$doc_OID} = $metavalue;
209 }
210 my $id = $self->get_number();
211 $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$id");
212}
213
214
215sub get_classify_info {
216 my $self = shift (@_);
217 my ($no_thistype) = @_;
218 $no_thistype = 0 unless defined $no_thistype;
219 my $memberof = &supports_memberof();
220
221 my @list = ();
222 if (defined $self->{'sortname'} && $self->{'sortname'} eq "nosort") {
223 @list = @{$self->{'list'}};
224 } else {
225 if (keys %{$self->{'list'}}) {
226 @list = sort {$self->{'list'}->{$a}
227 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
228 }
229 }
230
231 # organise into classification structure
232 my %classifyinfo = ('childtype'=>'VList',
233 'Title'=>$self->{'title'},
234 'contains'=>[]);
235 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
236 # always supports memberof
237 $classifyinfo{'supportsmemberof'} = $memberof;
238
239 foreach $OID (@list) {
240 my $hashref={};
241 # special oid format, if using offsets (from AZCompactList)
242 if ($OID =~ s/\.offset(\d+)$//) {
243 $hashref->{'offset'}=$1;
244 }
245 $hashref->{'OID'}=$OID;
246
247 push (@{$classifyinfo{'contains'}}, $hashref);
248 }
249
250 return \%classifyinfo;
251}
252
253sub supports_memberof {
254 my $self = shift(@_);
255
256 return "true";
257}
258
2591;
260
261
262
263
Note: See TracBrowser for help on using the repository browser.