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

Last change on this file since 17724 was 17724, checked in by kjdon, 15 years ago

added DBPlug and PPTPlug to plugin name map

  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 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# mapping from old plugin names to new ones for backwards compatibility
36# can remove at sometime in future when we no longer want to support old xxPlug names in the config file
37my $plugin_name_map = {
38 'ArcPlug' => 'ArchivesInfPlugin',
39 'RecPlug' => 'DirectoryPlugin',
40 'TEXTPlug' => 'TextPlugin',
41 'EMAILPlug' => 'EmailPlugin',
42 'SRCPlug' => 'SourceCodePlugin',
43 'NULPlug' => 'NulPlugin',
44 'W3ImgPlug' => 'W3ImagePlugin',
45 'PagedImgPlug' => 'PagedImagePlugin',
46 'METSPlug' => 'GreenstoneMETSPlugin',
47 'PPTPlug' => 'PowerPointPlugin',
48 'DBPlug' => 'DatabasePlugin'
49 };
50
51# global variables
52my $stats = {'num_processed' => 0,
53 'num_blocked' => 0,
54 'num_not_processed' => 0,
55 'num_not_recognised' => 0,
56 'num_archives' => 0
57 };
58
59#globaloptions contains any options that should be passed to all plugins
60my ($verbosity, $outhandle, $failhandle, $globaloptions);
61
62sub get_valid_pluginname {
63 my ($pluginname) = @_;
64 my $valid_name = $pluginname;
65 if (defined $plugin_name_map->{$pluginname}) {
66 $valid_name = $plugin_name_map->{$pluginname};
67 } elsif ($pluginname =~ /Plug$/) {
68 $valid_name =~ s/Plug/Plugin/;
69
70 }
71 return $valid_name;
72}
73sub load_plugin_require
74{
75 my ($pluginname) = @_;
76
77 my @check_list = ();
78
79 # pp_plugname shorthand for 'perllib' 'plugin' '$pluginname.pm'
80 my $pp_plugname
81 = &util::filename_cat('perllib', 'plugins', "${pluginname}.pm");
82 my $collectdir = $ENV{'GSDLCOLLECTDIR'};
83
84 # find the plugin
85 if (defined($ENV{'GSDLCOLLECTION'}))
86 {
87 my $customplugname
88 = &util::filename_cat($collectdir, "custom",$ENV{'GSDLCOLLECTION'},
89 $pp_plugname);
90 push(@check_list,$customplugname);
91 }
92
93 my $colplugname = &util::filename_cat($collectdir, $pp_plugname);
94 push(@check_list,$colplugname);
95
96 if (defined $ENV{'GSDLEXTS'}) {
97
98 my $ext_prefix = &util::filename_cat($ENV{'GSDLHOME'}, "ext");
99
100 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
101 foreach my $e (@extensions) {
102 my $extplugname = &util::filename_cat($ext_prefix, $e, $pp_plugname);
103 push(@check_list,$extplugname);
104
105 }
106 }
107
108
109 my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'}, $pp_plugname);
110 push(@check_list,$mainplugname);
111
112 my $success=0;
113 foreach my $plugname (@check_list) {
114 if (-e $plugname) {
115 require $plugname;
116 $success=1;
117 last;
118 }
119 }
120
121 if (!$success) {
122 &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
123 $pluginname);
124 die "\n";
125 }
126}
127
128sub load_plugin_for_info {
129 my ($pluginname) = shift @_;
130 $pluginname = &get_valid_pluginname($pluginname);
131 load_plugin_require($pluginname);
132
133 # create a plugin object
134 my ($plugobj);
135 my $options = "-gsdlinfo";
136
137 eval ("\$plugobj = new \$pluginname([],[$options])");
138 die "$@" if $@;
139
140 return $plugobj;
141}
142
143sub load_plugins {
144 my ($plugin_list) = shift @_;
145 my $incremental;
146 ($verbosity, $outhandle, $failhandle, $globaloptions, $incremental) = @_; # globals
147 my @plugin_objects = ();
148 $incremental = 0 unless (defined $incremental && $incremental == 1);
149 $verbosity = 2 unless defined $verbosity;
150 $outhandle = 'STDERR' unless defined $outhandle;
151 $failhandle = 'STDERR' unless defined $failhandle;
152
153 my $colplugindir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins");
154 unshift (@INC, $colplugindir);
155
156 map { $_ = "\"$_\""; } @$globaloptions;
157 my $globals = join (",", @$globaloptions);
158
159 foreach my $pluginoptions (@$plugin_list) {
160 my $pluginname = shift @$pluginoptions;
161 next unless defined $pluginname;
162 $pluginname = &get_valid_pluginname($pluginname);
163 load_plugin_require($pluginname);
164
165 # create a plugin object
166 my ($plugobj);
167 map { $_ = "\"$_\""; } @$pluginoptions;
168 my $options = join (",", @$pluginoptions);
169 if ($globals) {
170 if (@$pluginoptions) {
171 $options .= ",";
172 }
173 $options .= "$globals";
174 }
175 $options =~ s/\$/\\\$/g;
176
177 eval ("\$plugobj = new \$pluginname([],[$options])");
178 die "$@" if $@;
179
180 # initialize plugin
181 $plugobj->init($verbosity, $outhandle, $failhandle);
182
183 $plugobj->set_incremental($incremental);
184
185 # add this object to the list
186 push (@plugin_objects, $plugobj);
187 }
188
189 return \@plugin_objects;
190}
191
192
193sub begin {
194 my ($pluginfo, $base_dir, $processor, $maxdocs, $gli) = @_;
195
196 map { $_->{'gli'} = $gli; } @$pluginfo;
197 map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
198}
199
200sub file_block_read {
201 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
202
203
204 $gli = 0 unless defined $gli;
205
206 my $rv = 0;
207 my $glifile = $file;
208
209 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
210
211 # Announce to GLI that we are handling a file
212 print STDERR "<File n='$glifile'>\n" if $gli;
213
214 # the .kill file is a handy (if not very elegant) way of aborting
215 # an import.pl or buildcol.pl process
216 if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
217 gsprintf($outhandle, "{plugin.kill_file}\n");
218 die "\n";
219 }
220
221 foreach my $plugobj (@$pluginfo) {
222
223 $rv = $plugobj->file_block_read($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli);
224 #last if (defined $rv && $rv==1); # stop this file once we have found something to 'process' it
225 }
226
227}
228
229
230sub metadata_read {
231 my ($pluginfo, $base_dir, $file, $block_hash, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli, $aux) = @_;
232
233 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
234 $gli = 0 unless defined $gli;
235
236 my $rv = 0;
237 my $glifile = $file;
238
239 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
240
241 # Announce to GLI that we are handling a file
242 print STDERR "<File n='$glifile'>\n" if $gli;
243
244 # the .kill file is a handy (if not very elegant) way of aborting
245 # an import.pl or buildcol.pl process
246 if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
247 gsprintf($outhandle, "{plugin.kill_file}\n");
248 die "\n";
249 }
250
251 my $had_error = 0;
252 # pass this file by each of the plugins in turn until one
253 # is found which will process it
254 # read must return:
255 # undef - could not recognise
256 # -1 - tried but error
257 # 0 - blocked
258 # anything else for successful processing
259
260 foreach my $plugobj (@$pluginfo) {
261
262 $rv = $plugobj->metadata_read($pluginfo, $base_dir, $file, $block_hash,
263 $extrametakeys, $extrametadata, $processor, $maxdocs, $gli, $aux);
264
265 if (defined $rv) {
266 if ($rv == -1) {
267 # an error has occurred
268 $had_error = 1;
269 print STDERR "<ProcessingError n='$glifile'>\n" if $gli;
270 } else {
271 return $rv;
272 }
273 } # else undefined - was not recognised by the plugin
274 }
275
276 return 0;
277}
278
279sub read {
280 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli, $aux) = @_;
281
282 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
283 $total_count = 0 unless defined $total_count && $total_count =~ /\d/;
284 $gli = 0 unless defined $gli;
285
286 my $rv = 0;
287 my $glifile = $file;
288
289 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
290
291 # Announce to GLI that we are handling a file
292 print STDERR "<File n='$glifile'>\n" if $gli;
293
294 # the .kill file is a handy (if not very elegant) way of aborting
295 # an import.pl or buildcol.pl process
296 if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
297 gsprintf($outhandle, "{plugin.kill_file}\n");
298 die "\n";
299 }
300
301 my $had_error = 0;
302 # pass this file by each of the plugins in turn until one
303 # is found which will process it
304 # read must return:
305 # undef - could not recognise
306 # -1 - tried but error
307 # 0 - blocked
308 # anything else for successful processing
309
310 foreach my $plugobj (@$pluginfo) {
311
312 $rv = $plugobj->read($pluginfo, $base_dir, $file,
313 $block_hash, $metadata, $processor, $maxdocs,
314 $total_count, $gli, $aux);
315
316 if (defined $rv) {
317 if ($rv == -1) {
318 # an error has occurred
319 $had_error = 1;
320 } else {
321 return $rv;
322 }
323 } # else undefined - was not recognised by the plugin
324 }
325
326 if ($had_error) {
327 # was recognised but couldn't be processed
328 if ($verbosity >= 2) {
329 gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
330 }
331 # tell the GLI that it was not processed
332 print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
333
334 gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
335 $stats->{'num_not_processed'} ++;
336 } else {
337 # was not recognised
338 if ($verbosity >= 2) {
339 gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
340 }
341 # tell the GLI that it was not processed
342 print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
343
344 gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
345 $stats->{'num_not_recognised'} ++;
346 }
347 return 0;
348}
349
350# write out some general stats that the plugins have compiled - note that
351# the buildcol.pl process doesn't currently call this process so the stats
352# are only output after import.pl -
353sub write_stats {
354 my ($pluginfo, $statshandle, $faillog, $gli) = @_;
355
356 $gli = 0 unless defined $gli;
357
358 foreach my $plugobj (@$pluginfo) {
359 $plugobj->compile_stats($stats);
360 }
361
362 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
363 $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
364
365 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;
366
367 if ($total == 1) {
368 gsprintf($statshandle, "* {plugin.one_considered}\n");
369 } else {
370 gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
371 }
372 if ($stats->{'num_archives'}) {
373 if ($stats->{'num_archives'} == 1) {
374 gsprintf($statshandle, " ({plugin.including_archive})\n");
375 }
376 else {
377 gsprintf($statshandle, " ({plugin.including_archives})\n",
378 $stats->{'num_archives'});
379 }
380 }
381 if ($stats->{'num_processed'} == 1) {
382 gsprintf($statshandle, "* {plugin.one_included}\n");
383 } else {
384 gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
385 }
386 if ($stats->{'num_not_recognised'}) {
387 if ($stats->{'num_not_recognised'} == 1) {
388 gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
389 } else {
390 gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
391 $stats->{'num_not_recognised'});
392 }
393
394 }
395 if ($stats->{'num_not_processed'}) {
396 if ($stats->{'num_not_processed'} == 1) {
397 gsprintf($statshandle, "* {plugin.one_rejected}\n");
398 } else {
399 gsprintf($statshandle, "* {plugin.n_rejected}\n",
400 $stats->{'num_not_processed'});
401 }
402 }
403 if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
404 gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
405 }
406}
407
408sub end {
409 my ($pluginfo, $processor) = @_;
410 map { $_->end($processor); } @$pluginfo;
411}
412
413sub deinit {
414 my ($pluginfo, $processor) = @_;
415
416
417 map { $_->deinit($processor); } @$pluginfo;
418}
419
4201;
Note: See TracBrowser for help on using the repository browser.