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

Last change on this file since 9854 was 9545, checked in by kjdon, 19 years ago

removed an extraneous comment

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 15.1 KB
RevLine 
[1031]1#!/usr/bin/perl -w
[4]2
[538]3###########################################################################
4#
5# buildcol.pl -- This program will build a particular collection
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
[4]27
[4776]28# 11/04/03 Added usage datastructure - John Thompson
29
[1424]30package buildcol;
31
[4]32BEGIN {
33 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
34 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
[8]35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
[5882]36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
[8]37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
[946]38 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
[4]39}
40
[130]41use colcfg;
[4]42use parsargv;
43use util;
[1424]44use FileHandle;
[5606]45use gsprintf;
[4776]46use printusage;
[4]47
[4776]48my $mode_list =
49 [ { 'name' => "all",
[4873]50 'desc' => "{buildcol.mode.all}" },
[4776]51 { 'name' => "compress_text",
[4873]52 'desc' => "{buildcol.mode.compress_text}" },
[4776]53 { 'name' => "build_index",
[4873]54 'desc' => "{buildcol.mode.build_index}" },
[4776]55 { 'name' => "infodb",
[4873]56 'desc' => "{buildcol.mode.infodb}" } ];
[4]57
[4776]58my $arguments =
[8361]59 [ { 'name' => "remove_empty_classifications",
60 'desc' => "{buildcol.remove_empty_classifications}",
[6407]61 'type' => "flag",
62 'reqd' => "no",
[8361]63 'modegli' => "3" },
[6407]64 { 'name' => "archivedir",
[4873]65 'desc' => "{buildcol.archivedir}",
[4776]66 'type' => "string",
[6407]67 'reqd' => "no",
68 'hiddengli' => "yes" },
[4776]69 { 'name' => "builddir",
[4873]70 'desc' => "{buildcol.builddir}",
[4776]71 'type' => "string",
[6407]72 'reqd' => "no",
73 'hiddengli' => "yes" },
[4873]74# { 'name' => "cachedir",
75# 'desc' => "{buildcol.cachedir}",
76# 'type' => "string",
77# 'reqd' => "no" },
[6407]78 { 'name' => "collectdir",
79 'desc' => "{buildcol.collectdir}",
80 'type' => "string",
81 'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
82 'reqd' => "no",
83 'hiddengli' => "yes" },
84 { 'name' => "create_images",
85 'desc' => "{buildcol.create_images}",
86 'type' => "flag",
87 'reqd' => "no",
88 'modegli' => "4" },
[4776]89 { 'name' => "debug",
[4873]90 'desc' => "{buildcol.debug}",
[4776]91 'type' => "flag",
[6407]92 'reqd' => "no",
93 'hiddengli' => "yes" },
94 { 'name' => "faillog",
95 'desc' => "{buildcol.faillog}",
96 'type' => "string",
97 'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
98 'reqd' => "no",
99 'modegli' => "4" },
[4776]100 { 'name' => "index",
[4873]101 'desc' => "{buildcol.index}",
[4776]102 'type' => "string",
[6407]103 'reqd' => "no",
104 'modegli' => "3" },
[4776]105 { 'name' => "keepold",
[4873]106 'desc' => "{buildcol.keepold}",
[4776]107 'type' => "flag",
[6407]108 'reqd' => "no",
109 'hiddengli' => "yes" },
110 { 'name' => "language",
111 'desc' => "{scripts.language}",
112 'type' => "string",
113 'reqd' => "no",
114 'modegli' => "4" },
115 { 'name' => "maxdocs",
116 'desc' => "{buildcol.maxdocs}",
117 'type' => "int",
118 'reqd' => "no",
119 'hiddengli' => "yes" },
120 { 'name' => "mode",
121 'desc' => "{buildcol.mode}",
122 'type' => "enum",
123 'list' => $mode_list,
124 'deft' => "all",
125 'reqd' => "no",
126 'modegli' => "4" },
127 { 'name' => "no_strip_html",
128 'desc' => "{buildcol.no_strip_html}",
129 'type' => "flag",
130 'reqd' => "no",
131 'modegli' => "4" },
[4873]132 { 'name' => "no_text",
133 'desc' => "{buildcol.no_text}",
[4776]134 'type' => "flag",
[6407]135 'reqd' => "no",
136 'modegli' => "3" },
[4776]137 { 'name' => "out",
[4873]138 'desc' => "{buildcol.out}",
[4776]139 'type' => "string",
140 'deft' => "STDERR",
[6407]141 'reqd' => "no",
142 'hiddengli' => "yes" },
143 { 'name' => "verbosity",
144 'desc' => "{buildcol.verbosity}",
145 'type' => "int",
146 'deft' => "2",
147 'reqd' => "no",
148 'modegli' => "4" } ];
[4776]149
150my $options = { 'name' => "buildcol.pl",
[5094]151 'desc' => "{buildcol.desc}",
[4776]152 'args' => $arguments };
153
[6921]154sub gsprintf
[4776]155{
[6921]156 return &gsprintf::gsprintf(@_);
[4]157}
158
159
[4776]160&main();
161
162
[4]163sub main
164{
[783]165 my ($verbosity, $archivedir, $cachedir, $builddir, $maxdocs,
[8361]166 $debug, $mode, $indexname, $keepold, $remove_empty_classifications,
[1853]167 $create_images, $collectdir, $out, $buildtype, $textindex,
[6331]168 $no_strip_html, $no_text, $faillog, $gli);
[2355]169
[4776]170 # ***** 11-04-03 - John Thompson *****
171 my $xml = 0;
172 # ************************************
173
[2355]174 # note that no defaults are passed for most options as they're set
175 # later (after we check the collect.cfg file)
[4]176 if (!parsargv::parse(\@ARGV,
[4873]177 'language/.*/', \$language,
[2355]178 'verbosity/\d+/', \$verbosity,
[4]179 'archivedir/.*/', \$archivedir,
[4873]180 'cachedir/.*/', \$cachedir, # UNDOCUMENTED
[313]181 'builddir/.*/', \$builddir,
[2355]182 'maxdocs/^\-?\d+/', \$maxdocs,
[783]183 'debug', \$debug,
[2355]184 'mode/^(all|compress_text|build_index|infodb)$/', \$mode,
[783]185 'index/.*/', \$indexname,
[2336]186 'no_text', \$no_text,
[784]187 'keepold', \$keepold,
[8361]188 'remove_empty_classifications', \$remove_empty_classifications,
[1424]189 'create_images', \$create_images,
[1454]190 'collectdir/.*/', \$collectdir,
[1853]191 'out/.*/STDERR', \$out,
192 'no_strip_html', \$no_strip_html,
[4776]193 'faillog/.*/', \$faillog,
[6331]194 'gli', \$gli,
[4776]195 q^xml^, \$xml)) {
[6926]196 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
[4]197 die "\n";
198 }
199
[6945]200 # If $language has been specified, load the appropriate resource bundle
201 # (Otherwise, the default resource bundle will be loaded automatically)
202 if ($language) {
203 &gsprintf::load_language_specific_resource_bundle($language);
204 }
[6926]205
[4776]206 if ($xml) {
[6926]207 &PrintUsage::print_xml_usage($options);
[7950]208 print "\n";
209 return;
[4776]210 }
211
[7101]212 if ($gli) { # the gli wants strings to be in UTF-8
213 &gsprintf::output_strings_in_UTF8;
214 }
215
[1853]216 $textindex = "";
[1424]217 my $close_out = 0;
218 if ($out !~ /^(STDERR|STDOUT)$/i) {
[5094]219 open (OUT, ">$out") ||
[6921]220 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
[1424]221 $out = "buildcol::OUT";
222 $close_out = 1;
223 }
224 $out->autoflush(1);
225
[4]226 # get and check the collection
[1454]227 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
[6926]228 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
[4]229 die "\n";
230 }
[130]231
[2785]232 if ($faillog eq "") {
233 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
234 }
235 # note that we're appending to the faillog here (import.pl clears it each time)
236 # this could potentially create a situation where the faillog keeps being added
237 # to over multiple builds (if the import process is being skipped)
[6921]238 open (FAILLOG, ">>$faillog") ||
239 (&gsprintf(STDERR, "{common.cannot_open_fail_log}\n", $faillog) && die);
[2785]240 $faillog = 'buildcol::FAILLOG';
241 $faillog->autoflush(1);
242
[7330]243 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
244
[130]245 # read the configuration file
[1267]246 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
[130]247 if (-e $configfilename) {
248 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
[1853]249
[2355]250 if ($verbosity !~ /\d+/) {
251 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
252 $verbosity = $collectcfg->{'verbosity'};
253 } else {
254 $verbosity = 2; # the default
255 }
256 }
[4766]257 # we use searchtype for determining buildtype, but for old versions, use buildtype
258 if (defined $collectcfg->{'searchtype'}) {
259 $buildtype = "mgpp";
260 }
261 elsif (defined $collectcfg->{'buildtype'}) {
[1853]262 $buildtype = $collectcfg->{'buildtype'};
[4766]263 } else {
264 $buildtype = "mg"; #mg is the default
[1853]265 }
[130]266 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
267 $archivedir = $collectcfg->{'archivedir'};
268 }
269 if (defined $collectcfg->{'cachedir'} && $cachedir eq "") {
270 $cachedir = $collectcfg->{'cachedir'};
271 }
272 if (defined $collectcfg->{'builddir'} && $builddir eq "") {
273 $builddir = $collectcfg->{'builddir'};
274 }
[2355]275 if ($maxdocs !~ /\-?\d+/) {
276 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
277 $maxdocs = $collectcfg->{'maxdocs'};
278 } else {
279 $maxdocs = -1; # the default
280 }
[2336]281 }
[2355]282 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
283 $debug = 1;
284 }
285 if ($mode !~ /^(all|compress_text|build_index|infodb)$/) {
286 if (defined $collectcfg->{'mode'} && $collectcfg->{'mode'} =~ /^(all|compress_text|build_index|infodb)$/) {
287 $mode = $collectcfg->{'mode'};
288 } else {
289 $mode = "all"; # the default
290 }
291 }
292 if (defined $collectcfg->{'index'} && $indexname eq "") {
293 $indexname = $collectcfg->{'index'};
294 }
[2336]295 if (defined $collectcfg->{'no_text'} && $no_text == 0) {
[2355]296 if ($collectcfg->{'no_text'} =~ /^true$/i) {
[2336]297 $no_text = 1;
298 }
299 }
[9544]300 if (defined $collectcfg->{'no_strip_html'} && $no_strip_html == 0) {
301 if ($collectcfg->{'no_strip_html'} =~ /^true$/i) {
302 $no_strip_html = 1;
303 }
304 }
[8361]305 if (defined $collectcfg->{'remove_empty_classifications'} && $remove_empty_classifications == 0) {
306 if ($collectcfg->{'remove_empty_classifications'} =~ /^true$/i) {
307 $remove_empty_classifications = 1;
[2336]308 }
309 }
[2355]310 if (defined $collectcfg->{'keepold'} && $collectcfg->{'keepold'} =~ /^true$/i) {
311 $keepold = 1;
312 }
313 if (defined $collectcfg->{'create_images'} && $collectcfg->{'create_images'} =~ /^true$/i) {
314 $create_images = 1;
315 }
[1853]316 if ($buildtype eq "mgpp" && defined $collectcfg->{'textcompress'}) {
317 $textindex = $collectcfg->{'textcompress'};
318 }
[6331]319 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
320 $gli = 1;
321 }
[1853]322
[130]323 } else {
[6921]324 &gsprintf($out, "{common.cannot_find_cfg_file}\n", $configfilename) && die;
[4]325 }
[5094]326
[6331]327 $gli = 0 unless defined $gli;
328
329 print STDERR "<Build>\n" if $gli;
330
[1853]331 #set the text index
[7950]332 if (($buildtype eq "mgpp") || ($buildtype eq "lucene")) {
[1853]333 if ($textindex eq "") {
334 $textindex = "text";
335 }
336 }
337 else {
338 $textindex = "section:text";
339 }
340
[1267]341 # create default images if required
342 if ($create_images) {
343 my $collection_name = $collection;
[3729]344 $collection_name = $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'}
345 if defined $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'};
[1267]346 &create_images ($collection_name);
347 }
348
[4]349 # fill in the default archives and building directories if none
350 # were supplied, turn all \ into / and remove trailing /
[1454]351 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
[4]352 $archivedir =~ s/[\\\/]+/\//g;
353 $archivedir =~ s/\/$//;
[1454]354 $builddir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building") if $builddir eq "";
[4]355 $builddir =~ s/[\\\/]+/\//g;
356 $builddir =~ s/\/$//;
357
358 # update the archive cache if needed
359 if ($cachedir) {
[6921]360 &gsprintf($out, "{buildcol.updating_archive_cache}\n")
[5094]361 if ($verbosity >= 1);
[4]362
363 $cachedir =~ s/[\\\/]+$//;
364 $cachedir .= "/collect/$collection" unless
365 $cachedir =~ /collect\/$collection/;
366
367 $realarchivedir = "$cachedir/archives";
368 $realbuilddir = "$cachedir/building";
369 &util::mk_all_dir ($realarchivedir);
370 &util::mk_all_dir ($realbuilddir);
371 &util::cachedir ($archivedir, $realarchivedir, $verbosity);
372
373 } else {
374 $realarchivedir = $archivedir;
375 $realbuilddir = $builddir;
376 }
377
[1644]378 # build it in realbuilddir
[4]379 &util::mk_all_dir ($realbuilddir);
380
381
382 # if a builder class has been created for this collection, use it
[1853]383 # otherwise, use the mg or mgpp builder
[130]384 if (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}builder.pm") {
385 $builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
[4]386 $buildertype = "${collection}builder";
387 } else {
[8]388 $builderdir = "$ENV{'GSDLHOME'}/perllib";
[7950]389 if ($buildtype eq "lucene") {
390 $buildertype = "lucenebuilder";
391 }
392 elsif ($buildtype eq "mgpp") {
[1853]393 $buildertype = "mgppbuilder";
394 }
395 else {
396 $buildertype = "mgbuilder";
397 }
[4]398 }
399
400 require "$builderdir/$buildertype.pm";
401
402 eval("\$builder = new $buildertype(\$collection, " .
[313]403 "\$realarchivedir, \$realbuilddir, \$verbosity, " .
[8361]404 "\$maxdocs, \$debug, \$keepold, \$remove_empty_classifications, " .
[6331]405 "\$out, \$no_text, \$faillog, \$gli)");
[4]406 die "$@" if $@;
407
408 $builder->init();
409
[7950]410 if (($buildertype eq "mgppbuilder") && $no_strip_html) {
[1853]411 $builder->set_strip_html(0);
412 }
[7950]413
[783]414 if ($mode =~ /^all$/i) {
415 $builder->compress_text($textindex);
416 $builder->build_indexes($indexname);
417 $builder->make_infodatabase();
418 $builder->collect_specific();
419 } elsif ($mode =~ /^compress_text$/i) {
420 $builder->compress_text($textindex);
421 } elsif ($mode =~ /^build_index$/i) {
422 $builder->build_indexes($indexname);
423 } elsif ($mode =~ /^infodb$/i) {
424 $builder->make_infodatabase();
425 } else {
[6921]426 (&gsprintf(STDERR, "{buildcol.unknown_mode}\n", $mode) && die);
[783]427 }
[4]428
[783]429 $builder->make_auxiliary_files() if !$debug;
430 $builder->deinit();
431
432 if (($realbuilddir ne $builddir) && !$debug) {
[6921]433 &gsprintf($out, "{buildcol.copying_back_cached_build}\n")
[5094]434 if ($verbosity >= 1);
[4]435 &util::rm_r ($builddir);
436 &util::cp_r ($realbuilddir, $builddir);
437 }
[1424]438
439 close OUT if $close_out;
[2785]440 close FAILLOG;
[6407]441
442 print STDERR "</Build>\n" if $gli;
[4]443}
444
[1267]445sub create_images {
446 my ($collection_name) = @_;
[4]447
[3729]448 my $image_script = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "script", "gimp", "title_icon-1.2.pl");
[1267]449 if (!-e $image_script) {
[6921]450 &gsprintf($out, "{buildcol.no_image_script}", $image_script);
451 &gsprintf($out, "{buildcol.no_default_images}\n\n");
[1267]452 return;
453 }
454
455 my $imagedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "images");
456
457 &util::mk_all_dir ($imagedir);
458
459 # create the images
460 system ("$image_script -size 1.5 -image_dir \"$imagedir\" -filename $collection.gif -text \"$collection_name\"");
461 system ("$image_script -image_dir \"$imagedir\" -filename ${collection}sm.gif -text \"$collection_name\"");
462
463 # update the collect.cfg configuration file (this will need
464 # to be changed when the config file format changes)
465 if (!open (CFGFILE, $configfilename)) {
[6921]466 &gsprintf($out, "{buildcol.cannot_open_cfg_file}\n", $configfilename);
467 &gsprintf($out, "{buildcol.unlinked_col_images}\n");
[1267]468 return;
469 }
470
471 my $line = ""; my $file = "";
472 my $found = 0; my $foundsm = 0;
473 while (defined ($line = <CFGFILE>)) {
474 if ($line =~ /collectionmeta\s+iconcollection\s+/) {
[1309]475 $line = "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n";
[1267]476 $found = 1;
477 } elsif ($line =~ /collectionmeta\s+iconcollectionsmall\s+/) {
[1309]478 $line = "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n";
[1267]479 $foundsm = 1;
480 }
481 $file .= $line;
482 }
483 close CFGFILE;
484
[1383]485 $file .= "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n" if !$found;
486 $file .= "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n" if !$foundsm;
[1267]487
488 if (!open (CFGFILE, ">$configfilename")) {
[6921]489 &gsprintf($out, "{buildcol.cannot_open_cfg_file}\n", $configfilename);
490 &gsprintf($out, "{buildcol.unlinked_col_images}\n");
[1267]491 return;
492 }
493 print CFGFILE $file;
494 close CFGFILE;
495}
Note: See TracBrowser for help on using the repository browser.