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

Revision 14957, 19.0 KB (checked in by davidb, 11 years ago)

import.pl statements re-ordered to be more inkeeping with export.pl. The two programs share many similarities and at some point the common code should be moved out to a perl module and the scripts simplified to take advantage of that similarity

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