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

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

added a new option: OIDmetadata, which is used with OIDtype=assigned, to specify the metadata element that contains the identifier

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