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

Last change on this file since 15873 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
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 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
48# global variables
49my $stats = {'num_processed' => 0,
50 'num_blocked' => 0,
51 'num_not_processed' => 0,
52 'num_not_recognised' => 0,
53 'num_archives' => 0
54 };
55
56#globaloptions contains any options that should be passed to all plugins
57my ($verbosity, $outhandle, $failhandle, $globaloptions);
58
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}
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
81 # find the plugin
82 if (defined($ENV{'GSDLCOLLECTION'}))
83 {
84 my $customplugname
85 = &util::filename_cat($collectdir, "custom",$ENV{'GSDLCOLLECTION'},
86 $pp_plugname);
87 push(@check_list,$customplugname);
88 }
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) {
119 &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
120 $pluginname);
121 die "\n";
122 }
123}
124
125sub load_plugin_for_info {
126 my ($pluginname) = shift @_;
127 $pluginname = &get_valid_pluginname($pluginname);
128 load_plugin_require($pluginname);
129
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
140sub load_plugins {
141 my ($plugin_list) = shift @_;
142 my $incremental;
143 ($verbosity, $outhandle, $failhandle, $globaloptions, $incremental) = @_; # globals
144 my @plugin_objects = ();
145 $incremental = 0 unless (defined $incremental && $incremental == 1);
146 $verbosity = 2 unless defined $verbosity;
147 $outhandle = 'STDERR' unless defined $outhandle;
148 $failhandle = 'STDERR' unless defined $failhandle;
149
150 my $colplugindir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins");
151 unshift (@INC, $colplugindir);
152
153 map { $_ = "\"$_\""; } @$globaloptions;
154 my $globals = join (",", @$globaloptions);
155
156 foreach my $pluginoptions (@$plugin_list) {
157 my $pluginname = shift @$pluginoptions;
158 next unless defined $pluginname;
159 $pluginname = &get_valid_pluginname($pluginname);
160 load_plugin_require($pluginname);
161
162 # create a plugin object
163 my ($plugobj);
164 map { $_ = "\"$_\""; } @$pluginoptions;
165 my $options = join (",", @$pluginoptions);
166 if ($globals) {
167 if (@$pluginoptions) {
168 $options .= ",";
169 }
170 $options .= "$globals";
171 }
172 $options =~ s/\$/\\\$/g;
173
174 eval ("\$plugobj = new \$pluginname([],[$options])");
175 die "$@" if $@;
176
177 # initialize plugin
178 $plugobj->init($verbosity, $outhandle, $failhandle);
179
180 $plugobj->set_incremental($incremental);
181
182 # add this object to the list
183 push (@plugin_objects, $plugobj);
184 }
185
186 return \@plugin_objects;
187}
188
189
190sub begin {
191 my ($pluginfo, $base_dir, $processor, $maxdocs, $gli) = @_;
192
193 map { $_->{'gli'} = $gli; } @$pluginfo;
194 map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
195}
196
197
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
247sub read {
248 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli, $aux) = @_;
249
250 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
251 $total_count = 0 unless defined $total_count && $total_count =~ /\d/;
252 $gli = 0 unless defined $gli;
253
254 my $rv = 0;
255 my $glifile = $file;
256
257 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
258
259 # Announce to GLI that we are handling a file
260 print STDERR "<File n='$glifile'>\n" if $gli;
261
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")) {
265 gsprintf($outhandle, "{plugin.kill_file}\n");
266 die "\n";
267 }
268
269 my $had_error = 0;
270 # pass this file by each of the plugins in turn until one
271 # is found which will process it
272 # read must return:
273 # undef - could not recognise
274 # -1 - tried but error
275 # 0 - blocked
276 # anything else for successful processing
277
278 foreach my $plugobj (@$pluginfo) {
279
280 $rv = $plugobj->read($pluginfo, $base_dir, $file,
281 $metadata, $processor, $maxdocs, $total_count, $gli, $aux);
282
283 if (defined $rv) {
284 if ($rv == -1) {
285 # an error has occurred
286 $had_error = 1;
287 } else {
288 return $rv;
289 }
290 } # else undefined - was not recognised by the plugin
291 }
292
293 if ($had_error) {
294 # was recognised but couldn't be processed
295 if ($verbosity >= 2) {
296 gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
297 }
298 # tell the GLI that it was not processed
299 print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
300
301 gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
302 $stats->{'num_not_processed'} ++;
303 } else {
304 # was not recognised
305 if ($verbosity >= 2) {
306 gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
307 }
308 # tell the GLI that it was not processed
309 print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
310
311 gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
312 $stats->{'num_not_recognised'} ++;
313 }
314 return 0;
315}
316
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 {
321 my ($pluginfo, $statshandle, $faillog, $gli) = @_;
322
323 $gli = 0 unless defined $gli;
324
325 foreach my $plugobj (@$pluginfo) {
326 $plugobj->compile_stats($stats);
327 }
328
329 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
330 $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
331
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;
333
334 if ($total == 1) {
335 gsprintf($statshandle, "* {plugin.one_considered}\n");
336 } else {
337 gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
338 }
339 if ($stats->{'num_archives'}) {
340 if ($stats->{'num_archives'} == 1) {
341 gsprintf($statshandle, " ({plugin.including_archive})\n");
342 }
343 else {
344 gsprintf($statshandle, " ({plugin.including_archives})\n",
345 $stats->{'num_archives'});
346 }
347 }
348 if ($stats->{'num_processed'} == 1) {
349 gsprintf($statshandle, "* {plugin.one_included}\n");
350 } else {
351 gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
352 }
353 if ($stats->{'num_not_recognised'}) {
354 if ($stats->{'num_not_recognised'} == 1) {
355 gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
356 } else {
357 gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
358 $stats->{'num_not_recognised'});
359 }
360
361 }
362 if ($stats->{'num_not_processed'}) {
363 if ($stats->{'num_not_processed'} == 1) {
364 gsprintf($statshandle, "* {plugin.one_rejected}\n");
365 } else {
366 gsprintf($statshandle, "* {plugin.n_rejected}\n",
367 $stats->{'num_not_processed'});
368 }
369 }
370 if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
371 gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
372 }
373}
374
375sub end {
376 my ($pluginfo, $processor) = @_;
377 map { $_->end($processor); } @$pluginfo;
378}
379
380sub deinit {
381 my ($pluginfo, $processor) = @_;
382
383
384 map { $_->deinit($processor); } @$pluginfo;
385}
386
3871;
Note: See TracBrowser for help on using the repository browser.