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

Last change on this file since 1839 was 1839, checked in by paynter, 23 years ago

Updated classifiers to use the parsearg library instead of ad-hoc
"x=y" style parsing. (Backwards compatability maintained through
a quick hack to the load_classifier function in classfy.pm.)

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
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# options are:
28# metadata=Metaname -- (optional) all documents with Metaname metadata
29# will be included in list. if not included all documents
30# will be included in list.
31# sort=Meta -- (optional) sort documents in list alphabetically by
32# Meta. by default it will sort by Metaname, if neither
33# are set documents will be in build (random) order.
34# Meta may be Filename to sort by original filename or
35# nosort to force not to sort
36# title=Title -- (optional) the title field for this classification.
37# if not included title field will be Metaname.
38# if metadata is also not included title will be 'List'
39
40use BasClas;
41package List;
42
43use sorttools;
44
45sub BEGIN {
46 @ISA = ('BasClas');
47}
48
49sub print_usage {
50 print STDERR "
51 usage: classify List [options]
52 options:
53
54 -metadata X Metadata field used for classification,
55 list will be sorted by this element.
56
57 -title X (optional) Title field for this classification.
58 if not included title field will be Metaname.
59
60 -sort X (optional) Sort documents in list by this metadata field.
61 By default it will sort by Metaname, or (if this is not
62 set) in build (random) order.
63";
64}
65
66sub new {
67 my $class = shift (@_);
68 my $self = new BasClas($class, @_);
69
70 my ($metaname, $title, $sortname, $list);
71
72 if (!parsargv::parse(\@_,
73 q^metadata/.*/^, \$metaname,
74 q^title/.*/^, \$title,
75 q^sort/.*/^, \$sortname,
76 "allow_extra_options")) {
77
78 print STDERR "\nIncorrect options passed to $class, check your collect.cfg file\n";
79 &print_usage();
80 die "\n";
81 }
82
83 if (!$title) {
84 if ($metaname) {
85 $title = $metaname;
86 } else {
87 $title = 'List';
88 }
89 }
90
91 if (!$sortname) {
92 if ($metaname) {
93 $sortname = $metaname;
94 } else {
95 $sortname = undef;
96 }
97 }
98
99 if ($metaname) {
100 $list = {};
101 } else {
102 $list = [];
103 $metaname = undef;
104 }
105
106 $self->{'list'} = $list;
107 $self->{'metaname'} = $metaname;
108 $self->{'title'} = $title;
109 $self->{'sortname'} = $sortname;
110
111 return bless $self, $class;
112}
113
114sub init {
115 my $self = shift (@_);
116
117 if (defined $self->{'sortname'}) {
118 $self->{'list'} = {};
119 } else {
120 $self->{'list'} = [];
121 }
122}
123
124sub classify {
125 my $self = shift (@_);
126 my ($doc_obj) = @_;
127
128 my $doc_OID = $doc_obj->get_OID();
129
130 my $sortmeta = "";
131 if (defined $self->{'sortname'}) {
132 if ($self->{'sortname'} =~ /^filename$/i) {
133 $sortmeta = $doc_obj->get_source_filename();
134 } else {
135 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
136 $self->{'sortname'});
137 if (defined $sortmeta) {
138 if ($self->{'sortname'} eq "Creator") {
139 &sorttools::format_string_name_english (\$sortmeta);
140 } else {
141 &sorttools::format_string_english (\$sortmeta);
142 }
143 }
144 }
145 $sortmeta = "" unless defined $sortmeta;
146
147 if (defined $self->{'metaname'}) {
148 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
149 $self->{'metaname'});
150 if (defined $metavalue) {
151 if (defined $self->{'list'}->{$doc_OID}) {
152 my $outhandle = $self->{'outhandle'};
153 print $outhandle "WARNING: List::classify called multiple times for $doc_OID\n";
154 }
155 $self->{'list'}->{$doc_OID} = $sortmeta;
156 }
157 } else {
158 if (defined $self->{'list'}->{$doc_OID}) {
159 my $outhandle = $self->{'outhandle'};
160 print $outhandle "WARNING: List::classify called multiple times for $doc_OID\n";
161 }
162 $self->{'list'}->{$doc_OID} = $sortmeta;
163 }
164 } else {
165 if (defined $self->{'metaname'}) {
166 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
167 $self->{'metaname'});
168 if (defined $metavalue) {
169 push (@{$self->{'list'}}, $doc_OID);
170 }
171 } else {
172 push (@{$self->{'list'}}, $doc_OID);
173 }
174 }
175}
176
177sub get_classify_info {
178 my $self = shift (@_);
179 my ($no_thistype) = @_;
180 $no_thistype = 0 unless defined $no_thistype;
181
182 my @list = ();
183 if (defined $self->{'sortname'}) {
184 if (keys %{$self->{'list'}}) {
185 @list = sort {$self->{'list'}->{$a}
186 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
187 }
188 } else {
189 @list = @{$self->{'list'}};
190 }
191
192 # organise into classification structure
193 my %classifyinfo = ('childtype'=>'VList',
194 'Title'=>$self->{'title'},
195 'contains'=>[]);
196 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
197
198 foreach $OID (@list) {
199 push (@{$classifyinfo{'contains'}}, {'OID'=>$OID});
200 }
201
202 return \%classifyinfo;
203}
204
205
2061;
Note: See TracBrowser for help on using the repository browser.