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 |
|
---|
26 | package plugin;
|
---|
27 |
|
---|
28 | require util;
|
---|
29 | use gsprintf;
|
---|
30 |
|
---|
31 | my $stats = {'num_processed' => 0,
|
---|
32 | 'num_blocked' => 0,
|
---|
33 | 'num_not_processed' => 0,
|
---|
34 | 'num_not_recognised' => 0,
|
---|
35 | 'num_archives' => 0
|
---|
36 | };
|
---|
37 |
|
---|
38 |
|
---|
39 | sub gsprintf
|
---|
40 | {
|
---|
41 | return &gsprintf::gsprintf(@_);
|
---|
42 | }
|
---|
43 |
|
---|
44 | #globaloptions contains any options that should be passed to all plugins
|
---|
45 | sub 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 $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 { &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n", $pluginname) && die "\n";
|
---|
69 | # die "ERROR - couldn't find plugin \"$pluginname\"\n";
|
---|
70 | }
|
---|
71 |
|
---|
72 | # create a plugin object
|
---|
73 | my ($plugobj);
|
---|
74 | map { $_ = "\"$_\""; } @$pluginoptions;
|
---|
75 | my $options = join (",", @$pluginoptions);
|
---|
76 | if ($globals) {
|
---|
77 | if (@$pluginoptions) {
|
---|
78 | $options .= ",";
|
---|
79 | }
|
---|
80 | $options .= "$globals";
|
---|
81 | }
|
---|
82 | $options =~ s/\$/\\\$/g;
|
---|
83 |
|
---|
84 | eval ("\$plugobj = new \$pluginname($options)");
|
---|
85 | die "$@" if $@;
|
---|
86 |
|
---|
87 | # initialize plugin
|
---|
88 | $plugobj->init($verbosity, $outhandle, $failhandle);
|
---|
89 |
|
---|
90 | # add this object to the list
|
---|
91 | push (@plugin_objects, $plugobj);
|
---|
92 | }
|
---|
93 |
|
---|
94 | return \@plugin_objects;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | sub begin {
|
---|
99 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
100 |
|
---|
101 | map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
|
---|
102 | }
|
---|
103 |
|
---|
104 | sub read {
|
---|
105 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $gli, $aux) = @_;
|
---|
106 |
|
---|
107 | $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
|
---|
108 | $gli = 0 unless defined $gli;
|
---|
109 |
|
---|
110 | my $rv = 0;
|
---|
111 | my $glifile = $file;
|
---|
112 | $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
|
---|
113 | # Announce to GLI that we are handling a file
|
---|
114 | print STDERR "<File n='$glifile'>\n" if $gli;
|
---|
115 |
|
---|
116 | # the .kill file is a handy (if not very elegant) way of aborting
|
---|
117 | # an import.pl or buildcol.pl process
|
---|
118 | if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
|
---|
119 | &gsprintf($outhandle, "{plugin.kill_file}\n");
|
---|
120 | die "\n";
|
---|
121 | }
|
---|
122 |
|
---|
123 | my $had_error = 0;
|
---|
124 | # pass this file by each of the plugins in turn until one
|
---|
125 | # is found which will process it
|
---|
126 | # read must return:
|
---|
127 | # undef - could not recognise
|
---|
128 | # -1 - tried but error
|
---|
129 | # 0 - blocked
|
---|
130 | # anything else for successful processing
|
---|
131 | foreach $plugobj (@$pluginfo) {
|
---|
132 | $rv = $plugobj->read($pluginfo, $base_dir, $file,
|
---|
133 | $metadata, $processor, $maxdocs, $gli, $aux);
|
---|
134 | if (defined $rv) {
|
---|
135 | if ($rv == -1) {
|
---|
136 | # an error has occurred
|
---|
137 | $had_error = 1;
|
---|
138 | print STDERR "<ProcessingError n='$glifile'>\n" if $gli;
|
---|
139 | } else {
|
---|
140 | return $rv;
|
---|
141 | }
|
---|
142 | } # else undefined - was not recognised by the plugin
|
---|
143 | }
|
---|
144 |
|
---|
145 | if ($had_error) {
|
---|
146 | # was recognised but couldn't be processed
|
---|
147 | if ($verbosity >= 2) {
|
---|
148 | &gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
|
---|
149 | # print $outhandle "WARNING - no plugin could process $file\n";
|
---|
150 | }
|
---|
151 | # tell the GLI that it was not processed
|
---|
152 | print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
|
---|
153 |
|
---|
154 | $file =~ s/.*?([^\\\/]+)$/$1/;
|
---|
155 | &gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
|
---|
156 | # print $failhandle "$file: no plugin could process this file\n";
|
---|
157 | $stats->{'num_not_processed'} ++;
|
---|
158 | } else {
|
---|
159 | # was not recognised
|
---|
160 | if ($verbosity >= 2) {
|
---|
161 | &gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n", $file);
|
---|
162 | # print $outhandle "WARNING - no plugin could process $file\n";
|
---|
163 | }
|
---|
164 | # tell the GLI that it was not processed
|
---|
165 | print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
|
---|
166 |
|
---|
167 | $file =~ s/.*?([^\\\/]+)$/$1/;
|
---|
168 | &gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
|
---|
169 | # print $failhandle "$file: no plugin could process this file\n";
|
---|
170 | $stats->{'num_not_recognised'} ++;
|
---|
171 |
|
---|
172 | }
|
---|
173 |
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | # write out some general stats that the plugins have compiled - note that
|
---|
178 | # the buildcol.pl process doesn't currently call this process so the stats
|
---|
179 | # are only output after import.pl -
|
---|
180 | sub write_stats {
|
---|
181 | my ($pluginfo, $statshandle, $faillog, $gli) = @_;
|
---|
182 |
|
---|
183 | $gli = 0 unless defined $gli;
|
---|
184 |
|
---|
185 | foreach $plugobj (@$pluginfo) {
|
---|
186 | $plugobj->compile_stats($stats);
|
---|
187 | }
|
---|
188 |
|
---|
189 | my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
|
---|
190 | $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
|
---|
191 |
|
---|
192 | 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;
|
---|
193 |
|
---|
194 | if ($total == 1) {
|
---|
195 | &gsprintf($statshandle, "* {plugin.one_considered}\n");
|
---|
196 | } else {
|
---|
197 | &gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
|
---|
198 | }
|
---|
199 | if ($stats->{'num_archives'}) {
|
---|
200 | if ($stats->{'num_archives'} == 1) {
|
---|
201 | &gsprintf($statshandle, " ({plugin.including_archive})\n");
|
---|
202 | }
|
---|
203 | else {
|
---|
204 | &gsprintf($statshandle, " ({plugin.including_archives})\n", $stats->{'num_archives'});
|
---|
205 | }
|
---|
206 | }
|
---|
207 | if ($stats->{'num_processed'} == 1) {
|
---|
208 | &gsprintf($statshandle, "* {plugin.one_included}\n");
|
---|
209 | } else {
|
---|
210 | &gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
|
---|
211 | }
|
---|
212 | if ($stats->{'num_not_recognised'}) {
|
---|
213 | if ($stats->{'num_not_recognised'} == 1) {
|
---|
214 | &gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
|
---|
215 | } else {
|
---|
216 | &gsprintf($statshandle, "* {plugin.n_unrecognised}\n", $stats->{'num_not_recognised'});
|
---|
217 | }
|
---|
218 |
|
---|
219 | }
|
---|
220 | if ($stats->{'num_not_processed'}) {
|
---|
221 | if ($stats->{'num_not_processed'} == 1) {
|
---|
222 | &gsprintf($statshandle, "* {plugin.one_rejected}\n");
|
---|
223 | } else {
|
---|
224 | &gsprintf($statshandle, "* {plugin.n_rejected}\n", $stats->{'num_not_processed'});
|
---|
225 | }
|
---|
226 | }
|
---|
227 | if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
|
---|
228 | &gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | sub end {
|
---|
233 | my ($pluginfo, $processor) = @_;
|
---|
234 | map { $_->end($processor); } @$pluginfo;
|
---|
235 | }
|
---|
236 |
|
---|
237 | 1;
|
---|