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

Last change on this file since 6976 was 6968, checked in by kjdon, 20 years ago

all classifiers now use BasClas.buttonname for their buttonname option description.
removed the old print_usage methods and old usage notes.
added in a test for $self->{'info_only'} in new(): if this is set, don't try and parse the arguments cos we are only running classinfo.pl.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 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 # find the first available metadata
156 foreach $m (@{$self->{'meta_list'}}) {
157 $metavalue = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $m);
158 $metaname = $m;
159 last if defined $metavalue;
160 }
161 #if we haven't found a metavalue here, then the doc shouldn't be included
162 return unless defined $metavalue;
163 }
164
165 # we know the doc should be included, add it now if we are not sorting
166 if ($nosort) {
167 push (@{$self->{'list'}}, $doc_OID);
168 return;
169 }
170
171 #check for a sort element other than our metadata
172 if (defined $self->{'sortname'}) {
173 my $sortmeta;
174 if ($self->{'sortname'} =~ /^filename$/i) {
175 $sortmeta = $doc_obj->get_source_filename();
176 } else {
177 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sortname'});
178 if (defined $sortmeta) {
179 $sortmeta = $self->format_metadata_for_sorting($self->{'sortname'}, $sortmeta, $doc_obj);
180 }
181 }
182 $sortmeta = "" unless defined $sortmeta;
183 $self->{'list'}->{$doc_OID} = $sortmeta;
184 } else {
185 # we add to the list based on metadata value
186 # but we need to do the same formatting as for sort value
187 ($metavalue) = $self->format_metadata_for_sorting($metaname, $metavalue, $doc_obj);
188 $self->{'list'}->{$doc_OID} = $metavalue;
189 }
190}
191
192
193sub get_classify_info {
194 my $self = shift (@_);
195 my ($no_thistype) = @_;
196 $no_thistype = 0 unless defined $no_thistype;
197
198 my @list = ();
199 if (defined $self->{'sortname'} && $self->{'sortname'} eq "nosort") {
200 @list = @{$self->{'list'}};
201 } else {
202 if (keys %{$self->{'list'}}) {
203 @list = sort {$self->{'list'}->{$a}
204 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
205 }
206 }
207
208 # organise into classification structure
209 my %classifyinfo = ('childtype'=>'VList',
210 'Title'=>$self->{'title'},
211 'contains'=>[]);
212 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
213
214 foreach $OID (@list) {
215 push (@{$classifyinfo{'contains'}}, {'OID'=>$OID});
216 }
217
218 return \%classifyinfo;
219}
220
221
2221;
223
224
225
226
Note: See TracBrowser for help on using the repository browser.