source: trunk/gsdl/perllib/plugin.pm@ 2797

Last change on this file since 2797 was 2797, checked in by sjboddie, 23 years ago

* empty log message *

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 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
28require util;
29
30my $stats = {'num_processed' => 0,
31 'num_blocked' => 0,
32 'num_not_processed' => 0,
33 'num_archives' => 0
34 };
35
36sub load_plugins {
37 my ($plugin_list) = shift @_;
38 ($verbosity, $outhandle, $failhandle) = @_; # globals
39 my @plugin_objects = ();
40
41 $verbosity = 2 unless defined $verbosity;
42 $outhandle = STDERR unless defined $outhandle;
43 $failhandle = STDERR unless defined $failhandle;
44
45 foreach $pluginoptions (@$plugin_list) {
46 my $pluginname = shift @$pluginoptions;
47 next unless defined $pluginname;
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 { die "ERROR - couldn't find plugin \"$pluginname\"\n"; }
57
58 # create a plugin object
59 my ($plugobj);
60 map { $_ = "\"$_\""; } @$pluginoptions;
61 my $options = join (",", @$pluginoptions);
62 $options =~ s/\$/\\\$/g;
63 eval ("\$plugobj = new \$pluginname($options)");
64 die "$@" if $@;
65
66 # initialize plugin
67 $plugobj->init($verbosity, $outhandle, $failhandle);
68
69 # add this object to the list
70 push (@plugin_objects, $plugobj);
71 }
72
73 return \@plugin_objects;
74}
75
76
77sub begin {
78 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
79
80 map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
81}
82
83sub read {
84 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $aux) = @_;
85
86 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
87 my $rv = 0;
88
89 # the .kill file is a handy (if not very elegant) way of aborting
90 # an import.pl or buildcol.pl process
91 if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
92 print $outhandle "Process killed by .kill file\n";
93 die "\n";
94 }
95
96 # pass this file by each of the plugins in turn until one
97 # is found which will process it
98 foreach $plugobj (@$pluginfo) {
99 $rv = $plugobj->read($pluginfo, $base_dir, $file,
100 $metadata, $processor, $maxdocs, $aux);
101 return $rv if defined $rv;
102 }
103
104 if ($verbosity >= 2) {
105 print $outhandle "WARNING - no plugin could process $file\n";
106 }
107
108 $file =~ s/.*?([^\\\/]+)$/$1/;
109 print $failhandle "$file: no plugin could process this file\n";
110 $stats->{'num_not_processed'} ++;
111
112 return 0;
113}
114
115# write out some general stats that the plugins have compiled - note that
116# the buildcol.pl process doesn't currently call this process so the stats
117# are only output after import.pl -
118sub write_stats {
119 my ($pluginfo, $statshandle) = @_;
120
121 foreach $plugobj (@$pluginfo) {
122 $plugobj->compile_stats($stats);
123 }
124
125 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
126 $stats->{'num_not_processed'};
127
128 if ($total == 1) {
129 print $statshandle "* 1 document was considered for processing\n";
130 } else {
131 print $statshandle "* $total documents were considered for processing\n";
132 }
133 if ($stats->{'num_archives'}) {
134 print $statshandle " (including the contents of " . $stats->{'num_archives'} .
135 " ZIP/TAR archive";
136 if ($stats->{'num_archives'} == 1) {print $statshandle ")\n";}
137 else {print $statshandle "s)\n";}
138 }
139 if ($stats->{'num_processed'} == 1) {
140 print $statshandle "* 1 was processed and included in the collection\n";
141 } else {
142 print $statshandle "* " . $stats->{'num_processed'} . " were processed and included in the collection\n";
143 }
144 if ($stats->{'num_not_processed'}) {
145 if ($stats->{'num_not_processed'} == 1) {
146 print $statshandle "* 1 was rejected.";
147 } else {
148 print $statshandle "* " . $stats->{'num_not_processed'} . " were rejected.";
149 }
150 print $statshandle " See the fail log for a list of rejected documents\n";
151 }
152}
153
154sub end {
155 my ($pluginfo, $processor) = @_;
156 map { $_->end($processor); } @$pluginfo;
157}
158
1591;
Note: See TracBrowser for help on using the repository browser.