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

Last change on this file since 17571 was 17571, checked in by kjdon, 15 years ago

moved the make_absolute function to util.pm

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