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