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

Last change on this file since 14925 was 14925, checked in by dmn, 15 years ago

davidbs changes to update for gs3 building

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 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' => "site",
102 'desc' => "{import.site}",
103 'type' => "string",
104 'deft' => "",
105 'reqd' => "no",
106 'hiddengli' => "yes" },
107 { 'name' => "manifest",
108 'desc' => "{import.manifest}",
109 'type' => "string",
110 'deft' => "",
111 'reqd' => "no",
112 'hiddengli' => "yes" },
113 { 'name' => "debug",
114 'desc' => "{import.debug}",
115 'type' => "flag",
116 'reqd' => "no",
117 'hiddengli' => "yes" },
118 { 'name' => "faillog",
119 'desc' => "{import.faillog}",
120 'type' => "string",
121 # parsearg left "" as default
122 #'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
123 'deft' => "",
124 'reqd' => "no",
125 'modegli' => "4" },
126 { 'name' => "importdir",
127 'desc' => "{import.importdir}",
128 'type' => "string",
129 'reqd' => "no",
130 'hiddengli' => "yes" },
131 { 'name' => "incremental",
132 'desc' => "{import.incremental}",
133 'type' => "flag",
134 'hiddengli' => "yes" },
135 { 'name' => "keepold",
136 'desc' => "{import.keepold}",
137 'type' => "flag",
138 'reqd' => "no",
139 'hiddengli' => "yes" },
140 { 'name' => "removeold",
141 'desc' => "{import.removeold}",
142 'type' => "flag",
143 'reqd' => "no",
144 'hiddengli' => "yes" },
145 { 'name' => "language",
146 'desc' => "{scripts.language}",
147 'type' => "string",
148 'reqd' => "no",
149 'hiddengli' => "yes" },
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' => "2" },
166 { 'name' => "OIDmetadata",
167 'desc' => "{import.OIDmetadata}",
168 'type' => "metadata",
169 'deft' => "dc.Identifier",
170 'reqd' => "no",
171 'modegli' => "2" },
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' => "metadata",
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' => "groupsize",
204 'desc' => "{import.groupsize}",
205 'type' => "int",
206 'deft' => "1",
207 'reqd' => "no",
208 'modegli' => "3" },
209 { 'name' => "gzip",
210 'desc' => "{import.gzip}",
211 'type' => "flag",
212 'reqd' => "no",
213 'modegli' => "4" },
214 { 'name' => "statsfile",
215 'desc' => "{import.statsfile}",
216 'type' => "string",
217 'deft' => "STDERR",
218 'reqd' => "no",
219 'hiddengli' => "yes" },
220 { 'name' => "verbosity",
221 'desc' => "{import.verbosity}",
222 'type' => "int",
223 'range' => "0,",
224 # parsearg left "" as default
225 #'deft' => "2",
226 'reqd' => "no",
227 'modegli' => "4" },
228 { 'name' => "gli",
229 'desc' => "",
230 'type' => "flag",
231 'reqd' => "no",
232 'hiddengli' => "yes" },
233 { 'name' => "xml",
234 'desc' => "{scripts.xml}",
235 'type' => "flag",
236 'reqd' => "no",
237 'hiddengli' => "yes" }];
238
239my $options = { 'name' => "import.pl",
240 'desc' => "{import.desc}",
241 'args' => $arguments };
242
243
244&main();
245
246sub main {
247 my ($verbosity, $importdir, $archivedir, $site, $manifest, $incremental, $keepold,
248 $removeold, $saveas, $version,
249 $gzip, $groupsize, $OIDtype, $OIDmetadata, $debug,
250 $maxdocs, $collection, $configfilename, $collectcfg, $gs_mode,
251 $pluginfo, $sortmeta, $removeprefix, $removesuffix,
252 $archive_info_filename, $statsfile,
253 $archive_info, $processor, $out, $faillog, $collectdir, $gli, $language);
254
255 my $xml = 0;
256
257 my $service = "import";
258
259 my $hashParsingResult = {};
260 # general options available to all plugins
261 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
262 # Parse returns -1 if something has gone wrong
263 if($intArgLeftinAfterParsing == -1)
264 {
265 &PrintUsage::print_txt_usage($options, "{import.params}");
266 die "\n";
267 }
268
269 foreach my $strVariable (keys %$hashParsingResult)
270 {
271 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
272 }
273
274 # If $language has been specified, load the appropriate resource bundle
275 # (Otherwise, the default resource bundle will be loaded automatically)
276 if ($language && $language =~ /\S/) {
277 &gsprintf::load_language_specific_resource_bundle($language);
278 }
279
280 if ($xml) {
281 &PrintUsage::print_xml_usage($options);
282 print "\n";
283 return;
284 }
285
286 if ($gli) { # the gli wants strings to be in UTF-8
287 &gsprintf::output_strings_in_UTF8;
288 }
289
290 # now check that we had exactly one leftover arg, which should be
291 # the collection name. We don't want to do this earlier, cos
292 # -xml arg doesn't need a collection name
293 # Or if the user specified -h, then we output the usage also
294 if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
295 {
296 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
297 die "\n";
298 }
299
300 my $close_out = 0;
301 if ($out !~ /^(STDERR|STDOUT)$/i) {
302 open (OUT, ">$out") ||
303 (&gsprintf(STDERR, "{common.cannot_open_output_file}: $!\n", $out) && die);
304 $out = 'import::OUT';
305 $close_out = 1;
306 }
307 $out->autoflush(1);
308
309 # get and check the collection name
310 if (($collection = &colcfg::use_collection($site, @ARGV, $collectdir)) eq "") {
311 &PrintUsage::print_txt_usage($options, "{import.params}");
312 die "\n";
313 }
314
315 # add collection's perllib dir into include path in
316 # case we have collection specific modules
317 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
318
319 # check that we can open the faillog
320 if ($faillog eq "") {
321 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
322 }
323 open (FAILLOG, ">$faillog") ||
324 (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
325
326
327 my $faillogname = $faillog;
328 $faillog = 'import::FAILLOG';
329 $faillog->autoflush(1);
330
331 # Read in the collection configuration file.
332 ($configfilename, $gs_mode) = &colcfg::get_collect_cfg_name($out);
333
334 if ($gs_mode eq "gs2") {
335 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
336 } elsif ($gs_mode eq "gs3") {
337 $collectcfg = &colcfg::read_collection_cfg_xml ($configfilename);
338 }
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 }
346 # fill in the default import and archives directories if none
347 # were supplied, turn all \ into / and remove trailing /
348 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
349 $importdir =~ s/[\\\/]+/\//g;
350 $importdir =~ s/\/$//;
351 if (!-e $importdir) {
352 &gsprintf($out, "{import.no_import_dir}\n\n", $importdir);
353 die "\n";
354 }
355
356 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
357 $archivedir =~ s/[\\\/]+/\//g;
358 $archivedir =~ s/\/$//;
359
360 my $plugins = [];
361 if (defined $collectcfg->{'plugin'}) {
362 $plugins = $collectcfg->{'plugin'};
363 }
364 #some global options for the plugins
365 my @global_opts = ();
366
367 if ($verbosity !~ /\d+/) {
368 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
369 $verbosity = $collectcfg->{'verbosity'};
370 } else {
371 $verbosity = 2; # the default
372 }
373 }
374 if (defined $collectcfg->{'manifest'} && $manifest eq "") {
375 $manifest = $collectcfg->{'manifest'};
376 }
377
378 if (defined $collectcfg->{'gzip'} && !$gzip) {
379 if ($collectcfg->{'gzip'} =~ /^true$/i) {
380 $gzip = 1;
381 }
382 }
383
384 if ($maxdocs !~ /\-?\d+/) {
385 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
386 $maxdocs = $collectcfg->{'maxdocs'};
387 } else {
388 $maxdocs = -1; # the default
389 }
390 }
391 if ($groupsize == 1) {
392 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
393 $groupsize = $collectcfg->{'groupsize'};
394 }
395 }
396
397 if ($OIDtype !~ /^(hash|incremental|assigned|dirname)$/) {
398 if (defined $collectcfg->{'OIDtype'} && $collectcfg->{'OIDtype'} =~ /^(hash|incremental|assigned|dirname)$/) {
399 $OIDtype = $collectcfg->{'OIDtype'};
400 } else {
401 $OIDtype = "hash"; # the default
402 }
403 }
404
405 my ($plugout);
406 if (defined $collectcfg->{'plugout'}) {
407 $plugout = $collectcfg->{'plugout'};
408 }
409 else{
410 if ($saveas !~ /^(GA|METS)$/) {
411 push @$plugout,"GAPlugout";
412 }
413 else{
414 push @$plugout,$saveas."Plugout";
415 }
416 }
417
418 if (defined $collectcfg->{'sortmeta'} && (!defined $sortmeta || $sortmeta eq "")) {
419 $sortmeta = $collectcfg->{'sortmeta'};
420 }
421 # sortmeta cannot be used with group size
422 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
423 if (defined $sortmeta && $groupsize > 1) {
424 &gsprintf($out, "{import.cannot_sort}\n\n");
425 $sortmeta = undef;
426 }
427
428 if (defined $collectcfg->{'removeprefix'} && $removeprefix eq "") {
429 $removeprefix = $collectcfg->{'removeprefix'};
430 }
431
432 if (defined $collectcfg->{'removesuffix'} && $removesuffix eq "") {
433 $removesuffix = $collectcfg->{'removesuffix'};
434 }
435 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
436 $debug = 1;
437 }
438 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
439 $gli = 1;
440 }
441
442
443 # global plugin stuff
444 if (defined $collectcfg->{'separate_cjk'} && $collectcfg->{'separate_cjk'} =~ /^true$/i) {
445 push @global_opts, "-separate_cjk";
446 }
447
448 # check keepold and removeold
449 ($removeold, $keepold, $incremental) = &scriptutil::check_removeold_and_keepold($removeold, $keepold, $incremental, "archives", $collectcfg);
450
451 $gli = 0 unless defined $gli;
452
453 print STDERR "<Import>\n" if $gli;
454
455 my $manifest_lookup = new manifest();
456 if ($manifest ne "") {
457 my $manifest_filename = $manifest;
458
459 if ($manifest_filename !~ m/^[\\\/]/) {
460 $manifest_filename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, $manifest_filename);
461 }
462
463 $manifest =~ s/[\\\/]+/\//g;
464 $manifest =~ s/\/$//;
465
466 $manifest_lookup->parse($manifest_filename);
467 }
468
469
470 # load all the plugins
471 $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts, $incremental);
472 if (scalar(@$pluginfo) == 0) {
473 &gsprintf($out, "{import.no_plugins_loaded}\n");
474 die "\n";
475 }
476
477 # remove the old contents of the archives directory (and tmp directory) if needed
478 if ($removeold) {
479 if (-e $archivedir) {
480 &gsprintf($out, "{import.removing_archives}\n");
481 &util::rm_r ($archivedir);
482 }
483 my $tmpdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "tmp");
484 $tmpdir =~ s/[\\\/]+/\//g;
485 $tmpdir =~ s/\/$//;
486 if (-e $tmpdir) {
487 &gsprintf($out, "{import.removing_tmpdir}\n");
488 &util::rm_r ($tmpdir);
489 }
490 }
491 # create the archives dir if needed
492 &util::mk_all_dir($archivedir);
493
494 # read the archive information file
495 $archive_info_filename = &util::filename_cat ($archivedir, "archives.inf");
496 $archive_info = new arcinfo ();
497 $archive_info->load_info ($archive_info_filename);
498
499
500 ####Use Plugout####
501 push @$plugout,("-output_info",$archive_info) if (defined $archive_info);
502 push @$plugout,("-verbosity",$verbosity) if (defined $verbosity);
503 push @$plugout,("-gzip_output") if ($gzip);
504 push @$plugout,("-group_size",$groupsize) if (defined $groupsize);
505 push @$plugout,("-output_handle",$out) if (defined $out);
506 push @$plugout,("-debug") if ($debug);
507
508 $processor = &plugout::load_plugout($plugout);
509 $processor->setoutputdir ($archivedir);
510 $processor->set_sortmeta ($sortmeta, $removeprefix, $removesuffix) if defined $sortmeta;
511 $processor->set_OIDtype ($OIDtype, $OIDmetadata);
512
513 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs, $gli);
514
515 if ($manifest eq "") {
516 # process the import directory
517 &plugin::read ($pluginfo, $importdir, "", {}, $processor, $maxdocs, 0, $gli);
518 }
519 else {
520
521 # process any new files
522 foreach my $file (keys %{$manifest_lookup->{'index'}}) {
523 &plugin::read ($pluginfo, $importdir, $file, {}, $processor, $maxdocs, 0, $gli);
524 }
525
526 # record files marked for deletion in arcinfo
527 foreach my $file (keys %{$manifest_lookup->{'delete'}}) {
528 # consider finding it?
529 # $archive_info->add_info($OID,$doc_xml_file,"D");
530 }
531 }
532
533 &plugin::end($pluginfo, $processor);
534
535 &plugin::deinit($pluginfo, $processor);
536
537 # write out the archive information file
538 $processor->close_file_output() if $groupsize > 1;
539 $processor->close_group_output() if $processor->is_group();
540 # should we still do this in debug mode??
541 $archive_info->save_info($archive_info_filename);
542
543 # write out import stats
544 my $close_stats = 0;
545 if ($statsfile !~ /^(STDERR|STDOUT)$/i) {
546 if (open (STATS, ">$statsfile")) {
547 $statsfile = 'import::STATS';
548 $close_stats = 1;
549 } else {
550 &gsprintf($out, "{import.cannot_open_stats_file}", $statsfile);
551 &gsprintf($out, "{import.stats_backup}\n");
552 $statsfile = 'STDERR';
553 }
554 }
555
556 &gsprintf($out, "\n");
557 &gsprintf($out, "*********************************************\n");
558 &gsprintf($out, "{import.complete}\n");
559 &gsprintf($out, "*********************************************\n");
560
561 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
562 if ($close_stats) {
563 close STATS;
564 }
565
566 close OUT if $close_out;
567 close FAILLOG;
568}
Note: See TracBrowser for help on using the repository browser.