source: main/trunk/greenstone2/perllib/plugins/PrintInfo.pm@ 32586

Last change on this file since 32586 was 32586, 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:executable set to *
File size: 9.4 KB
Line 
1###########################################################################
2#
3# PrintInfo - most base plugin
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2008 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27## Most basic plugin, just handles parsing the arguments and printing out descriptions. Used for plugins and Extractor plugins
28
29package PrintInfo;
30
31BEGIN {
32 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
33}
34
35eval {require bytes};
36eval "require diagnostics"; # some perl distros (eg mac) don't have this
37
38# suppress the annoying "subroutine redefined" warning that various
39# plugins cause under perl 5.6
40$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
41
42use strict;
43no strict 'subs';
44
45use gsprintf 'gsprintf';
46use parse2;
47use printusage;
48
49my $arguments = [
50 { 'name' => "gs_version",
51 'desc' => "{PrintInfo.gs_version}",
52 'type' => "string",
53 'reqd' => "no",
54 'hiddengli' => "yes" },
55 { 'name' => "site",
56 'desc' => "{PrintInfo.site}",
57 'type' => "string",
58 'reqd' => "no",
59 'hiddengli' => "yes" }
60
61];
62
63my $options = { 'name' => "PrintInfo",
64 'desc' => "{PrintInfo.desc}",
65 'abstract' => "yes",
66 'inherits' => "no",
67 'args' => $arguments };
68
69# $auxiliary_plugin argument passed in by "on-the-side" plugin helpers such as Extractors and ImageConverter. We don't want parsing of args done by them.
70sub new
71{
72 my $class = shift (@_);
73 my ($pluginlist,$args,$hashArgOptLists, $auxiliary) = @_;
74 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
75
76 if ($plugin_name eq $class) {
77 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
78 push(@{$hashArgOptLists->{"OptList"}},$options);
79 }
80 my $self = {};
81 $self->{'outhandle'} = STDERR;
82 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
83 $self->{"info_only"} = 0;
84 $self->{'gs_version'} = "2";
85 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
86 # the args, just return the object.
87 # gsdlinfo must come before gs_version. both are set by plugin.pm
88 # The optional -site <site-name> (present only for GS3 and when not doing gsdlinfo),
89 # if it is there, also appears before gs_version and is also set by plugin.pm
90 my $v=0;
91 foreach my $strArg (@{$args})
92 {
93 if($v eq "-site") {
94 $self->{'site'} = $strArg;
95 }
96 elsif($v eq "-gs_version") {
97 $self->{'gs_version'} = $strArg;
98 last;
99 }
100 elsif($strArg eq "-gsdlinfo")
101 {
102 $self->{"info_only"} = 1;
103 #return bless $self, $class;
104 }
105 elsif ($strArg eq "-gs_version" || $strArg eq "-site") {
106 $v = $strArg;
107 }
108 }
109
110 if ($self->{"info_only"}) {
111 return bless $self, $class;
112 }
113 if (defined $auxiliary) { # don't parse the args here
114 return bless $self, $class;
115 }
116
117 # now that we are passed printing out info, we do need to add in this class's options so that they are available for parsing.
118 if ($plugin_name ne $class) {
119 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
120 push(@{$hashArgOptLists->{"OptList"}},$options);
121 }
122
123 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
124 {
125 my $classTempClass = bless $self, $class;
126 print STDERR "<BadPlugin p=$plugin_name>\n";
127 &gsprintf(STDERR, "\n{PrintInfo.bad_general_option}\n", $plugin_name);
128 $classTempClass->print_txt_usage(""); # Use default resource bundle
129 die "\n";
130 }
131
132 delete $self->{"info_only"};
133 # else parsing was successful.
134
135 $self->{'plugin_type'} = $plugin_name;
136
137 return bless $self, $class;
138
139}
140
141#sub init {
142#}
143
144sub set_incremental {
145 my $self = shift(@_);
146 my ($incremental_mode) = @_;
147
148 if (!defined $incremental_mode) {
149 $self->{'incremental'} = 0;
150 $self->{'incremental_mode'} = "none";
151 }
152 elsif ($incremental_mode eq "all") {
153 $self->{'incremental'} = 1;
154 $self->{'incremental_mode'} = "all";
155 }
156 else {
157 # none, onlyadd
158 $self->{'incremental'} = 0;
159 $self->{'incremental_mode'} = $incremental_mode;
160 }
161}
162
163sub get_arguments
164{
165 my $self = shift(@_);
166 my $optionlistref = $self->{'option_list'};
167 my @optionlist = @$optionlistref;
168 my $pluginoptions = pop(@$optionlistref);
169 my $pluginarguments = $pluginoptions->{'args'};
170 return $pluginarguments;
171}
172
173
174sub print_xml_usage
175{
176 my $self = shift(@_);
177 my $header = shift(@_);
178 my $high_level_information_only = shift(@_);
179
180 # XML output is always in UTF-8
181 gsprintf::output_strings_in_UTF8;
182
183 if ($header) {
184 &PrintUsage::print_xml_header("plugin");
185 }
186 $self->print_xml($high_level_information_only);
187}
188
189
190sub print_xml
191{
192 my $self = shift(@_);
193 my $high_level_information_only = shift(@_);
194
195 my $optionlistref = $self->{'option_list'};
196 my @optionlist = @$optionlistref;
197 my $pluginoptions = shift(@$optionlistref);
198 return if (!defined($pluginoptions));
199
200 # Find the process and block default expressions in the plugin arguments
201 my $process_exp = "";
202 my $block_exp = "";
203 if (defined($pluginoptions->{'args'})) {
204 foreach my $option (@{$pluginoptions->{'args'}}) {
205 if ($option->{'name'} eq "process_exp") {
206 $process_exp = $option->{'deft'};
207 }
208 if ($option->{'name'} eq "block_exp") {
209 $block_exp = $option->{'deft'};
210 }
211 }
212 }
213
214 gsprintf(STDERR, "<PlugInfo>\n");
215 gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
216 my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
217 $desc =~ s/</&amp;lt;/g; # doubly escaped
218 $desc =~ s/>/&amp;gt;/g;
219 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
220 gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
221 gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
222 gsprintf(STDERR, " <Processes>$process_exp</Processes>\n");
223 gsprintf(STDERR, " <Blocks>$block_exp</Blocks>\n");
224 gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
225 # adding new option that works with replace_srcdoc_with_html.pl
226 gsprintf(STDERR, " <SourceReplaceable>" . ($pluginoptions->{'srcreplaceable'} || "no") . "</SourceReplaceable>\n");
227 unless (defined($high_level_information_only)) {
228 gsprintf(STDERR, " <Arguments>\n");
229 if (defined($pluginoptions->{'args'})) {
230 &PrintUsage::print_options_xml($pluginoptions->{'args'});
231 }
232 gsprintf(STDERR, " </Arguments>\n");
233
234 # Recurse up the plugin hierarchy
235 $self->print_xml();
236 }
237 gsprintf(STDERR, "</PlugInfo>\n");
238}
239
240
241sub print_txt_usage
242{
243 my $self = shift(@_);
244 # Print the usage message for a plugin (recursively)
245 my $descoffset = $self->determine_description_offset(0);
246 $self->print_plugin_usage($descoffset, 1);
247}
248
249
250sub determine_description_offset
251{
252 my $self = shift(@_);
253 my $maxoffset = shift(@_);
254
255 my $optionlistref = $self->{'option_list'};
256 my @optionlist = @$optionlistref;
257 my $pluginoptions = shift(@$optionlistref);
258 return $maxoffset if (!defined($pluginoptions));
259
260 # Find the length of the longest option string of this plugin
261 my $pluginargs = $pluginoptions->{'args'};
262 if (defined($pluginargs)) {
263 my $longest = &PrintUsage::find_longest_option_string($pluginargs);
264 if ($longest > $maxoffset) {
265 $maxoffset = $longest;
266 }
267 }
268
269 # Recurse up the plugin hierarchy
270 $maxoffset = $self->determine_description_offset($maxoffset);
271 $self->{'option_list'} = \@optionlist;
272 return $maxoffset;
273}
274
275
276sub print_plugin_usage
277{
278 my $self = shift(@_);
279 my $descoffset = shift(@_);
280 my $isleafclass = shift(@_);
281
282 my $optionlistref = $self->{'option_list'};
283 my @optionlist = @$optionlistref;
284 my $pluginoptions = shift(@$optionlistref);
285 return if (!defined($pluginoptions));
286
287 my $pluginname = $pluginoptions->{'name'};
288 my $pluginargs = $pluginoptions->{'args'};
289 my $plugindesc = $pluginoptions->{'desc'};
290
291 # Produce the usage information using the data structure above
292 if ($isleafclass) {
293 if (defined($plugindesc)) {
294 gsprintf(STDERR, "$plugindesc\n\n");
295 }
296 gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
297 }
298
299 # Display the plugin options, if there are some
300 if (defined($pluginargs)) {
301 # Calculate the column offset of the option descriptions
302 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
303
304 if ($isleafclass) {
305 gsprintf(STDERR, " {common.specific_options}:\n");
306 }
307 else {
308 gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
309 }
310
311 # Display the plugin options
312 &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
313 }
314
315 # Recurse up the plugin hierarchy
316 $self->print_plugin_usage($descoffset, 0);
317 $self->{'option_list'} = \@optionlist;
318}
319
320
3211;
322
323
Note: See TracBrowser for help on using the repository browser.