root/gsdl/trunk/bin/script/import.pl @ 17142

Revision 17142, 18.7 KB (checked in by kjdon, 12 years ago)

tidied up import and export scripts a little. Reordered code to make them more similar in preparation for merging the scripts later. import only supports GA and GreenstoneMETS, export only supports Greenstone/FedoraMETS, DSpace, MARCXML. export.pl now only exports one collection at a time. And I have removed some options that didn't make sense - sortmeta, groupsize.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
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    if (defined $ENV{'GSDLEXTS'}) {
43    my @extensions = split(/:/,$ENV{'GSDLEXTS'});
44    foreach my $e (@extensions) {
45        my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
46
47        unshift (@INC, "$ext_prefix/perllib");
48        unshift (@INC, "$ext_prefix/perllib/cpan");
49        unshift (@INC, "$ext_prefix/perllib/plugins");
50        unshift (@INC, "$ext_prefix/perllib/plugouts");
51        unshift (@INC, "$ext_prefix/perllib/classify");
52    }
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
60use arcinfo;
61use colcfg;
62use plugin;
63use plugout;
64use manifest;
65use util;
66use scriptutil;
67use FileHandle;
68use gsprintf 'gsprintf';
69use printusage;
70use parse2;
71
72
73
74my $oidtype_list =
75    [ { 'name' => "hash",
76        'desc' => "{import.OIDtype.hash}" },
77      { 'name' => "assigned",
78        'desc' => "{import.OIDtype.assigned}" },
79      { 'name' => "incremental",
80        'desc' => "{import.OIDtype.incremental}" },
81      { 'name' => "dirname",
82        'desc' => "{import.OIDtype.dirname}" } ];
83
84
85# used to control output file format
86my $saveas_list =
87    [ { 'name' => "GA",
88        'desc' => "{export.saveas.GA}"},
89      { 'name' => "GreenstoneMETS",
90        'desc' => "{export.saveas.GreenstoneMETS}"},
91      ];
92
93
94# Possible attributes for each argument
95# name: The name of the argument
96# desc: A description (or more likely a reference to a description) for this argument
97# type: The type of control used to represent the argument. Options include: string, int, flag, regexp, metadata, language, enum etc
98# reqd: Is this argument required?
99# hiddengli: Is this argument hidden in GLI?
100# modegli: The lowest detail mode this argument is visible at in GLI
101
102my $saveas_argument
103    = { 'name' => "saveas",
104    'desc' => "{import.saveas}",
105    'type' => "enum",
106    'list' => $saveas_list,
107    'deft' => "GA",
108    'reqd' => "no",
109    'modegli' => "3" };
110
111
112my $arguments =
113    [
114      $saveas_argument,
115      { 'name' => "archivedir",
116    'desc' => "{import.archivedir}",
117    'type' => "string",
118    'reqd' => "no",
119        'hiddengli' => "yes" },
120      { 'name' => "importdir",
121    'desc' => "{import.importdir}",
122    'type' => "string",
123    'reqd' => "no",
124        'hiddengli' => "yes" },
125      { 'name' => "collectdir",
126    'desc' => "{import.collectdir}",
127    'type' => "string",
128    # parsearg left "" as default
129    #'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
130    'deft' => "",
131    'reqd' => "no",
132        'hiddengli' => "yes" },
133      { 'name' => "site",
134    'desc' => "{import.site}",
135    'type' => "string",
136    'deft' => "",
137    'reqd' => "no",
138        'hiddengli' => "yes" },
139      { 'name' => "manifest",
140    'desc' => "{import.manifest}",
141    'type' => "string",
142    'deft' => "",
143    'reqd' => "no",
144        'hiddengli' => "yes" },
145      { 'name' => "debug",
146    'desc' => "{import.debug}",
147    'type' => "flag",
148    'reqd' => "no",
149        'hiddengli' => "yes" },
150      { 'name' => "faillog",
151    'desc' => "{import.faillog}",
152    'type' => "string",
153    # parsearg left "" as default
154    #'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
155    'deft' => "",
156    'reqd' => "no",
157        'modegli' => "4" },
158      { 'name' => "incremental",
159    'desc' => "{import.incremental}",
160    'type' => "flag",
161    'hiddengli' => "yes" },
162      { 'name' => "keepold",
163    'desc' => "{import.keepold}",
164    'type' => "flag",
165    'reqd' => "no",
166    'hiddengli' => "yes" },
167      { 'name' => "removeold",
168    'desc' => "{import.removeold}",
169    'type' => "flag",
170    'reqd' => "no",
171    'hiddengli' => "yes" },
172      { 'name' => "language",
173    'desc' => "{scripts.language}",
174    'type' => "string",
175    'reqd' => "no",
176    'hiddengli' => "yes" },
177      { 'name' => "maxdocs",
178    'desc' => "{import.maxdocs}",
179    'type' => "int",
180    'reqd' => "no",
181    # parsearg left "" as default
182    #'deft' => "-1",
183    'range' => "1,",
184    'modegli' => "1" },
185      # don't set the default to hash - want to allow this to come from
186      # entry in collect.cfg but want to override it here
187      { 'name' => "OIDtype",
188    'desc' => "{import.OIDtype}",
189    'type' => "enum",
190    'list' => $oidtype_list,
191    # parsearg left "" as default
192    #'deft' => "hash",
193    'reqd' => "no",
194    'modegli' => "2" },
195      { 'name' => "OIDmetadata",
196    'desc' => "{import.OIDmetadata}",
197    'type' => "metadata",
198    'deft' => "dc.Identifier",
199    'reqd' => "no",
200    'modegli' => "2" },
201      { 'name' => "out",
202    'desc' => "{import.out}",
203    'type' => "string",
204    'deft' => "STDERR",
205    'reqd' => "no",
206        'hiddengli' => "yes" },
207      { 'name' => "sortmeta",
208    'desc' => "{import.sortmeta}",
209    'type' => "metadata",
210    'reqd' => "no",
211    'modegli' => "3" },
212      { 'name' => "reversesort",
213    'desc' => "{import.reversesort}",
214    'type' => "flag",
215    'reqd' => "no",
216    'modegli' => "3" },
217      { 'name' => "removeprefix",
218    'desc' => "{BasClas.removeprefix}",
219    'type' => "regexp",
220    'deft' => "",
221    'reqd' => "no",
222    'modegli' => "3" },
223      { 'name' => "removesuffix",
224    'desc' => "{BasClas.removesuffix}",
225    'type' => "regexp",
226    'deft' => "",
227    'reqd' => "no",
228    'modegli' => "3" },
229      { 'name' => "groupsize",
230    'desc' => "{import.groupsize}",
231    'type' => "int",
232    'deft' => "1",
233    'reqd' => "no",
234    'modegli' => "3" },
235      { 'name' => "gzip",
236    'desc' => "{import.gzip}",
237    'type' => "flag",
238    'reqd' => "no",
239    'modegli' => "4" },
240      { 'name' => "statsfile",
241    'desc' => "{import.statsfile}",
242    'type' => "string",
243    'deft' => "STDERR",
244    'reqd' => "no",
245        'hiddengli' => "yes" },
246      { 'name' => "verbosity",
247    'desc' => "{import.verbosity}",
248    'type' => "int",
249    'range' => "0,",
250    # parsearg left "" as default
251    #'deft' => "2",
252    'reqd' => "no",
253    'modegli' => "4" },
254      { 'name' => "gli",
255    'desc' => "{scripts.gli}",
256    'type' => "flag",
257    'reqd' => "no",
258    'hiddengli' => "yes" },
259      { 'name' => "xml",
260    'desc' => "{scripts.xml}",
261    'type' => "flag",
262    'reqd' => "no",
263    'hiddengli' => "yes" }];
264
265my $options = { 'name' => "import.pl",
266        'desc' => "{import.desc}",
267        'args' => $arguments };
268
269
270&main();
271
272sub main {
273    # params
274    my ($language, $verbosity, $debug,
275    $collectdir, $importdir, $archivedir, $site, $manifest,
276    $incremental, $keepold, $removeold,
277    $saveas,
278    $OIDtype, $OIDmetadata,
279    $maxdocs, $statsfile,
280    $out, $faillog, $gli,
281    $gzip, $groupsize,
282    $sortmeta, $reversesort, $removeprefix, $removesuffix
283    );
284
285    my $xml = 0;
286
287    # other vars
288    my ($configfilename, $collection, $collectcfg,
289    $archive_info_filename, $archive_info,
290    $gs_mode,
291    $processor, $pluginfo);
292
293    my $service = "import";
294
295    my $hashParsingResult = {};
296    # general options available to all plugins
297    my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
298    # Parse returns -1 if something has gone wrong
299    if ($intArgLeftinAfterParsing == -1)
300    {
301    &PrintUsage::print_txt_usage($options, "{import.params}");
302    die "\n";
303    }
304   
305    foreach my $strVariable (keys %$hashParsingResult)
306    {
307    eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
308    }
309
310    # If $language has been specified, load the appropriate resource bundle
311    # (Otherwise, the default resource bundle will be loaded automatically)
312    if ($language && $language =~ /\S/) {
313    &gsprintf::load_language_specific_resource_bundle($language);
314    }
315
316    if ($xml) {
317        &PrintUsage::print_xml_usage($options);
318    print "\n";
319    return;
320    }
321
322    if ($gli) { # the gli wants strings to be in UTF-8
323    &gsprintf::output_strings_in_UTF8;
324    }
325   
326    # now check that we had exactly one leftover arg, which should be
327    # the collection name. We don't want to do this earlier, cos
328    # -xml arg doesn't need a collection name
329    # Or if the user specified -h, then we output the usage also
330    if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
331    {
332    &PrintUsage::print_txt_usage($options, "{import.params}");
333    die "\n";
334    }
335
336    my $close_out = 0;
337    if ($out !~ /^(STDERR|STDOUT)$/i) {
338    open (OUT, ">$out") ||
339        (&gsprintf(STDERR, "{common.cannot_open_output_file}: $!\n", $out) && die);
340    $out = 'import::OUT';
341    $close_out = 1;
342    }
343    $out->autoflush(1);
344
345    # get and check the collection name
346    if (($collection = &colcfg::use_collection($site, @ARGV, $collectdir)) eq "") {
347    &PrintUsage::print_txt_usage($options, "{import.params}");
348    die "\n";
349    }
350
351    # add collection's perllib dir  into include path in
352    # case we have collection specific modules
353    unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
354
355    # check that we can open the faillog
356    if ($faillog eq "") {
357    $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
358    }
359    open (FAILLOG, ">$faillog") ||
360    (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
361
362   
363    my $faillogname = $faillog;
364    $faillog = 'import::FAILLOG';
365    $faillog->autoflush(1);
366   
367    # Read in the collection configuration file.
368    ($configfilename, $gs_mode) = &colcfg::get_collect_cfg_name($out);
369
370    if ($gs_mode eq "gs2") {
371        $collectcfg = &colcfg::read_collect_cfg ($configfilename);
372    } elsif ($gs_mode eq "gs3") {
373    $collectcfg = &colcfg::read_collection_cfg_xml ($configfilename);
374    }
375
376    if (defined $collectcfg->{'importdir'} && $importdir eq "") {
377    $importdir = $collectcfg->{'importdir'};
378    }
379    if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
380    $archivedir = $collectcfg->{'archivedir'};
381    }
382    # fill in the default import and archives directories if none
383    # were supplied, turn all \ into / and remove trailing /
384    $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
385    $importdir =~ s/[\\\/]+/\//g;
386    $importdir =~ s/\/$//;
387    if (!-e $importdir) {
388    &gsprintf($out, "{import.no_import_dir}\n\n", $importdir);
389    die "\n";
390    }
391
392    $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
393    $archivedir =~ s/[\\\/]+/\//g;
394    $archivedir =~ s/\/$//;
395
396    my $plugins = [];
397    if (defined $collectcfg->{'plugin'}) {
398    $plugins = $collectcfg->{'plugin'};
399    }
400    #some global options for the plugins
401    my @global_opts = ();
402
403    if ($verbosity !~ /\d+/) {
404    if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
405        $verbosity = $collectcfg->{'verbosity'};
406    } else {
407        $verbosity = 2; # the default
408    }
409    }
410    if (defined $collectcfg->{'manifest'} && $manifest eq "") {
411    $manifest = $collectcfg->{'manifest'};
412    }
413
414    if (defined $collectcfg->{'gzip'} && !$gzip) {
415    if ($collectcfg->{'gzip'} =~ /^true$/i) {
416        $gzip = 1;
417    }
418    }
419
420    if ($maxdocs !~ /\-?\d+/) {
421    if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
422        $maxdocs = $collectcfg->{'maxdocs'};
423    } else {
424        $maxdocs = -1; # the default
425    }
426    }
427    if ($groupsize == 1) {
428    if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
429        $groupsize = $collectcfg->{'groupsize'};
430    }
431    }
432
433    if (!defined $OIDtype || ($OIDtype !~ /^(hash|incremental|assigned|dirname)$/ )) {
434    if (defined $collectcfg->{'OIDtype'} && $collectcfg->{'OIDtype'} =~ /^(hash|incremental|assigned|dirname)$/) {
435        $OIDtype = $collectcfg->{'OIDtype'};
436    } else {
437        $OIDtype = "hash"; # the default
438    }
439    }
440
441    if (defined $collectcfg->{'sortmeta'} && (!defined $sortmeta || $sortmeta eq "")) {
442    $sortmeta = $collectcfg->{'sortmeta'};
443    }
444    # sortmeta cannot be used with group size
445    $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
446    if (defined $sortmeta && $groupsize > 1) {
447    &gsprintf($out, "{import.cannot_sort}\n\n");
448    $sortmeta = undef;
449    }
450   
451    if (defined $sortmeta) {
452    if (defined $collectcfg->{'reversesort'} && $collectcfg->{'reversesort'} =~ /^true$/i) {
453        $reversesort = 1;
454    }
455    } else {
456    # reversesort only valid with sortmeta
457    $reversesort = 0;
458    }
459    if (defined $collectcfg->{'removeprefix'} && $removeprefix eq "") {
460    $removeprefix = $collectcfg->{'removeprefix'};
461    }
462   
463    if (defined $collectcfg->{'removesuffix'} && $removesuffix eq "") {
464    $removesuffix = $collectcfg->{'removesuffix'};
465    }
466    if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
467    $debug = 1;
468    }
469    if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
470    $gli = 1;
471    }
472    $gli = 0 unless defined $gli;
473       
474    # check keepold and removeold
475    ($removeold, $keepold, $incremental) = &scriptutil::check_removeold_and_keepold($removeold, $keepold, $incremental, "archives", $collectcfg);
476 
477
478    print STDERR "<Import>\n" if $gli;
479   
480    my $manifest_lookup = new manifest();
481    if ($manifest ne "") { 
482    my $manifest_filename = $manifest;
483
484    if ($manifest_filename !~ m/^[\\\/]/) {
485        $manifest_filename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, $manifest_filename);
486    }
487
488    $manifest =~ s/[\\\/]+/\//g;
489    $manifest =~ s/\/$//;
490
491    $manifest_lookup->parse($manifest_filename);
492    }
493
494
495    # load all the plugins
496    $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts, $incremental);
497    if (scalar(@$pluginfo) == 0) {
498    &gsprintf($out, "{import.no_plugins_loaded}\n");
499    die "\n";
500    }
501
502    # remove the old contents of the archives directory (and tmp directory) if needed
503    if ($removeold) {
504    if (-e $archivedir) {
505        &gsprintf($out, "{import.removing_archives}\n");
506        &util::rm_r ($archivedir);
507    }
508    my $tmpdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "tmp");
509    $tmpdir =~ s/[\\\/]+/\//g;
510    $tmpdir =~ s/\/$//;
511    if (-e $tmpdir) {
512        &gsprintf($out, "{import.removing_tmpdir}\n");
513        &util::rm_r ($tmpdir);
514    }
515    }
516    # create the archives dir if needed
517    &util::mk_all_dir($archivedir);
518
519    # read the archive information file
520    $archive_info_filename = &util::filename_cat ($archivedir, "archives.inf");
521   
522    $archive_info = new arcinfo ();
523    $archive_info->load_info ($archive_info_filename);
524    if ($reversesort) {
525    $archive_info->reverse_sort();
526    }
527
528    ####Use Plugout####
529    my ($plugout);
530    if (defined $collectcfg->{'plugout'} && $collectcfg->{'plugout'} =~ /^(GA|GreenstoneMETS)Plugout/) {
531    $plugout = $collectcfg->{'plugout'};
532    }
533    else{
534    if ($saveas !~ /^(GA|GreenstoneMETS)$/) {
535        push @$plugout,"GAPlugout";
536    }
537    else{
538        push @$plugout,$saveas."Plugout";
539    }
540    }
541
542    push @$plugout,("-output_info",$archive_info) if (defined $archive_info);
543    push @$plugout,("-verbosity",$verbosity) if (defined $verbosity);
544    push @$plugout,("-gzip_output") if ($gzip);
545    push @$plugout,("-group_size",$groupsize) if (defined $groupsize);
546    push @$plugout,("-output_handle",$out) if (defined $out);
547    push @$plugout,("-debug") if ($debug);
548   
549    $processor = &plugout::load_plugout($plugout);                       
550    $processor->setoutputdir ($archivedir);
551    $processor->set_sortmeta ($sortmeta, $removeprefix, $removesuffix) if defined $sortmeta;
552    $processor->set_OIDtype ($OIDtype, $OIDmetadata);
553   
554    &plugin::begin($pluginfo, $importdir, $processor, $maxdocs, $gli);
555
556    if ($manifest eq "") {
557    # process the import directory
558    my $block_hash = {};
559    my $metadata = {};
560    # gobal blocking pass may set up some metadata
561    &plugin::file_block_read($pluginfo, $importdir, "", $block_hash, $metadata, $gli);
562    &plugin::read ($pluginfo, $importdir, "", $block_hash, $metadata, $processor, $maxdocs, 0, $gli);
563    }
564    else
565    {
566    # process any files marked for importing
567    foreach my $file (keys %{$manifest_lookup->{'import'}}) {
568        &plugin::read ($pluginfo, $importdir, $file, {}, {}, $processor, $maxdocs, 0, $gli);
569    }
570
571    # record files marked for deletion in arcinfo
572    foreach my $file (keys %{$manifest_lookup->{'delete'}}) {
573        # consider finding it?
574        # $archive_info->add_info($OID,$doc_xml_file,"D");
575    }
576    }
577
578    &plugin::end($pluginfo, $processor);
579
580    &plugin::deinit($pluginfo, $processor);
581
582    # write out the archive information file
583    $processor->close_file_output() if $groupsize > 1;
584    $processor->close_group_output() if $processor->is_group();
585
586# The following 'if' statement is in the export.pl version of the script,
587# but not (so far) the import.pl version.  Why is this?
588##    if ($saveas =~ m/^.*METS$/) {
589    # should we still do this in debug mode??
590
591    $archive_info->save_info($archive_info_filename);
592##    }
593   
594    # write out import stats
595    my $close_stats = 0;
596    if ($statsfile !~ /^(STDERR|STDOUT)$/i) {
597    if (open (STATS, ">$statsfile")) {
598        $statsfile = 'import::STATS';
599        $close_stats = 1;
600    } else {
601        &gsprintf($out, "{import.cannot_open_stats_file}", $statsfile);
602        &gsprintf($out, "{import.stats_backup}\n");
603        $statsfile = 'STDERR';
604    }
605    }
606
607    &gsprintf($out, "\n");
608    &gsprintf($out, "*********************************************\n");
609    &gsprintf($out, "{import.complete}\n");
610    &gsprintf($out, "*********************************************\n");
611
612    &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
613    if ($close_stats) {
614    close STATS;
615    }
616
617    close OUT if $close_out;
618    close FAILLOG;
619}
Note: See TracBrowser for help on using the browser.