source: trunk/gsdl/bin/script/import.pl@ 7950

Last change on this file since 7950 was 7950, checked in by davidb, 20 years ago

Exit status resulting from a 'die' statement changed to print and exit 0
combination so when run with -xml flag printing out details it isn't seen
stopping with an error.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 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/classify");
40}
41
42use arcinfo;
43use colcfg;
44use plugin;
45use docprint;
46use util;
47use parsargv;
48use FileHandle;
49use gsprintf;
50use printusage;
51
52my $oidtype_list =
53 [ { 'name' => "hash",
54 'desc' => "{import.OIDtype.hash}" },
55 { 'name' => "incremental",
56 'desc' => "{import.OIDtype.incremental}" } ];
57
58#** define to use the original GA format or METS format
59my $saveas_list =
60 [ { 'name' => "GA",
61 'desc' => "{import.saveas.GA}" },
62 { 'name' => "METS",
63 'desc' => "{import.saveas.METS}" } ];
64
65
66# Possible attributes for each argument
67# name: The name of the argument
68# desc: A description (or more likely a reference to a description) for this argument
69# type: The type of control used to represent the argument. Options include: string, int, flag, regexp, metadata, metadatum, language, enum etc
70# reqd: Is this argument required?
71# hiddengli: Is this argument hidden in GLI?
72# modegli: The lowest detail mode this argument is visible at in GLI
73
74my $arguments =
75 [ { 'name' => "archivedir",
76 'desc' => "{import.archivedir}",
77 'type' => "string",
78 'reqd' => "no",
79 'hiddengli' => "yes" },
80 { 'name' => "collectdir",
81 'desc' => "{import.collectdir}",
82 'type' => "string",
83 'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
84 'reqd' => "no",
85 'hiddengli' => "yes" },
86 { 'name' => "debug",
87 'desc' => "{import.debug}",
88 'type' => "flag",
89 'reqd' => "no",
90 'hiddengli' => "yes" },
91 { 'name' => "faillog",
92 'desc' => "{import.faillog}",
93 'type' => "string",
94 'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
95 'reqd' => "no",
96 'modegli' => "4" },
97 { 'name' => "groupsize",
98 'desc' => "{import.groupsize}",
99 'type' => "int",
100 'deft' => "1",
101 'reqd' => "no",
102 'modegli' => "3" },
103 { 'name' => "gzip",
104 'desc' => "{import.gzip}",
105 'type' => "flag",
106 'reqd' => "no",
107 'modegli' => "4" },
108 { 'name' => "importdir",
109 'desc' => "{import.importdir}",
110 'type' => "string",
111 'reqd' => "no",
112 'hiddengli' => "yes" },
113 { 'name' => "keepold",
114 'desc' => "{import.keepold}",
115 'type' => "flag",
116 'reqd' => "no",
117 'hiddengli' => "yes" },
118 { 'name' => "language",
119 'desc' => "{scripts.language}",
120 'type' => "string",
121 'reqd' => "no",
122 'modegli' => "4" },
123 { 'name' => "maxdocs",
124 'desc' => "{import.maxdocs}",
125 'type' => "int",
126 'reqd' => "no",
127 'range' => "1,",
128 'modegli' => "1" },
129 { 'name' => "OIDtype",
130 'desc' => "{import.OIDtype}",
131 'type' => "enum",
132 'list' => $oidtype_list,
133 'deft' => "hash",
134 'reqd' => "no",
135 'modegli' => "3" },
136 { 'name' => "out",
137 'desc' => "{import.out}",
138 'type' => "string",
139 'deft' => "STDERR",
140 'reqd' => "no",
141 'hiddengli' => "yes" },
142 { 'name' => "removeold",
143 'desc' => "{import.removeold}",
144 'type' => "flag",
145 'reqd' => "no",
146 'modegli' => "3" },
147 { 'name' => "saveas",
148 'desc' => "{import.saveas}",
149 'type' => "enum",
150 'list' => $saveas_list,
151 'deft' => "GA",
152 'reqd' => "no",
153 'modegli' => "3" },
154 { 'name' => "sortmeta",
155 'desc' => "{import.sortmeta}",
156 'type' => "metadata",
157 'reqd' => "no",
158 'modegli' => "2" },
159 { 'name' => "statsfile",
160 'desc' => "{import.statsfile}",
161 'type' => "string",
162 'deft' => "STDERR",
163 'reqd' => "no",
164 'hiddengli' => "yes" },
165 { 'name' => "verbosity",
166 'desc' => "{import.verbosity}",
167 'type' => "int",
168 'range' => "0,3",
169 'deft' => "2",
170 'reqd' => "no",
171 'modegli' => "4" } ];
172
173my $options = { 'name' => "import.pl",
174 'desc' => "{import.desc}",
175 'args' => $arguments };
176
177sub gsprintf
178{
179 return &gsprintf::gsprintf(@_);
180}
181
182
183&main();
184
185sub main {
186 my ($verbosity, $importdir, $archivedir, $keepold,
187 $removeold, $saveas, $gzip, $groupsize, $OIDtype, $debug,
188 $maxdocs, $collection, $configfilename, $collectcfg,
189 $pluginfo, $sortmeta, $archive_info_filename, $statsfile,
190 $archive_info, $processor, $out, $faillog, $collectdir, $gli);
191
192 # ***** 11-04-03 - John Thompson *****
193 my $xml = 0;
194 # ************************************
195
196 # note that no defaults are passed for most options as they're set
197 # later (after we check the collect.cfg file)
198 if (!parsargv::parse(\@ARGV,
199 'language/.*/', \$language,
200 'verbosity/\d+/', \$verbosity,
201 'importdir/.*/', \$importdir,
202 'archivedir/.*/', \$archivedir,
203 'keepold', \$keepold,
204 'removeold', \$removeold,
205 'saveas/^(GA|METS)$/GA', \$saveas,
206 'gzip', \$gzip,
207 'groupsize/\d+/1', \$groupsize,
208 'OIDtype/^(hash|incremental)$/', \$OIDtype,
209 'sortmeta/.*/', \$sortmeta,
210 'debug', \$debug,
211 'maxdocs/^\-?\d+/', \$maxdocs,
212 'collectdir/.*/', \$collectdir,
213 'out/.*/STDERR', \$out,
214 'statsfile/.*/STDERR', \$statsfile,
215 'faillog/.*/', \$faillog,
216 'gli', \$gli,
217 q^xml^, \$xml)) {
218 &PrintUsage::print_txt_usage($options, "{import.params}");
219 die "\n";
220 }
221
222 # If $language has been specified, load the appropriate resource bundle
223 # (Otherwise, the default resource bundle will be loaded automatically)
224 if ($language) {
225 &gsprintf::load_language_specific_resource_bundle($language);
226 }
227
228 if ($xml) {
229 &PrintUsage::print_xml_usage($options);
230 print "\n";
231 return;
232 }
233
234 if ($gli) { # the gli wants strings to be in UTF-8
235 &gsprintf::output_strings_in_UTF8;
236 }
237 my $close_out = 0;
238 if ($out !~ /^(STDERR|STDOUT)$/i) {
239 open (OUT, ">$out") ||
240 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
241 $out = 'import::OUT';
242 $close_out = 1;
243 }
244 $out->autoflush(1);
245
246 # set removeold to false if it has been defined
247 $removeold = 0 if ($keepold);
248
249 # get and check the collection name
250 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
251 &PrintUsage::print_txt_usage($options, "{import.params}");
252 die "\n";
253 }
254
255 if ($faillog eq "") {
256 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
257 }
258 open (FAILLOG, ">$faillog") ||
259 (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
260 my $faillogname = $faillog;
261 $faillog = 'import::FAILLOG';
262 $faillog->autoflush(1);
263
264 # check sortmeta
265 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
266 if (defined $sortmeta && $groupsize > 1) {
267 &gsprintf($out, "{import.cannot_sort}\n\n");
268 $sortmeta = undef;
269 }
270
271 # dynamically load 'docsave' module so it can pick up on a collection
272 # specific docsave.pm is specified.
273
274 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
275 require docsave;
276
277
278 # get the list of plugins for this collection and set any options that
279 # were specified in the collect.cfg (all import.pl options except
280 # -collectdir, -out and -faillog may be specified in the collect.cfg (these
281 # options must be known before we read the collect.cfg))
282 my $plugins = [];
283 my @global_opts = ();
284
285 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
286 if (-e $configfilename) {
287 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
288 if (defined $collectcfg->{'plugin'}) {
289 $plugins = $collectcfg->{'plugin'};
290 }
291
292 if ($verbosity !~ /\d+/) {
293 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
294 $verbosity = $collectcfg->{'verbosity'};
295 } else {
296 $verbosity = 2; # the default
297 }
298 }
299 if (defined $collectcfg->{'importdir'} && $importdir eq "") {
300 $importdir = $collectcfg->{'importdir'};
301 }
302 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
303 $archivedir = $collectcfg->{'archivedir'};
304 }
305 if (defined $collectcfg->{'removeold'}) {
306 if ($collectcfg->{'removeold'} =~ /^true$/i && !$keepold) {
307 $removeold = 1;
308 }
309 if ($collectcfg->{'removeold'} =~ /^false$/i && !$removeold) {
310 $removeold = 0;
311 }
312 }
313 if (defined $collectcfg->{'keepold'}) {
314 if ($collectcfg->{'keepold'} =~ /^false$/i && !$keepold) {
315 $removeold = 1;
316 }
317 }
318 if (defined $collectcfg->{'gzip'} && !$gzip) {
319 if ($collectcfg->{'gzip'} =~ /^true$/i) {
320 $gzip = 1;
321 }
322 }
323 if ($maxdocs !~ /\-?\d+/) {
324 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
325 $maxdocs = $collectcfg->{'maxdocs'};
326 } else {
327 $maxdocs = -1; # the default
328 }
329 }
330 if ($groupsize == 1) {
331 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
332 $groupsize = $collectcfg->{'groupsize'};
333 }
334 }
335 if ($OIDtype !~ /^(hash|incremental)$/) {
336 if (defined $collectcfg->{'OIDtype'} && $collectcfg->{'OIDtype'} =~ /^(hash|incremental)$/) {
337 $OIDtype = $collectcfg->{'OIDtype'};
338 } else {
339 $OIDtype = "hash"; # the default
340 }
341 }
342 if (defined $collectcfg->{'sortmeta'} && $sortmeta eq "") {
343 $sortmeta = $collectcfg->{'sortmeta'};
344 }
345 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
346 $debug = 1;
347 }
348 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
349 $gli = 1;
350 }
351
352 # global plugin stuff
353 if (defined $collectcfg->{'separate_cjk'}&& $collectcfg->{'separate_cjk'} =~ /^true$/i) {
354 push @global_opts, "-separate_cjk";
355 }
356
357
358 } else {
359 (&gsprintf($out, "{common.cannot_find_cfg_file}\n", $configfilename) && die);
360 }
361
362 $gli = 0 unless defined $gli;
363
364 print STDERR "<Import>\n" if $gli;
365
366 # fill in the default import and archives directories if none
367 # were supplied, turn all \ into / and remove trailing /
368 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
369 $importdir =~ s/[\\\/]+/\//g;
370 $importdir =~ s/\/$//;
371 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
372 $archivedir =~ s/[\\\/]+/\//g;
373 $archivedir =~ s/\/$//;
374
375 # load all the plugins
376 $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts);
377 if (scalar(@$pluginfo) == 0) {
378 &gsprintf($out, "{import.no_plugins_loaded}\n");
379 die "\n";
380 }
381
382 # remove the old contents of the archives directory if needed
383 if ($removeold && -e $archivedir) {
384 &gsprintf($out, "{import.removing_archives}\n");
385 &util::rm_r ($archivedir);
386 }
387
388 # read the archive information file
389 if (!$debug) {
390 $archive_info_filename = &util::filename_cat ($archivedir, "archives.inf");
391 $archive_info = new arcinfo ();
392 $archive_info->load_info ($archive_info_filename);
393
394 # create a docsave object to process the documents
395 $processor = new docsave ($collection, $archive_info, $verbosity, $gzip, $groupsize, $out);
396 $processor->setarchivedir ($archivedir);
397 $processor->set_sortmeta ($sortmeta) if defined $sortmeta;
398 $processor->set_OIDtype ($OIDtype);
399 $processor->set_saveas ($saveas);
400 } else {
401 $processor = new docprint ();
402 }
403
404 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs);
405
406 # process the import directory
407 &plugin::read ($pluginfo, $importdir, "", {}, $processor, $maxdocs, $gli);
408
409 &plugin::end($pluginfo, $processor);
410
411 # write out the archive information file
412 if (!$debug) {
413 $processor->close_file_output() if $groupsize > 1;
414 $archive_info->save_info($archive_info_filename);
415 }
416
417 # write out import stats
418 my $close_stats = 0;
419 if ($statsfile !~ /^(STDERR|STDOUT)$/i) {
420 if (open (STATS, ">$statsfile")) {
421 $statsfile = 'import::STATS';
422 $close_stats = 1;
423 } else {
424 &gsprintf($out, "{import.cannot_open_stats_file}", $statsfile);
425 &gsprintf($out, "{import.stats_backup}\n");
426 $statsfile = 'STDERR';
427 }
428 }
429
430 &gsprintf($out, "\n");
431 &gsprintf($out, "*********************************************\n");
432 &gsprintf($out, "{import.complete}\n");
433 &gsprintf($out, "*********************************************\n");
434
435 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
436 if ($close_stats) {
437 close STATS;
438 }
439
440 close OUT if $close_out;
441 close FAILLOG;
442}
Note: See TracBrowser for help on using the repository browser.