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
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
[1839]29use BasClas;
[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 {
[10253]38 @List::ISA = ('BasClas');
[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
[10218]64 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
65 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
[6968]66
[12891]67 my $self = new BasClas($classifierslist, $inputargs, $hashArgOptLists);
[287]68
[10218]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"});
[6942]73 $meta1 = $meta_list[0];
[10218]74 $self->{'meta_list'} = \@meta_list;
[6942]75 } else {
76 $meta1=undef;
77 @meta_list = undef;
78 }
[287]79
[10218]80 if (!$self->{"buttonname"}) {
81 if (defined ($self->{'metadata'})) {
82 $self->{"buttonname"} = $self->generate_title_from_metadata($self->{'metadata'});
[426]83 } else {
[10218]84 $self->{"buttonname"} = 'List';
[426]85 }
86 }
87
[10218]88 # Further setup
89 # $self->{"sort"} is handled a bit differently - kjdon
[6942]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
[10218]93 # to get no sorting, set $self->{"sort"} = 'nosort'
94 if (!$self->{"sort"}) {
95 if (defined ($self->{"metadata"})) {
96 $self->{"sort"} = undef;
[1839]97 } else {
[10218]98 $self->{"sort"} = "nosort";
[1839]99 }
[10218]100 }
101 if (defined $self->{"sort"} && $self->{"sort"} eq "nosort") {
102 $self->{'list'} = [];
[6942]103 } else {
[10218]104 $self->{'list'} = {};
[1839]105 }
[10218]106
107 # Clean out the unused keys
108 delete $self->{"metadata"}; # Delete this key
[6942]109
[1483]110 return bless $self, $class;
[287]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
[6942]124 # are we sorting the list??
125 my $nosort = 0;
[10218]126 if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
[6942]127 $nosort = 1;
128 }
129
130 my $metavalue;
131 my $metaname;
132 if (defined $self->{'meta_list'}) {
[7835]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
[8729]137
[7835]138 my $mdoffset=$doc_obj->{'mdoffset'} - 1;
[8729]139
140 foreach my $m (@{$self->{'meta_list'}}) {
141 my $values_listref=
142 $doc_obj->get_metadata($topsection, $m);
[10253]143 my $array_size = scalar(@{$values_listref});
[8729]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 }
[7835]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 }
[6942]163 }
[7835]164 # if we haven't found a metavalue, then the doc shouldn't be included
[6942]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
[10218]175 if (defined $self->{'sort'}) {
[6942]176 my $sortmeta;
[10218]177 if ($self->{'sort'} =~ /^filename$/i) {
[545]178 $sortmeta = $doc_obj->get_source_filename();
179 } else {
[10218]180 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sort'});
[10630]181 if (defined $sortmeta && !$self->{'no_metadata_formatting'}) {
[10218]182 $sortmeta = &sorttools::format_metadata_for_sorting($self->{'sort'}, $sortmeta, $doc_obj);
[287]183 }
[545]184 }
185 $sortmeta = "" unless defined $sortmeta;
[6942]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
[10630]190 ($metavalue) = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj) unless $self->{'no_metadata_formatting'};
[6942]191 $self->{'list'}->{$doc_OID} = $metavalue;
192 }
[8221]193 my $id = $self->get_number();
194 $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$id");
[6942]195}
[545]196
[287]197
[316]198sub get_classify_info {
[287]199 my $self = shift (@_);
[1086]200 my ($no_thistype) = @_;
201 $no_thistype = 0 unless defined $no_thistype;
[8221]202 my $memberof = &supports_memberof();
[287]203
[316]204 my @list = ();
[10218]205 if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
[6942]206 @list = @{$self->{'list'}};
207 } else {
[287]208 if (keys %{$self->{'list'}}) {
[316]209 @list = sort {$self->{'list'}->{$a}
210 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
[6942]211 }
[287]212 }
[316]213 # organise into classification structure
[1086]214 my %classifyinfo = ('childtype'=>'VList',
[10218]215 'Title'=>$self->{'buttonname'},
[316]216 'contains'=>[]);
[1086]217 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
[8221]218 # always supports memberof
219 $classifyinfo{'supportsmemberof'} = $memberof;
[1086]220
[10253]221 foreach my $OID (@list) {
[7835]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);
[316]230 }
231
232 return \%classifyinfo;
[287]233}
234
[8221]235sub supports_memberof {
236 my $self = shift(@_);
[287]237
[8221]238 return "true";
239}
240
[287]2411;
[6957]242
243
244
245
Note: See TracBrowser for help on using the repository browser.