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

Last change on this file since 17110 was 17032, checked in by kjdon, 16 years ago

changed a comment

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