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
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");
[946]39 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
[4]40}
41
42use arcinfo;
43use colcfg;
44use plugin;
[783]45use docprint;
[130]46use util;
47use parsargv;
[1424]48use FileHandle;
[5606]49use gsprintf;
[4776]50use printusage;
[4]51
[4776]52my $oidtype_list =
53 [ { 'name' => "hash",
[4873]54 'desc' => "{import.OIDtype.hash}" },
[4776]55 { 'name' => "incremental",
[4873]56 'desc' => "{import.OIDtype.incremental}" } ];
[4776]57
[7906]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
[6407]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
[4776]74my $arguments =
75 [ { 'name' => "archivedir",
[6921]76 'desc' => "{import.archivedir}",
[4776]77 'type' => "string",
[6407]78 'reqd' => "no",
79 'hiddengli' => "yes" },
[4776]80 { 'name' => "collectdir",
[4873]81 'desc' => "{import.collectdir}",
[4776]82 'type' => "string",
83 'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
[6407]84 'reqd' => "no",
85 'hiddengli' => "yes" },
[4776]86 { 'name' => "debug",
[4873]87 'desc' => "{import.debug}",
[4776]88 'type' => "flag",
[6407]89 'reqd' => "no",
90 'hiddengli' => "yes" },
[4776]91 { 'name' => "faillog",
[4873]92 'desc' => "{import.faillog}",
[4776]93 'type' => "string",
[6109]94 'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
[6407]95 'reqd' => "no",
96 'modegli' => "4" },
[4776]97 { 'name' => "groupsize",
[4873]98 'desc' => "{import.groupsize}",
[4776]99 'type' => "int",
100 'deft' => "1",
[6407]101 'reqd' => "no",
102 'modegli' => "3" },
[4776]103 { 'name' => "gzip",
[4873]104 'desc' => "{import.gzip}",
[4776]105 'type' => "flag",
[6407]106 'reqd' => "no",
107 'modegli' => "4" },
[4776]108 { 'name' => "importdir",
[4873]109 'desc' => "{import.importdir}",
[4776]110 'type' => "string",
[6407]111 'reqd' => "no",
112 'hiddengli' => "yes" },
[4776]113 { 'name' => "keepold",
[4873]114 'desc' => "{import.keepold}",
[4776]115 'type' => "flag",
[6407]116 'reqd' => "no",
117 'hiddengli' => "yes" },
118 { 'name' => "language",
119 'desc' => "{scripts.language}",
120 'type' => "string",
121 'reqd' => "no",
122 'modegli' => "4" },
[4776]123 { 'name' => "maxdocs",
[4873]124 'desc' => "{import.maxdocs}",
[4776]125 'type' => "int",
[6407]126 'reqd' => "no",
[7063]127 'range' => "1,",
[6407]128 'modegli' => "1" },
[4776]129 { 'name' => "OIDtype",
[4873]130 'desc' => "{import.OIDtype}",
[4776]131 'type' => "enum",
132 'list' => $oidtype_list,
133 'deft' => "hash",
[6407]134 'reqd' => "no",
135 'modegli' => "3" },
[4776]136 { 'name' => "out",
[4873]137 'desc' => "{import.out}",
[4776]138 'type' => "string",
139 'deft' => "STDERR",
[6407]140 'reqd' => "no",
141 'hiddengli' => "yes" },
[4776]142 { 'name' => "removeold",
[4873]143 'desc' => "{import.removeold}",
[4776]144 'type' => "flag",
[6407]145 'reqd' => "no",
146 'modegli' => "3" },
[7906]147 { 'name' => "saveas",
148 'desc' => "{import.saveas}",
149 'type' => "enum",
150 'list' => $saveas_list,
151 'deft' => "GA",
152 'reqd' => "no",
153 'modegli' => "3" },
[4776]154 { 'name' => "sortmeta",
[4873]155 'desc' => "{import.sortmeta}",
[6407]156 'type' => "metadata",
157 'reqd' => "no",
158 'modegli' => "2" },
[4776]159 { 'name' => "statsfile",
[4873]160 'desc' => "{import.statsfile}",
[4776]161 'type' => "string",
162 'deft' => "STDERR",
[6407]163 'reqd' => "no",
164 'hiddengli' => "yes" },
[4776]165 { 'name' => "verbosity",
[4873]166 'desc' => "{import.verbosity}",
[4776]167 'type' => "int",
[6109]168 'range' => "0,3",
[4776]169 'deft' => "2",
[6407]170 'reqd' => "no",
171 'modegli' => "4" } ];
[4776]172
173my $options = { 'name' => "import.pl",
[5093]174 'desc' => "{import.desc}",
[4776]175 'args' => $arguments };
176
[6921]177sub gsprintf
[4776]178{
[6921]179 return &gsprintf::gsprintf(@_);
[4]180}
181
[4776]182
[1424]183&main();
[4]184
185sub main {
[783]186 my ($verbosity, $importdir, $archivedir, $keepold,
[7906]187 $removeold, $saveas, $gzip, $groupsize, $OIDtype, $debug,
[2328]188 $maxdocs, $collection, $configfilename, $collectcfg,
[2785]189 $pluginfo, $sortmeta, $archive_info_filename, $statsfile,
[6330]190 $archive_info, $processor, $out, $faillog, $collectdir, $gli);
[2355]191
[4776]192 # ***** 11-04-03 - John Thompson *****
193 my $xml = 0;
194 # ************************************
195
[2355]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,
[4873]199 'language/.*/', \$language,
[2355]200 'verbosity/\d+/', \$verbosity,
[130]201 'importdir/.*/', \$importdir,
[2355]202 'archivedir/.*/', \$archivedir,
203 'keepold', \$keepold,
204 'removeold', \$removeold,
[7906]205 'saveas/^(GA|METS)$/GA', \$saveas,
[2355]206 'gzip', \$gzip,
[2766]207 'groupsize/\d+/1', \$groupsize,
[2355]208 'OIDtype/^(hash|incremental)$/', \$OIDtype,
209 'sortmeta/.*/', \$sortmeta,
[783]210 'debug', \$debug,
[2355]211 'maxdocs/^\-?\d+/', \$maxdocs,
[2287]212 'collectdir/.*/', \$collectdir,
[2755]213 'out/.*/STDERR', \$out,
[2785]214 'statsfile/.*/STDERR', \$statsfile,
[4776]215 'faillog/.*/', \$faillog,
[6330]216 'gli', \$gli,
[4776]217 q^xml^, \$xml)) {
[6926]218 &PrintUsage::print_txt_usage($options, "{import.params}");
[4]219 die "\n";
220 }
[130]221
[6945]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 }
[6926]227
[4776]228 if ($xml) {
[6926]229 &PrintUsage::print_xml_usage($options);
[7950]230 print "\n";
231 return;
[4776]232 }
233
[7101]234 if ($gli) { # the gli wants strings to be in UTF-8
235 &gsprintf::output_strings_in_UTF8;
236 }
[1424]237 my $close_out = 0;
238 if ($out !~ /^(STDERR|STDOUT)$/i) {
[5093]239 open (OUT, ">$out") ||
[6921]240 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
[1424]241 $out = 'import::OUT';
242 $close_out = 1;
243 }
244 $out->autoflush(1);
245
[130]246 # set removeold to false if it has been defined
247 $removeold = 0 if ($keepold);
248
249 # get and check the collection name
[2287]250 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
[6926]251 &PrintUsage::print_txt_usage($options, "{import.params}");
[4]252 die "\n";
253 }
[2785]254
255 if ($faillog eq "") {
256 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
[2755]257 }
[5093]258 open (FAILLOG, ">$faillog") ||
[6921]259 (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
[3402]260 my $faillogname = $faillog;
[2785]261 $faillog = 'import::FAILLOG';
262 $faillog->autoflush(1);
[130]263
[1287]264 # check sortmeta
265 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
266 if (defined $sortmeta && $groupsize > 1) {
[6921]267 &gsprintf($out, "{import.cannot_sort}\n\n");
[1287]268 $sortmeta = undef;
269 }
270
[843]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
[2355]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
[2755]280 # -collectdir, -out and -faillog may be specified in the collect.cfg (these
[2355]281 # options must be known before we read the collect.cfg))
[814]282 my $plugins = [];
[6584]283 my @global_opts = ();
284
[2287]285 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
[130]286 if (-e $configfilename) {
287 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
[814]288 if (defined $collectcfg->{'plugin'}) {
289 $plugins = $collectcfg->{'plugin'};
[4]290 }
[2355]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 }
[130]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 }
[2355]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 }
[2766]330 if ($groupsize == 1) {
331 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
[2355]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 }
[6330]348 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
349 $gli = 1;
350 }
[2355]351
[6584]352 # global plugin stuff
353 if (defined $collectcfg->{'separate_cjk'}&& $collectcfg->{'separate_cjk'} =~ /^true$/i) {
354 push @global_opts, "-separate_cjk";
355 }
356
357
[98]358 } else {
[6921]359 (&gsprintf($out, "{common.cannot_find_cfg_file}\n", $configfilename) && die);
[4]360 }
[6330]361
362 $gli = 0 unless defined $gli;
363
364 print STDERR "<Import>\n" if $gli;
[4]365
[130]366 # fill in the default import and archives directories if none
367 # were supplied, turn all \ into / and remove trailing /
[2287]368 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
[130]369 $importdir =~ s/[\\\/]+/\//g;
370 $importdir =~ s/\/$//;
[2287]371 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
[130]372 $archivedir =~ s/[\\\/]+/\//g;
373 $archivedir =~ s/\/$//;
[4]374
375 # load all the plugins
[6584]376 $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts);
[4]377 if (scalar(@$pluginfo) == 0) {
[6921]378 &gsprintf($out, "{import.no_plugins_loaded}\n");
[4]379 die "\n";
380 }
[5093]381
[130]382 # remove the old contents of the archives directory if needed
383 if ($removeold && -e $archivedir) {
[6921]384 &gsprintf($out, "{import.removing_archives}\n");
[130]385 &util::rm_r ($archivedir);
386 }
[6921]387
[4]388 # read the archive information file
[783]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);
[4]393
[783]394 # create a docsave object to process the documents
[1424]395 $processor = new docsave ($collection, $archive_info, $verbosity, $gzip, $groupsize, $out);
[783]396 $processor->setarchivedir ($archivedir);
[1287]397 $processor->set_sortmeta ($sortmeta) if defined $sortmeta;
[2328]398 $processor->set_OIDtype ($OIDtype);
[7906]399 $processor->set_saveas ($saveas);
[783]400 } else {
401 $processor = new docprint ();
402 }
[4]403
[843]404 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs);
405
[4]406 # process the import directory
[6330]407 &plugin::read ($pluginfo, $importdir, "", {}, $processor, $maxdocs, $gli);
[4]408
[2287]409 &plugin::end($pluginfo, $processor);
[2785]410
[4]411 # write out the archive information file
[783]412 if (!$debug) {
[1287]413 $processor->close_file_output() if $groupsize > 1;
[783]414 $archive_info->save_info($archive_info_filename);
415 }
[2785]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 {
[6921]424 &gsprintf($out, "{import.cannot_open_stats_file}", $statsfile);
425 &gsprintf($out, "{import.stats_backup}\n");
[2785]426 $statsfile = 'STDERR';
427 }
428 }
429
[6921]430 &gsprintf($out, "\n");
431 &gsprintf($out, "*********************************************\n");
432 &gsprintf($out, "{import.complete}\n");
433 &gsprintf($out, "*********************************************\n");
[2785]434
[6330]435 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
[2785]436 if ($close_stats) {
437 close STATS;
438 }
439
[1424]440 close OUT if $close_out;
[2785]441 close FAILLOG;
[4]442}
Note: See TracBrowser for help on using the repository browser.