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

Last change on this file since 6054 was 5882, checked in by davidb, 21 years ago

'cpan' added to perllib path to help plugins and classifiers find supporting
files

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