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

Last change on this file since 5645 was 5645, checked in by mdewsnip, 21 years ago

Moved classifier descriptions into the resource bundle (perllib/strings.rb).

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 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# buttonname=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
40# 12/05/02 Added usage datastructure - John Thompson
41
42use BasClas;
43package List;
44
45use sorttools;
46
47sub BEGIN {
48 @ISA = ('BasClas');
49}
50
51my $arguments =
52 [ { 'name' => "metadata",
53 'desc' => "{List.metadata}",
54 'type' => "metadata",
55 'reqd' => "yes" },
56 { 'name' => "buttonname",
57 'desc' => "{List.buttonname}",
58 'type' => "string",
59 'deft' => "Metadata element specified with -metadata",
60 'reqd' => "no" },
61 { 'name' => "sort",
62 'desc' => "{List.sort}",
63 'type' => "string",
64 'deft' => "Metadata field specified with -metadata",
65 'reqd' => "no" } ];
66
67my $options = { 'name' => "List",
68 'desc' => "{List.desc}",
69 'inherits' => "Yes",
70 'args' => $arguments };
71
72# sub print_usage {
73# print STDERR "
74# usage: classify List [options]
75# options:
76
77# -metadata X Metadata field used for classification,
78# list will be sorted by this element.
79
80# -buttonname X (optional) Title field for this classification.
81# if not included title field will be Metaname.
82
83# -sort X (optional) Sort documents in list by this metadata field.
84# By default it will sort by Metaname, or (if this is not
85# set) in build (random) order.
86# ";
87# }
88
89sub new {
90 my $class = shift (@_);
91 my $self = new BasClas($class, @_);
92
93 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
94 my $option_list = $self->{'option_list'};
95 push( @{$option_list}, $options );
96
97 my ($metaname, $title, $sortname, $list);
98
99 if (!parsargv::parse(\@_,
100 q^metadata/.*/^, \$metaname,
101 q^buttonname/.*/^, \$title,
102 q^sort/.*/^, \$sortname,
103 "allow_extra_options")) {
104
105 print STDERR "\nIncorrect options passed to $class, check your collect.cfg file\n";
106 $self->print_txt_usage(""); # Use default resource bundle
107 die "\n";
108 }
109
110 if (!$title) {
111 if ($metaname) {
112 $title = $metaname;
113 } else {
114 $title = 'List';
115 }
116 }
117
118 if (!$sortname) {
119 if ($metaname) {
120 $sortname = $metaname;
121 } else {
122 $sortname = undef;
123 }
124 }
125
126 if ($metaname) {
127 $list = {};
128 } else {
129 $list = [];
130 $metaname = undef;
131 }
132
133 $self->{'list'} = $list;
134 $self->{'metaname'} = $metaname;
135 $self->{'title'} = $title;
136 $self->{'sortname'} = $sortname;
137
138 return bless $self, $class;
139}
140
141sub init {
142 my $self = shift (@_);
143
144 if (defined $self->{'sortname'}) {
145 $self->{'list'} = {};
146 } else {
147 $self->{'list'} = [];
148 }
149}
150
151sub classify {
152 my $self = shift (@_);
153 my ($doc_obj) = @_;
154
155 my $doc_OID = $doc_obj->get_OID();
156
157 my $sortmeta = "";
158 if (defined $self->{'sortname'}) {
159 if ($self->{'sortname'} =~ /^filename$/i) {
160 $sortmeta = $doc_obj->get_source_filename();
161 } else {
162 $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
163 $self->{'sortname'});
164 if (defined $sortmeta) {
165
166 my $lang = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Language');
167 $lang = 'en' unless defined $lang;
168 if ($lang eq 'en') {
169 if ($self->{'sortname'} eq "Creator") {
170 &sorttools::format_string_name_english (\$sortmeta);
171 } else {
172 &sorttools::format_string_english (\$sortmeta);
173 }
174 }
175 }
176 }
177 $sortmeta = "" unless defined $sortmeta;
178
179 if (defined $self->{'metaname'}) {
180 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
181 $self->{'metaname'});
182 if (defined $metavalue) {
183 if (defined $self->{'list'}->{$doc_OID}) {
184 my $outhandle = $self->{'outhandle'};
185 print $outhandle "WARNING: List::classify called multiple times for $doc_OID\n";
186 }
187 $self->{'list'}->{$doc_OID} = $sortmeta;
188 }
189 } else {
190 if (defined $self->{'list'}->{$doc_OID}) {
191 my $outhandle = $self->{'outhandle'};
192 print $outhandle "WARNING: List::classify called multiple times for $doc_OID\n";
193 }
194 $self->{'list'}->{$doc_OID} = $sortmeta;
195 }
196 } else {
197 if (defined $self->{'metaname'}) {
198 my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
199 $self->{'metaname'});
200 if (defined $metavalue) {
201 push (@{$self->{'list'}}, $doc_OID);
202 }
203 } else {
204 push (@{$self->{'list'}}, $doc_OID);
205 }
206 }
207}
208
209sub get_classify_info {
210 my $self = shift (@_);
211 my ($no_thistype) = @_;
212 $no_thistype = 0 unless defined $no_thistype;
213
214 my @list = ();
215 if (defined $self->{'sortname'}) {
216 if (keys %{$self->{'list'}}) {
217 @list = sort {$self->{'list'}->{$a}
218 cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
219 }
220 } else {
221 @list = @{$self->{'list'}};
222 }
223
224 # organise into classification structure
225 print STDERR "List::get_classify_info::title = " . $self->{'title'} . "\n";
226 my %classifyinfo = ('childtype'=>'VList',
227 'Title'=>$self->{'title'},
228 'contains'=>[]);
229 $classifyinfo{'thistype'} = 'Invisible' unless $no_thistype;
230
231 foreach $OID (@list) {
232 push (@{$classifyinfo{'contains'}}, {'OID'=>$OID});
233 }
234
235 return \%classifyinfo;
236}
237
238
2391;
Note: See TracBrowser for help on using the repository browser.