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

Last change on this file since 16788 was 16788, checked in by davidb, 16 years ago

Now checks for extended plugins and classifiers as well as in standard place

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