source: trunk/gsdl/bin/script/buildcol.pl@ 10256

Last change on this file since 10256 was 10256, checked in by kjdon, 19 years ago

added use strict, and fixed up compile erros

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 15.6 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# buildcol.pl -- This program will build a particular collection
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# 11/04/03 Added usage datastructure - John Thompson
29
30package buildcol;
31
32BEGIN {
33 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
34 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
38 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
39}
40
41use colcfg;
42use util;
43use FileHandle;
44use gsprintf;
45use printusage;
46use parse2;
47
48use strict;
49no strict 'refs'; # allow filehandles to be variables and vice versa
50no strict 'subs'; # allow barewords (eg STDERR) as function arguments
51
52my $mode_list =
53 [ { 'name' => "all",
54 'desc' => "{buildcol.mode.all}" },
55 { 'name' => "compress_text",
56 'desc' => "{buildcol.mode.compress_text}" },
57 { 'name' => "build_index",
58 'desc' => "{buildcol.mode.build_index}" },
59 { 'name' => "infodb",
60 'desc' => "{buildcol.mode.infodb}" } ];
61
62my $arguments =
63 [ { 'name' => "remove_empty_classifications",
64 'desc' => "{buildcol.remove_empty_classifications}",
65 'type' => "flag",
66 'reqd' => "no",
67 'modegli' => "3" },
68 { 'name' => "archivedir",
69 'desc' => "{buildcol.archivedir}",
70 'type' => "string",
71 'reqd' => "no",
72 'hiddengli' => "yes" },
73 { 'name' => "builddir",
74 'desc' => "{buildcol.builddir}",
75 'type' => "string",
76 'reqd' => "no",
77 'hiddengli' => "yes" },
78# { 'name' => "cachedir",
79# 'desc' => "{buildcol.cachedir}",
80# 'type' => "string",
81# 'reqd' => "no" },
82 { 'name' => "collectdir",
83 'desc' => "{buildcol.collectdir}",
84 'type' => "string",
85 # parsearg left "" as default
86 #'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
87 'reqd' => "no",
88 'hiddengli' => "yes" },
89 { 'name' => "create_images",
90 'desc' => "{buildcol.create_images}",
91 'type' => "flag",
92 'reqd' => "no",
93 'modegli' => "4" },
94 { 'name' => "debug",
95 'desc' => "{buildcol.debug}",
96 'type' => "flag",
97 'reqd' => "no",
98 'hiddengli' => "yes" },
99 { 'name' => "faillog",
100 'desc' => "{buildcol.faillog}",
101 'type' => "string",
102 # parsearg left "" as default
103 #'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
104 'reqd' => "no",
105 'modegli' => "4" },
106 { 'name' => "index",
107 'desc' => "{buildcol.index}",
108 'type' => "string",
109 'reqd' => "no",
110 'modegli' => "3" },
111 { 'name' => "keepold",
112 'desc' => "{buildcol.keepold}",
113 'type' => "flag",
114 'reqd' => "no",
115 'hiddengli' => "yes" },
116 { 'name' => "language",
117 'desc' => "{scripts.language}",
118 'type' => "string",
119 'reqd' => "no",
120 'modegli' => "4" },
121 { 'name' => "maxdocs",
122 'desc' => "{buildcol.maxdocs}",
123 'type' => "int",
124 'reqd' => "no",
125 'hiddengli' => "yes" },
126 { 'name' => "mode",
127 'desc' => "{buildcol.mode}",
128 'type' => "enum",
129 'list' => $mode_list,
130 # parsearg left "" as default
131# 'deft' => "all",
132 'reqd' => "no",
133 'modegli' => "4" },
134 { 'name' => "no_strip_html",
135 'desc' => "{buildcol.no_strip_html}",
136 'type' => "flag",
137 'reqd' => "no",
138 'modegli' => "4" },
139 { 'name' => "no_text",
140 'desc' => "{buildcol.no_text}",
141 'type' => "flag",
142 'reqd' => "no",
143 'modegli' => "3" },
144 { 'name' => "out",
145 'desc' => "{buildcol.out}",
146 'type' => "string",
147 'deft' => "STDERR",
148 'reqd' => "no",
149 'hiddengli' => "yes" },
150 { 'name' => "verbosity",
151 'desc' => "{buildcol.verbosity}",
152 'type' => "int",
153 # parsearg left "" as default
154 #'deft' => "2",
155 'reqd' => "no",
156 'modegli' => "4" },
157 { 'name' => "gli",
158 'desc' => "",
159 'type' => "flag",
160 'reqd' => "no",
161 'hiddengli' => "yes" },
162 { 'name' => "xml",
163 'desc' => "",
164 'type' => "flag",
165 'reqd' => "no",
166 'hiddengli' => "yes" } ];
167
168my $options = { 'name' => "buildcol.pl",
169 'desc' => "{buildcol.desc}",
170 'args' => $arguments };
171
172
173# globals
174my $collection;
175my $configfilename;
176my $out;
177
178sub gsprintf
179{
180 return &gsprintf::gsprintf(@_);
181}
182
183
184
185&main();
186
187sub main
188{
189 # command line args
190 my ($verbosity, $archivedir, $cachedir, $builddir, $maxdocs,
191 $debug, $mode, $indexname, $keepold, $remove_empty_classifications,
192 $create_images, $collectdir, $build, $type, $textindex,
193 $no_strip_html, $no_text, $faillog, $gli, $index, $language);
194
195 my $xml = 0;
196
197 my $hashParsingResult = {};
198 my $blnParseFailed = "false";
199 # general options available to all plugins
200 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
201 # If there are more than one argument left after parsing, it mean user input too many arguments.
202 # Error occoured will return 0
203 if($intArgLeftinAfterParsing != 1)
204 {
205 $blnParseFailed = "true";
206 }
207 if($blnParseFailed eq "true")
208 {
209 print "";
210 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
211 die "\n";
212 }
213 foreach my $strVariable (keys %$hashParsingResult)
214 {
215 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
216 }
217
218 # If $language has been specified, load the appropriate resource bundle
219 # (Otherwise, the default resource bundle will be loaded automatically)
220 if ($language && $language =~ /\S/) {
221 &gsprintf::load_language_specific_resource_bundle($language);
222 }
223
224 if ($xml) {
225 &PrintUsage::print_xml_usage($options);
226 print "\n";
227 return;
228 }
229
230 if ($gli) { # the gli wants strings to be in UTF-8
231 &gsprintf::output_strings_in_UTF8;
232 }
233
234 $textindex = "";
235 my $close_out = 0;
236 if ($out !~ /^(STDERR|STDOUT)$/i) {
237 open (OUT, ">$out") ||
238 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
239 $out = "buildcol::OUT";
240 $close_out = 1;
241 }
242 $out->autoflush(1);
243
244 # get and check the collection
245 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
246 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
247 die "\n";
248 }
249
250 if ($faillog eq "") {
251 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
252 }
253 # note that we're appending to the faillog here (import.pl clears it each time)
254 # this could potentially create a situation where the faillog keeps being added
255 # to over multiple builds (if the import process is being skipped)
256 open (FAILLOG, ">>$faillog") ||
257 (&gsprintf(STDERR, "{common.cannot_open_fail_log}\n", $faillog) && die);
258 $faillog = 'buildcol::FAILLOG';
259 $faillog->autoflush(1);
260
261 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
262
263 # read the configuration file
264 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
265 my ($collectcfg, $buildtype);
266
267 if (-e $configfilename) {
268 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
269
270 if ($verbosity !~ /\d+/) {
271 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
272 $verbosity = $collectcfg->{'verbosity'};
273 } else {
274 $verbosity = 2; # the default
275 }
276 }
277 # we use searchtype for determining buildtype, but for old versions, use buildtype
278 if (defined $collectcfg->{'buildtype'}) {
279 $buildtype = $collectcfg->{'buildtype'};
280 } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
281 $buildtype = "mgpp";
282 } else {
283 $buildtype = "mg"; #mg is the default
284 }
285 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
286 $archivedir = $collectcfg->{'archivedir'};
287 }
288 if (defined $collectcfg->{'cachedir'} && $cachedir eq "") {
289 $cachedir = $collectcfg->{'cachedir'};
290 }
291 if (defined $collectcfg->{'builddir'} && $builddir eq "") {
292 $builddir = $collectcfg->{'builddir'};
293 }
294 if ($maxdocs !~ /\-?\d+/) {
295 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
296 $maxdocs = $collectcfg->{'maxdocs'};
297 } else {
298 $maxdocs = -1; # the default
299 }
300 }
301 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
302 $debug = 1;
303 }
304 if ($mode !~ /^(all|compress_text|build_index|infodb)$/) {
305 if (defined $collectcfg->{'mode'} && $collectcfg->{'mode'} =~ /^(all|compress_text|build_index|infodb)$/) {
306 $mode = $collectcfg->{'mode'};
307 } else {
308 $mode = "all"; # the default
309 }
310 }
311 if (defined $collectcfg->{'index'} && $indexname eq "") {
312 $indexname = $collectcfg->{'index'};
313 }
314 if (defined $collectcfg->{'no_text'} && $no_text == 0) {
315 if ($collectcfg->{'no_text'} =~ /^true$/i) {
316 $no_text = 1;
317 }
318 }
319 if (defined $collectcfg->{'no_strip_html'} && $no_strip_html == 0) {
320 if ($collectcfg->{'no_strip_html'} =~ /^true$/i) {
321 $no_strip_html = 1;
322 }
323 }
324 if (defined $collectcfg->{'remove_empty_classifications'} && $remove_empty_classifications == 0) {
325 if ($collectcfg->{'remove_empty_classifications'} =~ /^true$/i) {
326 $remove_empty_classifications = 1;
327 }
328 }
329 if (defined $collectcfg->{'keepold'} && $collectcfg->{'keepold'} =~ /^true$/i) {
330 $keepold = 1;
331 }
332 if (defined $collectcfg->{'create_images'} && $collectcfg->{'create_images'} =~ /^true$/i) {
333 $create_images = 1;
334 }
335 if ($buildtype eq "mgpp" && defined $collectcfg->{'textcompress'}) {
336 $textindex = $collectcfg->{'textcompress'};
337 }
338 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
339 $gli = 1;
340 }
341
342 } else {
343 &gsprintf($out, "{common.cannot_find_cfg_file}\n", $configfilename) && die;
344 }
345
346 $gli = 0 unless defined $gli;
347
348 print STDERR "<Build>\n" if $gli;
349
350 #set the text index
351 if (($buildtype eq "mgpp") || ($buildtype eq "lucene")) {
352 if ($textindex eq "") {
353 $textindex = "text";
354 }
355 }
356 else {
357 $textindex = "section:text";
358 }
359
360 # create default images if required
361 if ($create_images) {
362 my $collection_name = $collection;
363 $collection_name = $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'}
364 if defined $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'};
365 &create_images ($collection_name);
366 }
367
368 # fill in the default archives and building directories if none
369 # were supplied, turn all \ into / and remove trailing /
370
371 my ($realarchivedir, $realbuilddir);
372 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
373 $archivedir =~ s/[\\\/]+/\//g;
374 $archivedir =~ s/\/$//;
375 $builddir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building") if $builddir eq "";
376 $builddir =~ s/[\\\/]+/\//g;
377 $builddir =~ s/\/$//;
378
379 # update the archive cache if needed
380 if ($cachedir) {
381 &gsprintf($out, "{buildcol.updating_archive_cache}\n")
382 if ($verbosity >= 1);
383
384 $cachedir =~ s/[\\\/]+$//;
385 $cachedir .= "/collect/$collection" unless
386 $cachedir =~ /collect\/$collection/;
387
388 $realarchivedir = "$cachedir/archives";
389 $realbuilddir = "$cachedir/building";
390 &util::mk_all_dir ($realarchivedir);
391 &util::mk_all_dir ($realbuilddir);
392 &util::cachedir ($archivedir, $realarchivedir, $verbosity);
393
394 } else {
395 $realarchivedir = $archivedir;
396 $realbuilddir = $builddir;
397 }
398
399 # build it in realbuilddir
400 &util::mk_all_dir ($realbuilddir);
401
402 my ($buildertype, $builderdir, $builder);
403 # if a builder class has been created for this collection, use it
404 # otherwise, use the mg or mgpp builder
405 if (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}builder.pm") {
406 $builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
407 $buildertype = "${collection}builder";
408 } else {
409 $builderdir = "$ENV{'GSDLHOME'}/perllib";
410 if ($buildtype eq "lucene") {
411 $buildertype = "lucenebuilder";
412 }
413 elsif ($buildtype eq "mgpp") {
414 $buildertype = "mgppbuilder";
415 }
416 else {
417 $buildertype = "mgbuilder";
418 }
419 }
420
421 require "$builderdir/$buildertype.pm";
422
423 eval("\$builder = new $buildertype(\$collection, " .
424 "\$realarchivedir, \$realbuilddir, \$verbosity, " .
425 "\$maxdocs, \$debug, \$keepold, \$remove_empty_classifications, " .
426 "\$out, \$no_text, \$faillog, \$gli)");
427 die "$@" if $@;
428
429 $builder->init();
430
431 if (($buildertype eq "mgppbuilder") && $no_strip_html) {
432 $builder->set_strip_html(0);
433 }
434
435 if ($mode =~ /^all$/i) {
436 $builder->compress_text($textindex);
437 $builder->build_indexes($indexname);
438 $builder->make_infodatabase();
439 $builder->collect_specific();
440 } elsif ($mode =~ /^compress_text$/i) {
441 $builder->compress_text($textindex);
442 } elsif ($mode =~ /^build_index$/i) {
443 $builder->build_indexes($indexname);
444 } elsif ($mode =~ /^infodb$/i) {
445 $builder->make_infodatabase();
446 } else {
447 (&gsprintf(STDERR, "{buildcol.unknown_mode}\n", $mode) && die);
448 }
449
450 $builder->make_auxiliary_files() if !$debug;
451 $builder->deinit();
452
453 if (($realbuilddir ne $builddir) && !$debug) {
454 &gsprintf($out, "{buildcol.copying_back_cached_build}\n")
455 if ($verbosity >= 1);
456 &util::rm_r ($builddir);
457 &util::cp_r ($realbuilddir, $builddir);
458 }
459
460 close OUT if $close_out;
461 close FAILLOG;
462
463 print STDERR "</Build>\n" if $gli;
464}
465
466sub create_images {
467 my ($collection_name) = @_;
468
469 my $image_script = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "script", "gimp", "title_icon-1.2.pl");
470 if (!-e $image_script) {
471 &gsprintf($out, "{buildcol.no_image_script}", $image_script);
472 &gsprintf($out, "{buildcol.no_default_images}\n\n");
473 return;
474 }
475
476 my $imagedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "images");
477
478 &util::mk_all_dir ($imagedir);
479
480 # create the images
481 system ("$image_script -size 1.5 -image_dir \"$imagedir\" -filename $collection.gif -text \"$collection_name\"");
482 system ("$image_script -image_dir \"$imagedir\" -filename ${collection}sm.gif -text \"$collection_name\"");
483
484 # update the collect.cfg configuration file (this will need
485 # to be changed when the config file format changes)
486 if (!open (CFGFILE, $configfilename)) {
487 &gsprintf($out, "{buildcol.cannot_open_cfg_file}\n", $configfilename);
488 &gsprintf($out, "{buildcol.unlinked_col_images}\n");
489 return;
490 }
491
492 my $line = ""; my $file = "";
493 my $found = 0; my $foundsm = 0;
494 while (defined ($line = <CFGFILE>)) {
495 if ($line =~ /collectionmeta\s+iconcollection\s+/) {
496 $line = "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n";
497 $found = 1;
498 } elsif ($line =~ /collectionmeta\s+iconcollectionsmall\s+/) {
499 $line = "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n";
500 $foundsm = 1;
501 }
502 $file .= $line;
503 }
504 close CFGFILE;
505
506 $file .= "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n" if !$found;
507 $file .= "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n" if !$foundsm;
508
509 if (!open (CFGFILE, ">$configfilename")) {
510 &gsprintf($out, "{buildcol.cannot_open_cfg_file}\n", $configfilename);
511 &gsprintf($out, "{buildcol.unlinked_col_images}\n");
512 return;
513 }
514 print CFGFILE $file;
515 close CFGFILE;
516}
Note: See TracBrowser for help on using the repository browser.