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 | # global variables
|
---|
36 | my $stats = {'num_processed' => 0,
|
---|
37 | 'num_blocked' => 0,
|
---|
38 | 'num_not_processed' => 0,
|
---|
39 | 'num_not_recognised' => 0,
|
---|
40 | 'num_archives' => 0
|
---|
41 | };
|
---|
42 |
|
---|
43 | #globaloptions contains any options that should be passed to all plugins
|
---|
44 | my ($verbosity, $outhandle, $failhandle, $globaloptions);
|
---|
45 |
|
---|
46 | sub load_plugin_for_info {
|
---|
47 | my ($pluginname) = shift @_;
|
---|
48 |
|
---|
49 | # find the plugin
|
---|
50 | my $customplugname;
|
---|
51 | if (defined($ENV{'GSDLCOLLECTION'}))
|
---|
52 | {
|
---|
53 | $customplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "custom", $ENV{'GSDLCOLLECTION'},
|
---|
54 | 'perllib', 'plugins', "${pluginname}.pm");
|
---|
55 | }
|
---|
56 | my $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, 'perllib', 'plugins',
|
---|
57 | "${pluginname}.pm");
|
---|
58 | my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'}, 'perllib', 'plugins',
|
---|
59 | "${pluginname}.pm");
|
---|
60 | if (defined($customplugname) && -e $customplugname) { require $customplugname; }
|
---|
61 | elsif (-e $colplugname) { require $colplugname; }
|
---|
62 | elsif (-e $mainplugname) { require $mainplugname; }
|
---|
63 | else {
|
---|
64 | &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
|
---|
65 | $pluginname);
|
---|
66 | die "\n";
|
---|
67 | }
|
---|
68 |
|
---|
69 | # create a plugin object
|
---|
70 | my ($plugobj);
|
---|
71 | my $options = "-gsdlinfo";
|
---|
72 |
|
---|
73 | eval ("\$plugobj = new \$pluginname([],[$options])");
|
---|
74 | die "$@" if $@;
|
---|
75 |
|
---|
76 | return $plugobj;
|
---|
77 | }
|
---|
78 |
|
---|
79 | sub load_plugins {
|
---|
80 | my ($plugin_list) = shift @_;
|
---|
81 | my $incremental;
|
---|
82 | ($verbosity, $outhandle, $failhandle, $globaloptions, $incremental) = @_; # globals
|
---|
83 | my @plugin_objects = ();
|
---|
84 | $incremental = 0 unless (defined $incremental && $incremental == 1);
|
---|
85 | $verbosity = 2 unless defined $verbosity;
|
---|
86 | $outhandle = 'STDERR' unless defined $outhandle;
|
---|
87 | $failhandle = 'STDERR' unless defined $failhandle;
|
---|
88 |
|
---|
89 | my $colplugindir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins");
|
---|
90 | unshift (@INC, $colplugindir);
|
---|
91 |
|
---|
92 | map { $_ = "\"$_\""; } @$globaloptions;
|
---|
93 | my $globals = join (",", @$globaloptions);
|
---|
94 |
|
---|
95 | foreach my $pluginoptions (@$plugin_list) {
|
---|
96 | my $pluginname = shift @$pluginoptions;
|
---|
97 | next unless defined $pluginname;
|
---|
98 |
|
---|
99 | # find the plugin
|
---|
100 | my $customplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "custom", $ENV{'GSDLCOLLECTION'},
|
---|
101 | 'perllib', 'plugins', "${pluginname}.pm");
|
---|
102 | my $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, 'perllib', 'plugins',
|
---|
103 | "${pluginname}.pm");
|
---|
104 | my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'}, 'perllib', 'plugins',
|
---|
105 | "${pluginname}.pm");
|
---|
106 | if (-e $customplugname) { require $customplugname; }
|
---|
107 | elsif (-e $colplugname) { require $colplugname; }
|
---|
108 | elsif (-e $mainplugname) { require $mainplugname; }
|
---|
109 | else {
|
---|
110 | gsprintf($outhandle, "{plugin.could_not_find_plugin}\n",
|
---|
111 | $pluginname);
|
---|
112 | die "\n";
|
---|
113 | }
|
---|
114 |
|
---|
115 | # create a plugin object
|
---|
116 | my ($plugobj);
|
---|
117 | map { $_ = "\"$_\""; } @$pluginoptions;
|
---|
118 | my $options = join (",", @$pluginoptions);
|
---|
119 | if ($globals) {
|
---|
120 | if (@$pluginoptions) {
|
---|
121 | $options .= ",";
|
---|
122 | }
|
---|
123 | $options .= "$globals";
|
---|
124 | }
|
---|
125 | $options =~ s/\$/\\\$/g;
|
---|
126 |
|
---|
127 | eval ("\$plugobj = new \$pluginname([],[$options])");
|
---|
128 | die "$@" if $@;
|
---|
129 |
|
---|
130 | # initialize plugin
|
---|
131 | $plugobj->init($verbosity, $outhandle, $failhandle);
|
---|
132 |
|
---|
133 | $plugobj->set_incremental($incremental);
|
---|
134 |
|
---|
135 | # add this object to the list
|
---|
136 | push (@plugin_objects, $plugobj);
|
---|
137 | }
|
---|
138 |
|
---|
139 | return \@plugin_objects;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | sub begin {
|
---|
144 | my ($pluginfo, $base_dir, $processor, $maxdocs, $gli) = @_;
|
---|
145 |
|
---|
146 | map { $_->{'gli'} = $gli; } @$pluginfo;
|
---|
147 | map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | sub metadata_read {
|
---|
152 | my ($pluginfo, $base_dir, $file, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli, $aux) = @_;
|
---|
153 |
|
---|
154 | $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
|
---|
155 | $gli = 0 unless defined $gli;
|
---|
156 |
|
---|
157 | my $rv = 0;
|
---|
158 | my $glifile = $file;
|
---|
159 |
|
---|
160 | $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
|
---|
161 |
|
---|
162 | # Announce to GLI that we are handling a file
|
---|
163 | print STDERR "<File n='$glifile'>\n" if $gli;
|
---|
164 |
|
---|
165 | # the .kill file is a handy (if not very elegant) way of aborting
|
---|
166 | # an import.pl or buildcol.pl process
|
---|
167 | if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
|
---|
168 | gsprintf($outhandle, "{plugin.kill_file}\n");
|
---|
169 | die "\n";
|
---|
170 | }
|
---|
171 |
|
---|
172 | my $had_error = 0;
|
---|
173 | # pass this file by each of the plugins in turn until one
|
---|
174 | # is found which will process it
|
---|
175 | # read must return:
|
---|
176 | # undef - could not recognise
|
---|
177 | # -1 - tried but error
|
---|
178 | # 0 - blocked
|
---|
179 | # anything else for successful processing
|
---|
180 |
|
---|
181 | foreach my $plugobj (@$pluginfo) {
|
---|
182 |
|
---|
183 | $rv = $plugobj->metadata_read($pluginfo, $base_dir, $file,
|
---|
184 | $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli, $aux);
|
---|
185 |
|
---|
186 | if (defined $rv) {
|
---|
187 | if ($rv == -1) {
|
---|
188 | # an error has occurred
|
---|
189 | $had_error = 1;
|
---|
190 | print STDERR "<ProcessingError n='$glifile'>\n" if $gli;
|
---|
191 | } else {
|
---|
192 | return $rv;
|
---|
193 | }
|
---|
194 | } # else undefined - was not recognised by the plugin
|
---|
195 | }
|
---|
196 |
|
---|
197 | return 0;
|
---|
198 | }
|
---|
199 |
|
---|
200 | sub read {
|
---|
201 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli, $aux) = @_;
|
---|
202 |
|
---|
203 | $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
|
---|
204 | $total_count = 0 unless defined $total_count && $total_count =~ /\d/;
|
---|
205 | $gli = 0 unless defined $gli;
|
---|
206 |
|
---|
207 | my $rv = 0;
|
---|
208 | my $glifile = $file;
|
---|
209 |
|
---|
210 | $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
|
---|
211 |
|
---|
212 | # Announce to GLI that we are handling a file
|
---|
213 | print STDERR "<File n='$glifile'>\n" if $gli;
|
---|
214 |
|
---|
215 | # the .kill file is a handy (if not very elegant) way of aborting
|
---|
216 | # an import.pl or buildcol.pl process
|
---|
217 | if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
|
---|
218 | gsprintf($outhandle, "{plugin.kill_file}\n");
|
---|
219 | die "\n";
|
---|
220 | }
|
---|
221 |
|
---|
222 | my $had_error = 0;
|
---|
223 | # pass this file by each of the plugins in turn until one
|
---|
224 | # is found which will process it
|
---|
225 | # read must return:
|
---|
226 | # undef - could not recognise
|
---|
227 | # -1 - tried but error
|
---|
228 | # 0 - blocked
|
---|
229 | # anything else for successful processing
|
---|
230 |
|
---|
231 | foreach my $plugobj (@$pluginfo) {
|
---|
232 |
|
---|
233 | $rv = $plugobj->read($pluginfo, $base_dir, $file,
|
---|
234 | $metadata, $processor, $maxdocs, $total_count, $gli, $aux);
|
---|
235 |
|
---|
236 | if (defined $rv) {
|
---|
237 | if ($rv == -1) {
|
---|
238 | # an error has occurred
|
---|
239 | $had_error = 1;
|
---|
240 | } else {
|
---|
241 | return $rv;
|
---|
242 | }
|
---|
243 | } # else undefined - was not recognised by the plugin
|
---|
244 | }
|
---|
245 |
|
---|
246 | if ($had_error) {
|
---|
247 | # was recognised but couldn't be processed
|
---|
248 | if ($verbosity >= 2) {
|
---|
249 | gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
|
---|
250 | }
|
---|
251 | # tell the GLI that it was not processed
|
---|
252 | print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
|
---|
253 |
|
---|
254 | gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
|
---|
255 | $stats->{'num_not_processed'} ++;
|
---|
256 | } else {
|
---|
257 | # was not recognised
|
---|
258 | if ($verbosity >= 2) {
|
---|
259 | gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
|
---|
260 | }
|
---|
261 | # tell the GLI that it was not processed
|
---|
262 | print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
|
---|
263 |
|
---|
264 | gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
|
---|
265 | $stats->{'num_not_recognised'} ++;
|
---|
266 | }
|
---|
267 | return 0;
|
---|
268 | }
|
---|
269 |
|
---|
270 | # write out some general stats that the plugins have compiled - note that
|
---|
271 | # the buildcol.pl process doesn't currently call this process so the stats
|
---|
272 | # are only output after import.pl -
|
---|
273 | sub write_stats {
|
---|
274 | my ($pluginfo, $statshandle, $faillog, $gli) = @_;
|
---|
275 |
|
---|
276 | $gli = 0 unless defined $gli;
|
---|
277 |
|
---|
278 | foreach my $plugobj (@$pluginfo) {
|
---|
279 | $plugobj->compile_stats($stats);
|
---|
280 | }
|
---|
281 |
|
---|
282 | my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
|
---|
283 | $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
|
---|
284 |
|
---|
285 | 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;
|
---|
286 |
|
---|
287 | if ($total == 1) {
|
---|
288 | gsprintf($statshandle, "* {plugin.one_considered}\n");
|
---|
289 | } else {
|
---|
290 | gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
|
---|
291 | }
|
---|
292 | if ($stats->{'num_archives'}) {
|
---|
293 | if ($stats->{'num_archives'} == 1) {
|
---|
294 | gsprintf($statshandle, " ({plugin.including_archive})\n");
|
---|
295 | }
|
---|
296 | else {
|
---|
297 | gsprintf($statshandle, " ({plugin.including_archives})\n",
|
---|
298 | $stats->{'num_archives'});
|
---|
299 | }
|
---|
300 | }
|
---|
301 | if ($stats->{'num_processed'} == 1) {
|
---|
302 | gsprintf($statshandle, "* {plugin.one_included}\n");
|
---|
303 | } else {
|
---|
304 | gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
|
---|
305 | }
|
---|
306 | if ($stats->{'num_not_recognised'}) {
|
---|
307 | if ($stats->{'num_not_recognised'} == 1) {
|
---|
308 | gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
|
---|
309 | } else {
|
---|
310 | gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
|
---|
311 | $stats->{'num_not_recognised'});
|
---|
312 | }
|
---|
313 |
|
---|
314 | }
|
---|
315 | if ($stats->{'num_not_processed'}) {
|
---|
316 | if ($stats->{'num_not_processed'} == 1) {
|
---|
317 | gsprintf($statshandle, "* {plugin.one_rejected}\n");
|
---|
318 | } else {
|
---|
319 | gsprintf($statshandle, "* {plugin.n_rejected}\n",
|
---|
320 | $stats->{'num_not_processed'});
|
---|
321 | }
|
---|
322 | }
|
---|
323 | if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
|
---|
324 | gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | sub end {
|
---|
329 | my ($pluginfo, $processor) = @_;
|
---|
330 | map { $_->end($processor); } @$pluginfo;
|
---|
331 | }
|
---|
332 |
|
---|
333 | sub deinit {
|
---|
334 | my ($pluginfo, $processor) = @_;
|
---|
335 |
|
---|
336 |
|
---|
337 | map { $_->deinit($processor); } @$pluginfo;
|
---|
338 | }
|
---|
339 |
|
---|
340 | 1;
|
---|