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

Last change on this file since 14119 was 14112, checked in by sjboddie, 17 years ago

More modifications to support additional collection-level customisations
to be put in gsdl/collect/COLLECTION/custom/COLLECTION. basebuilder.pm,
classify.pm, colcfg.pm, and plugin.pm were modified to allow
collection-specific plugins, classifiers, builders, and buildprocs to
be located in the new locations. These changes should not have any effect
on existing collections.

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