source: gsdl/trunk/perllib/classify/SimpleList.pm@ 18565

Last change on this file since 18565 was 18565, checked in by kjdon, 15 years ago

file content changes for classifier rename

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