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