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

Last change on this file since 10482 was 10482, checked in by kjdon, 19 years ago

removed debug statement

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