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

Last change on this file since 6332 was 6332, checked in by jmt12, 20 years ago

When -gli argument is provided to calling script these modules will now output gli specific, non-language nor verbosity specific, messages

  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 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, $gli, $aux) = @_;
95
96 $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
97 $gli = 0 unless defined $gli;
98
99 my $rv = 0;
100
101 # Announce to GLI that we are handling a file
102 print STDERR "<File n='$file'>\n" if $gli;
103
104 # the .kill file is a handy (if not very elegant) way of aborting
105 # an import.pl or buildcol.pl process
106 if (-e &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, ".kill")) {
107 &gsprintf($outhandle, "{plugin.kill_file}\n");
108 # print $outhandle "Process killed by .kill file\n";
109 die "\n";
110 }
111
112 # pass this file by each of the plugins in turn until one
113 # is found which will process it
114 foreach $plugobj (@$pluginfo) {
115 $rv = $plugobj->read($pluginfo, $base_dir, $file,
116 $metadata, $processor, $maxdocs, $gli, $aux);
117 return $rv if defined $rv;
118 }
119
120 if ($verbosity >= 2) {
121 &gsprintf($outhandle, "{plugin.no_plugin_could_process}\n", $file);
122 # print $outhandle "WARNING - no plugin could process $file\n";
123 }
124
125 $file =~ s/.*?([^\\\/]+)$/$1/;
126 &gsprintf($failhandle, "$file: {plugin.no_plugin_could_process_this_file}\n");
127 # print $failhandle "$file: no plugin could process this file\n";
128 $stats->{'num_not_processed'} ++;
129
130 return 0;
131}
132
133# write out some general stats that the plugins have compiled - note that
134# the buildcol.pl process doesn't currently call this process so the stats
135# are only output after import.pl -
136sub write_stats {
137 my ($pluginfo, $statshandle, $faillog, $gli) = @_;
138
139 $gli = 0 unless defined $gli;
140
141 foreach $plugobj (@$pluginfo) {
142 $plugobj->compile_stats($stats);
143 }
144
145 my $total = $stats->{'num_processed'} + $stats->{'num_blocked'} +
146 $stats->{'num_not_processed'};
147
148 print STDERR "<ImportComplete c='$stats->{'num_processed'}' p='$stats->{'num_processed'}'>\n" if $gli;
149
150 if ($total == 1) {
151 &gsprintf($statshandle, "* {plugin.one_considered}\n");
152 # print $statshandle "* 1 document was considered for processing\n";
153 } else {
154 &gsprintf($statshandle, "* {plugin.n_considered}\n", $total);
155 # print $statshandle "* $total documents were considered for processing\n";
156 }
157 if ($stats->{'num_archives'}) {
158 # print $statshandle " (including the contents of " . $stats->{'num_archives'} .
159 # " ZIP/TAR archive";
160 if ($stats->{'num_archives'} == 1) {
161 &gsprintf($statshandle, " ({plugin.including_archive})\n");
162 # print $statshandle ")\n";}
163 }
164 else {
165 &gsprintf($statshandle, " ({plugin.including_archives})\n", $stats->{'num_archives'});
166 # print $statshandle "s)\n";}
167 }
168 }
169 if ($stats->{'num_processed'} == 1) {
170 &gsprintf($statshandle, "* {plugin.one_included}\n");
171 # print $statshandle "* 1 was processed and included in the collection\n";
172 } else {
173 &gsprintf($statshandle, "* {plugin.n_included}\n", $stats->{'num_processed'});
174 # print $statshandle "* " . $stats->{'num_processed'} . " were processed and included in the collection\n";
175 }
176 if ($stats->{'num_not_processed'}) {
177 if ($stats->{'num_not_processed'} == 1) {
178 &gsprintf($statshandle, "* {plugin.one_rejected}\n");
179 # print $statshandle "* 1 was rejected.";
180 } else {
181 &gsprintf($statshandle, "* {plugin.n_rejected}\n", $stats->{'num_not_processed'});
182 # print $statshandle "* " . $stats->{'num_not_processed'} . " were rejected.";
183 }
184 &gsprintf($statshandle, " {plugin.see_faillog}\n", $faillog);
185 # print $statshandle " See $faillog for a list of rejected documents\n";
186 }
187}
188
189sub end {
190 my ($pluginfo, $processor) = @_;
191 map { $_->end($processor); } @$pluginfo;
192}
193
1941;
Note: See TracBrowser for help on using the repository browser.