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

Last change on this file since 10481 was 10481, checked in by mdewsnip, 19 years ago

Oops... forgot a "my" for a new variable.

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