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

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

Added some missing "use dbutil".

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