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

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

fixed up maxdocs - now pass an extra parameter to the read function

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