root/main/trunk/greenstone2/bin/script/g2f-import.pl @ 21687

Revision 21687, 7.2 KB (checked in by ak19, 10 years ago)

1. Minor changes for when when fedora uses Greenstone 3's tomcat: Only for GS3 in this case, don't need the file gsdl.xml since the file greenstone3.xml in GS3's tomcat already contains the necessary information. 2. In g2futil.pm: When writing out the file gsdl.xml, tries for 1 x 20 seconds to see whether the fedora server has started up again.

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