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