source: main/trunk/greenstone2/perllib/plugin.pm@ 21290

Last change on this file since 21290 was 21290, checked in by kjdon, 11 years ago

extension handling extended to include gs3 extensions, added removeold method

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