source: main/tags/2.52/gsdl/perllib/plugin.pm@ 25422

Last change on this file since 25422 was 7904, checked in by chi, 20 years ago

Minor changes to layout of code.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 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 read {
108 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $gli, $aux) = @_;
109
110
111 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
112 $gli = 0 unless defined $gli;
113
114
115 my $rv = 0;
116 my $glifile = $file;
117
118 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
119 # Announce to GLI that we are handling a file
120 print STDERR "<File n='$glifile'>\n" if $gli;
121
122
123 # the .kill file is a handy (if not very elegant) way of aborting
124 # an import.pl or buildcol.pl process
125 if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
126 gsprintf($outhandle, "{plugin.kill_file}\n");
127 die "\n";
128 }
129
130 my $had_error = 0;
131 # pass this file by each of the plugins in turn until one
132 # is found which will process it
133 # read must return:
134 # undef - could not recognise
135 # -1 - tried but error
136 # 0 - blocked
137 # anything else for successful processing
138 foreach my $plugobj (@$pluginfo) {
139 $rv = $plugobj->read($pluginfo, $base_dir, $file,
140 $metadata, $processor, $maxdocs, $gli, $aux);
141 if (defined $rv) {
142 if ($rv == -1) {
143 # an error has occurred
144 $had_error = 1;
145 print STDERR "<ProcessingError n='$glifile'>\n" if $gli;
146 } else {
147 return $rv;
148 }
149 } # else undefined - was not recognised by the plugin
150 }
151
152 if ($had_error) {
153 # was recognised but couldn't be processed
154 if ($verbosity >= 2) {
155 gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
156 }
157 # tell the GLI that it was not processed
158 print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
159
160 $file =~ s/.*?([^\\\/]+)$/$1/;
161 gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
162 $stats->{'num_not_processed'} ++;
163 } else {
164 # was not recognised
165 if ($verbosity >= 2) {
166 gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
167 }
168 # tell the GLI that it was not processed
169 print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
170
171 $file =~ s/.*?([^\\\/]+)$/$1/;
172 gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
173 $stats->{'num_not_recognised'} ++;
174 }
175
176 return 0;
177}
178
179# write out some general stats that the plugins have compiled - note that
180# the buildcol.pl process doesn't currently call this process so the stats
181# are only output after import.pl -
182sub write_stats {
183 my ($pluginfo, $statshandle, $faillog, $gli) = @_;
184
185 $gli = 0 unless defined $gli;
186
187 foreach my $plugobj (@$pluginfo) {
188 $plugobj->compile_stats($stats);
189 }
190
191 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
192 $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
193
194 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;
195
196 if ($total == 1) {
197 gsprintf($statshandle, "* {plugin.one_considered}\n");
198 } else {
199 gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
200 }
201 if ($stats->{'num_archives'}) {
202 if ($stats->{'num_archives'} == 1) {
203 gsprintf($statshandle, " ({plugin.including_archive})\n");
204 }
205 else {
206 gsprintf($statshandle, " ({plugin.including_archives})\n",
207 $stats->{'num_archives'});
208 }
209 }
210 if ($stats->{'num_processed'} == 1) {
211 gsprintf($statshandle, "* {plugin.one_included}\n");
212 } else {
213 gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
214 }
215 if ($stats->{'num_not_recognised'}) {
216 if ($stats->{'num_not_recognised'} == 1) {
217 gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
218 } else {
219 gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
220 $stats->{'num_not_recognised'});
221 }
222
223 }
224 if ($stats->{'num_not_processed'}) {
225 if ($stats->{'num_not_processed'} == 1) {
226 gsprintf($statshandle, "* {plugin.one_rejected}\n");
227 } else {
228 gsprintf($statshandle, "* {plugin.n_rejected}\n",
229 $stats->{'num_not_processed'});
230 }
231 }
232 if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
233 gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
234 }
235}
236
237sub end {
238 my ($pluginfo, $processor) = @_;
239 map { $_->end($processor); } @$pluginfo;
240}
241
2421;
Note: See TracBrowser for help on using the repository browser.