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

Last change on this file since 18751 was 18441, checked in by davidb, 15 years ago

Modifications for incremental building to support files that need to be deleted

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