source: trunk/gsdl/perllib/plugin.pm@ 11089

Last change on this file since 11089 was 10579, checked in by kjdon, 19 years ago

copied classify.pm and BasClas.pm, added -gsdlinfo flag - if this is present (it gets set by pluginfo.pl) then don't bother parsing the args - just set up the arg data structures

  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1###########################################################################
2#
3# plugin.pm -- functions to handle using plugins
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
26package plugin;
27
28use strict; # to pick up typos and undeclared variables...
29no strict 'refs'; # ...but allow filehandles to be variables and vice versa
30no strict 'subs';
31
32require util;
33use gsprintf 'gsprintf';
34
35# global variables
36my $stats = {'num_processed' => 0,
37 'num_blocked' => 0,
38 'num_not_processed' => 0,
39 'num_not_recognised' => 0,
40 'num_archives' => 0
41 };
42
43#globaloptions contains any options that should be passed to all plugins
44my ($verbosity, $outhandle, $failhandle, $globaloptions);
45
46sub load_plugin_for_info {
47 my ($pluginname) = shift @_;
48
49 # find the plugin
50 my $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins",
51 "${pluginname}.pm");
52 my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/plugins",
53 "${pluginname}.pm");
54 if (-e $colplugname) { require $colplugname; }
55 elsif (-e $mainplugname) { require $mainplugname; }
56 else {
57 &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
58 $pluginname);
59 die "\n";
60 }
61
62 # create a plugin object
63 my ($plugobj);
64 my $options = "-gsdlinfo";
65
66 eval ("\$plugobj = new \$pluginname([],[$options])");
67 die "$@" if $@;
68
69 return $plugobj;
70}
71
72sub load_plugins {
73 my ($plugin_list) = shift @_;
74 my $keepold;
75 ($verbosity, $outhandle, $failhandle, $globaloptions, $keepold) = @_; # globals
76 my @plugin_objects = ();
77 $keepold = 0 unless (defined $keepold && $keepold == 1);
78 $verbosity = 2 unless defined $verbosity;
79 $outhandle = 'STDERR' unless defined $outhandle;
80 $failhandle = 'STDERR' unless defined $failhandle;
81
82 map { $_ = "\"$_\""; } @$globaloptions;
83 my $globals = join (",", @$globaloptions);
84
85 foreach my $pluginoptions (@$plugin_list) {
86 my $pluginname = shift @$pluginoptions;
87 next unless defined $pluginname;
88
89 # find the plugin
90 my $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins",
91 "${pluginname}.pm");
92 my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/plugins",
93 "${pluginname}.pm");
94 if (-e $colplugname) { require $colplugname; }
95 elsif (-e $mainplugname) { require $mainplugname; }
96 else {
97 gsprintf($outhandle, "{plugin.could_not_find_plugin}\n",
98 $pluginname);
99 die "\n";
100 }
101
102 # create a plugin object
103 my ($plugobj);
104 map { $_ = "\"$_\""; } @$pluginoptions;
105 my $options = join (",", @$pluginoptions);
106 if ($globals) {
107 if (@$pluginoptions) {
108 $options .= ",";
109 }
110 $options .= "$globals";
111 }
112 $options =~ s/\$/\\\$/g;
113
114 eval ("\$plugobj = new \$pluginname([],[$options])");
115 die "$@" if $@;
116
117 # initialize plugin
118 $plugobj->init($verbosity, $outhandle, $failhandle);
119
120 $plugobj->set_keepold($keepold);
121
122 # add this object to the list
123 push (@plugin_objects, $plugobj);
124 }
125
126 return \@plugin_objects;
127}
128
129
130sub begin {
131 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
132
133
134 map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
135}
136
137
138sub metadata_read {
139 my ($pluginfo, $base_dir, $file, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli, $aux) = @_;
140
141 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
142 $gli = 0 unless defined $gli;
143
144 my $rv = 0;
145 my $glifile = $file;
146
147 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
148
149 # Announce to GLI that we are handling a file
150 print STDERR "<File n='$glifile'>\n" if $gli;
151
152 # the .kill file is a handy (if not very elegant) way of aborting
153 # an import.pl or buildcol.pl process
154 if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
155 gsprintf($outhandle, "{plugin.kill_file}\n");
156 die "\n";
157 }
158
159 my $had_error = 0;
160 # pass this file by each of the plugins in turn until one
161 # is found which will process it
162 # read must return:
163 # undef - could not recognise
164 # -1 - tried but error
165 # 0 - blocked
166 # anything else for successful processing
167
168 foreach my $plugobj (@$pluginfo) {
169
170 $rv = $plugobj->metadata_read($pluginfo, $base_dir, $file,
171 $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli, $aux);
172
173 if (defined $rv) {
174 if ($rv == -1) {
175 # an error has occurred
176 $had_error = 1;
177 print STDERR "<ProcessingError n='$glifile'>\n" if $gli;
178 } else {
179 return $rv;
180 }
181 } # else undefined - was not recognised by the plugin
182 }
183
184 return 0;
185}
186
187sub read {
188 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli, $aux) = @_;
189
190 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
191 $total_count = 0 unless defined $total_count && $total_count =~ /\d/;
192 $gli = 0 unless defined $gli;
193
194 my $rv = 0;
195 my $glifile = $file;
196
197 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
198
199 # Announce to GLI that we are handling a file
200 print STDERR "<File n='$glifile'>\n" if $gli;
201
202 # the .kill file is a handy (if not very elegant) way of aborting
203 # an import.pl or buildcol.pl process
204 if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
205 gsprintf($outhandle, "{plugin.kill_file}\n");
206 die "\n";
207 }
208
209 my $had_error = 0;
210 # pass this file by each of the plugins in turn until one
211 # is found which will process it
212 # read must return:
213 # undef - could not recognise
214 # -1 - tried but error
215 # 0 - blocked
216 # anything else for successful processing
217
218 foreach my $plugobj (@$pluginfo) {
219
220 $rv = $plugobj->read($pluginfo, $base_dir, $file,
221 $metadata, $processor, $maxdocs, $total_count, $gli, $aux);
222
223 if (defined $rv) {
224 if ($rv == -1) {
225 # an error has occurred
226 $had_error = 1;
227 } else {
228 return $rv;
229 }
230 } # else undefined - was not recognised by the plugin
231 }
232
233 if ($had_error) {
234 # was recognised but couldn't be processed
235 if ($verbosity >= 2) {
236 gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
237 }
238 # tell the GLI that it was not processed
239 print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
240
241 $file =~ s/.*?([^\\\/]+)$/$1/;
242 gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
243 $stats->{'num_not_processed'} ++;
244 } else {
245 # was not recognised
246 if ($verbosity >= 2) {
247 gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
248 }
249 # tell the GLI that it was not processed
250 print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
251
252 $file =~ s/.*?([^\\\/]+)$/$1/;
253 gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
254 $stats->{'num_not_recognised'} ++;
255 }
256 return 0;
257}
258
259# write out some general stats that the plugins have compiled - note that
260# the buildcol.pl process doesn't currently call this process so the stats
261# are only output after import.pl -
262sub write_stats {
263 my ($pluginfo, $statshandle, $faillog, $gli) = @_;
264
265 $gli = 0 unless defined $gli;
266
267 foreach my $plugobj (@$pluginfo) {
268 $plugobj->compile_stats($stats);
269 }
270
271 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
272 $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
273
274 print STDERR "<ImportComplete considered='$total' processed='$stats->{'num_processed'}' blocked='$stats->{'num_blocked'}' ignored='$stats->{'num_not_recognised'}' failed='$stats->{'num_not_processed'}'>\n" if $gli;
275
276 if ($total == 1) {
277 gsprintf($statshandle, "* {plugin.one_considered}\n");
278 } else {
279 gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
280 }
281 if ($stats->{'num_archives'}) {
282 if ($stats->{'num_archives'} == 1) {
283 gsprintf($statshandle, " ({plugin.including_archive})\n");
284 }
285 else {
286 gsprintf($statshandle, " ({plugin.including_archives})\n",
287 $stats->{'num_archives'});
288 }
289 }
290 if ($stats->{'num_processed'} == 1) {
291 gsprintf($statshandle, "* {plugin.one_included}\n");
292 } else {
293 gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
294 }
295 if ($stats->{'num_not_recognised'}) {
296 if ($stats->{'num_not_recognised'} == 1) {
297 gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
298 } else {
299 gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
300 $stats->{'num_not_recognised'});
301 }
302
303 }
304 if ($stats->{'num_not_processed'}) {
305 if ($stats->{'num_not_processed'} == 1) {
306 gsprintf($statshandle, "* {plugin.one_rejected}\n");
307 } else {
308 gsprintf($statshandle, "* {plugin.n_rejected}\n",
309 $stats->{'num_not_processed'});
310 }
311 }
312 if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
313 gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
314 }
315}
316
317sub end {
318 my ($pluginfo, $processor) = @_;
319 map { $_->end($processor); } @$pluginfo;
320}
321
322sub deinit {
323 my ($pluginfo, $processor) = @_;
324
325
326 map { $_->deinit($processor); } @$pluginfo;
327}
328
3291;
Note: See TracBrowser for help on using the repository browser.