source: gsdl/trunk/bin/script/g2f-import.pl@ 14959

Last change on this file since 14959 was 14959, checked in by davidb, 15 years ago

Greenstone to Fedora scripts. Similar to import.pl and buildcol.pl, g2f-import.pl first converts documents into FedoraMETS and then g2f-buildcol.pl ingests them into a Fedora repository. Needs to have FEDORA_HOME set.

  • Property svn:executable set to *
File size: 7.3 KB
Line 
1#!/usr/bin/perl -w
2
3BEGIN
4{
5 if (!defined $ENV{'GSDLHOME'}) {
6 print STDERR "Environment variable GSDLHOME not set.\n";
7 print STDERR " Have you sourced Greenstone's 'setup.bash' file?\n";
8 exit 1;
9 }
10
11 $ENV{'FEDORA_HOSTNAME'} = "localhost" if (!defined $ENV{'FEDORA_HOSTNAME'});
12 $ENV{'FEDORA_SERVER_PORT'} = "8080" if (!defined $ENV{'FEDORA_SERVER_PORT'});
13 $ENV{'FEDORA_USER'} = "fedoraAdmin" if (!defined $ENV{'FEDORA_USER'});
14 $ENV{'FEDORA_PASS'} = "fedoraAdmin" if (!defined $ENV{'FEDORA_PASS'});
15 $ENV{'FEDORA_PROTOCOL'} = "http" if (!defined $ENV{'FEDORA_PROTOCOL'});
16 $ENV{'FEDORA_PID_NAMESPACE'} = "greenstone" if (!defined $ENV{'FEDORA_PID_NAMESPACE'});
17
18 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/");
19}
20
21
22use strict;
23no strict 'refs'; # allow filehandles to be variables and vice versa
24no strict 'subs'; # allow barewords (e.g. STDERR) as function arguments
25
26use util;
27use gsprintf 'gsprintf';
28use printusage;
29use parse2;
30
31
32use g2futil;
33
34
35my $arguments =
36 [
37 { 'name' => "verbosity",
38 'desc' => "Level of verbosity generated",
39 'type' => "string",
40 'deft' => '1',
41 'reqd' => "no",
42 'hiddengli' => "no" },
43 { 'name' => "hostname",
44 'desc' => "Domain hostname of Fedora server",
45 'type' => "string",
46 'deft' => $ENV{'FEDORA_HOSTNAME'},
47 'reqd' => "no",
48 'hiddengli' => "no" },
49 { 'name' => "port",
50 'desc' => "Port that the Fedora server is running on.",
51 'type' => "string",
52 'deft' => $ENV{'FEDORA_SERVER_PORT'},
53 'reqd' => "no",
54 'hiddengli' => "no" },
55 { 'name' => "username",
56 'desc' => "Fedora admin username",
57 'type' => "string",
58 'deft' => $ENV{'FEDORA_USER'},
59 'reqd' => "no",
60 'hiddengli' => "no" },
61 { 'name' => "password",
62 'desc' => "Fedora admin password",
63 'type' => "string",
64 'deft' => $ENV{'FEDORA_PASS'},
65 'reqd' => "no",
66 'hiddengli' => "no" },
67 { 'name' => "protocol",
68 'desc' => "Fedora protocol, e.g. 'http' or 'https'",
69 'type' => "string",
70 'deft' => $ENV{'FEDORA_PROTOCOL'},
71 'reqd' => "no",
72 'hiddengli' => "no" },
73 { 'name' => "pidnamespace",
74 'desc' => "Fedora prefix for PIDs",
75 'type' => "string",
76 'deft' => $ENV{'FEDORA_PID_NAMESPACE'},
77 'reqd' => "no",
78 'hiddengli' => "no" },
79 { 'name' => "maxdocs",
80 'desc' => "{import.maxdocs}",
81 'type' => "int",
82 'reqd' => "no",
83 'range' => "1,",
84 'modegli' => "1" },
85 { 'name' => "gli",
86 'desc' => "",
87 'type' => "flag",
88 'reqd' => "no",
89 'hiddengli' => "yes" },
90 { 'name' => "xml",
91 'desc' => "{scripts.xml}",
92 'type' => "flag",
93 'reqd' => "no",
94 'hiddengli' => "yes" },
95 { 'name' => "removeold",
96 'desc' => "{import.removeold}",
97 'type' => "flag",
98 'reqd' => "no",
99 'modegli' => "3" },
100 { 'name' => "language",
101 'desc' => "{scripts.language}",
102 'type' => "string",
103 'reqd' => "no",
104 'modegli' => "4" },
105 { 'name' => "collectdir",
106 'desc' => "{import.collectdir}",
107 'type' => "string",
108 'deft' => "",
109 'reqd' => "no",
110 'hiddengli' => "yes" },
111
112
113 ];
114
115my $prog_options
116 = { 'name' => "g2fimport.pl",
117 'desc' => "Generate Fedora METS format using Greenstone document processing capabilities",
118 'args' => $arguments };
119
120
121sub main
122{
123 my (@ARGV) = @_;
124
125 my $GSDLHOME = $ENV{'GSDLHOME'};
126
127
128 my $options = {};
129 # general options available to all plugins
130 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$options,"allow_extra_options");
131
132 # Something went wrong with parsing
133 if ($intArgLeftinAfterParsing ==-1)
134 {
135 &PrintUsage::print_txt_usage($prog_options, "[options] greenstone-col");
136 die "\n";
137 }
138
139 my $xml = $options->{'xml'};
140 my $gli = $options->{'gli'};
141
142 if ($intArgLeftinAfterParsing != 1)
143 {
144 if ($xml) {
145 &PrintUsage::print_xml_usage($prog_options);
146 print "\n";
147 return;
148 }
149 else {
150 &PrintUsage::print_txt_usage($prog_options, "[options] greenstone-col");
151 print "\n";
152 return;
153 }
154
155 }
156
157 if ($gli) { # the gli wants strings to be in UTF-8
158 &gsprintf::output_strings_in_UTF8;
159 }
160
161 my $gs_col = $ARGV[0];
162
163 my $verbosity = $options->{'verbosity'};
164 my $hostname = $options->{'hostname'};
165 my $port = $options->{'port'};
166 my $username = $options->{'username'};
167 my $password = $options->{'password'};
168 my $protocol = $options->{'protocol'};
169 my $pid_namespace = $options->{'pidnamespace'};
170
171 # The following are needed in the FedoraMETS plugout
172 $ENV{'FEDORA_HOSTNAME'} = $hostname;
173 $ENV{'FEDORA_SERVER_PORT'} = $port;
174
175 my $language = $options->{'language'};
176 my $collectdir = $options->{'collectdir'};
177 my $removeold = $options->{'removeold'};
178 my $maxdocs = $options->{'maxdocs'};
179
180 if (!$collectdir) {
181 # Explicitly set one
182
183 ## $collectdir = util::filename_cat($ENV{'GREENSTONE3_HOME'},
184 ## "web","sites","localsite","collect");
185
186 $collectdir = util::filename_cat($ENV{'GSDLHOME'},"collect");
187
188 }
189
190 my $full_gs_col = util::filename_cat($collectdir,$gs_col);
191
192 if (!-e $full_gs_col ) {
193 print STDERR "Unable to find Greenstone collection $full_gs_col\n";
194 exit 1;
195 }
196
197 my $archives_dir = &util::filename_cat($full_gs_col,"archives");
198 my $export_dir = &util::filename_cat($full_gs_col,"export");
199
200 if ( -e $export_dir ) {
201 print "***\n";
202 print "* Removing existing Greenstone $gs_col objects from Fedora $pid_namespace\n";
203 print "***\n";
204
205 # readdir
206 if (opendir(DIR, $export_dir)) {
207 my @hash_dirs = grep { /\.dir$/ } readdir(DIR);
208 closedir DIR;
209
210
211 # for each hash dir, purge its respective PID
212 foreach my $hd (@hash_dirs) {
213
214 my $full_hd = &util::filename_cat($export_dir,$hd);
215 my $hash_id = &g2futil::get_hash_id($full_hd);
216
217 next if !defined $hash_id;
218
219 my $pid = "$pid_namespace:$gs_col-$hash_id";
220
221 my $dsinfo_status = &g2futil::run_datastore_info($pid,$options);
222
223 if ($dsinfo_status == 0) {
224 print " $pid being removed.\n";
225 &g2futil::run_purge($pid,$options);
226 }
227 else {
228 print " $pid not present.\n";
229 }
230 }
231 }
232 else {
233 print STDERR "Error: Unable to open directory $export_dir: $!\n";
234 exit;
235 }
236 }
237
238 print "***\n";
239 print "* Processing $gs_col into FedoraMETS format\n";
240 print "***\n";
241
242 my $gs_export_opts = "-saveas FedoraMETS -fedora_namespace $pid_namespace -verbosity $verbosity";
243
244 $gs_export_opts .= " -gli" if ($gli);
245
246 $gs_export_opts .= " -language $language" if ($language);
247 $gs_export_opts .= " -collectdir $collectdir" if ($collectdir);
248 $gs_export_opts .= " -removeold" if ($removeold);
249 $gs_export_opts .= " -maxdocs $maxdocs" if ($maxdocs);
250
251 $gs_export_opts .= " -exportdir $export_dir";
252
253 my $gs_export_arguments = "$gs_export_opts $gs_col";
254
255 &g2futil::run_cmd("export.pl", $gs_export_arguments, $options);
256
257
258 print "***\n";
259 print "* Processing $gs_col for GLI \n";
260 print "***\n";
261
262 my $gs_import_opts = "-verbosity $verbosity";
263
264 $gs_import_opts .= " -gli" if ($gli);
265
266 $gs_import_opts .= " -language $language" if ($language);
267 $gs_import_opts .= " -collectdir $collectdir" if ($collectdir);
268 $gs_import_opts .= " -removeold" if ($removeold);
269 $gs_import_opts .= " -maxdocs $maxdocs" if ($maxdocs);
270
271## $gs_import_opts .= " -archivedir $archive_dir"; # Not needed?
272
273 my $gs_import_arguments = "$gs_import_opts $gs_col";
274
275 &g2futil::run_cmd("import.pl", $gs_import_arguments, $options);
276
277
278}
279
280&main(@ARGV);
281
282
283
Note: See TracBrowser for help on using the repository browser.