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

Last change on this file since 14556 was 14556, checked in by shaoqun, 15 years ago

make it use the plugout defined in the config file first

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