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

Last change on this file since 5682 was 5682, checked in by mdewsnip, 21 years ago

Changed to use the gsprintf module, which makes using strings from the resource bundle much easier.

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