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

Last change on this file since 14239 was 14239, checked in by mdewsnip, 17 years ago

Fixed problem with loading custom plugins/classifiers when running pluginfo.pl -listall or classinfo.pl -listall.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 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
[14239]50 my $customplugname;
51 if (defined($ENV{'GSDLCOLLECTION'}))
52 {
53 $customplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "custom", $ENV{'GSDLCOLLECTION'},
[14112]54 'perllib', 'plugins', "${pluginname}.pm");
[14239]55 }
[14112]56 my $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, 'perllib', 'plugins',
[10579]57 "${pluginname}.pm");
[14112]58 my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'}, 'perllib', 'plugins',
[10579]59 "${pluginname}.pm");
[14239]60 if (defined($customplugname) && -e $customplugname) { require $customplugname; }
[14112]61 elsif (-e $colplugname) { require $colplugname; }
[10579]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
[4]79sub load_plugins {
[1431]80 my ($plugin_list) = shift @_;
[12968]81 my $incremental;
82 ($verbosity, $outhandle, $failhandle, $globaloptions, $incremental) = @_; # globals
[4]83 my @plugin_objects = ();
[12968]84 $incremental = 0 unless (defined $incremental && $incremental == 1);
[1243]85 $verbosity = 2 unless defined $verbosity;
[7829]86 $outhandle = 'STDERR' unless defined $outhandle;
87 $failhandle = 'STDERR' unless defined $failhandle;
[1243]88
[13933]89 my $colplugindir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins");
90 unshift (@INC, $colplugindir);
91
[6584]92 map { $_ = "\"$_\""; } @$globaloptions;
93 my $globals = join (",", @$globaloptions);
94
[7829]95 foreach my $pluginoptions (@$plugin_list) {
[809]96 my $pluginname = shift @$pluginoptions;
97 next unless defined $pluginname;
98
[4]99 # find the plugin
[14112]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',
[809]103 "${pluginname}.pm");
[14112]104 my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'}, 'perllib', 'plugins',
[809]105 "${pluginname}.pm");
[14112]106 if (-e $customplugname) { require $customplugname; }
107 elsif (-e $colplugname) { require $colplugname; }
[134]108 elsif (-e $mainplugname) { require $mainplugname; }
[7829]109 else {
110 gsprintf($outhandle, "{plugin.could_not_find_plugin}\n",
111 $pluginname);
112 die "\n";
113 }
[4]114
115 # create a plugin object
116 my ($plugobj);
[809]117 map { $_ = "\"$_\""; } @$pluginoptions;
118 my $options = join (",", @$pluginoptions);
[6584]119 if ($globals) {
120 if (@$pluginoptions) {
121 $options .= ",";
122 }
123 $options .= "$globals";
124 }
[1244]125 $options =~ s/\$/\\\$/g;
[7904]126
[10218]127 eval ("\$plugobj = new \$pluginname([],[$options])");
[4]128 die "$@" if $@;
[809]129
[1243]130 # initialize plugin
[2785]131 $plugobj->init($verbosity, $outhandle, $failhandle);
[10478]132
[12968]133 $plugobj->set_incremental($incremental);
[1243]134
[4]135 # add this object to the list
136 push (@plugin_objects, $plugobj);
137 }
138
139 return \@plugin_objects;
140}
141
[835]142
143sub begin {
[11333]144 my ($pluginfo, $base_dir, $processor, $maxdocs, $gli) = @_;
[835]145
[11333]146 map { $_->{'gli'} = $gli; } @$pluginfo;
[835]147 map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
148}
149
[10155]150
[8515]151sub 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
[4]200sub read {
[9853]201 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli, $aux) = @_;
[4]202
[809]203 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
[9853]204 $total_count = 0 unless defined $total_count && $total_count =~ /\d/;
[6332]205 $gli = 0 unless defined $gli;
206
[315]207 my $rv = 0;
[7363]208 my $glifile = $file;
[7904]209
[7363]210 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
[8515]211
[6332]212 # Announce to GLI that we are handling a file
[7363]213 print STDERR "<File n='$glifile'>\n" if $gli;
[8515]214
[1454]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")) {
[7829]218 gsprintf($outhandle, "{plugin.kill_file}\n");
[1454]219 die "\n";
220 }
[8515]221
[7363]222 my $had_error = 0;
[4]223 # pass this file by each of the plugins in turn until one
224 # is found which will process it
[7363]225 # read must return:
226 # undef - could not recognise
227 # -1 - tried but error
228 # 0 - blocked
229 # anything else for successful processing
[8515]230
[7829]231 foreach my $plugobj (@$pluginfo) {
[8515]232
233 $rv = $plugobj->read($pluginfo, $base_dir, $file,
[9853]234 $metadata, $processor, $maxdocs, $total_count, $gli, $aux);
[8515]235
236 if (defined $rv) {
[7363]237 if ($rv == -1) {
[7904]238 # an error has occurred
[7363]239 $had_error = 1;
240 } else {
[7904]241 return $rv;
[7363]242 }
243 } # else undefined - was not recognised by the plugin
[4]244 }
[7904]245
[7363]246 if ($had_error) {
247 # was recognised but couldn't be processed
248 if ($verbosity >= 2) {
[7829]249 gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
[7363]250 }
251 # tell the GLI that it was not processed
252 print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
[7904]253
[7829]254 gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
[7363]255 $stats->{'num_not_processed'} ++;
256 } else {
257 # was not recognised
258 if ($verbosity >= 2) {
[7829]259 gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
[7363]260 }
261 # tell the GLI that it was not processed
262 print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
263
[7829]264 gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
[7363]265 $stats->{'num_not_recognised'} ++;
[170]266 }
[315]267 return 0;
[4]268}
269
[2785]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 -
273sub write_stats {
[6332]274 my ($pluginfo, $statshandle, $faillog, $gli) = @_;
[2785]275
[6332]276 $gli = 0 unless defined $gli;
277
[7829]278 foreach my $plugobj (@$pluginfo) {
[2785]279 $plugobj->compile_stats($stats);
280 }
281
282 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
[7363]283 $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
[2785]284
[7363]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;
[6332]286
[2785]287 if ($total == 1) {
[7829]288 gsprintf($statshandle, "* {plugin.one_considered}\n");
[2785]289 } else {
[7829]290 gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
[2785]291 }
292 if ($stats->{'num_archives'}) {
[5682]293 if ($stats->{'num_archives'} == 1) {
[7829]294 gsprintf($statshandle, " ({plugin.including_archive})\n");
[5682]295 }
296 else {
[7829]297 gsprintf($statshandle, " ({plugin.including_archives})\n",
298 $stats->{'num_archives'});
[5682]299 }
[2785]300 }
301 if ($stats->{'num_processed'} == 1) {
[7829]302 gsprintf($statshandle, "* {plugin.one_included}\n");
[2785]303 } else {
[7829]304 gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
[2785]305 }
[7363]306 if ($stats->{'num_not_recognised'}) {
307 if ($stats->{'num_not_recognised'} == 1) {
[7829]308 gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
[7363]309 } else {
[7829]310 gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
311 $stats->{'num_not_recognised'});
[7363]312 }
313
314 }
[2797]315 if ($stats->{'num_not_processed'}) {
316 if ($stats->{'num_not_processed'} == 1) {
[7829]317 gsprintf($statshandle, "* {plugin.one_rejected}\n");
[2797]318 } else {
[7829]319 gsprintf($statshandle, "* {plugin.n_rejected}\n",
320 $stats->{'num_not_processed'});
[5682]321 }
[7363]322 }
323 if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
[7829]324 gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
[2797]325 }
[2785]326}
327
[835]328sub end {
[1587]329 my ($pluginfo, $processor) = @_;
330 map { $_->end($processor); } @$pluginfo;
[835]331}
[4]332
[10155]333sub deinit {
334 my ($pluginfo, $processor) = @_;
335
336
337 map { $_->deinit($processor); } @$pluginfo;
338}
339
[4]3401;
Note: See TracBrowser for help on using the repository browser.