source: main/trunk/greenstone2/perllib/plugin.pm

Last change on this file was 32587, checked in by ak19, 5 years ago

Renaming 'site_name' parameter used by GS SQL Plugout and Plugin to 'site' for consistency with existing uses of site.

  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 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
[21307]28
[7829]29use strict; # to pick up typos and undeclared variables...
30no strict 'refs'; # ...but allow filehandles to be variables and vice versa
[10579]31no strict 'subs';
[7829]32
[134]33require util;
[27303]34use FileUtils;
[7829]35use gsprintf 'gsprintf';
[4]36
[15873]37# mapping from old plugin names to new ones for backwards compatibility
[17032]38# can remove at sometime in future when we no longer want to support old xxPlug names in the config file
[15873]39my $plugin_name_map = {
[17746]40 'GAPlug' => 'GreenstoneXMLPlugin',
[15873]41 'ArcPlug' => 'ArchivesInfPlugin',
42 'RecPlug' => 'DirectoryPlugin',
43 'TEXTPlug' => 'TextPlugin',
[17731]44 'XMLPlug' => 'ReadXMLFile',
[15873]45 'EMAILPlug' => 'EmailPlugin',
46 'SRCPlug' => 'SourceCodePlugin',
47 'NULPlug' => 'NulPlugin',
[17731]48 'W3ImgPlug' => 'HTMLImagePlugin',
[17030]49 'PagedImgPlug' => 'PagedImagePlugin',
[17724]50 'METSPlug' => 'GreenstoneMETSPlugin',
51 'PPTPlug' => 'PowerPointPlugin',
[17731]52 'PSPlug' => 'PostScriptPlugin',
[17724]53 'DBPlug' => 'DatabasePlugin'
[15873]54 };
55
[7829]56# global variables
[2785]57my $stats = {'num_processed' => 0,
58 'num_blocked' => 0,
59 'num_not_processed' => 0,
[7363]60 'num_not_recognised' => 0,
[2785]61 'num_archives' => 0
62 };
63
[7829]64#globaloptions contains any options that should be passed to all plugins
65my ($verbosity, $outhandle, $failhandle, $globaloptions);
[5682]66
[15873]67sub get_valid_pluginname {
68 my ($pluginname) = @_;
69 my $valid_name = $pluginname;
70 if (defined $plugin_name_map->{$pluginname}) {
71 $valid_name = $plugin_name_map->{$pluginname};
72 } elsif ($pluginname =~ /Plug$/) {
73 $valid_name =~ s/Plug/Plugin/;
74
75 }
76 return $valid_name;
77}
[21290]78
[14933]79sub load_plugin_require
80{
81 my ($pluginname) = @_;
82
83 my @check_list = ();
84
85 # pp_plugname shorthand for 'perllib' 'plugin' '$pluginname.pm'
86 my $pp_plugname
[27303]87 = &FileUtils::filenameConcatenate('perllib', 'plugins', "${pluginname}.pm");
[14933]88 my $collectdir = $ENV{'GSDLCOLLECTDIR'};
89
[10579]90 # find the plugin
[14239]91 if (defined($ENV{'GSDLCOLLECTION'}))
92 {
[14933]93 my $customplugname
[27303]94 = &FileUtils::filenameConcatenate($collectdir, "custom",$ENV{'GSDLCOLLECTION'},
[14933]95 $pp_plugname);
96 push(@check_list,$customplugname);
[14239]97 }
[14933]98
[27303]99 my $colplugname = &FileUtils::filenameConcatenate($collectdir, $pp_plugname);
[14933]100 push(@check_list,$colplugname);
101
102 if (defined $ENV{'GSDLEXTS'}) {
103
[27303]104 my $ext_prefix = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "ext");
[14933]105
106 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
107 foreach my $e (@extensions) {
[27303]108 my $extplugname = &FileUtils::filenameConcatenate($ext_prefix, $e, $pp_plugname);
[14933]109 push(@check_list,$extplugname);
110
111 }
112 }
[21290]113 if (defined $ENV{'GSDL3EXTS'}) {
[14933]114
[27303]115 my $ext_prefix = &FileUtils::filenameConcatenate($ENV{'GSDL3SRCHOME'}, "ext");
[14933]116
[21290]117 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
118 foreach my $e (@extensions) {
[27303]119 my $extplugname = &FileUtils::filenameConcatenate($ext_prefix, $e, $pp_plugname);
[21290]120 push(@check_list,$extplugname);
121
122 }
123 }
124
125
[27303]126 my $mainplugname = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, $pp_plugname);
[14933]127 push(@check_list,$mainplugname);
128
129 my $success=0;
130 foreach my $plugname (@check_list) {
[27623]131 if (&FileUtils::fileExists($plugname)) {
[26223]132 # lets add perllib folder to INC
[27303]133 # check it isn't already there first [jmt12]
[26223]134 my ($perllibfolder) = $plugname =~ /^(.*[\/\\]perllib)[\/\\]plugins/;
[27623]135 if (&FileUtils::directoryExists($perllibfolder))
[27303]136 {
137 my $found_perllibfolder = 0;
138 foreach my $path (@INC)
139 {
140 if ($path eq $perllibfolder)
141 {
142 $found_perllibfolder = 1;
143 last;
144 }
145 }
146 if (!$found_perllibfolder)
147 {
[26223]148 unshift (@INC, $perllibfolder);
[27303]149 }
150 }
[14933]151 require $plugname;
152 $success=1;
153 last;
154 }
155 }
156
157 if (!$success) {
[10579]158 &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
[14933]159 $pluginname);
[10579]160 die "\n";
161 }
[14933]162}
[10579]163
[14933]164sub load_plugin_for_info {
[25957]165 my ($pluginname, $gs_version) = (@_);
[15873]166 $pluginname = &get_valid_pluginname($pluginname);
[14933]167 load_plugin_require($pluginname);
168
[10579]169 # create a plugin object
170 my ($plugobj);
[25957]171 my $options = "-gsdlinfo,-gs_version,$gs_version";
[10579]172
173 eval ("\$plugobj = new \$pluginname([],[$options])");
174 die "$@" if $@;
175
176 return $plugobj;
177}
178
[4]179sub load_plugins {
[1431]180 my ($plugin_list) = shift @_;
[32539]181 my ($incremental_mode, $gs_version, $site);
182 ($verbosity, $outhandle, $failhandle, $globaloptions, $incremental_mode, $gs_version, $site) = @_; # globals
[4]183 my @plugin_objects = ();
[1243]184 $verbosity = 2 unless defined $verbosity;
[7829]185 $outhandle = 'STDERR' unless defined $outhandle;
186 $failhandle = 'STDERR' unless defined $failhandle;
[1243]187
[27303]188 # before pushing collection perl and plugin directories onto INC, test that
189 # they aren't already there [jmt12]
190 &util::augmentINC(&FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},'perllib'));
191 &util::augmentINC(&FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},'perllib','plugins'));
[13933]192
[6584]193 map { $_ = "\"$_\""; } @$globaloptions;
194 my $globals = join (",", @$globaloptions);
195
[7829]196 foreach my $pluginoptions (@$plugin_list) {
[809]197 my $pluginname = shift @$pluginoptions;
198 next unless defined $pluginname;
[15873]199 $pluginname = &get_valid_pluginname($pluginname);
[14933]200 load_plugin_require($pluginname);
[4]201
202 # create a plugin object
203 my ($plugobj);
[25803]204 # put quotes around each option to the plugin, unless the option is already quoted
205 map { $_ = "\"$_\"" unless ($_ =~ m/^\s*\".*\"\s*$/) ; } @$pluginoptions;
[32587]206 my $site_option = $site ? "\"-site\",\"$site\"," : "";
[32539]207 my $options = "$site_option"."-gs_version,$gs_version,".join (",", @$pluginoptions);
[6584]208 if ($globals) {
209 if (@$pluginoptions) {
210 $options .= ",";
211 }
212 $options .= "$globals";
213 }
[20613]214 # need to escape backslash before putting in to the eval
[22087]215 # but watch out for any \" (which shouldn't be further escaped)
[22221]216 $options =~ s/\\([^"])/\\\\$1/g; #"
[1244]217 $options =~ s/\$/\\\$/g;
[22087]218
[10218]219 eval ("\$plugobj = new \$pluginname([],[$options])");
[4]220 die "$@" if $@;
[809]221
[1243]222 # initialize plugin
[2785]223 $plugobj->init($verbosity, $outhandle, $failhandle);
[10478]224
[20578]225 $plugobj->set_incremental($incremental_mode);
[1243]226
[4]227 # add this object to the list
228 push (@plugin_objects, $plugobj);
229 }
230
231 return \@plugin_objects;
232}
233
[835]234
235sub begin {
[11333]236 my ($pluginfo, $base_dir, $processor, $maxdocs, $gli) = @_;
[835]237
[11333]238 map { $_->{'gli'} = $gli; } @$pluginfo;
[835]239 map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
240}
241
[21307]242 sub remove_all {
[21290]243 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
[24345]244
[21307]245 map { $_->remove_all($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
[21290]246}
247
[21307]248sub remove_some {
[21618]249 my ($pluginfo, $infodbtype, $archivedir, $deleted_files) = @_;
[21307]250 return if (scalar(@$deleted_files)==0);
[23170]251 $infodbtype = "gdbm" if $infodbtype eq "gdbm-txtgz";
[21618]252 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-src", $archivedir);
[21290]253
[32566]254 my $all_files_processed_successfully = 1;
255
[21307]256 foreach my $file (@$deleted_files) {
[21564]257 # use 'archiveinf-src' info database to look up all the OIDs
[21307]258 # that this file is used in (note in most cases, it's just one OID)
[32565]259
260 my $processed_file = 0; # set to 1 if a plugin could process the file and did so successfully
[21307]261
[30597]262 my $file_with_placeholders = &util::abspath_to_placeholders($file);
263 my $src_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_src_filename, $file_with_placeholders);
[21307]264 my $oids = $src_rec->{'oid'};
[21314]265 my $rv;
266 foreach my $plugobj (@$pluginfo) {
267
268 $rv = $plugobj->remove_one($file, $oids, $archivedir);
269 if (defined $rv && $rv != -1) {
[32565]270 $processed_file = 1;
271 last; # break and continue with outer for loop, to process other deleted files
[21314]272 } # else undefined (was not recognised by the plugin) or there was an error, try the next one
273 }
[32565]274
275 if (!$processed_file) { # no plugin could recognise file.
276 # Should we continue processing other deleted files or not?
[32566]277 print STDERR "WARNING: plugin::remove_some() failed to process $file with oid(s) ". join(",", @$oids) . "\n";
278 #return 0;
[32572]279 $all_files_processed_successfully = 0;
[32565]280 } # else some plugin processed the current deleted file
281 # continue to process next deleted file
282
[21307]283 }
[32566]284 return $all_files_processed_successfully; # callers don't seem to do anything with return val
[32565]285}
[21307]286
[16381]287sub file_block_read {
288 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
[10155]289
[16381]290
291 $gli = 0 unless defined $gli;
292
293 my $rv = 0;
294 my $glifile = $file;
295
296 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
297
298 # Announce to GLI that we are handling a file
299 print STDERR "<File n='$glifile'>\n" if $gli;
300
301 # the .kill file is a handy (if not very elegant) way of aborting
302 # an import.pl or buildcol.pl process
[27623]303 if (&FileUtils::fileExists(&FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, ".kill"))) {
[16381]304 gsprintf($outhandle, "{plugin.kill_file}\n");
305 die "\n";
306 }
307
308 foreach my $plugobj (@$pluginfo) {
309
310 $rv = $plugobj->file_block_read($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli);
311 #last if (defined $rv && $rv==1); # stop this file once we have found something to 'process' it
312 }
313
314}
315
316
[8515]317sub metadata_read {
[19497]318 my ($pluginfo, $base_dir, $file, $block_hash,
319 $extrametakeys, $extrametadata, $extrametafile,
[23212]320 $processor, $gli, $aux) = @_;
[8515]321
322 $gli = 0 unless defined $gli;
323
324 my $rv = 0;
325 my $glifile = $file;
326
327 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
328
329 # Announce to GLI that we are handling a file
330 print STDERR "<File n='$glifile'>\n" if $gli;
331
332 # the .kill file is a handy (if not very elegant) way of aborting
333 # an import.pl or buildcol.pl process
[27623]334 if (&FileUtils::fileExists(&FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, ".kill"))) {
[8515]335 gsprintf($outhandle, "{plugin.kill_file}\n");
336 die "\n";
337 }
338
339 my $had_error = 0;
340 # pass this file by each of the plugins in turn until one
341 # is found which will process it
342 # read must return:
343 # undef - could not recognise
344 # -1 - tried but error
345 # 0 - blocked
346 # anything else for successful processing
347
348 foreach my $plugobj (@$pluginfo) {
349
[16381]350 $rv = $plugobj->metadata_read($pluginfo, $base_dir, $file, $block_hash,
[19497]351 $extrametakeys, $extrametadata, $extrametafile,
[23212]352 $processor, $gli, $aux);
[8515]353
354 if (defined $rv) {
355 if ($rv == -1) {
356 # an error has occurred
357 $had_error = 1;
358 print STDERR "<ProcessingError n='$glifile'>\n" if $gli;
359 } else {
360 return $rv;
361 }
362 } # else undefined - was not recognised by the plugin
363 }
364
365 return 0;
366}
367
[4]368sub read {
[16381]369 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli, $aux) = @_;
[4]370
[809]371 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
[9853]372 $total_count = 0 unless defined $total_count && $total_count =~ /\d/;
[6332]373 $gli = 0 unless defined $gli;
374
[315]375 my $rv = 0;
[7363]376 my $glifile = $file;
[7904]377
[7363]378 $glifile =~ s/^[\/\\]+//; # file sometimes starts with a / so get rid of it
[8515]379
[6332]380 # Announce to GLI that we are handling a file
[7363]381 print STDERR "<File n='$glifile'>\n" if $gli;
[8515]382
[1454]383 # the .kill file is a handy (if not very elegant) way of aborting
384 # an import.pl or buildcol.pl process
[27623]385 if (&FileUtils::fileExists(&FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, ".kill"))) {
[7829]386 gsprintf($outhandle, "{plugin.kill_file}\n");
[1454]387 die "\n";
388 }
[18441]389
[7363]390 my $had_error = 0;
[4]391 # pass this file by each of the plugins in turn until one
392 # is found which will process it
[7363]393 # read must return:
394 # undef - could not recognise
395 # -1 - tried but error
396 # 0 - blocked
397 # anything else for successful processing
[8515]398
[7829]399 foreach my $plugobj (@$pluginfo) {
[8515]400
401 $rv = $plugobj->read($pluginfo, $base_dir, $file,
[16381]402 $block_hash, $metadata, $processor, $maxdocs,
403 $total_count, $gli, $aux);
[8515]404
405 if (defined $rv) {
[7363]406 if ($rv == -1) {
[7904]407 # an error has occurred
[7363]408 $had_error = 1;
409 } else {
[7904]410 return $rv;
[7363]411 }
412 } # else undefined - was not recognised by the plugin
[4]413 }
[7904]414
[7363]415 if ($had_error) {
416 # was recognised but couldn't be processed
417 if ($verbosity >= 2) {
[7829]418 gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
[7363]419 }
420 # tell the GLI that it was not processed
421 print STDERR "<NonProcessedFile n='$glifile'>\n" if $gli;
[7904]422
[7829]423 gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
[7363]424 $stats->{'num_not_processed'} ++;
425 } else {
426 # was not recognised
427 if ($verbosity >= 2) {
[7829]428 gsprintf($outhandle, "{plugin.no_plugin_could_recognise}\n",$file);
[7363]429 }
430 # tell the GLI that it was not processed
431 print STDERR "<NonRecognisedFile n='$glifile'>\n" if $gli;
432
[7829]433 gsprintf($failhandle, "$file: {plugin.no_plugin_could_recognise_this_file}\n");
[7363]434 $stats->{'num_not_recognised'} ++;
[170]435 }
[315]436 return 0;
[4]437}
438
[2785]439# write out some general stats that the plugins have compiled - note that
440# the buildcol.pl process doesn't currently call this process so the stats
441# are only output after import.pl -
442sub write_stats {
[6332]443 my ($pluginfo, $statshandle, $faillog, $gli) = @_;
[2785]444
[6332]445 $gli = 0 unless defined $gli;
446
[7829]447 foreach my $plugobj (@$pluginfo) {
[2785]448 $plugobj->compile_stats($stats);
449 }
450
451 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
[7363]452 $stats->{'num_not_processed'} + $stats->{'num_not_recognised'};
[2785]453
[7363]454 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]455
[2785]456 if ($total == 1) {
[7829]457 gsprintf($statshandle, "* {plugin.one_considered}\n");
[2785]458 } else {
[7829]459 gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
[2785]460 }
461 if ($stats->{'num_archives'}) {
[5682]462 if ($stats->{'num_archives'} == 1) {
[7829]463 gsprintf($statshandle, " ({plugin.including_archive})\n");
[5682]464 }
465 else {
[7829]466 gsprintf($statshandle, " ({plugin.including_archives})\n",
467 $stats->{'num_archives'});
[5682]468 }
[2785]469 }
470 if ($stats->{'num_processed'} == 1) {
[7829]471 gsprintf($statshandle, "* {plugin.one_included}\n");
[2785]472 } else {
[7829]473 gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
[2785]474 }
[7363]475 if ($stats->{'num_not_recognised'}) {
476 if ($stats->{'num_not_recognised'} == 1) {
[7829]477 gsprintf($statshandle, "* {plugin.one_unrecognised}\n");
[7363]478 } else {
[7829]479 gsprintf($statshandle, "* {plugin.n_unrecognised}\n",
480 $stats->{'num_not_recognised'});
[7363]481 }
482
483 }
[2797]484 if ($stats->{'num_not_processed'}) {
485 if ($stats->{'num_not_processed'} == 1) {
[7829]486 gsprintf($statshandle, "* {plugin.one_rejected}\n");
[2797]487 } else {
[7829]488 gsprintf($statshandle, "* {plugin.n_rejected}\n",
489 $stats->{'num_not_processed'});
[5682]490 }
[7363]491 }
492 if ($stats->{'num_not_processed'} || $stats->{'num_not_recognised'}) {
[7829]493 gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
[2797]494 }
[2785]495}
496
[835]497sub end {
[1587]498 my ($pluginfo, $processor) = @_;
499 map { $_->end($processor); } @$pluginfo;
[835]500}
[4]501
[10155]502sub deinit {
503 my ($pluginfo, $processor) = @_;
504
505
506 map { $_->deinit($processor); } @$pluginfo;
507}
508
[4]5091;
Note: See TracBrowser for help on using the repository browser.