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

Last change on this file since 12003 was 12003, checked in by davidb, 18 years ago

Scripts upgraded to perform more efficiently with incremental addition.

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