source: main/trunk/greenstone2/bin/script/pluginfo.pl@ 31888

Last change on this file since 31888 was 25957, checked in by kjdon, 12 years ago

adding in support for plugins knowing what version of greenstone (2/3) they are running in. gs_version now passed in when loading plugins, and '-gs_version 2/3' will be added to input args. base plugin class PrintInfo defines the gs_version arg

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# pluginfo.pl --
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# This program will print info about a plugin
29
30use strict;
31no strict 'subs'; # allow barewords (eg STDERR) as function arguments
32
33BEGIN {
34 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
35 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
38# unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
39 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
40
41 if (defined $ENV{'GSDLEXTS'}) {
42 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
43 foreach my $e (@extensions) {
44 my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
45
46 unshift (@INC, "$ext_prefix/perllib");
47 unshift (@INC, "$ext_prefix/perllib/cpan");
48 unshift (@INC, "$ext_prefix/perllib/plugins");
49 }
50 }
51 if (defined $ENV{'GSDL3EXTS'}) {
52 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
53 foreach my $e (@extensions) {
54 my $ext_prefix = "$ENV{'GSDL3SRCHOME'}/ext/$e";
55
56 unshift (@INC, "$ext_prefix/perllib");
57 unshift (@INC, "$ext_prefix/perllib/cpan");
58 unshift (@INC, "$ext_prefix/perllib/plugins");
59 }
60 }
61
62}
63
64use plugin;
65use util;
66use gsprintf;
67use printusage;
68use parse2;
69
70my $arguments =
71 [ { 'name' => "site",
72 'desc' => "{pluginfo.site}",
73 'type' => "string",
74 'reqd' => "no" },
75 { 'name' => "collection",
76 'desc' => "{pluginfo.collection}",
77 'type' => "string",
78 'reqd' => "no" },
79 { 'name' => "gs_version",
80 'desc' => "{pluginfo.gs_version}",
81 'type' => "string",
82 'reqd' => "no",
83 'hiddengli' => "yes" },
84 { 'name' => "xml",
85 'desc' => "{scripts.xml}",
86 'type' => "flag",
87 'reqd' => "no" },
88 { 'name' => "listall",
89 'desc' => "{scripts.listall}",
90 'type' => "flag",
91 'reqd' => "no" },
92 { 'name' => "describeall",
93 'desc' => "{scripts.describeall}",
94 'type' => "flag",
95 'reqd' => "no" },
96 { 'name' => "language",
97 'desc' => "{scripts.language}",
98 'type' => "string",
99 'reqd' => "no" } ];
100
101my $options = { 'name' => "pluginfo.pl",
102 'desc' => "{pluginfo.desc}",
103 'args' => $arguments };
104
105sub gsprintf
106{
107 return &gsprintf::gsprintf(@_);
108}
109
110
111sub main {
112 my $site = "";
113 my $collection = "";
114 my $gs_version = "";
115 my $xml = 0;
116 my $listall = 0;
117 my $describeall = 0;
118 my ($language, $encoding);
119
120 my $hashParsingResult = {};
121 # general options available to all plugins
122 my $unparsed_args = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
123 # parse returns -1 if an error occurred
124 if ($unparsed_args == -1) {
125
126 PrintUsage::print_txt_usage($options, "{pluginfo.params}");
127 die "\n";
128 }
129 foreach my $strVariable (keys %$hashParsingResult)
130 {
131 my $value = $hashParsingResult->{$strVariable};
132 # test to make sure the variable name is 'safe'
133 if ($strVariable !~ /^\w+$/) {
134 die "variable name '$strVariable' isn't safe!";
135 }
136 eval "\$$strVariable = \$value";
137 }
138
139 if ($xml) {
140 &gsprintf::set_print_freetext_for_xml();
141 }
142
143 # if language wasn't specified, see if it is set in the environment
144 # (LC_ALL or LANG)
145 if (!$language && ($_=$ENV{'LC_ALL'} or $_=$ENV{'LANG'})) {
146 m/^([^\.]+)\.?(.*)/;
147 $language=$1;
148 $encoding=$2; # might be undef...
149# gsprintf::load_language* thinks "fr" is completely different to "fr_FR"...
150 $language =~ s/_.*$//;
151 }
152
153 # If $language has been set, load the appropriate resource bundle
154 # (Otherwise, the default resource bundle will be loaded automatically)
155 if ($language) {
156 gsprintf::load_language_specific_resource_bundle($language);
157 if ($encoding) {
158 $encoding =~ tr/-/_/;
159 $encoding = lc($encoding);
160 $encoding =~ s/utf_8/utf8/; # special
161 $gsprintf::specialoutputencoding=$encoding;
162 }
163 }
164
165 # If there is not exactly 1 argument left (plugin name), then the arguments were wrong
166 # If the user specified -h, then we output the usage also
167 if((@ARGV && $ARGV[0] =~ /^\-+h/) )
168 {
169 PrintUsage::print_txt_usage($options, "{pluginfo.params}");
170 die "\n";
171 }
172
173 # If there is not exactly 1 argument left (plugin name), then the arguments were wrong (apart from if we had listall or describeall set)
174 if ($listall == 0 && $describeall ==0 && $unparsed_args == 0) {
175 gsprintf(STDERR, "{pluginfo.no_plugin_name}\n\n");
176 PrintUsage::print_txt_usage($options, "{pluginfo.params}", 1);
177 die "\n";
178 }
179
180 # we had some arguments that we weren't expecting
181 if ($unparsed_args > 1) {
182 pop(@ARGV); # assume that the last arg is the plugin name
183 gsprintf(STDERR, "{common.invalid_options}\n\n", join (',', @ARGV));
184 PrintUsage::print_txt_usage($options, "{pluginfo.params}", 1);
185 die "\n";
186 }
187
188 my $plugin = shift (@ARGV);
189 if (defined $plugin) {
190 $plugin =~ s/\.pm$//; # allow xxxPlugin.pm as the argument
191 }
192
193 if ($site ne "") {
194 # assume Greenstone 3
195 $gs_version = "3" if $gs_version eq "";
196 if ($collection ne "") {
197 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect", $collection);
198 } else {
199 # Probably more useful to default to GS2 area for plugins
200 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
201 }
202 }
203 else {
204 $gs_version = "2" if $gs_version eq "";
205 if ($collection ne "") {
206 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
207 } else {
208 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
209 }
210 }
211
212 if ($listall || $describeall) {
213 my $plugins_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "plugins");
214 my @plugin_list = ();
215 if (opendir (INDIR, $plugins_dir)) {
216 @plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
217 closedir (INDIR);
218 }
219
220 if ((defined $ENV{'GSDLEXTS'}) && ($collection eq "")) {
221 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
222 foreach my $e (@extensions) {
223 my $ext_prefix = &util::filename_cat($ENV{'GSDLHOME'},"ext",$e);
224 my $ext_plugins_dir = &util::filename_cat($ext_prefix, "perllib", "plugins");
225
226 if (opendir (INDIR, $ext_plugins_dir)) {
227 my @ext_plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
228 closedir (INDIR);
229
230 push(@plugin_list,@ext_plugin_list);
231 }
232
233 }
234 }
235 if ((defined $ENV{'GSDL3EXTS'}) && ($collection eq "")) {
236 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
237 foreach my $e (@extensions) {
238 my $ext_prefix = &util::filename_cat($ENV{'GSDL3SRCHOME'},"ext",$e);
239 my $ext_plugins_dir = &util::filename_cat($ext_prefix, "perllib", "plugins");
240
241 if (opendir (INDIR, $ext_plugins_dir)) {
242 my @ext_plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
243 closedir (INDIR);
244
245 push(@plugin_list,@ext_plugin_list);
246 }
247
248 }
249 }
250
251 # load up the plugins before writing out the xml so that any error
252 # messages are not inside the XML output (can cause parsing to fail)
253 my @plugobj_list;
254 foreach my $plugin (@plugin_list) {
255 $plugin =~ s/\.pm$//;
256 my $plugobj = &plugin::load_plugin_for_info ($plugin, $gs_version);
257 push (@plugobj_list, $plugobj);
258 }
259
260 &gsprintf::set_print_xml_tags();
261 print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
262 print STDERR "<PluginList length=\"" . scalar(@plugin_list) . "\">\n";
263 foreach my $plugobj (@plugobj_list) {
264 if ($describeall) {
265 $plugobj->print_xml_usage(0);
266 }
267 else {
268 $plugobj->print_xml_usage(0, 1);
269 }
270 }
271 print STDERR "</PluginList>\n";
272
273
274 }
275 else {
276 &print_single_plugin($plugin, $gs_version, $xml, 1);
277 }
278}
279
280
281sub print_single_plugin {
282 my ($plugin, $gs_version, $xml, $header) = @_;
283 my $plugobj = &plugin::load_plugin_for_info ($plugin, $gs_version);
284 if ($xml) {
285 &gsprintf::set_print_xml_tags();
286 $plugobj->print_xml_usage($header);
287 }
288 else {
289
290 # this causes us to automatically send output to a pager, if one is
291 # set, AND our output is going to a terminal
292 # active state perl on windows doesn't do open(handle, "-|");
293 if ($ENV{'GSDLOS'} !~ /windows/ && -t STDOUT) {
294 my $pager = $ENV{"PAGER"};
295 if (! $pager) {$pager="(less || more)"}
296 my $pid = open(STDIN, "-|"); # this does a fork... see man perlipc(1)
297 if (!defined $pid) {
298 gsprintf(STDERR, "pluginfo.pl - can't fork: $!");
299 } else {
300 if ($pid != 0) { # parent (ie forking) process. child gets 0
301 exec ($pager);
302 }
303 }
304 open(STDERR,">&STDOUT"); # so it's easier to pipe output
305 }
306
307 gsprintf(STDERR, "\n{pluginfo.passing_options}\n\n");
308 gsprintf(STDERR, "{pluginfo.option_types}:\n\n");
309 gsprintf(STDERR, "{pluginfo.specific_options}\n\n");
310 gsprintf(STDERR, "{pluginfo.general_options}\n\n");
311 gsprintf(STDERR, "$plugin {pluginfo.info}:\n\n");
312
313 $plugobj->print_txt_usage();
314 }
315
316}
317
318&main ();
Note: See TracBrowser for help on using the repository browser.