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

Last change on this file since 11334 was 11333, checked in by mdewsnip, 18 years ago

Now consistently sets $self->{'gli'} in plugin::begin.

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