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

Last change on this file since 21786 was 21786, checked in by kjdon, 14 years ago

removing disable_OAI option. As far as I know, its only used for gs3, to determine whether to output the serviceRack or not, but this is now done in collectionConfig.xml

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