source: main/trunk/greenstone2/perllib/classify/SimpleList.pm

Last change on this file was 33902, checked in by kjdon, 4 years ago

pass in new casefold and accentfold options to format_metadata_for_sorting

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
RevLine 
[537]1###########################################################################
2#
[18565]3# SimpleList.pm --
[537]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
[18565]27# to see the options, run "perl -S classinfo.pl SimpleList"
[287]28
[17209]29use BaseClassifier;
[18565]30package SimpleList;
[287]31
[10253]32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
[385]35use sorttools;
36
[1483]37sub BEGIN {
[18565]38 @SimpleList::ISA = ('BaseClassifier');
[1483]39}
40
[3540]41my $arguments =
[4759]42 [ { 'name' => "metadata",
[19234]43 'desc' => "{SimpleList.metadata}",
[3540]44 'type' => "metadata",
[6942]45 'reqd' => "no" },
[4759]46 { 'name' => "sort",
[19234]47 'desc' => "{SimpleList.sort}",
[11665]48 'type' => "metadata",
[4759]49 'reqd' => "no" } ];
[3540]50
[18565]51my $options = { 'name' => "SimpleList",
52 'desc' => "{SimpleList.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"}) {
[20427]77 # strip off ex.
78 $self->{"metadata"} = $self->strip_ex_from_metadata($self->{'metadata'});
[20008]79 @meta_list = split(/,|;/, $self->{"metadata"});
[6942]80 $meta1 = $meta_list[0];
[10218]81 $self->{'meta_list'} = \@meta_list;
[6942]82 } else {
83 $meta1=undef;
84 @meta_list = undef;
85 }
[287]86
[10218]87 if (!$self->{"buttonname"}) {
88 if (defined ($self->{'metadata'})) {
89 $self->{"buttonname"} = $self->generate_title_from_metadata($self->{'metadata'});
[426]90 } else {
[10218]91 $self->{"buttonname"} = 'List';
[426]92 }
93 }
94
[10218]95 # Further setup
96 # $self->{"sort"} is handled a bit differently - kjdon
[6942]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
[10218]100 # to get no sorting, set $self->{"sort"} = 'nosort'
101 if (!$self->{"sort"}) {
102 if (defined ($self->{"metadata"})) {
103 $self->{"sort"} = undef;
[1839]104 } else {
[10218]105 $self->{"sort"} = "nosort";
[1839]106 }
[10218]107 }
[20427]108 if (defined $self->{'sort'}) {
109 $self->{'sort'} = $self->strip_ex_from_metadata($self->{'sort'});
110 }
[10218]111 if (defined $self->{"sort"} && $self->{"sort"} eq "nosort") {
112 $self->{'list'} = [];
[6942]113 } else {
[10218]114 $self->{'list'} = {};
[1839]115 }
[10218]116
117 # Clean out the unused keys
118 delete $self->{"metadata"}; # Delete this key
[6942]119
[1483]120 return bless $self, $class;
[287]121}
122
123sub init {
124 my $self = shift (@_);
125
126}
127
128sub classify {
129 my $self = shift (@_);
[23116]130 my ($doc_obj) = @_;
[287]131
132 my $doc_OID = $doc_obj->get_OID();
133
[18455]134
[6942]135 # are we sorting the list??
136 my $nosort = 0;
[10218]137 if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
[6942]138 $nosort = 1;
139 }
140
141 my $metavalue;
142 my $metaname;
143 if (defined $self->{'meta_list'}) {
[7835]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
[8729]148
[7835]149 my $mdoffset=$doc_obj->{'mdoffset'} - 1;
[8729]150
151 foreach my $m (@{$self->{'meta_list'}}) {
152 my $values_listref=
153 $doc_obj->get_metadata($topsection, $m);
[10253]154 my $array_size = scalar(@{$values_listref});
[8729]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 }
[7835]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 }
[6942]174 }
[7835]175 # if we haven't found a metavalue, then the doc shouldn't be included
[6942]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
[10218]186 if (defined $self->{'sort'}) {
[6942]187 my $sortmeta;
[10218]188 if ($self->{'sort'} =~ /^filename$/i) {
[545]189 $sortmeta = $doc_obj->get_source_filename();
190 } else {
[10218]191 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sort'});
[10630]192 if (defined $sortmeta && !$self->{'no_metadata_formatting'}) {
[33902]193 $sortmeta = &sorttools::format_metadata_for_sorting($self->{'sort'}, $sortmeta, $doc_obj, $self->{'casefold_metadata_for_sorting'}, $self->{'accentfold_metadata_for_sorting'});
[287]194 }
[545]195 }
196 $sortmeta = "" unless defined $sortmeta;
[6942]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
[33902]201 ($metavalue) = &sorttools::format_metadata_for_sorting($metaname, $metavalue, $doc_obj, $self->{'casefold_metadata_for_sorting'}, $self->{'accentfold_metadata_for_sorting'}) unless $self->{'no_metadata_formatting'};
[6942]202 $self->{'list'}->{$doc_OID} = $metavalue;
203 }
[8221]204 my $id = $self->get_number();
205 $doc_obj->add_metadata($doc_obj->get_top_section(), "memberof", "CL$id");
[6942]206}
[545]207
[287]208
[316]209sub get_classify_info {
[287]210 my $self = shift (@_);
[18569]211 my ($gli, $no_thistype) = @_;
[1086]212 $no_thistype = 0 unless defined $no_thistype;
[8221]213 my $memberof = &supports_memberof();
[287]214
[316]215 my @list = ();
[10218]216 if (defined $self->{'sort'} && $self->{'sort'} eq "nosort") {
[6942]217 @list = @{$self->{'list'}};
218 } else {
[287]219 if (keys %{$self->{'list'}}) {
[316]220 @list = sort {$self->{'list'}->{$a}
221 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
[6942]222 }
[287]223 }
[316]224 # organise into classification structure
[1086]225 my %classifyinfo = ('childtype'=>'VList',
[10218]226 'Title'=>$self->{'buttonname'},
[316]227 'contains'=>[]);
[1086]228 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
[8221]229 # always supports memberof
230 $classifyinfo{'supportsmemberof'} = $memberof;
[1086]231
[10253]232 foreach my $OID (@list) {
[7835]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);
[316]241 }
242
243 return \%classifyinfo;
[287]244}
245
[8221]246sub supports_memberof {
247 my $self = shift(@_);
[287]248
[8221]249 return "true";
250}
251
[287]2521;
[6957]253
254
255
256
Note: See TracBrowser for help on using the repository browser.