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

Last change on this file since 11484 was 10630, checked in by kjdon, 19 years ago

added -no_metadata_formatting option to avoid the format_metadata_for_sorting call

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