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

Last change on this file since 12625 was 12545, checked in by kjdon, 18 years ago

changed parse2::parse so that it returns -1 on error, 0 on success, or if allow_extra_options is specified, then on success returns the number of args left over. allowed the use of -h to print the usage for all scripts

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