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

Last change on this file since 16300 was 15873, checked in by kjdon, 16 years ago

plugin overhaul: all the plugins have new names. Have added a mapping between old names and new names so that if we are building an old collection that specifies xxPlug then we will load up the appropriate new ones

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