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

Last change on this file since 18455 was 18455, checked in by davidb, 15 years ago

Addition of 'edit_mode' parameter to classify(). This can be either 'add' 'delete' or 'reindex' (should think about renaming the last one to something more appropriate, e.g. update).

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
RevLine 
[537]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
[287]26# simple list classifier plugin
[6942]27# to see the options, run "perl -S classinfo.pl List"
[287]28
[17209]29use BaseClassifier;
[287]30package List;
31
[10253]32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
[385]35use sorttools;
36
[1483]37sub BEGIN {
[17209]38 @List::ISA = ('BaseClassifier');
[1483]39}
40
[3540]41my $arguments =
[4759]42 [ { 'name' => "metadata",
[4873]43 'desc' => "{List.metadata}",
[3540]44 'type' => "metadata",
[6942]45 'reqd' => "no" },
[4759]46 { 'name' => "sort",
[4873]47 'desc' => "{List.sort}",
[11665]48 'type' => "metadata",
[4759]49 'reqd' => "no" } ];
[3540]50
[4759]51my $options = { 'name' => "List",
[5645]52 'desc' => "{List.desc}",
[6408]53 'abstract' => "no",
54 'inherits' => "yes",
[4759]55 'args' => $arguments };
[3540]56
[1839]57
[287]58sub new {
[10218]59 my ($class) = shift (@_);
[3540]60
[10218]61 my ($classifierslist,$inputargs,$hashArgOptLists) = @_;
62 push(@$classifierslist, $class);
[4759]63
[17209]64 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
65 push(@{$hashArgOptLists->{"OptList"}},$options);
[6968]66
[17209]67 my $self = new BaseClassifier($classifierslist, $inputargs, $hashArgOptLists);
[287]68
[17209]69 if ($self->{'info_only'}) {
70 # don't worry about any options etc
71 return bless $self, $class;
72 }
73
[10218]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"});
[6942]78 $meta1 = $meta_list[0];
[10218]79 $self->{'meta_list'} = \@meta_list;
[6942]80 } else {
81 $meta1=undef;
82 @meta_list = undef;
83 }
[287]84
[10218]85 if (!$self->{"buttonname"}) {
86 if (defined ($self->{'metadata'})) {
87 $self->{"buttonname"} = $self->generate_title_from_metadata($self->{'metadata'});
[426]88 } else {
[10218]89 $self->{"buttonname"} = 'List';
[426]90 }
91 }
92
[10218]93 # Further setup
94 # $self->{"sort"} is handled a bit differently - kjdon
[6942]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
[10218]98 # to get no sorting, set $self->{"sort"} = 'nosort'
99 if (!$self->{"sort"}) {
100 if (defined ($self->{"metadata"})) {
101 $self->{"sort"} = undef;
[1839]102 } else {
[10218]103 $self->{"sort"} = "nosort";
[1839]104 }
[10218]105 }
106 if (defined $self->{"sort"} && $self->{"sort"} eq "nosort") {
107 $self->{'list'} = [];
[6942]108 } else {
[10218]109 $self->{'list'} = {};
[1839]110 }
[10218]111
112 # Clean out the unused keys
113 delete $self->{"metadata"}; # Delete this key
[6942]114
[1483]115 return bless $self, $class;
[287]116}
117
118sub init {
119 my $self = shift (@_);
120
121}
122
123sub classify {
124 my $self = shift (@_);
[18455]125 my ($doc_obj, $edit_mode) = @_;
[287]126
127 my $doc_OID = $doc_obj->get_OID();
128
[18455]129
[6942]130 # are we sorting the list??
131 my $nosort = 0;
[10218]132 if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
[6942]133 $nosort = 1;
134 }
135
[18455]136 if ($edit_mode eq "delete") {
137 if ($nosort) {
138 $self->oid_array_delete($doc_OID,'list');
139 }
140 else {
141 $self->oid_hash_delete($doc_OID,'list');
142 }
143 return;
144 }
145
[6942]146 my $metavalue;
147 my $metaname;
148 if (defined $self->{'meta_list'}) {
[7835]149 my $topsection=$doc_obj->get_top_section();
150
151 # find the correct bit of metadata, if multi-valued metadata field
152 if (exists $doc_obj->{'mdoffset'}) { # set by AZCompactList
[8729]153
[7835]154 my $mdoffset=$doc_obj->{'mdoffset'} - 1;
[8729]155
156 foreach my $m (@{$self->{'meta_list'}}) {
157 my $values_listref=
158 $doc_obj->get_metadata($topsection, $m);
[10253]159 my $array_size = scalar(@{$values_listref});
[8729]160 if ($array_size==0 || $array_size < $mdoffset+1) {
161 $mdoffset = $mdoffset - $array_size;
162 } else {
163 $metaname = $m;
164 # get the correct value using the offset
165 $metavalue=@$values_listref[$mdoffset];
166 # use a special format for docOID...
167 $doc_OID .= ".offset$mdoffset";
168 last;
169 }
170 }
[7835]171 } else {
172 # use the first available metadata
173 foreach my $m (@{$self->{'meta_list'}}) {
174 $metavalue = $doc_obj->
175 get_metadata_element($topsection, $m);
176 $metaname = $m;
177 last if defined $metavalue;
178 }
[6942]179 }
[7835]180 # if we haven't found a metavalue, then the doc shouldn't be included
[6942]181 return unless defined $metavalue;
182 }
183
184 # we know the doc should be included, add it now if we are not sorting
185 if ($nosort) {
186 push (@{$self->{'list'}}, $doc_OID);
187 return;
188 }
189
190 #check for a sort element other than our metadata
[10218]191 if (defined $self->{'sort'}) {
[6942]192 my $sortmeta;
[10218]193 if ($self->{'sort'} =~ /^filename$/i) {
[545]194 $sortmeta = $doc_obj->get_source_filename();
195 } else {
[10218]196 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sort'});
[10630]197 if (defined $sortmeta && !$self->{'no_metadata_formatting'}) {
[10218]198 $sortmeta = &sorttools::format_metadata_for_sorting($self->{'sort'}, $sortmeta, $doc_obj);
[287]199 }
[545]200 }
201 $sortmeta = "" unless defined $sortmeta;
[6942]202 $self->{'list'}->{$doc_OID} = $sortmeta;
203 } else {
204 # we add to the list based on metadata value
205 # but we need to do the same formatting as for sort value
[10630]206 ($metavalue) = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj) unless $self->{'no_metadata_formatting'};
[6942]207 $self->{'list'}->{$doc_OID} = $metavalue;
208 }
[8221]209 my $id = $self->get_number();
210 $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$id");
[6942]211}
[545]212
[287]213
[316]214sub get_classify_info {
[287]215 my $self = shift (@_);
[1086]216 my ($no_thistype) = @_;
217 $no_thistype = 0 unless defined $no_thistype;
[8221]218 my $memberof = &supports_memberof();
[287]219
[316]220 my @list = ();
[10218]221 if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
[6942]222 @list = @{$self->{'list'}};
223 } else {
[287]224 if (keys %{$self->{'list'}}) {
[316]225 @list = sort {$self->{'list'}->{$a}
226 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
[6942]227 }
[287]228 }
[316]229 # organise into classification structure
[1086]230 my %classifyinfo = ('childtype'=>'VList',
[10218]231 'Title'=>$self->{'buttonname'},
[316]232 'contains'=>[]);
[1086]233 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
[8221]234 # always supports memberof
235 $classifyinfo{'supportsmemberof'} = $memberof;
[1086]236
[10253]237 foreach my $OID (@list) {
[7835]238 my $hashref={};
239 # special oid format, if using offsets (from AZCompactList)
240 if ($OID =~ s/\.offset(\d+)$//) {
241 $hashref->{'offset'}=$1;
242 }
243 $hashref->{'OID'}=$OID;
244
245 push (@{$classifyinfo{'contains'}}, $hashref);
[316]246 }
247
248 return \%classifyinfo;
[287]249}
250
[8221]251sub supports_memberof {
252 my $self = shift(@_);
[287]253
[8221]254 return "true";
255}
256
[287]2571;
[6957]258
259
260
261
Note: See TracBrowser for help on using the repository browser.