source: main/trunk/greenstone2/bin/script/buildcol.pl@ 21613

Last change on this file since 21613 was 21613, checked in by mdewsnip, 14 years ago

Changed calls to read_collection_cfg() so the infodbtype value is set to be the default infodbtype (from dbutil), if not defined in the collect.cfg file. Part of making the code less GDBM-specific.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 19.8 KB
Line 
1#!/usr/bin/perl -w
2
3## @file buildcol.pl
4# This program will build a particular collection.
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22#
23# @author New Zealand Digital Library Project unless otherwise stated
24# @copy 1999 New Zealand Digital Library Project
25#
26package buildcol;
27
28BEGIN {
29 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
30 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
31 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
32 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
33 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
34 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/XML/XPath");
35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
37
38 if (defined $ENV{'GSDLEXTS'}) {
39 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
40 foreach my $e (@extensions) {
41 my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
42
43 unshift (@INC, "$ext_prefix/perllib");
44 unshift (@INC, "$ext_prefix/perllib/cpan");
45 unshift (@INC, "$ext_prefix/perllib/plugins");
46 unshift (@INC, "$ext_prefix/perllib/classify");
47 }
48 }
49 if (defined $ENV{'GSDL3EXTS'}) {
50 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
51 foreach my $e (@extensions) {
52 my $ext_prefix = "$ENV{'GSDL3SRCHOME'}/ext/$e";
53
54 unshift (@INC, "$ext_prefix/perllib");
55 unshift (@INC, "$ext_prefix/perllib/cpan");
56 unshift (@INC, "$ext_prefix/perllib/plugins");
57 unshift (@INC, "$ext_prefix/perllib/classify");
58 }
59 }
60
61}
62
63use colcfg;
64use util;
65use scriptutil;
66use FileHandle;
67use gsprintf;
68use printusage;
69use parse2;
70
71use strict;
72no strict 'refs'; # allow filehandles to be variables and vice versa
73no strict 'subs'; # allow barewords (eg STDERR) as function arguments
74
75
76my $mode_list =
77 [ { 'name' => "all",
78 'desc' => "{buildcol.mode.all}" },
79 { 'name' => "compress_text",
80 'desc' => "{buildcol.mode.compress_text}" },
81 { 'name' => "build_index",
82 'desc' => "{buildcol.mode.build_index}" },
83 { 'name' => "infodb",
84 'desc' => "{buildcol.mode.infodb}" } ];
85
86my $sec_index_list =
87 [ {'name' => "never",
88 'desc' => "{buildcol.sections_index_document_metadata.never}" },
89 {'name' => "always",
90 'desc' => "{buildcol.sections_index_document_metadata.always}" },
91 {'name' => "unless_section_metadata_exists",
92 'desc' => "{buildcol.sections_index_document_metadata.unless_section_metadata_exists}" }
93 ];
94
95my $arguments =
96 [ { 'name' => "remove_empty_classifications",
97 'desc' => "{buildcol.remove_empty_classifications}",
98 'type' => "flag",
99 'reqd' => "no",
100 'modegli' => "2" },
101 { 'name' => "archivedir",
102 'desc' => "{buildcol.archivedir}",
103 'type' => "string",
104 'reqd' => "no",
105 'hiddengli' => "yes" },
106 { 'name' => "builddir",
107 'desc' => "{buildcol.builddir}",
108 'type' => "string",
109 'reqd' => "no",
110 'hiddengli' => "yes" },
111# { 'name' => "cachedir",
112# 'desc' => "{buildcol.cachedir}",
113# 'type' => "string",
114# 'reqd' => "no" },
115 { 'name' => "collectdir",
116 'desc' => "{buildcol.collectdir}",
117 'type' => "string",
118 # parsearg left "" as default
119 #'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
120 'reqd' => "no",
121 'hiddengli' => "yes" },
122 { 'name' => "site",
123 'desc' => "{buildcol.site}",
124 'type' => "string",
125 'deft' => "",
126 'reqd' => "no",
127 'hiddengli' => "yes" },
128 { 'name' => "debug",
129 'desc' => "{buildcol.debug}",
130 'type' => "flag",
131 'reqd' => "no",
132 'hiddengli' => "yes" },
133 { 'name' => "faillog",
134 'desc' => "{buildcol.faillog}",
135 'type' => "string",
136 # parsearg left "" as default
137 #'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
138 'reqd' => "no",
139 'modegli' => "3" },
140 { 'name' => "index",
141 'desc' => "{buildcol.index}",
142 'type' => "string",
143 'reqd' => "no",
144 'modegli' => "3" },
145 { 'name' => "incremental",
146 'desc' => "{buildcol.incremental}",
147 'type' => "flag",
148 'hiddengli' => "yes" },
149 { 'name' => "keepold",
150 'desc' => "{buildcol.keepold}",
151 'type' => "flag",
152 'reqd' => "no",
153 #'modegli' => "3",
154 'hiddengli' => "yes" },
155 { 'name' => "removeold",
156 'desc' => "{buildcol.removeold}",
157 'type' => "flag",
158 'reqd' => "no",
159 #'modegli' => "3",
160 'hiddengli' => "yes" },
161 { 'name' => "language",
162 'desc' => "{scripts.language}",
163 'type' => "string",
164 'reqd' => "no",
165 'modegli' => "3" },
166 { 'name' => "maxdocs",
167 'desc' => "{buildcol.maxdocs}",
168 'type' => "int",
169 'reqd' => "no",
170 'hiddengli' => "yes" },
171 { 'name' => "maxnumeric",
172 'desc' => "{buildcol.maxnumeric}",
173 'type' => "int",
174 'reqd' => "no",
175 'deft' => "4",
176 'range' => "4,512",
177 'modegli' => "3" },
178 { 'name' => "mode",
179 'desc' => "{buildcol.mode}",
180 'type' => "enum",
181 'list' => $mode_list,
182 # parsearg left "" as default
183# 'deft' => "all",
184 'reqd' => "no",
185 'modegli' => "3" },
186 { 'name' => "no_strip_html",
187 'desc' => "{buildcol.no_strip_html}",
188 'type' => "flag",
189 'reqd' => "no",
190 'modegli' => "3" },
191 { 'name' => "no_text",
192 'desc' => "{buildcol.no_text}",
193 'type' => "flag",
194 'reqd' => "no",
195 'modegli' => "2" },
196 { 'name' => "sections_index_document_metadata",
197 'desc' => "{buildcol.sections_index_document_metadata}",
198 'type' => "enum",
199 'list' => $sec_index_list,
200 'reqd' => "no",
201 'modegli' => "2" },
202 { 'name' => "out",
203 'desc' => "{buildcol.out}",
204 'type' => "string",
205 'deft' => "STDERR",
206 'reqd' => "no",
207 'hiddengli' => "yes" },
208 { 'name' => "verbosity",
209 'desc' => "{buildcol.verbosity}",
210 'type' => "int",
211 # parsearg left "" as default
212 #'deft' => "2",
213 'reqd' => "no",
214 'modegli' => "3" },
215 { 'name' => "gli",
216 'desc' => "",
217 'type' => "flag",
218 'reqd' => "no",
219 'hiddengli' => "yes" },
220 { 'name' => "xml",
221 'desc' => "{scripts.xml}",
222 'type' => "flag",
223 'reqd' => "no",
224 'hiddengli' => "yes" },
225 { 'name' => "disable_OAI",
226 'desc' => "{buildcol.disable_OAI}",
227 'type' => "flag",
228 'reqd' => "no",
229 'modegli' => "2",
230 'hiddengli' => "yes" },
231 ];
232
233my $options = { 'name' => "buildcol.pl",
234 'desc' => "{buildcol.desc}",
235 'args' => $arguments };
236
237
238# globals
239my $collection;
240my $configfilename;
241my $out;
242
243# used to signify "gs2"(default) or "gs3"
244my $gs_mode = "gs2";
245
246## @method gsprintf()
247# Print a string to the screen after looking it up from a locale dependant
248# strings file. This function is losely based on the idea of resource
249# bundles as used in Java.
250#
251# @param $error The STDERR stream.
252# @param $text The string containing GS keys that should be replaced with
253# their locale dependant equivilents.
254# @param $out The output stream.
255# @return The locale-based string to output.
256#
257sub gsprintf()
258{
259 return &gsprintf::gsprintf(@_);
260}
261## gsprintf() ##
262
263&main();
264
265## @method main()
266#
267# [Parses up and validates the arguments to the build process before creating
268# the appropriate build process to do the actual work - John]
269#
270# @note Added true incremental support - John Thompson, DL Consulting Ltd.
271# @note There were several bugs regarding using directories other than
272# "import" or "archives" during import and build quashed. - John
273# Thompson, DL Consulting Ltd.
274#
275# @param $incremental If true indicates this build should not regenerate all
276# the index and metadata files, and should instead just
277# append the information found in the archives directory
278# to the existing files. If this requires some complex
279# work so as to correctly insert into a classifier so be
280# it. Of course none of this is done here - instead the
281# incremental argument is passed to the document
282# processor.
283#
284sub main
285{
286 # command line args
287 my ($verbosity, $archivedir, $cachedir, $builddir, $site, $maxdocs,
288 $debug, $mode, $indexname, $removeold, $keepold,
289 $incremental, $incremental_mode,
290 $remove_empty_classifications,
291 $collectdir, $build, $type, $textindex,
292 $no_strip_html, $no_text, $faillog, $gli, $index, $language,
293 $sections_index_document_metadata, $maxnumeric,
294 $disable_OAI);
295
296 my $xml = 0;
297 my $hashParsingResult = {};
298 # general options available to all plugins
299 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
300
301 # If parse returns -1 then something has gone wrong
302 if ($intArgLeftinAfterParsing == -1)
303 {
304 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
305 die "\n";
306 }
307
308 foreach my $strVariable (keys %$hashParsingResult)
309 {
310 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
311 }
312
313 # If $language has been specified, load the appropriate resource bundle
314 # (Otherwise, the default resource bundle will be loaded automatically)
315 if ($language && $language =~ /\S/) {
316 &gsprintf::load_language_specific_resource_bundle($language);
317 }
318
319 if ($xml) {
320 &PrintUsage::print_xml_usage($options);
321 print "\n";
322 return;
323 }
324
325 if ($gli) { # the gli wants strings to be in UTF-8
326 &gsprintf::output_strings_in_UTF8;
327 }
328
329 # now check that we had exactly one leftover arg, which should be
330 # the collection name. We don't want to do this earlier, cos
331 # -xml arg doesn't need a collection name
332 # Or if the user specified -h, then we output the usage also
333 if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
334 {
335 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
336 die "\n";
337 }
338
339 $textindex = "";
340 my $close_out = 0;
341 if ($out !~ /^(STDERR|STDOUT)$/i) {
342 open (OUT, ">$out") ||
343 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
344 $out = "buildcol::OUT";
345 $close_out = 1;
346 }
347 $out->autoflush(1);
348
349 # get and check the collection
350 if (($collection = &colcfg::use_collection($site, @ARGV, $collectdir)) eq "") {
351 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
352 die "\n";
353 }
354
355 if ($faillog eq "") {
356 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
357 }
358 # note that we're appending to the faillog here (import.pl clears it each time)
359 # this could potentially create a situation where the faillog keeps being added
360 # to over multiple builds (if the import process is being skipped)
361 open (FAILLOG, ">>$faillog") ||
362 (&gsprintf(STDERR, "{common.cannot_open_fail_log}\n", $faillog) && die);
363 $faillog = 'buildcol::FAILLOG';
364 $faillog->autoflush(1);
365
366 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
367 # Don't know why this didn't already happen, but now collection specific
368 # classify and plugins directory also added to include path
369 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib/classify");
370 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib/plugins");
371
372 # Read in the collection configuration file.
373 my ($collectcfg, $buildtype);
374 ($configfilename, $gs_mode) = &colcfg::get_collect_cfg_name($out);
375 $collectcfg = &colcfg::read_collection_cfg ($configfilename, $gs_mode);
376
377 # If the infodbtype value wasn't defined in the collect.cfg file, use the default
378 if (!defined($collectcfg->{'infodbtype'}))
379 {
380 $collectcfg->{'infodbtype'} = &dbutil::get_default_infodb_type();
381 }
382
383 if ($verbosity !~ /\d+/) {
384 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
385 $verbosity = $collectcfg->{'verbosity'};
386 } else {
387 $verbosity = 2; # the default
388 }
389 }
390 # we use searchtype for determining buildtype, but for old versions, use buildtype
391 if (defined $collectcfg->{'buildtype'}) {
392 $buildtype = $collectcfg->{'buildtype'};
393 } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
394 $buildtype = "mgpp";
395 } else {
396 $buildtype = "mg"; #mg is the default
397 }
398 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
399 $archivedir = $collectcfg->{'archivedir'};
400 }
401 if (defined $collectcfg->{'cachedir'} && $cachedir eq "") {
402 $cachedir = $collectcfg->{'cachedir'};
403 }
404 if (defined $collectcfg->{'builddir'} && $builddir eq "") {
405 $builddir = $collectcfg->{'builddir'};
406 }
407 if ($maxdocs !~ /\-?\d+/) {
408 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
409 $maxdocs = $collectcfg->{'maxdocs'};
410 } else {
411 $maxdocs = -1; # the default
412 }
413 }
414 if (defined $collectcfg->{'maxnumeric'} && $collectcfg->{'maxnumeric'} =~ /\d+/) {
415 $maxnumeric = $collectcfg->{'maxnumeric'};
416 }
417
418 if ($maxnumeric < 4 || $maxnumeric > 512) {
419 $maxnumeric = 4;
420 }
421
422 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
423 $debug = 1;
424 }
425 if ($mode !~ /^(all|compress_text|build_index|infodb)$/) {
426 if (defined $collectcfg->{'mode'} && $collectcfg->{'mode'} =~ /^(all|compress_text|build_index|infodb)$/) {
427 $mode = $collectcfg->{'mode'};
428 } else {
429 $mode = "all"; # the default
430 }
431 }
432 if (defined $collectcfg->{'index'} && $indexname eq "") {
433 $indexname = $collectcfg->{'index'};
434 }
435 if (defined $collectcfg->{'no_text'} && $no_text == 0) {
436 if ($collectcfg->{'no_text'} =~ /^true$/i) {
437 $no_text = 1;
438 }
439 }
440 if (defined $collectcfg->{'no_strip_html'} && $no_strip_html == 0) {
441 if ($collectcfg->{'no_strip_html'} =~ /^true$/i) {
442 $no_strip_html = 1;
443 }
444 }
445 if (defined $collectcfg->{'remove_empty_classifications'} && $remove_empty_classifications == 0) {
446 if ($collectcfg->{'remove_empty_classifications'} =~ /^true$/i) {
447 $remove_empty_classifications = 1;
448 }
449 }
450
451 if ($buildtype eq "mgpp" && defined $collectcfg->{'textcompress'}) {
452 $textindex = $collectcfg->{'textcompress'};
453 }
454 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
455 $gli = 1;
456 }
457
458 if ($sections_index_document_metadata !~ /\S/ && defined $collectcfg->{'sections_index_document_metadata'}) {
459 $sections_index_document_metadata = $collectcfg->{'sections_index_document_metadata'};
460 }
461
462 if ($sections_index_document_metadata !~ /^(never|always|unless_section_metadata_exists)$/) {
463 $sections_index_document_metadata = "never";
464 }
465
466 ($removeold, $keepold, $incremental, $incremental_mode)
467 = &scriptutil::check_removeold_and_keepold($removeold, $keepold,
468 $incremental, "building",
469 $collectcfg);
470
471 $gli = 0 unless defined $gli;
472
473 # If the disable_OAI flag is not present, the option $disable_OAI with the value of 0 will be passed to basebuilder.pm
474 $disable_OAI = 0 unless defined $disable_OAI;
475
476 # New argument to track whether build is incremental
477 $incremental = 0 unless defined $incremental;
478
479 print STDERR "<Build>\n" if $gli;
480
481 #set the text index
482 if (($buildtype eq "mgpp") || ($buildtype eq "lucene")) {
483 if ($textindex eq "") {
484 $textindex = "text";
485 }
486 }
487 else {
488 $textindex = "section:text";
489 }
490
491 # fill in the default archives and building directories if none
492 # were supplied, turn all \ into / and remove trailing /
493
494 my ($realarchivedir, $realbuilddir);
495 # Modified so that the archivedir, if provided as an argument, is made
496 # absolute if it isn't already
497 if ($archivedir eq "")
498 {
499 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
500 }
501 else
502 {
503 $archivedir = &util::make_absolute($ENV{'GSDLCOLLECTDIR'}, $archivedir);
504 }
505 # End Mod
506 $archivedir =~ s/[\\\/]+/\//g;
507 $archivedir =~ s/\/$//;
508
509 if ($builddir eq "") {
510 $builddir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building");
511 if ($incremental) {
512 &gsprintf($out, "{buildcol.incremental_default_builddir}\n");
513 }
514 }
515 $builddir =~ s/[\\\/]+/\//g;
516 $builddir =~ s/\/$//;
517
518 # update the archive cache if needed
519 if ($cachedir) {
520 &gsprintf($out, "{buildcol.updating_archive_cache}\n")
521 if ($verbosity >= 1);
522
523 $cachedir =~ s/[\\\/]+$//;
524 $cachedir .= "/collect/$collection" unless
525 $cachedir =~ /collect\/$collection/;
526
527 $realarchivedir = "$cachedir/archives";
528 $realbuilddir = "$cachedir/building";
529 &util::mk_all_dir ($realarchivedir);
530 &util::mk_all_dir ($realbuilddir);
531 &util::cachedir ($archivedir, $realarchivedir, $verbosity);
532
533 } else {
534 $realarchivedir = $archivedir;
535 $realbuilddir = $builddir;
536 }
537
538 # build it in realbuilddir
539 &util::mk_all_dir ($realbuilddir);
540
541 my ($buildertype, $builderdir, $builder);
542 # if a builder class has been created for this collection, use it
543 # otherwise, use the mg or mgpp builder
544 if (-e "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib/custombuilder.pm") {
545 $builderdir = "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib";
546 $buildertype = "custombuilder";
547 } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/custombuilder.pm") {
548 $builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
549 $buildertype = "custombuilder";
550 } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}builder.pm") {
551 $builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
552 $buildertype = "${collection}builder";
553 } else {
554 $builderdir = "$ENV{'GSDLHOME'}/perllib";
555 if ($buildtype eq "lucene") {
556 $buildertype = "lucenebuilder";
557 }
558 elsif ($buildtype eq "mgpp") {
559 $buildertype = "mgppbuilder";
560 }
561 else {
562 $buildertype = "mgbuilder";
563 }
564 }
565
566 require "$builderdir/$buildertype.pm";
567
568 eval("\$builder = new $buildertype(\$collection, " .
569 "\$realarchivedir, \$realbuilddir, \$verbosity, " .
570 "\$maxdocs, \$debug, \$keepold, \$incremental, \$incremental_mode, " .
571 "\$remove_empty_classifications, " .
572 "\$out, \$no_text, \$faillog, \$gli, \$disable_OAI)");
573 die "$@" if $@;
574
575 $builder->init();
576 $builder->set_maxnumeric($maxnumeric);
577
578 if (($buildertype eq "mgppbuilder") && $no_strip_html) {
579 $builder->set_strip_html(0);
580 }
581 if ($sections_index_document_metadata ne "never") {
582 $builder->set_sections_index_document_metadata($sections_index_document_metadata);
583 }
584
585 if ($mode =~ /^all$/i) {
586 $builder->compress_text($textindex);
587 $builder->build_indexes($indexname);
588 $builder->make_infodatabase();
589 $builder->collect_specific();
590 } elsif ($mode =~ /^compress_text$/i) {
591 $builder->compress_text($textindex);
592 } elsif ($mode =~ /^build_index$/i) {
593 $builder->build_indexes($indexname);
594 } elsif ($mode =~ /^infodb$/i) {
595 $builder->make_infodatabase();
596 } else {
597 (&gsprintf(STDERR, "{buildcol.unknown_mode}\n", $mode) && die);
598 }
599
600 $builder->make_auxiliary_files() if !$debug;
601 $builder->deinit();
602
603 if (($realbuilddir ne $builddir) && !$debug) {
604 &gsprintf($out, "{buildcol.copying_back_cached_build}\n")
605 if ($verbosity >= 1);
606 &util::rm_r ($builddir);
607 &util::cp_r ($realbuilddir, $builddir);
608 }
609
610 close OUT if $close_out;
611 close FAILLOG;
612
613 print STDERR "</Build>\n" if $gli;
614}
615## main() ##
616
617
Note: See TracBrowser for help on using the repository browser.