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 |
|
---|
26 | package plugin;
|
---|
27 |
|
---|
28 | use strict; # to pick up typos and undeclared variables...
|
---|
29 | no strict 'refs'; # ...but allow filehandles to be variables and vice versa
|
---|
30 | no strict 'subs';
|
---|
31 |
|
---|
32 | require util;
|
---|
33 | use 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 names in the config file
|
---|
37 | my $plugin_name_map = {
|
---|
38 | 'GAPlug' => 'GreenstoneXMLPlugin',
|
---|
39 | 'ArcPlug' => 'ArchivesInfPlugin',
|
---|
40 | 'RecPlug' => 'DirectoryPlugin',
|
---|
41 | 'TEXTPlug' => 'TextPlugin',
|
---|
42 | 'XMLPlug' => 'ReadXMLFile',
|
---|
43 | 'EMAILPlug' => 'EmailPlugin',
|
---|
44 | 'SRCPlug' => 'SourceCodePlugin',
|
---|
45 | 'NULPlug' => 'NulPlugin',
|
---|
46 | 'W3ImgPlug' => 'HTMLImagePlugin',
|
---|
47 | 'PagedImgPlug' => 'PagedImagePlugin',
|
---|
48 | 'METSPlug' => 'GreenstoneMETSPlugin',
|
---|
49 | 'PPTPlug' => 'PowerPointPlugin',
|
---|
50 | 'PSPlug' => 'PostScriptPlugin',
|
---|
51 | 'DBPlug' => 'DatabasePlugin'
|
---|
52 | };
|
---|
53 |
|
---|
54 | # global variables
|
---|
55 | my $stats = {'num_processed' => 0,
|
---|
56 | 'num_blocked' => 0,
|
---|
57 | 'num_not_processed' => 0,
|
---|
58 | 'num_not_recognised' => 0,
|
---|
59 | 'num_archives' => 0
|
---|
60 | };
|
---|
61 |
|
---|
62 | #globaloptions contains any options that should be passed to all plugins
|
---|
63 | my ($verbosity, $outhandle, $failhandle, $globaloptions);
|
---|
64 |
|
---|
65 | sub 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 | }
|
---|
76 | sub 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 |
|
---|
87 | # find the plugin
|
---|
88 | if (defined($ENV{'GSDLCOLLECTION'}))
|
---|
89 | {
|
---|
90 | my $customplugname
|
---|
91 | = &util::filename_cat($collectdir, "custom",$ENV{'GSDLCOLLECTION'},
|
---|
92 | $pp_plugname);
|
---|
93 | push(@check_list,$customplugname);
|
---|
94 | }
|
---|
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) {
|
---|
125 | &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
|
---|
126 | $pluginname);
|
---|
127 | die "\n";
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | sub load_plugin_for_info {
|
---|
132 | my ($pluginname) = shift @_;
|
---|
133 | $pluginname = &get_valid_pluginname($pluginname);
|
---|
134 | load_plugin_require($pluginname);
|
---|
135 |
|
---|
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 |
|
---|
146 | sub load_plugins {
|
---|
147 | my ($plugin_list) = shift @_;
|
---|
148 | my $incremental;
|
---|
149 | ($verbosity, $outhandle, $failhandle, $globaloptions, $incremental) = @_; # globals
|
---|
150 | my @plugin_objects = ();
|
---|
151 | $incremental = 0 unless (defined $incremental && $incremental == 1);
|
---|
152 | $verbosity = 2 unless defined $verbosity;
|
---|
153 | $outhandle = 'STDERR' unless defined $outhandle;
|
---|
154 | $failhandle = 'STDERR' unless defined $failhandle;
|
---|
155 |
|
---|
156 | my $colplugindir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins");
|
---|
157 | unshift (@INC, $colplugindir);
|
---|
158 |
|
---|
159 | map { $_ = "\"$_\""; } @$globaloptions;
|
---|
160 | my $globals = join (",", @$globaloptions);
|
---|
161 |
|
---|
162 | foreach my $pluginoptions (@$plugin_list) {
|
---|
163 | my $pluginname = shift @$pluginoptions;
|
---|
164 | next unless defined $pluginname;
|
---|
165 | $pluginname = &get_valid_pluginname($pluginname);
|
---|
166 | load_plugin_require($pluginname);
|
---|
167 |
|
---|
168 | # create a plugin object
|
---|
169 | my ($plugobj);
|
---|
170 | map { $_ = "\"$_\""; } @$pluginoptions;
|
---|
171 | my $options = join (",", @$pluginoptions);
|
---|
172 | if ($globals) {
|
---|
173 | if (@$pluginoptions) {
|
---|
174 | $options .= ",";
|
---|
175 | }
|
---|
176 | $options .= "$globals";
|
---|
177 | }
|
---|
178 | $options =~ s/\$/\\\$/g;
|
---|
179 |
|
---|
180 | eval ("\$plugobj = new \$pluginname([],[$options])");
|
---|
181 | die "$@" if $@;
|
---|
182 |
|
---|
183 | # initialize plugin
|
---|
184 | $plugobj->init($verbosity, $outhandle, $failhandle);
|
---|
185 |
|
---|
186 | $plugobj->set_incremental($incremental);
|
---|
187 |
|
---|
188 | # add this object to the list
|
---|
189 | push (@plugin_objects, $plugobj);
|
---|
190 | }
|
---|
191 |
|
---|
192 | return \@plugin_objects;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | sub begin {
|
---|
197 | my ($pluginfo, $base_dir, $processor, $maxdocs, $gli) = @_;
|
---|
198 |
|
---|
199 | map { $_->{'gli'} = $gli; } @$pluginfo;
|
---|
200 | map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
|
---|
201 | }
|
---|
202 |
|
---|
203 | sub file_block_read {
|
---|
204 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
|
---|
205 |
|
---|
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 |
|
---|
233 | sub metadata_read {
|
---|
234 | my ($pluginfo, $base_dir, $file, $block_hash, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli, $aux) = @_;
|
---|
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 |
|
---|
265 | $rv = $plugobj->metadata_read($pluginfo, $base_dir, $file, $block_hash,
|
---|
266 | $extrametakeys, $extrametadata, $processor, $maxdocs, $gli, $aux);
|
---|
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 |
|
---|
282 | sub read {
|
---|
283 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli, $aux) = @_;
|
---|
284 |
|
---|
285 | $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
|
---|
286 | $total_count = 0 unless defined $total_count && $total_count =~ /\d/;
|
---|
287 | $gli = 0 unless defined $gli;
|
---|
288 |
|
---|
289 | my $rv = 0;
|
---|
290 | my $glifile = $file;
|
---|
291 |
|
---|
292 | $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
|
---|
293 |
|
---|
294 | # Announce to GLI that we are handling a file
|
---|
295 | print STDERR "<File n='$glifile'>\n" if $gli;
|
---|
296 |
|
---|
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")) {
|
---|
300 | gsprintf($outhandle, "{plugin.kill_file}\n");
|
---|
301 | die "\n";
|
---|
302 | }
|
---|
303 |
|
---|
304 | my $had_error = 0;
|
---|
305 | # pass this file by each of the plugins in turn until one
|
---|
306 | # is found which will process it
|
---|
307 | # read must return:
|
---|
308 | # undef - could not recognise
|
---|
309 | # -1 - tried but error
|
---|
310 | # 0 - blocked
|
---|
311 | # anything else for successful processing
|
---|
312 |
|
---|
313 | foreach my $plugobj (@$pluginfo) {
|
---|
314 |
|
---|
315 | $rv = $plugobj->read($pluginfo, $base_dir, $file,
|
---|
316 | $block_hash, $metadata, $processor, $maxdocs,
|
---|
317 | $total_count, $gli, $aux);
|
---|
318 |
|
---|
319 | if (defined $rv) {
|
---|
320 | if ($rv == -1) {
|
---|
321 | # an error has occurred
|
---|
322 | $had_error = 1;
|
---|
323 | } else {
|
---|
324 | return $rv;
|
---|
325 | }
|
---|
326 | } # else undefined - was not recognised by the plugin
|
---|
327 | }
|
---|
328 |
|
---|
329 | if ($had_error) {
|
---|
330 | # was recognised but couldn't be processed
|
---|
331 | if ($verbosity >= 2) {
|
---|
332 | gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
|
---|
333 | }
|
---|
334 | # tell the GLI that it was not processed
|
---|
335 | print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
|
---|
336 |
|
---|
337 | gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
|
---|
338 | $stats->{'num_not_processed'} ++;
|
---|
339 | } else {
|
---|
340 | # was not recognised
|
---|
341 | if ($verbosity >= 2) {
|
---|
342 | gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
|
---|
343 | }
|
---|
344 | # tell the GLI that it was not processed
|
---|
345 | print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
|
---|
346 |
|
---|
347 | gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
|
---|
348 | $stats->{'num_not_recognised'} ++;
|
---|
349 | }
|
---|
350 | return 0;
|
---|
351 | }
|
---|
352 |
|
---|
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 -
|
---|
356 | sub write_stats {
|
---|
357 | my ($pluginfo, $statshandle, $faillog, $gli) = @_;
|
---|
358 |
|
---|
359 | $gli = 0 unless defined $gli;
|
---|
360 |
|
---|
361 | foreach my $plugobj (@$pluginfo) {
|
---|
362 | $plugobj->compile_stats($stats);
|
---|
363 | }
|
---|
364 |
|
---|
365 | my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
|
---|
366 | $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
|
---|
367 |
|
---|
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;
|
---|
369 |
|
---|
370 | if ($total == 1) {
|
---|
371 | gsprintf($statshandle, "* {plugin.one_considered}\n");
|
---|
372 | } else {
|
---|
373 | gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
|
---|
374 | }
|
---|
375 | if ($stats->{'num_archives'}) {
|
---|
376 | if ($stats->{'num_archives'} == 1) {
|
---|
377 | gsprintf($statshandle, " ({plugin.including_archive})\n");
|
---|
378 | }
|
---|
379 | else {
|
---|
380 | gsprintf($statshandle, " ({plugin.including_archives})\n",
|
---|
381 | $stats->{'num_archives'});
|
---|
382 | }
|
---|
383 | }
|
---|
384 | if ($stats->{'num_processed'} == 1) {
|
---|
385 | gsprintf($statshandle, "* {plugin.one_included}\n");
|
---|
386 | } else {
|
---|
387 | gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
|
---|
388 | }
|
---|
389 | if ($stats->{'num_not_recognised'}) {
|
---|
390 | if ($stats->{'num_not_recognised'} == 1) {
|
---|
391 | gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
|
---|
392 | } else {
|
---|
393 | gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
|
---|
394 | $stats->{'num_not_recognised'});
|
---|
395 | }
|
---|
396 |
|
---|
397 | }
|
---|
398 | if ($stats->{'num_not_processed'}) {
|
---|
399 | if ($stats->{'num_not_processed'} == 1) {
|
---|
400 | gsprintf($statshandle, "* {plugin.one_rejected}\n");
|
---|
401 | } else {
|
---|
402 | gsprintf($statshandle, "* {plugin.n_rejected}\n",
|
---|
403 | $stats->{'num_not_processed'});
|
---|
404 | }
|
---|
405 | }
|
---|
406 | if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
|
---|
407 | gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | sub end {
|
---|
412 | my ($pluginfo, $processor) = @_;
|
---|
413 | map { $_->end($processor); } @$pluginfo;
|
---|
414 | }
|
---|
415 |
|
---|
416 | sub deinit {
|
---|
417 | my ($pluginfo, $processor) = @_;
|
---|
418 |
|
---|
419 |
|
---|
420 | map { $_->deinit($processor); } @$pluginfo;
|
---|
421 | }
|
---|
422 |
|
---|
423 | 1;
|
---|