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

Last change on this file since 10162 was 10162, checked in by davidb, 19 years ago

import.pl and export.pl updated in-line with merging of expinfo and arcinfo
into one class (the "merged" class is arcinfo as this was the original class
and expinfo was unnecessarily cloned from it).

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