[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 |
|
---|
| 26 | package plugin;
|
---|
| 27 |
|
---|
[7829] | 28 | use strict; # to pick up typos and undeclared variables...
|
---|
| 29 | no strict 'refs'; # ...but allow filehandles to be variables and vice versa
|
---|
[10579] | 30 | no strict 'subs';
|
---|
[7829] | 31 |
|
---|
[134] | 32 | require util;
|
---|
[7829] | 33 | use gsprintf 'gsprintf';
|
---|
[4] | 34 |
|
---|
[7829] | 35 | # global variables
|
---|
[2785] | 36 | my $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
|
---|
| 44 | my ($verbosity, $outhandle, $failhandle, $globaloptions);
|
---|
[5682] | 45 |
|
---|
[10579] | 46 | sub 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] | 72 | sub load_plugins {
|
---|
[1431] | 73 | my ($plugin_list) = shift @_;
|
---|
[12968] | 74 | my $incremental;
|
---|
| 75 | ($verbosity, $outhandle, $failhandle, $globaloptions, $incremental) = @_; # globals
|
---|
[4] | 76 | my @plugin_objects = ();
|
---|
[12968] | 77 | $incremental = 0 unless (defined $incremental && $incremental == 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 |
|
---|
[12968] | 120 | $plugobj->set_incremental($incremental);
|
---|
[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 |
|
---|
| 130 | sub 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] | 138 | sub 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] | 187 | sub 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 |
|
---|
[7829] | 241 | gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
|
---|
[7363] | 242 | $stats->{'num_not_processed'} ++;
|
---|
| 243 | } else {
|
---|
| 244 | # was not recognised
|
---|
| 245 | if ($verbosity >= 2) {
|
---|
[7829] | 246 | gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
|
---|
[7363] | 247 | }
|
---|
| 248 | # tell the GLI that it was not processed
|
---|
| 249 | print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
|
---|
| 250 |
|
---|
[7829] | 251 | gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
|
---|
[7363] | 252 | $stats->{'num_not_recognised'} ++;
|
---|
[170] | 253 | }
|
---|
[315] | 254 | return 0;
|
---|
[4] | 255 | }
|
---|
| 256 |
|
---|
[2785] | 257 | # write out some general stats that the plugins have compiled - note that
|
---|
| 258 | # the buildcol.pl process doesn't currently call this process so the stats
|
---|
| 259 | # are only output after import.pl -
|
---|
| 260 | sub write_stats {
|
---|
[6332] | 261 | my ($pluginfo, $statshandle, $faillog, $gli) = @_;
|
---|
[2785] | 262 |
|
---|
[6332] | 263 | $gli = 0 unless defined $gli;
|
---|
| 264 |
|
---|
[7829] | 265 | foreach my $plugobj (@$pluginfo) {
|
---|
[2785] | 266 | $plugobj->compile_stats($stats);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
|
---|
[7363] | 270 | $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
|
---|
[2785] | 271 |
|
---|
[7363] | 272 | 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] | 273 |
|
---|
[2785] | 274 | if ($total == 1) {
|
---|
[7829] | 275 | gsprintf($statshandle, "* {plugin.one_considered}\n");
|
---|
[2785] | 276 | } else {
|
---|
[7829] | 277 | gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
|
---|
[2785] | 278 | }
|
---|
| 279 | if ($stats->{'num_archives'}) {
|
---|
[5682] | 280 | if ($stats->{'num_archives'} == 1) {
|
---|
[7829] | 281 | gsprintf($statshandle, " ({plugin.including_archive})\n");
|
---|
[5682] | 282 | }
|
---|
| 283 | else {
|
---|
[7829] | 284 | gsprintf($statshandle, " ({plugin.including_archives})\n",
|
---|
| 285 | $stats->{'num_archives'});
|
---|
[5682] | 286 | }
|
---|
[2785] | 287 | }
|
---|
| 288 | if ($stats->{'num_processed'} == 1) {
|
---|
[7829] | 289 | gsprintf($statshandle, "* {plugin.one_included}\n");
|
---|
[2785] | 290 | } else {
|
---|
[7829] | 291 | gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
|
---|
[2785] | 292 | }
|
---|
[7363] | 293 | if ($stats->{'num_not_recognised'}) {
|
---|
| 294 | if ($stats->{'num_not_recognised'} == 1) {
|
---|
[7829] | 295 | gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
|
---|
[7363] | 296 | } else {
|
---|
[7829] | 297 | gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
|
---|
| 298 | $stats->{'num_not_recognised'});
|
---|
[7363] | 299 | }
|
---|
| 300 |
|
---|
| 301 | }
|
---|
[2797] | 302 | if ($stats->{'num_not_processed'}) {
|
---|
| 303 | if ($stats->{'num_not_processed'} == 1) {
|
---|
[7829] | 304 | gsprintf($statshandle, "* {plugin.one_rejected}\n");
|
---|
[2797] | 305 | } else {
|
---|
[7829] | 306 | gsprintf($statshandle, "* {plugin.n_rejected}\n",
|
---|
| 307 | $stats->{'num_not_processed'});
|
---|
[5682] | 308 | }
|
---|
[7363] | 309 | }
|
---|
| 310 | if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
|
---|
[7829] | 311 | gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
|
---|
[2797] | 312 | }
|
---|
[2785] | 313 | }
|
---|
| 314 |
|
---|
[835] | 315 | sub end {
|
---|
[1587] | 316 | my ($pluginfo, $processor) = @_;
|
---|
| 317 | map { $_->end($processor); } @$pluginfo;
|
---|
[835] | 318 | }
|
---|
[4] | 319 |
|
---|
[10155] | 320 | sub deinit {
|
---|
| 321 | my ($pluginfo, $processor) = @_;
|
---|
| 322 |
|
---|
| 323 |
|
---|
| 324 | map { $_->deinit($processor); } @$pluginfo;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[4] | 327 | 1;
|
---|