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

Last change on this file since 20571 was 20571, checked in by davidb, 15 years ago

Introduction of variable 'incremental_mode' that is set to 'none', 'onlyadd', or 'all' depending on settings of -removeold -keepold and -incremental. Some minor edits to tidy up the code have also been made in this commit

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