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

Last change on this file since 12399 was 12399, checked in by kjdon, 18 years ago

I rearranged some stuff so that all the essential checks are done first. Also added in a check for missing import directory

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 16.2 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 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
41}
42
43use arcinfo;
44use colcfg;
45use plugin;
46use plugout;
47use docprint;
48use manifest;
49use util;
50use scriptutil;
51use FileHandle;
52use gsprintf 'gsprintf';
53use printusage;
54use parse2;
55
56
57
58use strict;
59no strict 'refs'; # allow filehandles to be variables and vice versa
60no strict 'subs'; # allow barewords (eg STDERR) as function arguments
61
62my $oidtype_list =
63 [ { 'name' => "hash",
64 'desc' => "{import.OIDtype.hash}" },
65 { 'name' => "incremental",
66 'desc' => "{import.OIDtype.incremental}" },
67 { 'name' => "assigned",
68 'desc' => "{import.OIDtype.assigned}" },
69 { 'name' => "dirname",
70 'desc' => "{import.OIDtype.dirname}" } ];
71
72#** define to use the original GA format or METS format
73my $saveas_list =
74 [ { 'name' => "GA",
75 'desc' => "{import.saveas.GA}" },
76 { 'name' => "METS",
77 'desc' => "{import.saveas.METS}" } ];
78
79
80# Possible attributes for each argument
81# name: The name of the argument
82# desc: A description (or more likely a reference to a description) for this argument
83# type: The type of control used to represent the argument. Options include: string, int, flag, regexp, metadata, language, enum etc
84# reqd: Is this argument required?
85# hiddengli: Is this argument hidden in GLI?
86# modegli: The lowest detail mode this argument is visible at in GLI
87
88my $arguments =
89 [ { 'name' => "archivedir",
90 'desc' => "{import.archivedir}",
91 'type' => "string",
92 'reqd' => "no",
93 'hiddengli' => "yes" },
94 { 'name' => "collectdir",
95 'desc' => "{import.collectdir}",
96 'type' => "string",
97 # parsearg left "" as default
98 #'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
99 'deft' => "",
100 'reqd' => "no",
101 'hiddengli' => "yes" },
102 { 'name' => "manifest",
103 'desc' => "{import.manifest}",
104 'type' => "string",
105 'deft' => "",
106 'reqd' => "no",
107 'hiddengli' => "yes" },
108 { 'name' => "debug",
109 'desc' => "{import.debug}",
110 'type' => "flag",
111 'reqd' => "no",
112 'hiddengli' => "yes" },
113 { 'name' => "faillog",
114 'desc' => "{import.faillog}",
115 'type' => "string",
116 # parsearg left "" as default
117 #'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
118 'deft' => "",
119 'reqd' => "no",
120 'modegli' => "4" },
121 { 'name' => "groupsize",
122 'desc' => "{import.groupsize}",
123 'type' => "int",
124 'deft' => "1",
125 'reqd' => "no",
126 'modegli' => "3" },
127 { 'name' => "gzip",
128 'desc' => "{import.gzip}",
129 'type' => "flag",
130 'reqd' => "no",
131 'modegli' => "4" },
132 { 'name' => "importdir",
133 'desc' => "{import.importdir}",
134 'type' => "string",
135 'reqd' => "no",
136 'hiddengli' => "yes" },
137 { 'name' => "keepold",
138 'desc' => "{import.keepold}",
139 'type' => "flag",
140 'reqd' => "no",
141 'modegli' => "3" },
142 { 'name' => "removeold",
143 'desc' => "{import.removeold}",
144 'type' => "flag",
145 'reqd' => "no",
146 'modegli' => "3" },
147 { 'name' => "language",
148 'desc' => "{scripts.language}",
149 'type' => "string",
150 'reqd' => "no",
151 'modegli' => "4" },
152 { 'name' => "maxdocs",
153 'desc' => "{import.maxdocs}",
154 'type' => "int",
155 'reqd' => "no",
156 # parsearg left "" as default
157 #'deft' => "-1",
158 'range' => "1,",
159 'modegli' => "1" },
160 { 'name' => "OIDtype",
161 'desc' => "{import.OIDtype}",
162 'type' => "enum",
163 'list' => $oidtype_list,
164 # parsearg left "" as default
165 #'deft' => "hash",
166 'reqd' => "no",
167 'modegli' => "3" },
168 { 'name' => "OIDmetadata",
169 'desc' => "{import.OIDmetadata}",
170 'type' => "metadata",
171 'deft' => "dc.Identifier",
172 'reqd' => "no",
173 'modegli' => "3" },
174 { 'name' => "out",
175 'desc' => "{import.out}",
176 'type' => "string",
177 'deft' => "STDERR",
178 'reqd' => "no",
179 'hiddengli' => "yes" },
180 { 'name' => "saveas",
181 'desc' => "{import.saveas}",
182 'type' => "enum",
183 'list' => $saveas_list,
184 'deft' => "GA",
185 'reqd' => "no",
186 'modegli' => "3" },
187 { 'name' => "sortmeta",
188 'desc' => "{import.sortmeta}",
189 'type' => "metadata",
190# 'type' => "string",
191 'reqd' => "no",
192 'modegli' => "3" },
193 { 'name' => "removeprefix",
194 'desc' => "{BasClas.removeprefix}",
195 'type' => "regexp",
196 'deft' => "",
197 'reqd' => "no",
198 'modegli' => "3" },
199 { 'name' => "removesuffix",
200 'desc' => "{BasClas.removesuffix}",
201 'type' => "regexp",
202 'deft' => "",
203 'reqd' => "no",
204 'modegli' => "3" },
205 { 'name' => "statsfile",
206 'desc' => "{import.statsfile}",
207 'type' => "string",
208 'deft' => "STDERR",
209 'reqd' => "no",
210 'hiddengli' => "yes" },
211 { 'name' => "verbosity",
212 'desc' => "{import.verbosity}",
213 'type' => "int",
214 'range' => "0,",
215 # parsearg left "" as default
216 #'deft' => "2",
217 'reqd' => "no",
218 'modegli' => "4" },
219 { 'name' => "gli",
220 'desc' => "",
221 'type' => "flag",
222 'reqd' => "no",
223 'hiddengli' => "yes" },
224 { 'name' => "xml",
225 'desc' => "{scripts.xml}",
226 'type' => "flag",
227 'reqd' => "no",
228 'hiddengli' => "yes" }];
229
230my $options = { 'name' => "import.pl",
231 'desc' => "{import.desc}",
232 'args' => $arguments };
233
234
235&main();
236
237sub main {
238 my ($verbosity, $importdir, $archivedir, $manifest, $keepold,
239 $removeold, $saveas, $version,
240 $gzip, $groupsize, $OIDtype, $OIDmetadata, $debug,
241 $maxdocs, $collection, $configfilename, $collectcfg,
242 $pluginfo, $sortmeta, $removeprefix, $removesuffix,
243 $archive_info_filename, $statsfile,
244 $archive_info, $processor, $out, $faillog, $collectdir, $gli, $language);
245
246 my $xml = 0;
247
248 my $service = "import";
249
250 my $hashParsingResult = {};
251 my $blnParseFailed = "false";
252 # general options available to all plugins
253 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
254 # If there is more than one argument left after parsing, it mean user input too many arguments.
255 if($intArgLeftinAfterParsing > 1)
256 {
257 &PrintUsage::print_txt_usage($options, "{import.params}");
258 die "\n";
259 }
260
261 foreach my $strVariable (keys %$hashParsingResult)
262 {
263 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
264 }
265
266 # If $language has been specified, load the appropriate resource bundle
267 # (Otherwise, the default resource bundle will be loaded automatically)
268 if ($language && $language =~ /\S/) {
269 &gsprintf::load_language_specific_resource_bundle($language);
270 }
271
272 if ($xml) {
273 &PrintUsage::print_xml_usage($options);
274 print "\n";
275 return;
276 }
277
278 if ($gli) { # the gli wants strings to be in UTF-8
279 &gsprintf::output_strings_in_UTF8;
280 }
281
282 my $close_out = 0;
283 if ($out !~ /^(STDERR|STDOUT)$/i) {
284 open (OUT, ">$out") ||
285 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
286 $out = 'import::OUT';
287 $close_out = 1;
288 }
289 $out->autoflush(1);
290
291 # get and check the collection name
292 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
293 &PrintUsage::print_txt_usage($options, "{import.params}");
294 die "\n";
295 }
296
297 # add collection's perllib dir into include path in
298 # case we have collection specific modules
299 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
300
301 # check that we can open the faillog
302 if ($faillog eq "") {
303 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
304 }
305 open (FAILLOG, ">$faillog") ||
306 (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
307
308
309 my $faillogname = $faillog;
310 $faillog = 'import::FAILLOG';
311 $faillog->autoflush(1);
312
313 # check that there is a collect.cfg file
314 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
315 if (!-e $configfilename) {
316 (&gsprintf($out, "{common.cannot_find_cfg_file}\n", $configfilename) && die);
317 }
318 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
319
320
321 if (defined $collectcfg->{'importdir'} && $importdir eq "") {
322 $importdir = $collectcfg->{'importdir'};
323 }
324 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
325 $archivedir = $collectcfg->{'archivedir'};
326 }
327 # fill in the default import and archives directories if none
328 # were supplied, turn all \ into / and remove trailing /
329 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
330 $importdir =~ s/[\\\/]+/\//g;
331 $importdir =~ s/\/$//;
332 if (!-e $importdir) {
333 &gsprintf($out, "{import.no_import_dir}\n\n", $importdir);
334 die "\n";
335 }
336
337 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
338 $archivedir =~ s/[\\\/]+/\//g;
339 $archivedir =~ s/\/$//;
340
341 my $plugins = [];
342 if (defined $collectcfg->{'plugin'}) {
343 $plugins = $collectcfg->{'plugin'};
344 }
345 #some global options for the plugins
346 my @global_opts = ();
347
348 if ($verbosity !~ /\d+/) {
349 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
350 $verbosity = $collectcfg->{'verbosity'};
351 } else {
352 $verbosity = 2; # the default
353 }
354 }
355 if (defined $collectcfg->{'manifest'} && $manifest eq "") {
356 $manifest = $collectcfg->{'manifest'};
357 }
358
359 if (defined $collectcfg->{'gzip'} && !$gzip) {
360 if ($collectcfg->{'gzip'} =~ /^true$/i) {
361 $gzip = 1;
362 }
363 }
364
365 if ($maxdocs !~ /\-?\d+/) {
366 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
367 $maxdocs = $collectcfg->{'maxdocs'};
368 } else {
369 $maxdocs = -1; # the default
370 }
371 }
372 if ($groupsize == 1) {
373 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
374 $groupsize = $collectcfg->{'groupsize'};
375 }
376 }
377 if ($OIDtype !~ /^(hash|incremental|assigned|dirname)$/) {
378 if (defined $collectcfg->{'OIDtype'} && $collectcfg->{'OIDtype'} =~ /^(hash|incremental|assigned|dirname)$/) {
379 $OIDtype = $collectcfg->{'OIDtype'};
380 } else {
381 $OIDtype = "hash"; # the default
382 }
383 }
384
385
386 if ($saveas !~ /^(GA|METS)$/) {
387 if (defined $collectcfg->{'plugout'} && $collectcfg->{'plugout'}[0] =~ /^(GAPlugout|METSPlugout)$/) {
388 $saveas = $collectcfg->{'plugout'}[0];
389 } else {
390 $saveas ="GAPlugout";
391 }
392 }
393
394 if (defined $collectcfg->{'sortmeta'} && (!defined $sortmeta || $sortmeta eq "")) {
395 $sortmeta = $collectcfg->{'sortmeta'};
396 }
397 # sortmeta cannot be used with group size
398 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
399 if (defined $sortmeta && $groupsize > 1) {
400 &gsprintf($out, "{import.cannot_sort}\n\n");
401 $sortmeta = undef;
402 }
403
404 if (defined $collectcfg->{'removeprefix'} && $removeprefix eq "") {
405 $removeprefix = $collectcfg->{'removeprefix'};
406 }
407
408 if (defined $collectcfg->{'removesuffix'} && $removesuffix eq "") {
409 $removesuffix = $collectcfg->{'removesuffix'};
410 }
411 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
412 $debug = 1;
413 }
414 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
415 $gli = 1;
416 }
417
418
419 # global plugin stuff
420 if (defined $collectcfg->{'separate_cjk'} && $collectcfg->{'separate_cjk'} =~ /^true$/i) {
421 push @global_opts, "-separate_cjk";
422 }
423
424 # check keepold and removeold
425 ($removeold, $keepold) = &scriptutil::check_removeold_and_keepold($removeold, $keepold, "archives", $collectcfg);
426
427 $gli = 0 unless defined $gli;
428
429 print STDERR "<Import>\n" if $gli;
430
431 my $manifest_lookup = new manifest();
432 if ($manifest ne "") {
433 my $manifest_filename = $manifest;
434
435 if ($manifest_filename !~ m/^[\\\/]/) {
436 $manifest_filename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, $manifest_filename);
437 }
438
439 $manifest =~ s/[\\\/]+/\//g;
440 $manifest =~ s/\/$//;
441
442 $manifest_lookup->parse($manifest_filename);
443 }
444
445
446 # load all the plugins
447 $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts);
448 if (scalar(@$pluginfo) == 0) {
449 &gsprintf($out, "{import.no_plugins_loaded}\n");
450 die "\n";
451 }
452
453 # remove the old contents of the archives directory (and tmp directory) if needed
454 if ($removeold) {
455 if (-e $archivedir) {
456 &gsprintf($out, "{import.removing_archives}\n");
457 &util::rm_r ($archivedir);
458 }
459 my $tmpdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "tmp");
460 $tmpdir =~ s/[\\\/]+/\//g;
461 $tmpdir =~ s/\/$//;
462 if (-e $tmpdir) {
463 &gsprintf($out, "{import.removing_tmpdir}\n");
464 &util::rm_r ($tmpdir);
465 }
466 }
467 # create the archives dir if needed
468 &util::mk_all_dir($archivedir);
469
470 # read the archive information file
471 if (!$debug) {
472 $archive_info_filename = &util::filename_cat ($archivedir, "archives.inf");
473 $archive_info = new arcinfo ();
474 $archive_info->load_info ($archive_info_filename);
475
476
477 ####Use Plugout####
478 my ($plugout_name);
479 if ($saveas !~ /^(GA|METS)Plugout$/ ){
480 $plugout_name = $saveas."Plugout";
481 }
482 else{
483 $plugout_name = $saveas;
484 }
485
486 my $opts=[];
487
488 push @$opts,("-output_info",$archive_info) if (defined $archive_info);
489
490 push @$opts,("-verbosity",$verbosity) if (defined $verbosity);
491 push @$opts,("-gzip_output",$gzip) if (defined $gzip);
492 push @$opts,("-group_size",$groupsize) if (defined $groupsize);
493 push @$opts,("-output_handle",$out) if (defined $out);
494
495
496 $processor = &plugout::load_plugout($plugout_name,$opts);
497 $processor->setoutputdir ($archivedir);
498 $processor->set_sortmeta ($sortmeta, $removeprefix, $removesuffix) if defined $sortmeta;
499 $processor->set_OIDtype ($OIDtype);
500
501
502 } else {
503 $processor = new docprint ();
504 }
505
506 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs, $gli);
507
508 if ($manifest eq "") {
509 # process the import directory
510 &plugin::read ($pluginfo, $importdir, "", {}, $processor, $maxdocs, 0, $gli);
511 }
512 else {
513
514 # process any new files
515 foreach my $file (keys %{$manifest_lookup->{'index'}}) {
516 &plugin::read ($pluginfo, $importdir, $file, {}, $processor, $maxdocs, 0, $gli);
517 }
518
519 # record files marked for deletion in arcinfo
520 foreach my $file (keys %{$manifest_lookup->{'delete'}}) {
521 # consider finding it?
522 # $archive_info->add_info($OID,$doc_xml_file,"D");
523 }
524 }
525
526 &plugin::end($pluginfo, $processor);
527
528 &plugin::deinit($pluginfo, $processor);
529
530 # write out the archive information file
531 if (!$debug) {
532 $processor->close_file_output() if $groupsize > 1;
533 $processor->close_group_output() if $processor->is_group();
534 $archive_info->save_info($archive_info_filename);
535 }
536
537 # write out import stats
538 my $close_stats = 0;
539 if ($statsfile !~ /^(STDERR|STDOUT)$/i) {
540 if (open (STATS, ">$statsfile")) {
541 $statsfile = 'import::STATS';
542 $close_stats = 1;
543 } else {
544 &gsprintf($out, "{import.cannot_open_stats_file}", $statsfile);
545 &gsprintf($out, "{import.stats_backup}\n");
546 $statsfile = 'STDERR';
547 }
548 }
549
550 &gsprintf($out, "\n");
551 &gsprintf($out, "*********************************************\n");
552 &gsprintf($out, "{import.complete}\n");
553 &gsprintf($out, "*********************************************\n");
554
555 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
556 if ($close_stats) {
557 close STATS;
558 }
559
560 close OUT if $close_out;
561 close FAILLOG;
562}
Note: See TracBrowser for help on using the repository browser.