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

Last change on this file since 5094 was 5094, checked in by mdewsnip, 21 years ago

Moved all output messages into the resource bundle (perllib/strings.rb), in preparation for translation.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 18.0 KB
Line 
1#!/usr/bin/perl -w
2
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###########################################################################
27
28# 11/04/03 Added usage datastructure - John Thompson
29
30package buildcol;
31
32BEGIN {
33 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
34 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
38}
39
40use colcfg;
41use parsargv;
42use util;
43use FileHandle;
44use printusage;
45
46my $mode_list =
47 [ { 'name' => "all",
48 'desc' => "{buildcol.mode.all}" },
49 { 'name' => "compress_text",
50 'desc' => "{buildcol.mode.compress_text}" },
51 { 'name' => "build_index",
52 'desc' => "{buildcol.mode.build_index}" },
53 { 'name' => "infodb",
54 'desc' => "{buildcol.mode.infodb}" } ];
55
56my $arguments =
57 [ { 'name' => "archivedir",
58 'desc' => "{buildcol.archivedir}",
59 'type' => "string",
60 'reqd' => "no" },
61 { 'name' => "verbosity",
62 'desc' => "{buildcol.verbosity}",
63 'type' => "int",
64 'deft' => "2",
65 'reqd' => "no" },
66 { 'name' => "builddir",
67 'desc' => "{buildcol.builddir}",
68 'type' => "string",
69 'reqd' => "no" },
70# { 'name' => "cachedir",
71# 'desc' => "{buildcol.cachedir}",
72# 'type' => "string",
73# 'reqd' => "no" },
74 { 'name' => "maxdocs",
75 'desc' => "{buildcol.maxdocs}",
76 'type' => "int",
77 'reqd' => "no" },
78 { 'name' => "debug",
79 'desc' => "{buildcol.debug}",
80 'type' => "flag",
81 'reqd' => "no" },
82 { 'name' => "mode",
83 'desc' => "{buildcol.mode}",
84 'type' => "enum",
85 'list' => $mode_list,
86 'deft' => "all",
87 'reqd' => "no" },
88 { 'name' => "index",
89 'desc' => "{buildcol.index}",
90 'type' => "string",
91 'reqd' => "no" },
92 { 'name' => "keepold",
93 'desc' => "{buildcol.keepold}",
94 'type' => "flag",
95 'reqd' => "no" },
96 { 'name' => "no_text",
97 'desc' => "{buildcol.no_text}",
98 'type' => "flag",
99 'reqd' => "no" },
100 { 'name' => "allclassifications",
101 'desc' => "{buildcol.allclassifications}",
102 'type' => "flag",
103 'reqd' => "no" },
104 { 'name' => "create_images",
105 'desc' => "{buildcol.create_images}",
106 'type' => "flag",
107 'reqd' => "no" },
108 { 'name' => "collectdir",
109 'desc' => "{buildcol.collectdir}",
110 'type' => "string",
111 'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
112 'reqd' => "no" },
113 { 'name' => "out",
114 'desc' => "{buildcol.out}",
115 'type' => "string",
116 'deft' => "STDERR",
117 'reqd' => "no" },
118 { 'name' => "no_strip_html",
119 'desc' => "{buildcol.no_strip_html}",
120 'type' => "flag",
121 'reqd' => "no" },
122 { 'name' => "faillog",
123 'desc' => "{buildcol.faillog}",
124 'type' => "string",
125 'deft' => &util::filename_cat("<collectdir>", "colname", "etc", "fail.log"),
126 'reqd' => "no" },
127 { 'name' => "language",
128 'desc' => "{scripts.language}",
129 'type' => "string",
130 'reqd' => "no" } ];
131
132my $options = { 'name' => "buildcol.pl",
133 'desc' => "{buildcol.desc}",
134 'args' => $arguments };
135
136
137sub print_xml_usage
138{
139 local $language = shift(@_);
140
141 &PrintUsage::print_xml_header();
142
143 print STDERR "<Info>\n";
144 print STDERR " <Name>$options->{'name'}</Name>\n";
145 print STDERR " <Desc>" . &lookup_string($options->{'desc'}) . "</Desc>\n";
146 print STDERR " <Arguments>\n";
147 if (defined($options->{'args'})) {
148 &PrintUsage::print_options_xml($language, $options->{'args'});
149 }
150 print STDERR " </Arguments>\n";
151 print STDERR "</Info>\n";
152}
153
154
155sub print_txt_usage
156{
157 local $language = shift(@_);
158
159 local $programname = $options->{'name'};
160 local $programargs = $options->{'args'};
161
162 # Find the length of the longest option string
163 local $descoffset = 0;
164 if (defined($programargs)) {
165 $descoffset = &PrintUsage::find_longest_option_string($programargs);
166 }
167
168 # Produce the usage information using the data structure above
169 print STDERR " " . &lookup_string("{common.usage}") . ": $programname";
170 print STDERR " " . &lookup_string("{buildcol.params}") . "\n\n";
171
172 # Display the program options, if there are some
173 if (defined($programargs)) {
174 # Calculate the column offset of the option descriptions
175 local $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
176
177 print STDERR " " . &lookup_string("{common.options}") . ":\n";
178
179 # Display the program options
180 &PrintUsage::print_options_txt($language, $programargs, $optiondescoffset);
181 }
182}
183
184
185sub lookup_string
186{
187 return &PrintUsage::lookup_string($language, shift(@_));
188}
189
190
191# sub print_usage {
192# print STDOUT "\n";
193# print STDOUT "buildcol.pl: Builds the indexes of a Greenstone collection.\n\n";
194# print STDOUT " usage: $0 [options] collection-name\n\n";
195# print STDOUT " options:\n";
196# print STDOUT " -verbosity number 0=none, 3=lots\n";
197# print STDOUT " -archivedir directory Where the archives live\n";
198# print STDOUT " -builddir directory Where to put the built indexes\n";
199# print STDOUT " -maxdocs number Maximum number of documents to build\n";
200# print STDOUT " -debug Print output to STDOUT\n";
201# print STDOUT " -mode all|compress_text|build_index|infodb\n";
202# print STDOUT " -index indexname Index to build (will build all in\n";
203# print STDOUT " config file if not set)\n";
204# print STDOUT " -keepold will not destroy the current contents of the\n";
205# print STDOUT " building directory\n";
206# print STDOUT " -no_text Don't store compressed text. This option is\n";
207# print STDOUT " useful for minimizing the size of the built\n";
208# print STDOUT " indexes if you intend always to display the\n";
209# print STDOUT " original documents at run time (i.e. you won't\n";
210# print STDOUT " be able to retrieve the compressed text version)\n";
211# print STDOUT " -allclassifications Don't remove empty classifications\n";
212# print STDOUT " -create_images Attempt to create default images for new\n";
213# print STDOUT " collection. This relies on the Gimp being\n";
214# print STDOUT " installed along with relevant perl modules\n";
215# print STDOUT " to allow scripting from perl\n";
216# print STDOUT " -collectdir directory Collection directory (defaults to " .
217# &util::filename_cat ($ENV{'GSDLHOME'}, "collect") . ")\n";
218# print STDOUT " -out Filename or handle to print output status to.\n";
219# print STDOUT " The default is STDERR\n";
220# print STDOUT " -no_strip_html Do not strip the html tags from the indexed text\n";
221# print STDOUT " (only used for mgpp collections).\n\n";
222# print STDOUT " -faillog name Fail log filename. This log receives the filenames\n";
223# print STDOUT " of any files which fail to be processed (defaults.\n";
224# print STDOUT " to " .
225# &util::filename_cat("<collectdir>", "colname", "etc", "fail.log") . ")\n";
226# print STDOUT " [Type \"perl -S buildcol.pl | more\" if this help text scrolled off your screen]";
227# print STDOUT "\n" unless $ENV{'GSDLOS'} =~ /^windows$/i;
228# }
229
230
231&main();
232
233
234sub main
235{
236 my ($verbosity, $archivedir, $cachedir, $builddir, $maxdocs,
237 $debug, $mode, $indexname, $keepold, $allclassifications,
238 $create_images, $collectdir, $out, $buildtype, $textindex,
239 $no_strip_html, $no_text, $faillog, $language);
240
241 # ***** 11-04-03 - John Thompson *****
242 my $xml = 0;
243 # ************************************
244
245 # note that no defaults are passed for most options as they're set
246 # later (after we check the collect.cfg file)
247 if (!parsargv::parse(\@ARGV,
248 'language/.*/', \$language,
249 'verbosity/\d+/', \$verbosity,
250 'archivedir/.*/', \$archivedir,
251 'cachedir/.*/', \$cachedir, # UNDOCUMENTED
252 'builddir/.*/', \$builddir,
253 'maxdocs/^\-?\d+/', \$maxdocs,
254 'debug', \$debug,
255 'mode/^(all|compress_text|build_index|infodb)$/', \$mode,
256 'index/.*/', \$indexname,
257 'no_text', \$no_text,
258 'keepold', \$keepold,
259 'allclassifications', \$allclassifications,
260 'create_images', \$create_images,
261 'collectdir/.*/', \$collectdir,
262 'out/.*/STDERR', \$out,
263 'no_strip_html', \$no_strip_html,
264 'faillog/.*/', \$faillog,
265 q^xml^, \$xml)) {
266 &print_txt_usage($language);
267 die "\n";
268 }
269
270 if ($xml) {
271 &print_xml_usage($language);
272 die "\n";
273 }
274
275 $textindex = "";
276 my $close_out = 0;
277 if ($out !~ /^(STDERR|STDOUT)$/i) {
278 open (OUT, ">$out") ||
279 die &lookup_string("{common.cannot_open_output_file}") . " $out\n";
280 $out = "buildcol::OUT";
281 $close_out = 1;
282 }
283 $out->autoflush(1);
284
285 # get and check the collection
286 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
287 &print_txt_usage($language);
288 die "\n";
289 }
290
291 if ($faillog eq "") {
292 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
293 }
294 # note that we're appending to the faillog here (import.pl clears it each time)
295 # this could potentially create a situation where the faillog keeps being added
296 # to over multiple builds (if the import process is being skipped)
297 open (FAILLOG, ">>$faillog")
298 || die &lookup_string("{common.cannot_open_fail_log}") . " $faillog\n";
299 $faillog = 'buildcol::FAILLOG';
300 $faillog->autoflush(1);
301
302 # read the configuration file
303 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
304 if (-e $configfilename) {
305 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
306
307 if ($verbosity !~ /\d+/) {
308 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
309 $verbosity = $collectcfg->{'verbosity'};
310 } else {
311 $verbosity = 2; # the default
312 }
313 }
314 # we use searchtype for determining buildtype, but for old versions, use buildtype
315 if (defined $collectcfg->{'searchtype'}) {
316 $buildtype = "mgpp";
317 }
318 elsif (defined $collectcfg->{'buildtype'}) {
319 $buildtype = $collectcfg->{'buildtype'};
320 } else {
321 $buildtype = "mg"; #mg is the default
322 }
323 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
324 $archivedir = $collectcfg->{'archivedir'};
325 }
326 if (defined $collectcfg->{'cachedir'} && $cachedir eq "") {
327 $cachedir = $collectcfg->{'cachedir'};
328 }
329 if (defined $collectcfg->{'builddir'} && $builddir eq "") {
330 $builddir = $collectcfg->{'builddir'};
331 }
332 if ($maxdocs !~ /\-?\d+/) {
333 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
334 $maxdocs = $collectcfg->{'maxdocs'};
335 } else {
336 $maxdocs = -1; # the default
337 }
338 }
339 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
340 $debug = 1;
341 }
342 if ($mode !~ /^(all|compress_text|build_index|infodb)$/) {
343 if (defined $collectcfg->{'mode'} && $collectcfg->{'mode'} =~ /^(all|compress_text|build_index|infodb)$/) {
344 $mode = $collectcfg->{'mode'};
345 } else {
346 $mode = "all"; # the default
347 }
348 }
349 if (defined $collectcfg->{'index'} && $indexname eq "") {
350 $indexname = $collectcfg->{'index'};
351 }
352 if (defined $collectcfg->{'no_text'} && $no_text == 0) {
353 if ($collectcfg->{'no_text'} =~ /^true$/i) {
354 $no_text = 1;
355 }
356 }
357 if (defined $collectcfg->{'allclassifications'} && $allclassifications == 0) {
358 if ($collectcfg->{'allclassifications'} =~ /^true$/i) {
359 $allclassifications = 1;
360 }
361 }
362 if (defined $collectcfg->{'keepold'} && $collectcfg->{'keepold'} =~ /^true$/i) {
363 $keepold = 1;
364 }
365 if (defined $collectcfg->{'create_images'} && $collectcfg->{'create_images'} =~ /^true$/i) {
366 $create_images = 1;
367 }
368 if ($buildtype eq "mgpp" && defined $collectcfg->{'textcompress'}) {
369 $textindex = $collectcfg->{'textcompress'};
370 }
371
372 } else {
373 die &lookup_string("{common.cannot_find_cfg_file}") . " $configfilename\n";
374 }
375
376 #set the text index
377 if ($buildtype eq "mgpp") {
378 if ($textindex eq "") {
379 $textindex = "text";
380 }
381 }
382 else {
383 $textindex = "section:text";
384 }
385
386 # create default images if required
387 if ($create_images) {
388 my $collection_name = $collection;
389 $collection_name = $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'}
390 if defined $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'};
391 &create_images ($collection_name);
392 }
393
394 # fill in the default archives and building directories if none
395 # were supplied, turn all \ into / and remove trailing /
396 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
397 $archivedir =~ s/[\\\/]+/\//g;
398 $archivedir =~ s/\/$//;
399 $builddir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building") if $builddir eq "";
400 $builddir =~ s/[\\\/]+/\//g;
401 $builddir =~ s/\/$//;
402
403 # update the archive cache if needed
404 if ($cachedir) {
405 print $out &lookup_string("{buildcol.updating_archive_cache}") . "\n"
406 if ($verbosity >= 1);
407
408 $cachedir =~ s/[\\\/]+$//;
409 $cachedir .= "/collect/$collection" unless
410 $cachedir =~ /collect\/$collection/;
411
412 $realarchivedir = "$cachedir/archives";
413 $realbuilddir = "$cachedir/building";
414 &util::mk_all_dir ($realarchivedir);
415 &util::mk_all_dir ($realbuilddir);
416 &util::cachedir ($archivedir, $realarchivedir, $verbosity);
417
418 } else {
419 $realarchivedir = $archivedir;
420 $realbuilddir = $builddir;
421 }
422
423 # build it in realbuilddir
424 &util::mk_all_dir ($realbuilddir);
425
426
427 # if a builder class has been created for this collection, use it
428 # otherwise, use the mg or mgpp builder
429 if (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}builder.pm") {
430 $builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
431 $buildertype = "${collection}builder";
432 } else {
433 $builderdir = "$ENV{'GSDLHOME'}/perllib";
434 if ($buildtype eq "mgpp") {
435 $buildertype = "mgppbuilder";
436 }
437 else {
438 $buildertype = "mgbuilder";
439 }
440 }
441
442 require "$builderdir/$buildertype.pm";
443
444 eval("\$builder = new $buildertype(\$collection, " .
445 "\$realarchivedir, \$realbuilddir, \$verbosity, " .
446 "\$maxdocs, \$debug, \$keepold, \$allclassifications, " .
447 "\$out, \$no_text, \$faillog)");
448 die "$@" if $@;
449
450 $builder->init();
451
452 if ($buildertype eq "mgppbuilder" && $no_strip_html) {
453 $builder->set_strip_html(0);
454 }
455 if ($mode =~ /^all$/i) {
456 $builder->compress_text($textindex);
457 $builder->build_indexes($indexname);
458 $builder->make_infodatabase();
459 $builder->collect_specific();
460 } elsif ($mode =~ /^compress_text$/i) {
461 $builder->compress_text($textindex);
462 } elsif ($mode =~ /^build_index$/i) {
463 $builder->build_indexes($indexname);
464 } elsif ($mode =~ /^infodb$/i) {
465 $builder->make_infodatabase();
466 } else {
467 die &lookup_string("{buildcol.unknown_mode}") . " $mode\n";
468 }
469
470 $builder->make_auxiliary_files() if !$debug;
471 $builder->deinit();
472
473 if (($realbuilddir ne $builddir) && !$debug) {
474 print $out &lookup_string("{buildcol.copying_back_cached_build}") . "\n"
475 if ($verbosity >= 1);
476 &util::rm_r ($builddir);
477 &util::cp_r ($realbuilddir, $builddir);
478 }
479
480 close OUT if $close_out;
481 close FAILLOG;
482}
483
484sub create_images {
485 my ($collection_name) = @_;
486
487 my $image_script = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "script", "gimp", "title_icon-1.2.pl");
488 if (!-e $image_script) {
489 print $out &lookup_string("{buildcol.no_image_script}") . ": $image_script.";
490 print $out &lookup_string("{buildcol.no_default_images}") . "\n\n";
491 return;
492 }
493
494 my $imagedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "images");
495
496 &util::mk_all_dir ($imagedir);
497
498 # create the images
499 system ("$image_script -size 1.5 -image_dir \"$imagedir\" -filename $collection.gif -text \"$collection_name\"");
500 system ("$image_script -image_dir \"$imagedir\" -filename ${collection}sm.gif -text \"$collection_name\"");
501
502 # update the collect.cfg configuration file (this will need
503 # to be changed when the config file format changes)
504 if (!open (CFGFILE, $configfilename)) {
505 print $out &lookup_string("{buildcol.cannot_open_cfg_file}") . ": $configfilename\n";
506 print $out &lookup_string("{buildcol.unlinked_col_images}") . "\n";
507 return;
508 }
509
510 my $line = ""; my $file = "";
511 my $found = 0; my $foundsm = 0;
512 while (defined ($line = <CFGFILE>)) {
513 if ($line =~ /collectionmeta\s+iconcollection\s+/) {
514 $line = "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n";
515 $found = 1;
516 } elsif ($line =~ /collectionmeta\s+iconcollectionsmall\s+/) {
517 $line = "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n";
518 $foundsm = 1;
519 }
520 $file .= $line;
521 }
522 close CFGFILE;
523
524 $file .= "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n" if !$found;
525 $file .= "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n" if !$foundsm;
526
527 if (!open (CFGFILE, ">$configfilename")) {
528 print $out &lookup_string("{buildcol.cannot_open_cfg_file}") . ": $configfilename\n";
529 print $out &lookup_string("{buildcol.unlinked_col_images}") . "\n";
530 return;
531 }
532 print CFGFILE $file;
533 close CFGFILE;
534}
535
Note: See TracBrowser for help on using the repository browser.