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

Last change on this file since 12357 was 12357, checked in by mdewsnip, 18 years ago

Put back in adding GSDLCOLLECTDIR/perllib to INC, so collection-specific Perl modules can be used.

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