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

Last change on this file since 29097 was 29097, checked in by kjdon, 10 years ago

not sure if we need this catch for collection undefined

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