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

Last change on this file since 17215 was 17215, checked in by ak19, 16 years ago

Minor. Description of verbosity option uses existing descriptive string loaded from strings.properties rather than a fixed static English-language string.

  • Property svn:executable set to *
File size: 6.8 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' => "{import.verbosity}",
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 # method that will tell Fedora where the ultimate output of g2f-import and g2f-build
191 # will be found: Points Fedora to the collect directory where FedoraMETS will be output.
192 &g2futil::write_gsdl_xml_file($hostname, $collectdir, $options);
193
194 my $full_gs_col = &util::filename_cat($collectdir,$gs_col);
195
196 if (!-e $full_gs_col ) {
197 print STDERR "Unable to find Greenstone collection $full_gs_col\n";
198 exit 1;
199 }
200
201 my $export_dir = &util::filename_cat($full_gs_col,"export");
202
203 if ( -e $export_dir ) {
204 print "***\n";
205 print "* Updating existing Greenstone $gs_col objects from Fedora $pid_namespace\n";
206 print "***\n";
207
208 # readdir
209 if (opendir(DIR, $export_dir)) {
210
211 closedir DIR;
212 my @hash_dirs = &g2futil::get_all_hash_dirs($export_dir,$maxdocs);
213
214 # for each hash dir, purge its respective PID
215 foreach my $full_hd (@hash_dirs) {
216
217 my $hash_id = &g2futil::get_hash_id($full_hd);
218
219 next if !defined $hash_id;
220
221 my $pid = "$pid_namespace:$gs_col-$hash_id";
222
223 my $dsinfo_status = &g2futil::run_datastore_info($pid,$options);
224
225 if ($dsinfo_status == 0) {
226 print " $pid being updated.\n";
227 &g2futil::run_purge($pid,$options);
228 }
229 else {
230 print " $pid not present.\n";
231 }
232 }
233 }
234 else {
235 print STDERR "Error: Unable to open directory $export_dir: $!\n";
236 exit;
237 }
238 }
239
240 print "***\n";
241 print "* Processing $gs_col into FedoraMETS format\n";
242 print "***\n";
243
244 my $gs_export_opts = "-saveas FedoraMETS -fedora_namespace $pid_namespace -verbosity $verbosity";
245
246 $gs_export_opts .= " -gli" if ($gli);
247
248 $gs_export_opts .= " -language $language" if ($language);
249 $gs_export_opts .= " -collectdir $collectdir" if ($collectdir);
250 $gs_export_opts .= " -removeold" if ($removeold);
251 $gs_export_opts .= " -maxdocs $maxdocs" if ($maxdocs);
252
253 $gs_export_opts .= " -exportdir $export_dir";
254
255 my $gs_export_arguments = "$gs_export_opts $gs_col";
256
257 &g2futil::run_cmd("export.pl", $gs_export_arguments, $options);
258}
259
260&main(@ARGV);
261
262
263
Note: See TracBrowser for help on using the repository browser.