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

Last change on this file since 1587 was 1587, checked in by nzdl, 24 years ago

* empty log message *

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.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/plugins");
38 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
39}
40
41use arcinfo;
42use colcfg;
43use plugin;
44use docprint;
45use util;
46use parsargv;
47use FileHandle;
48
49sub print_usage {
50 print STDERR "\n usage: $0 [options] collection-name\n\n";
51 print STDERR " options:\n";
52 print STDERR " -verbosity number 0=none, 3=lots\n";
53 print STDERR " -importdir directory Where the original material lives\n";
54 print STDERR " -archivedir directory Where the converted material ends up\n";
55 print STDERR " -keepold Will not destroy the current contents of the\n";
56 print STDERR " archives directory (the default)\n";
57 print STDERR " -removeold Will remove the old contents of the archives\n";
58 print STDERR " directory -- use with care\n";
59 print STDERR " -gzip Use gzip to compress resulting gml documents\n";
60 print STDERR " (don't forget to include ZIPPlug in your plugin\n";
61 print STDERR " list when building from compressed documents)\n";
62 print STDERR " -maxdocs number Maximum number of documents to import\n";
63 print STDERR " -groupsize number Number of GML documents to group into one file\n";
64 print STDERR " -sortmeta metadata Sort documents alphabetically by metadata for\n";
65 print STDERR " building. This will be disabled if groupsize > 1\n";
66 print STDERR " -debug Print imported text to STDOUT\n";
67 print STDERR " -collectdir directory Collection directory (defaults to " .
68 &util::filename_cat ($ENV{'GSDLHOME'}, "collect") . ")\n";
69 print STDERR " -out Filename or handle to print output status to.\n";
70 print STDERR " The default is STDERR\n\n";
71}
72
73&main();
74
75sub main {
76 my ($verbosity, $importdir, $archivedir, $keepold,
77 $removeold, $gzip, $groupsize, $debug, $maxdocs, $collection,
78 $configfilename, $collectcfg, $pluginfo, $sortmeta,
79 $archive_info_filename, $archive_info, $processor,
80 $out, $collectdir);
81 if (!parsargv::parse(\@ARGV,
82 'verbosity/\d+/2', \$verbosity,
83 'importdir/.*/', \$importdir,
84 'archivedir/.*/', \$archivedir,
85 'keepold', \$keepold,
86 'removeold', \$removeold,
87 'gzip', \$gzip,
88 'groupsize/\d+/1', \$groupsize,
89 'sortmeta/.*/', \$sortmeta,
90 'debug', \$debug,
91 'maxdocs/^\-?\d+/-1', \$maxdocs,
92 'collectdir/.*/', \$collectdir,
93 'out/.*/STDERR', \$out)) {
94 &print_usage();
95 die "\n";
96 }
97
98 my $close_out = 0;
99 if ($out !~ /^(STDERR|STDOUT)$/i) {
100 open (OUT, ">$out") || die "Couldn't open output file $out\n";
101 $out = 'import::OUT';
102 $close_out = 1;
103 }
104 $out->autoflush(1);
105
106 # set removeold to false if it has been defined
107 $removeold = 0 if ($keepold);
108
109 # get and check the collection name
110 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
111 &print_usage();
112 die "\n";
113 }
114
115 # check sortmeta
116 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
117 if (defined $sortmeta && $groupsize > 1) {
118 print $out "WARNING: import.pl cannot sort documents when groupsize > 1\n";
119 print $out " sortmeta option will be ignored\n\n";
120 $sortmeta = undef;
121 }
122
123 # dynamically load 'docsave' module so it can pick up on a collection
124 # specific docsave.pm is specified.
125
126 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
127 require docsave;
128
129
130 # get the list of plugins for this collection
131 my $plugins = [];
132 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
133 if (-e $configfilename) {
134 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
135 if (defined $collectcfg->{'plugin'}) {
136 $plugins = $collectcfg->{'plugin'};
137 }
138 if (defined $collectcfg->{'importdir'} && $importdir eq "") {
139 $importdir = $collectcfg->{'importdir'};
140 }
141 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
142 $archivedir = $collectcfg->{'archivedir'};
143 }
144 if (defined $collectcfg->{'removeold'}) {
145 if ($collectcfg->{'removeold'} =~ /^true$/i && !$keepold) {
146 $removeold = 1;
147 }
148 if ($collectcfg->{'removeold'} =~ /^false$/i && !$removeold) {
149 $removeold = 0;
150 }
151 }
152 } else {
153 die "Couldn't find the configuration file $configfilename\n";
154 }
155
156 # fill in the default import and archives directories if none
157 # were supplied, turn all \ into / and remove trailing /
158 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
159 $importdir =~ s/[\\\/]+/\//g;
160 $importdir =~ s/\/$//;
161 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
162 $archivedir =~ s/[\\\/]+/\//g;
163 $archivedir =~ s/\/$//;
164
165 # load all the plugins
166 $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out);
167 if (scalar(@$pluginfo) == 0) {
168 print $out "No plugins were loaded.\n";
169 die "\n";
170 }
171
172 # remove the old contents of the archives directory if needed
173 if ($removeold && -e $archivedir) {
174 print $out "Warning - removing current contents of the archives directory\n";
175 print $out " in preparation for the import\n";
176 sleep(5); # just in case...
177 &util::rm_r ($archivedir);
178 }
179
180 # read the archive information file
181 if (!$debug) {
182 $archive_info_filename = &util::filename_cat ($archivedir, "archives.inf");
183 $archive_info = new arcinfo ();
184 $archive_info->load_info ($archive_info_filename);
185
186 # create a docsave object to process the documents
187 $processor = new docsave ($collection, $archive_info, $verbosity, $gzip, $groupsize, $out);
188 $processor->setarchivedir ($archivedir);
189 $processor->set_sortmeta ($sortmeta) if defined $sortmeta;
190 } else {
191 $processor = new docprint ();
192 }
193
194 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs);
195
196 # process the import directory
197 &plugin::read ($pluginfo, $importdir, "", {}, $processor, $maxdocs);
198
199 &plugin::end($pluginfo, $processor);
200
201 # write out the archive information file
202 if (!$debug) {
203 $processor->close_file_output() if $groupsize > 1;
204 $archive_info->save_info($archive_info_filename);
205 }
206 close OUT if $close_out;
207}
Note: See TracBrowser for help on using the repository browser.