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
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 docprint;
48use manifest;
49use util;
50use scriptutil;
51use FileHandle;
52use gsprintf 'gsprintf';
53use printusage;
54use parse2;
55
56
57
58use strict;
59no strict 'refs'; # allow filehandles to be variables and vice versa
60no strict 'subs'; # allow barewords (eg STDERR) as function arguments
61
62my $oidtype_list =
63 [ { 'name' => "hash",
64 'desc' => "{import.OIDtype.hash}" },
65 { 'name' => "incremental",
66 'desc' => "{import.OIDtype.incremental}" },
67 { 'name' => "assigned",
68 'desc' => "{import.OIDtype.assigned}" },
69 { 'name' => "dirname",
70 'desc' => "{import.OIDtype.dirname}" } ];
71
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
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
88my $arguments =
89 [ { 'name' => "archivedir",
90 'desc' => "{import.archivedir}",
91 'type' => "string",
92 'reqd' => "no",
93 'hiddengli' => "yes" },
94 { 'name' => "collectdir",
95 'desc' => "{import.collectdir}",
96 'type' => "string",
97 # parsearg left "" as default
98 #'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
99 'deft' => "",
100 'reqd' => "no",
101 'hiddengli' => "yes" },
102 { 'name' => "manifest",
103 'desc' => "{import.manifest}",
104 'type' => "string",
105 'deft' => "",
106 'reqd' => "no",
107 'hiddengli' => "yes" },
108 { 'name' => "debug",
109 'desc' => "{import.debug}",
110 'type' => "flag",
111 'reqd' => "no",
112 'hiddengli' => "yes" },
113 { 'name' => "faillog",
114 'desc' => "{import.faillog}",
115 'type' => "string",
116 # parsearg left "" as default
117 #'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
118 'deft' => "",
119 'reqd' => "no",
120 'modegli' => "4" },
121 { 'name' => "groupsize",
122 'desc' => "{import.groupsize}",
123 'type' => "int",
124 'deft' => "1",
125 'reqd' => "no",
126 'modegli' => "3" },
127 { 'name' => "gzip",
128 'desc' => "{import.gzip}",
129 'type' => "flag",
130 'reqd' => "no",
131 'modegli' => "4" },
132 { 'name' => "importdir",
133 'desc' => "{import.importdir}",
134 'type' => "string",
135 'reqd' => "no",
136 'hiddengli' => "yes" },
137 { 'name' => "keepold",
138 'desc' => "{import.keepold}",
139 'type' => "flag",
140 'reqd' => "no",
141 'modegli' => "3" },
142 { 'name' => "removeold",
143 'desc' => "{import.removeold}",
144 'type' => "flag",
145 'reqd' => "no",
146 'modegli' => "3" },
147 { 'name' => "language",
148 'desc' => "{scripts.language}",
149 'type' => "string",
150 'reqd' => "no",
151 'modegli' => "4" },
152 { 'name' => "maxdocs",
153 'desc' => "{import.maxdocs}",
154 'type' => "int",
155 'reqd' => "no",
156 # parsearg left "" as default
157 #'deft' => "-1",
158 'range' => "1,",
159 'modegli' => "1" },
160 { 'name' => "OIDtype",
161 'desc' => "{import.OIDtype}",
162 'type' => "enum",
163 'list' => $oidtype_list,
164 # parsearg left "" as default
165 #'deft' => "hash",
166 'reqd' => "no",
167 'modegli' => "3" },
168 { 'name' => "OIDmetadata",
169 'desc' => "{import.OIDmetadata}",
170 'type' => "metadata",
171 'deft' => "dc.Identifier",
172 'reqd' => "no",
173 'modegli' => "3" },
174 { 'name' => "out",
175 'desc' => "{import.out}",
176 'type' => "string",
177 'deft' => "STDERR",
178 'reqd' => "no",
179 'hiddengli' => "yes" },
180 { 'name' => "saveas",
181 'desc' => "{import.saveas}",
182 'type' => "enum",
183 'list' => $saveas_list,
184 'deft' => "GA",
185 'reqd' => "no",
186 'modegli' => "3" },
187 { 'name' => "sortmeta",
188 'desc' => "{import.sortmeta}",
189 'type' => "metadatum",
190# 'type' => "string",
191 'reqd' => "no",
192 'modegli' => "3" },
193 { 'name' => "removeprefix",
194 'desc' => "{BasClas.removeprefix}",
195 'type' => "regexp",
196 'deft' => "",
197 'reqd' => "no",
198 'modegli' => "3" },
199 { 'name' => "removesuffix",
200 'desc' => "{BasClas.removesuffix}",
201 'type' => "regexp",
202 'deft' => "",
203 'reqd' => "no",
204 'modegli' => "3" },
205 { 'name' => "statsfile",
206 'desc' => "{import.statsfile}",
207 'type' => "string",
208 'deft' => "STDERR",
209 'reqd' => "no",
210 'hiddengli' => "yes" },
211 { 'name' => "verbosity",
212 'desc' => "{import.verbosity}",
213 'type' => "int",
214 'range' => "0,",
215 # parsearg left "" as default
216 #'deft' => "2",
217 'reqd' => "no",
218 'modegli' => "4" },
219 { 'name' => "gli",
220 'desc' => "",
221 'type' => "flag",
222 'reqd' => "no",
223 'hiddengli' => "yes" },
224 { 'name' => "xml",
225 'desc' => "{scripts.xml}",
226 'type' => "flag",
227 'reqd' => "no",
228 'hiddengli' => "yes" }];
229
230my $options = { 'name' => "import.pl",
231 'desc' => "{import.desc}",
232 'args' => $arguments };
233
234
235&main();
236
237sub main {
238 my ($verbosity, $importdir, $archivedir, $manifest, $keepold,
239 $removeold, $saveas, $version,
240 $gzip, $groupsize, $OIDtype, $OIDmetadata, $debug,
241 $maxdocs, $collection, $configfilename, $collectcfg,
242 $pluginfo, $sortmeta, $removeprefix, $removesuffix,
243 $archive_info_filename, $statsfile,
244 $archive_info, $processor, $out, $faillog, $collectdir, $gli, $language);
245
246 my $xml = 0;
247
248 my $service = "import";
249
250 my $hashParsingResult = {};
251 my $blnParseFailed = "false";
252 # general options available to all plugins
253 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
254 # If there is more than one argument left after parsing, it mean user input too many arguments.
255 if($intArgLeftinAfterParsing > 1)
256 {
257 &PrintUsage::print_txt_usage($options, "{import.params}");
258 die "\n";
259 }
260
261 foreach my $strVariable (keys %$hashParsingResult)
262 {
263 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
264 }
265
266 # If $language has been specified, load the appropriate resource bundle
267 # (Otherwise, the default resource bundle will be loaded automatically)
268 if ($language && $language =~ /\S/) {
269 &gsprintf::load_language_specific_resource_bundle($language);
270 }
271
272 if ($xml) {
273 &PrintUsage::print_xml_usage($options);
274 print "\n";
275 return;
276 }
277
278 if ($gli) { # the gli wants strings to be in UTF-8
279 &gsprintf::output_strings_in_UTF8;
280 }
281
282 my $close_out = 0;
283 if ($out !~ /^(STDERR|STDOUT)$/i) {
284 open (OUT, ">$out") ||
285 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
286 $out = 'import::OUT';
287 $close_out = 1;
288 }
289 $out->autoflush(1);
290
291 # get and check the collection name
292 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
293 &PrintUsage::print_txt_usage($options, "{import.params}");
294 die "\n";
295 }
296 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
297
298 if ($faillog eq "") {
299 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
300 }
301 open (FAILLOG, ">$faillog") ||
302 (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
303
304
305 my $faillogname = $faillog;
306 $faillog = 'import::FAILLOG';
307 $faillog->autoflush(1);
308
309 # check sortmeta
310 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
311 if (defined $sortmeta && $groupsize > 1) {
312 &gsprintf($out, "{import.cannot_sort}\n\n");
313 $sortmeta = undef;
314 }
315
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
318 # -collectdir, -out and -faillog may be specified in the collect.cfg (these
319 # options must be known before we read the collect.cfg))
320 my $plugins = [];
321 my @global_opts = ();
322
323 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
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 }
332
333 if ($verbosity !~ /\d+/) {
334 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
335 $verbosity = $collectcfg->{'verbosity'};
336 } else {
337 $verbosity = 2; # the default
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 if (defined $collectcfg->{'manifest'} && $manifest eq "") {
347 $manifest = $collectcfg->{'manifest'};
348 }
349
350 if (defined $collectcfg->{'gzip'} && !$gzip) {
351 if ($collectcfg->{'gzip'} =~ /^true$/i) {
352 $gzip = 1;
353 }
354 }
355 if ($maxdocs !~ /\-?\d+/) {
356 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
357 $maxdocs = $collectcfg->{'maxdocs'};
358 } else {
359 $maxdocs = -1; # the default
360 }
361 }
362 if ($groupsize == 1) {
363 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
364 $groupsize = $collectcfg->{'groupsize'};
365 }
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
372 }
373 }
374
375
376 if ($saveas !~ /^(GA|METS)$/) {
377 if (defined $collectcfg->{'plugout'} && $collectcfg->{'plugout'}[0] =~ /^(GAPlugout|METSPlugout)$/) {
378 $saveas = $collectcfg->{'plugout'}[0];
379 } else {
380 $saveas ="GAPlugout";
381 }
382 }
383
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
411 $gli = 0 unless defined $gli;
412
413 print STDERR "<Import>\n" if $gli;
414
415 # fill in the default import and archives directories if none
416 # were supplied, turn all \ into / and remove trailing /
417 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
418 $importdir =~ s/[\\\/]+/\//g;
419 $importdir =~ s/\/$//;
420 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
421 $archivedir =~ s/[\\\/]+/\//g;
422 $archivedir =~ s/\/$//;
423
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
439 # load all the plugins
440 $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts);
441 if (scalar(@$pluginfo) == 0) {
442 &gsprintf($out, "{import.no_plugins_loaded}\n");
443 die "\n";
444 }
445
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 }
452 my $tmpdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "tmp");
453 $tmpdir =~ s/[\\\/]+/\//g;
454 $tmpdir =~ s/\/$//;
455 if (-e $tmpdir) {
456 &gsprintf($out, "{import.removing_tmpdir}\n");
457 &util::rm_r ($tmpdir);
458 }
459 }
460
461 # read the archive information file
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);
466
467
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
479 push @$opts,("-output_info",$archive_info) if (defined $archive_info);
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
486
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);
491
492
493 } else {
494 $processor = new docprint ();
495 }
496
497 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs, $gli);
498
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
517 &plugin::end($pluginfo, $processor);
518
519 &plugin::deinit($pluginfo, $processor);
520
521 # write out the archive information file
522 if (!$debug) {
523 $processor->close_file_output() if $groupsize > 1;
524 $processor->close_group_output() if $processor->is_group();
525 $archive_info->save_info($archive_info_filename);
526 }
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 {
535 &gsprintf($out, "{import.cannot_open_stats_file}", $statsfile);
536 &gsprintf($out, "{import.stats_backup}\n");
537 $statsfile = 'STDERR';
538 }
539 }
540
541 &gsprintf($out, "\n");
542 &gsprintf($out, "*********************************************\n");
543 &gsprintf($out, "{import.complete}\n");
544 &gsprintf($out, "*********************************************\n");
545
546 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
547 if ($close_stats) {
548 close STATS;
549 }
550
551 close OUT if $close_out;
552 close FAILLOG;
553}
Note: See TracBrowser for help on using the repository browser.