source: main/tags/2.81/gsdl/perllib/classify/List.pm@ 25190

Last change on this file since 25190 was 17209, checked in by kjdon, 16 years ago

BasClas renamed to BaseClassifier, tidied up constructors

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 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 BaseClassifier;
30package List;
31
32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
35use sorttools;
36
37sub BEGIN {
38 @List::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' => "List",
52 'desc' => "{List.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) = @_;
126
127 my $doc_OID = $doc_obj->get_OID();
128
129 # are we sorting the list??
130 my $nosort = 0;
131 if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
132 $nosort = 1;
133 }
134
135 my $metavalue;
136 my $metaname;
137 if (defined $self->{'meta_list'}) {
138 my $topsection=$doc_obj->get_top_section();
139
140 # find the correct bit of metadata, if multi-valued metadata field
141 if (exists $doc_obj->{'mdoffset'}) { # set by AZCompactList
142
143 my $mdoffset=$doc_obj->{'mdoffset'} - 1;
144
145 foreach my $m (@{$self->{'meta_list'}}) {
146 my $values_listref=
147 $doc_obj->get_metadata($topsection, $m);
148 my $array_size = scalar(@{$values_listref});
149 if ($array_size==0 || $array_size < $mdoffset+1) {
150 $mdoffset = $mdoffset - $array_size;
151 } else {
152 $metaname = $m;
153 # get the correct value using the offset
154 $metavalue=@$values_listref[$mdoffset];
155 # use a special format for docOID...
156 $doc_OID .= ".offset$mdoffset";
157 last;
158 }
159 }
160 } else {
161 # use the first available metadata
162 foreach my $m (@{$self->{'meta_list'}}) {
163 $metavalue = $doc_obj->
164 get_metadata_element($topsection, $m);
165 $metaname = $m;
166 last if defined $metavalue;
167 }
168 }
169 # if we haven't found a metavalue, then the doc shouldn't be included
170 return unless defined $metavalue;
171 }
172
173 # we know the doc should be included, add it now if we are not sorting
174 if ($nosort) {
175 push (@{$self->{'list'}}, $doc_OID);
176 return;
177 }
178
179 #check for a sort element other than our metadata
180 if (defined $self->{'sort'}) {
181 my $sortmeta;
182 if ($self->{'sort'} =~ /^filename$/i) {
183 $sortmeta = $doc_obj->get_source_filename();
184 } else {
185 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sort'});
186 if (defined $sortmeta && !$self->{'no_metadata_formatting'}) {
187 $sortmeta = &sorttools::format_metadata_for_sorting($self->{'sort'}, $sortmeta, $doc_obj);
188 }
189 }
190 $sortmeta = "" unless defined $sortmeta;
191 $self->{'list'}->{$doc_OID} = $sortmeta;
192 } else {
193 # we add to the list based on metadata value
194 # but we need to do the same formatting as for sort value
195 ($metavalue) = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj) unless $self->{'no_metadata_formatting'};
196 $self->{'list'}->{$doc_OID} = $metavalue;
197 }
198 my $id = $self->get_number();
199 $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$id");
200}
201
202
203sub get_classify_info {
204 my $self = shift (@_);
205 my ($no_thistype) = @_;
206 $no_thistype = 0 unless defined $no_thistype;
207 my $memberof = &supports_memberof();
208
209 my @list = ();
210 if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
211 @list = @{$self->{'list'}};
212 } else {
213 if (keys %{$self->{'list'}}) {
214 @list = sort {$self->{'list'}->{$a}
215 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
216 }
217 }
218 # organise into classification structure
219 my %classifyinfo = ('childtype'=>'VList',
220 'Title'=>$self->{'buttonname'},
221 'contains'=>[]);
222 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
223 # always supports memberof
224 $classifyinfo{'supportsmemberof'} = $memberof;
225
226 foreach my $OID (@list) {
227 my $hashref={};
228 # special oid format, if using offsets (from AZCompactList)
229 if ($OID =~ s/\.offset(\d+)$//) {
230 $hashref->{'offset'}=$1;
231 }
232 $hashref->{'OID'}=$OID;
233
234 push (@{$classifyinfo{'contains'}}, $hashref);
235 }
236
237 return \%classifyinfo;
238}
239
240sub supports_memberof {
241 my $self = shift(@_);
242
243 return "true";
244}
245
2461;
247
248
249
250
Note: See TracBrowser for help on using the repository browser.