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

Last change on this file since 12891 was 12891, checked in by mdewsnip, 18 years ago

Tidied up that horrible long line in the new() function of every classifier.

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