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
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' => "remove_empty_classifications",
60 'desc' => "{buildcol.remove_empty_classifications}",
61 'type' => "flag",
62 'reqd' => "no",
63 'modegli' => "3" },
64 { 'name' => "archivedir",
65 'desc' => "{buildcol.archivedir}",
66 'type' => "string",
67 'reqd' => "no",
68 'hiddengli' => "yes" },
69 { 'name' => "builddir",
70 'desc' => "{buildcol.builddir}",
71 'type' => "string",
72 'reqd' => "no",
73 'hiddengli' => "yes" },
74# { 'name' => "cachedir",
75# 'desc' => "{buildcol.cachedir}",
76# 'type' => "string",
77# 'reqd' => "no" },
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" },
89 { 'name' => "debug",
90 'desc' => "{buildcol.debug}",
91 'type' => "flag",
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" },
100 { 'name' => "index",
101 'desc' => "{buildcol.index}",
102 'type' => "string",
103 'reqd' => "no",
104 'modegli' => "3" },
105 { 'name' => "keepold",
106 'desc' => "{buildcol.keepold}",
107 'type' => "flag",
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" },
132 { 'name' => "no_text",
133 'desc' => "{buildcol.no_text}",
134 'type' => "flag",
135 'reqd' => "no",
136 'modegli' => "3" },
137 { 'name' => "out",
138 'desc' => "{buildcol.out}",
139 'type' => "string",
140 'deft' => "STDERR",
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" } ];
149
150my $options = { 'name' => "buildcol.pl",
151 'desc' => "{buildcol.desc}",
152 'args' => $arguments };
153
154sub gsprintf
155{
156 return &gsprintf::gsprintf(@_);
157}
158
159
160&main();
161
162
163sub main
164{
165 my ($verbosity, $archivedir, $cachedir, $builddir, $maxdocs,
166 $debug, $mode, $indexname, $keepold, $remove_empty_classifications,
167 $create_images, $collectdir, $out, $buildtype, $textindex,
168 $no_strip_html, $no_text, $faillog, $gli);
169
170 # ***** 11-04-03 - John Thompson *****
171 my $xml = 0;
172 # ************************************
173
174 # note that no defaults are passed for most options as they're set
175 # later (after we check the collect.cfg file)
176 if (!parsargv::parse(\@ARGV,
177 'language/.*/', \$language,
178 'verbosity/\d+/', \$verbosity,
179 'archivedir/.*/', \$archivedir,
180 'cachedir/.*/', \$cachedir, # UNDOCUMENTED
181 'builddir/.*/', \$builddir,
182 'maxdocs/^\-?\d+/', \$maxdocs,
183 'debug', \$debug,
184 'mode/^(all|compress_text|build_index|infodb)$/', \$mode,
185 'index/.*/', \$indexname,
186 'no_text', \$no_text,
187 'keepold', \$keepold,
188 'remove_empty_classifications', \$remove_empty_classifications,
189 'create_images', \$create_images,
190 'collectdir/.*/', \$collectdir,
191 'out/.*/STDERR', \$out,
192 'no_strip_html', \$no_strip_html,
193 'faillog/.*/', \$faillog,
194 'gli', \$gli,
195 q^xml^, \$xml)) {
196 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
197 die "\n";
198 }
199
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 }
205
206 if ($xml) {
207 &PrintUsage::print_xml_usage($options);
208 print "\n";
209 return;
210 }
211
212 if ($gli) { # the gli wants strings to be in UTF-8
213 &gsprintf::output_strings_in_UTF8;
214 }
215
216 $textindex = "";
217 my $close_out = 0;
218 if ($out !~ /^(STDERR|STDOUT)$/i) {
219 open (OUT, ">$out") ||
220 (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
221 $out = "buildcol::OUT";
222 $close_out = 1;
223 }
224 $out->autoflush(1);
225
226 # get and check the collection
227 if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
228 &PrintUsage::print_txt_usage($options, "{buildcol.params}");
229 die "\n";
230 }
231
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)
238 open (FAILLOG, ">>$faillog") ||
239 (&gsprintf(STDERR, "{common.cannot_open_fail_log}\n", $faillog) && die);
240 $faillog = 'buildcol::FAILLOG';
241 $faillog->autoflush(1);
242
243 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
244
245 # read the configuration file
246 $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
247 if (-e $configfilename) {
248 $collectcfg = &colcfg::read_collect_cfg ($configfilename);
249
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 }
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'}) {
262 $buildtype = $collectcfg->{'buildtype'};
263 } else {
264 $buildtype = "mg"; #mg is the default
265 }
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 }
275 if ($maxdocs !~ /\-?\d+/) {
276 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
277 $maxdocs = $collectcfg->{'maxdocs'};
278 } else {
279 $maxdocs = -1; # the default
280 }
281 }
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 }
295 if (defined $collectcfg->{'no_text'} && $no_text == 0) {
296 if ($collectcfg->{'no_text'} =~ /^true$/i) {
297 $no_text = 1;
298 }
299 }
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 }
305 if (defined $collectcfg->{'remove_empty_classifications'} && $remove_empty_classifications == 0) {
306 if ($collectcfg->{'remove_empty_classifications'} =~ /^true$/i) {
307 $remove_empty_classifications = 1;
308 }
309 }
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 }
316 if ($buildtype eq "mgpp" && defined $collectcfg->{'textcompress'}) {
317 $textindex = $collectcfg->{'textcompress'};
318 }
319 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
320 $gli = 1;
321 }
322
323 } else {
324 &gsprintf($out, "{common.cannot_find_cfg_file}\n", $configfilename) && die;
325 }
326
327 $gli = 0 unless defined $gli;
328
329 print STDERR "<Build>\n" if $gli;
330
331 #set the text index
332 if (($buildtype eq "mgpp") || ($buildtype eq "lucene")) {
333 if ($textindex eq "") {
334 $textindex = "text";
335 }
336 }
337 else {
338 $textindex = "section:text";
339 }
340
341 # create default images if required
342 if ($create_images) {
343 my $collection_name = $collection;
344 $collection_name = $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'}
345 if defined $collectcfg->{'collectionmeta'}->{'collectionname'}->{'default'};
346 &create_images ($collection_name);
347 }
348
349 # fill in the default archives and building directories if none
350 # were supplied, turn all \ into / and remove trailing /
351 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives") if $archivedir eq "";
352 $archivedir =~ s/[\\\/]+/\//g;
353 $archivedir =~ s/\/$//;
354 $builddir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building") if $builddir eq "";
355 $builddir =~ s/[\\\/]+/\//g;
356 $builddir =~ s/\/$//;
357
358 # update the archive cache if needed
359 if ($cachedir) {
360 &gsprintf($out, "{buildcol.updating_archive_cache}\n")
361 if ($verbosity >= 1);
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
378 # build it in realbuilddir
379 &util::mk_all_dir ($realbuilddir);
380
381
382 # if a builder class has been created for this collection, use it
383 # otherwise, use the mg or mgpp builder
384 if (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}builder.pm") {
385 $builderdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
386 $buildertype = "${collection}builder";
387 } else {
388 $builderdir = "$ENV{'GSDLHOME'}/perllib";
389 if ($buildtype eq "lucene") {
390 $buildertype = "lucenebuilder";
391 }
392 elsif ($buildtype eq "mgpp") {
393 $buildertype = "mgppbuilder";
394 }
395 else {
396 $buildertype = "mgbuilder";
397 }
398 }
399
400 require "$builderdir/$buildertype.pm";
401
402 eval("\$builder = new $buildertype(\$collection, " .
403 "\$realarchivedir, \$realbuilddir, \$verbosity, " .
404 "\$maxdocs, \$debug, \$keepold, \$remove_empty_classifications, " .
405 "\$out, \$no_text, \$faillog, \$gli)");
406 die "$@" if $@;
407
408 $builder->init();
409
410 if (($buildertype eq "mgppbuilder") && $no_strip_html) {
411 $builder->set_strip_html(0);
412 }
413
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 {
426 (&gsprintf(STDERR, "{buildcol.unknown_mode}\n", $mode) && die);
427 }
428
429 $builder->make_auxiliary_files() if !$debug;
430 $builder->deinit();
431
432 if (($realbuilddir ne $builddir) && !$debug) {
433 &gsprintf($out, "{buildcol.copying_back_cached_build}\n")
434 if ($verbosity >= 1);
435 &util::rm_r ($builddir);
436 &util::cp_r ($realbuilddir, $builddir);
437 }
438
439 close OUT if $close_out;
440 close FAILLOG;
441
442 print STDERR "</Build>\n" if $gli;
443}
444
445sub create_images {
446 my ($collection_name) = @_;
447
448 my $image_script = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "script", "gimp", "title_icon-1.2.pl");
449 if (!-e $image_script) {
450 &gsprintf($out, "{buildcol.no_image_script}", $image_script);
451 &gsprintf($out, "{buildcol.no_default_images}\n\n");
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)) {
466 &gsprintf($out, "{buildcol.cannot_open_cfg_file}\n", $configfilename);
467 &gsprintf($out, "{buildcol.unlinked_col_images}\n");
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+/) {
475 $line = "collectionmeta iconcollection _httpprefix_/collect/$collection/images/$collection.gif\n";
476 $found = 1;
477 } elsif ($line =~ /collectionmeta\s+iconcollectionsmall\s+/) {
478 $line = "collectionmeta iconcollectionsmall _httpprefix_/collect/$collection/images/${collection}sm.gif\n";
479 $foundsm = 1;
480 }
481 $file .= $line;
482 }
483 close CFGFILE;
484
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;
487
488 if (!open (CFGFILE, ">$configfilename")) {
489 &gsprintf($out, "{buildcol.cannot_open_cfg_file}\n", $configfilename);
490 &gsprintf($out, "{buildcol.unlinked_col_images}\n");
491 return;
492 }
493 print CFGFILE $file;
494 close CFGFILE;
495}
Note: See TracBrowser for help on using the repository browser.