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