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