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

Last change on this file since 21290 was 21290, checked in by kjdon, 14 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
RevLine 
[537]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###########################################################################
[4]25
26package plugin;
27
[7829]28use strict; # to pick up typos and undeclared variables...
29no strict 'refs'; # ...but allow filehandles to be variables and vice versa
[10579]30no strict 'subs';
[7829]31
[134]32require util;
[7829]33use gsprintf 'gsprintf';
[4]34
[15873]35# mapping from old plugin names to new ones for backwards compatibility
[17032]36# can remove at sometime in future when we no longer want to support old xxPlug names in the config file
[15873]37my $plugin_name_map = {
[17746]38 'GAPlug' => 'GreenstoneXMLPlugin',
[15873]39 'ArcPlug' => 'ArchivesInfPlugin',
40 'RecPlug' => 'DirectoryPlugin',
41 'TEXTPlug' => 'TextPlugin',
[17731]42 'XMLPlug' => 'ReadXMLFile',
[15873]43 'EMAILPlug' => 'EmailPlugin',
44 'SRCPlug' => 'SourceCodePlugin',
45 'NULPlug' => 'NulPlugin',
[17731]46 'W3ImgPlug' => 'HTMLImagePlugin',
[17030]47 'PagedImgPlug' => 'PagedImagePlugin',
[17724]48 'METSPlug' => 'GreenstoneMETSPlugin',
49 'PPTPlug' => 'PowerPointPlugin',
[17731]50 'PSPlug' => 'PostScriptPlugin',
[17724]51 'DBPlug' => 'DatabasePlugin'
[15873]52 };
53
[7829]54# global variables
[2785]55my $stats = {'num_processed' => 0,
56 'num_blocked' => 0,
57 'num_not_processed' => 0,
[7363]58 'num_not_recognised' => 0,
[2785]59 'num_archives' => 0
60 };
61
[7829]62#globaloptions contains any options that should be passed to all plugins
63my ($verbosity, $outhandle, $failhandle, $globaloptions);
[5682]64
[15873]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}
[21290]76
[14933]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
[10579]88 # find the plugin
[14239]89 if (defined($ENV{'GSDLCOLLECTION'}))
90 {
[14933]91 my $customplugname
92 = &util::filename_cat($collectdir, "custom",$ENV{'GSDLCOLLECTION'},
93 $pp_plugname);
94 push(@check_list,$customplugname);
[14239]95 }
[14933]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 }
[21290]111 if (defined $ENV{'GSDL3EXTS'}) {
[14933]112
[21290]113 my $ext_prefix = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "ext");
[14933]114
[21290]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
[14933]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) {
[10579]137 &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
[14933]138 $pluginname);
[10579]139 die "\n";
140 }
[14933]141}
[10579]142
[14933]143sub load_plugin_for_info {
144 my ($pluginname) = shift @_;
[15873]145 $pluginname = &get_valid_pluginname($pluginname);
[14933]146 load_plugin_require($pluginname);
147
[10579]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
[4]158sub load_plugins {
[1431]159 my ($plugin_list) = shift @_;
[20578]160 my $incremental_mode;
161 ($verbosity, $outhandle, $failhandle, $globaloptions, $incremental_mode) = @_; # globals
[4]162 my @plugin_objects = ();
[1243]163 $verbosity = 2 unless defined $verbosity;
[7829]164 $outhandle = 'STDERR' unless defined $outhandle;
165 $failhandle = 'STDERR' unless defined $failhandle;
[1243]166
[13933]167 my $colplugindir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins");
168 unshift (@INC, $colplugindir);
169
[6584]170 map { $_ = "\"$_\""; } @$globaloptions;
171 my $globals = join (",", @$globaloptions);
172
[7829]173 foreach my $pluginoptions (@$plugin_list) {
[809]174 my $pluginname = shift @$pluginoptions;
175 next unless defined $pluginname;
[15873]176 $pluginname = &get_valid_pluginname($pluginname);
[14933]177 load_plugin_require($pluginname);
[4]178
179 # create a plugin object
180 my ($plugobj);
[809]181 map { $_ = "\"$_\""; } @$pluginoptions;
182 my $options = join (",", @$pluginoptions);
[6584]183 if ($globals) {
184 if (@$pluginoptions) {
185 $options .= ",";
186 }
187 $options .= "$globals";
188 }
[20613]189 # need to escape backslash before putting in to the eval
190 $options =~ s/\\/\\\\/g;
[1244]191 $options =~ s/\$/\\\$/g;
[10218]192 eval ("\$plugobj = new \$pluginname([],[$options])");
[4]193 die "$@" if $@;
[809]194
[1243]195 # initialize plugin
[2785]196 $plugobj->init($verbosity, $outhandle, $failhandle);
[10478]197
[20578]198 $plugobj->set_incremental($incremental_mode);
[1243]199
[4]200 # add this object to the list
201 push (@plugin_objects, $plugobj);
202 }
203
204 return \@plugin_objects;
205}
206
[835]207
208sub begin {
[11333]209 my ($pluginfo, $base_dir, $processor, $maxdocs, $gli) = @_;
[835]210
[11333]211 map { $_->{'gli'} = $gli; } @$pluginfo;
[835]212 map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
213}
214
[21290]215 sub removeold {
216 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
217
218 map { $_->removeold($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
219}
220
221
[16381]222sub file_block_read {
223 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
[10155]224
[16381]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
[8515]252sub metadata_read {
[19497]253 my ($pluginfo, $base_dir, $file, $block_hash,
254 $extrametakeys, $extrametadata, $extrametafile,
255 $processor, $maxdocs, $gli, $aux) = @_;
[8515]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
[16381]286 $rv = $plugobj->metadata_read($pluginfo, $base_dir, $file, $block_hash,
[19497]287 $extrametakeys, $extrametadata, $extrametafile,
288 $processor, $maxdocs, $gli, $aux);
[8515]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
[4]304sub read {
[16381]305 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli, $aux) = @_;
[4]306
[809]307 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
[9853]308 $total_count = 0 unless defined $total_count && $total_count =~ /\d/;
[6332]309 $gli = 0 unless defined $gli;
310
[315]311 my $rv = 0;
[7363]312 my $glifile = $file;
[7904]313
[7363]314 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
[8515]315
[6332]316 # Announce to GLI that we are handling a file
[7363]317 print STDERR "<File n='$glifile'>\n" if $gli;
[8515]318
[1454]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")) {
[7829]322 gsprintf($outhandle, "{plugin.kill_file}\n");
[1454]323 die "\n";
324 }
[18441]325
[7363]326 my $had_error = 0;
[4]327 # pass this file by each of the plugins in turn until one
328 # is found which will process it
[7363]329 # read must return:
330 # undef - could not recognise
331 # -1 - tried but error
332 # 0 - blocked
333 # anything else for successful processing
[8515]334
[7829]335 foreach my $plugobj (@$pluginfo) {
[8515]336
337 $rv = $plugobj->read($pluginfo, $base_dir, $file,
[16381]338 $block_hash, $metadata, $processor, $maxdocs,
339 $total_count, $gli, $aux);
[8515]340
341 if (defined $rv) {
[7363]342 if ($rv == -1) {
[7904]343 # an error has occurred
[7363]344 $had_error = 1;
345 } else {
[7904]346 return $rv;
[7363]347 }
348 } # else undefined - was not recognised by the plugin
[4]349 }
[7904]350
[7363]351 if ($had_error) {
352 # was recognised but couldn't be processed
353 if ($verbosity >= 2) {
[7829]354 gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
[7363]355 }
356 # tell the GLI that it was not processed
357 print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
[7904]358
[7829]359 gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
[7363]360 $stats->{'num_not_processed'} ++;
361 } else {
362 # was not recognised
363 if ($verbosity >= 2) {
[7829]364 gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
[7363]365 }
366 # tell the GLI that it was not processed
367 print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
368
[7829]369 gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
[7363]370 $stats->{'num_not_recognised'} ++;
[170]371 }
[315]372 return 0;
[4]373}
374
[2785]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 {
[6332]379 my ($pluginfo, $statshandle, $faillog, $gli) = @_;
[2785]380
[6332]381 $gli = 0 unless defined $gli;
382
[7829]383 foreach my $plugobj (@$pluginfo) {
[2785]384 $plugobj->compile_stats($stats);
385 }
386
387 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
[7363]388 $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
[2785]389
[7363]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;
[6332]391
[2785]392 if ($total == 1) {
[7829]393 gsprintf($statshandle, "* {plugin.one_considered}\n");
[2785]394 } else {
[7829]395 gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
[2785]396 }
397 if ($stats->{'num_archives'}) {
[5682]398 if ($stats->{'num_archives'} == 1) {
[7829]399 gsprintf($statshandle, " ({plugin.including_archive})\n");
[5682]400 }
401 else {
[7829]402 gsprintf($statshandle, " ({plugin.including_archives})\n",
403 $stats->{'num_archives'});
[5682]404 }
[2785]405 }
406 if ($stats->{'num_processed'} == 1) {
[7829]407 gsprintf($statshandle, "* {plugin.one_included}\n");
[2785]408 } else {
[7829]409 gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
[2785]410 }
[7363]411 if ($stats->{'num_not_recognised'}) {
412 if ($stats->{'num_not_recognised'} == 1) {
[7829]413 gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
[7363]414 } else {
[7829]415 gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
416 $stats->{'num_not_recognised'});
[7363]417 }
418
419 }
[2797]420 if ($stats->{'num_not_processed'}) {
421 if ($stats->{'num_not_processed'} == 1) {
[7829]422 gsprintf($statshandle, "* {plugin.one_rejected}\n");
[2797]423 } else {
[7829]424 gsprintf($statshandle, "* {plugin.n_rejected}\n",
425 $stats->{'num_not_processed'});
[5682]426 }
[7363]427 }
428 if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
[7829]429 gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
[2797]430 }
[2785]431}
432
[835]433sub end {
[1587]434 my ($pluginfo, $processor) = @_;
435 map { $_->end($processor); } @$pluginfo;
[835]436}
[4]437
[10155]438sub deinit {
439 my ($pluginfo, $processor) = @_;
440
441
442 map { $_->deinit($processor); } @$pluginfo;
443}
444
[4]4451;
Note: See TracBrowser for help on using the repository browser.