source: main/trunk/greenstone2/bin/script/import.pl@ 31700

Last change on this file since 31700 was 31700, checked in by kjdon, 5 years ago

export.pl has -saveas_options so you can pass in plugout options. added it to import.pl too.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# import.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
29# This program will import a number of files into a particular collection
30
31package import;
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/plugins");
39 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugouts");
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 unshift (@INC, "$ext_prefix/perllib/plugouts");
50 }
51 }
52 if (defined $ENV{'GSDL3EXTS'}) {
53 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
54 foreach my $e (@extensions) {
55 my $ext_prefix = "$ENV{'GSDL3SRCHOME'}/ext/$e";
56
57 unshift (@INC, "$ext_prefix/perllib");
58 unshift (@INC, "$ext_prefix/perllib/cpan");
59 unshift (@INC, "$ext_prefix/perllib/plugins");
60 unshift (@INC, "$ext_prefix/perllib/plugouts");
61 }
62 }
63
64 if ((defined $ENV{'DEBUG_UNICODE'}) && (defined $ENV{'DEBUG_UNICODE'})) {
65 binmode(STDERR,":utf8");
66 }
67}
68
69# Pragma
70use strict;
71no strict 'subs'; # allow barewords (eg STDERR) as function arguments
72use warnings;
73
74# Modules
75use Symbol qw<qualify>; # Needed for runtime loading of modules [jmt12]
76
77# Greenstone Modules
78use FileUtils;
79use inexport;
80use util;
81use gsprintf 'gsprintf';
82
83
84# used to control output file format
85my $saveas_list =
86 [ { 'name' => "GreenstoneXML",
87 'desc' => "{export.saveas.GreenstoneXML}"},
88 { 'name' => "GreenstoneMETS",
89 'desc' => "{export.saveas.GreenstoneMETS}"},
90 ];
91
92
93# Possible attributes for each argument
94# name: The name of the argument
95# desc: A description (or more likely a reference to a description) for this argument
96# type: The type of control used to represent the argument. Options include: string, int, flag, regexp, metadata, language, enum etc
97# reqd: Is this argument required?
98# hiddengli: Is this argument hidden in GLI?
99# modegli: The lowest detail mode this argument is visible at in GLI
100
101my $saveas_argument
102 = { 'name' => "saveas",
103 'desc' => "{import.saveas}",
104 'type' => "enum",
105 'list' => $saveas_list,
106 'deft' => "GreenstoneXML",
107 'reqd' => "no",
108 'modegli' => "3" };
109
110
111my $arguments =
112 [
113 $saveas_argument,
114 { 'name' => "saveas_options",
115 'desc' => "{import.saveas_options}",
116 'type' => "string",
117 'reqd' => "no" },
118 { 'name' => "sortmeta",
119 'desc' => "{import.sortmeta}",
120 'type' => "string",
121 #'type' => "metadata", #doesn't work properly in GLI
122 'reqd' => "no",
123 'modegli' => "2" },
124 { 'name' => "removeprefix",
125 'desc' => "{BasClas.removeprefix}",
126 'type' => "regexp",
127 'deft' => "",
128 'reqd' => "no",
129 'modegli' => "3" },
130 { 'name' => "removesuffix",
131 'desc' => "{BasClas.removesuffix}",
132 'type' => "regexp",
133 'deft' => "",
134 'reqd' => "no",
135 'modegli' => "3" },
136 { 'name' => "groupsize",
137 'desc' => "{import.groupsize}",
138 'type' => "int",
139 'deft' => "1",
140 'reqd' => "no",
141 'modegli' => "2" },
142 { 'name' => "archivedir",
143 'desc' => "{import.archivedir}",
144 'type' => "string",
145 'reqd' => "no",
146 'deft' => "archives",
147 'hiddengli' => "yes" },
148 @$inexport::directory_arguments,
149 { 'name' => "gzip",
150 'desc' => "{import.gzip}",
151 'type' => "flag",
152 'reqd' => "no",
153 'modegli' => "3" },
154 @$inexport::arguments,
155 { 'name' => "NO_IMPORT",
156 'desc' => "{import.NO_IMPORT}",
157 'type' => "flag",
158 'reqd' => "no",
159 'modegli' => "3"}
160];
161
162my $options = { 'name' => "import.pl",
163 'desc' => "{import.desc}",
164 'args' => $arguments };
165
166my $function_to_inexport_subclass_mappings = {};
167
168sub main
169{
170 # Dynamically include arguments from any subclasses of inexport we find
171 # in the extensions directory
172 if (defined $ENV{'GSDLEXTS'})
173 {
174 &_scanForSubclasses($ENV{'GSDLHOME'}, $ENV{'GSDLEXTS'});
175 }
176 if (defined $ENV{'GSDL3EXTS'})
177 {
178 &_scanForSubclasses($ENV{'GSDL3SRCHOME'}, $ENV{'GSDL3EXTS'});
179 }
180
181 # Loop through arguments, checking to see if any depend on a specific
182 # subclass of InExport. Note that we load the first subclass we encounter
183 # so only support a single 'override' ATM.
184 my $inexport_subclass;
185 foreach my $argument (@ARGV)
186 {
187 if ($argument eq "-NO_IMPORT") {
188 &gsprintf(STDERR, "{import.NO_IMPORT_set}\n\n");
189 exit 0;
190 }
191 # proper arguments start with a hyphen
192 if ($argument =~ /^-/ && defined $function_to_inexport_subclass_mappings->{$argument})
193 {
194 my $required_inexport_subclass = $function_to_inexport_subclass_mappings->{$argument};
195 if (!defined $inexport_subclass)
196 {
197 $inexport_subclass = $required_inexport_subclass;
198 }
199 # Oh noes! The user has included specific arguments from two different
200 # inexport subclasses... this isn't supported
201 elsif ($inexport_subclass ne $required_inexport_subclass)
202 {
203 print STDERR "Error! You cannot specify arguments from two different extention specific inexport modules: " . $inexport_subclass . " != " . $required_inexport_subclass . "\n";
204 exit;
205 }
206 }
207 }
208
209 my $inexport;
210 if (defined $inexport_subclass)
211 {
212 print "* Loading Overriding InExport Module: " . $inexport_subclass . "\n";
213 require $inexport_subclass . '.pm';
214 $inexport = new $inexport_subclass("import",\@ARGV,$options);
215 }
216
217 # We don't have a overridden inexport, or the above command failed somehow
218 # so load the base inexport class
219 if (!defined $inexport)
220 {
221 $inexport = new inexport("import",\@ARGV,$options);
222 }
223
224 my $collection = $inexport->get_collection();
225
226 if (defined $collection)
227 {
228 my ($config_filename,$collect_cfg) = $inexport->read_collection_cfg($collection,$options);
229 if ($collect_cfg->{'NO_IMPORT'}) {
230 &gsprintf(STDERR, "{import.NO_IMPORT_set}\n\n");
231 exit 0;
232 }
233 #$inexport->set_collection_options($collect_cfg);
234 &set_collection_options($inexport, $collect_cfg);
235
236
237 my $pluginfo = $inexport->process_files($config_filename,$collect_cfg);
238
239 $inexport->generate_statistics($pluginfo);
240 }
241
242 $inexport->deinit();
243}
244# main()
245
246# @function _scanForSubclasses()
247# @param $dir The extension directory to look within
248# @param $exts A list of the available extensions (as a colon separated string)
249# @return The number of subclasses of InExport found as an Integer
250sub _scanForSubclasses
251{
252 my ($dir, $exts) = @_;
253 my $inexport_class_count = 0;
254 my $ext_prefix = &FileUtils::filenameConcatenate($dir, "ext");
255 my @extensions = split(/:/, $exts);
256 foreach my $e (@extensions)
257 {
258 # - any subclass of InExport must be prefixed with the name of the ext
259 my $package_name = $e . 'inexport';
260 $package_name =~ s/[^a-z]//gi; # package names have limited characters
261 my $inexport_filename = $package_name . '.pm';
262 my $inexport_path = &FileUtils::filenameConcatenate($ext_prefix, $e, 'perllib', $inexport_filename);
263 # see if we have a subclass of InExport lurking in that extension folder
264 if (-f $inexport_path)
265 {
266 # - note we load the filename (with pm) unlike normal modules
267 require $inexport_filename;
268 # - make call to the newly created package
269 my $symbol = qualify('getSupportedArguments', $package_name);
270 # - strict prevents strings being used as function calls, so temporarily
271 # disable that pragma
272 no strict;
273 # - lets check that the function we are about to call actually exists
274 if ( defined &{$symbol} )
275 {
276 my $extra_arguments = &{$symbol}();
277 foreach my $argument (@{$extra_arguments})
278 {
279 # - record a mapping from each extra arguments to the inexport class
280 # that supports it. We put the hyphen on here to make comparing
281 # with command line arguments even easier
282 $function_to_inexport_subclass_mappings->{'-' . $argument->{'name'}} = $package_name;
283 # - and them add them as acceptable arguments to import.pl
284 push(@{$options->{'args'}}, $argument);
285 }
286 $inexport_class_count++;
287 }
288 else
289 {
290 print "Warning! A subclass of InExport module (named '" . $inexport_filename . "') does not implement the required getSupportedArguments() function - ignoring. Found in: " . $inexport_path . "\n";
291 }
292 }
293 }
294 return $inexport_class_count;
295}
296# _scanForInExportModules()
297
298# look up collect.cfg for import options, then all inexport version for the
299# common ones
300sub set_collection_options
301{
302
303 my ($inexport, $collectcfg) = @_;
304 my $out = $inexport->{'out'};
305
306 # check all options for default_optname - this will be set if the parsing
307 # code has just set the value based on the arg default. In this case,
308 # check in collect.cfg for the option
309
310 # groupsize can only be defined for import, not export, and actually only
311 # applies to GreenstoneXML format.
312 if (defined $inexport->{'default_groupsize'}) {
313 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
314 $inexport->{'groupsize'} = $collectcfg->{'groupsize'};
315 }
316
317 }
318 if (defined $inexport->{'default_saveas'}) {
319 if (defined $collectcfg->{'saveas'}
320 && $collectcfg->{'saveas'} =~ /^(GreenstoneXML|GreenstoneMETS)$/) {
321 $inexport->{'saveas'} = $collectcfg->{'saveas'};
322 } else {
323 $inexport->{'saveas'} = "GreenstoneXML"; # the default
324 }
325 }
326 if (!defined $inexport->{'saveas_options'} || $inexport->{'saveas_options'} eq "") {
327 if (defined $collectcfg->{'saveas_options'} ){
328 $inexport->{'saveas_options'} = $collectcfg->{'saveas_options'};
329 }
330 }
331
332 my $sortmeta = $inexport->{'sortmeta'};
333 if (defined $collectcfg->{'sortmeta'} && $sortmeta eq "") {
334 $sortmeta = $collectcfg->{'sortmeta'};
335 }
336 # sortmeta cannot be used with group size
337 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
338 if (defined $sortmeta && $inexport->{'groupsize'} > 1) {
339 &gsprintf($out, "{import.cannot_sort}\n\n");
340 $sortmeta = undef;
341 }
342 if (defined $sortmeta) {
343 &gsprintf($out, "{import.sortmeta_paired_with_ArchivesInfPlugin}\n\n");
344 }
345 $inexport->{'sortmeta'} = $sortmeta;
346
347 if (defined $collectcfg->{'removeprefix'} && $inexport->{'removeprefix'} eq "") {
348 $inexport->{'removeprefix'} = $collectcfg->{'removeprefix'};
349 }
350
351 if (defined $collectcfg->{'removesuffix'} && $inexport->{'removesuffix'} eq "") {
352 $inexport->{'removesuffix'} = $collectcfg->{'removesuffix'};
353 }
354
355 $inexport->set_collection_options($collectcfg);
356
357}
358&main();
Note: See TracBrowser for help on using the repository browser.