source: gsdl/trunk/perllib/basebuilder.pm@ 14384

Last change on this file since 14384 was 14384, checked in by xiao, 17 years ago

a new added ->{'collect_cfg_preserve'} is used for gs3 only and to be passed to &colcfg::write_build_cfg_xml in sub make_auxilary_files later in this basebuilder.pm, we use this preserve object because ->{'collect_cfg'}->{'classify'} somewhat gets modified during the calling of &classify::load_classifiers.

  • Property svn:keywords set to Author Date Id Revision
File size: 20.1 KB
Line 
1###########################################################################
2#
3# basebuilder.pm -- base class for collection builders
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package basebuilder;
27
28use strict;
29no strict 'refs'; # allow filehandles to be variables and viceversa
30
31use classify;
32use cfgread;
33use colcfg;
34use plugin;
35use util;
36use FileHandle;
37
38BEGIN {
39 # set autoflush on for STDERR and STDOUT so that mgpp
40 # doesn't get out of sync with plugins
41 STDOUT->autoflush(1);
42 STDERR->autoflush(1);
43}
44
45END {
46 STDOUT->autoflush(0);
47 STDERR->autoflush(0);
48}
49
50our $maxdocsize = 12000;
51
52# used to signify "gs2"(default) or "gs3"
53my $gs_mode = "gs2";
54
55sub new {
56 my ($class, $collection, $source_dir, $build_dir, $verbosity,
57 $maxdocs, $debug, $keepold, $incremental, $incremental_dlc,
58 $remove_empty_classifications,
59 $outhandle, $no_text, $failhandle, $gli, $disable_OAI) = @_;
60
61 $outhandle = *STDERR unless defined $outhandle;
62 $no_text = 0 unless defined $no_text;
63 $failhandle = *STDERR unless defined $failhandle;
64
65 # create a builder object
66 my $self = bless {'collection'=>$collection,
67 'source_dir'=>$source_dir,
68 'build_dir'=>$build_dir,
69 'verbosity'=>$verbosity,
70 'maxdocs'=>$maxdocs,
71 'debug'=>$debug,
72 'keepold'=>$keepold,
73 'incremental'=>$incremental,
74 'incremental_dlc' => $incremental_dlc,
75 'remove_empty_classifications'=>$remove_empty_classifications,
76 'outhandle'=>$outhandle,
77 'no_text'=>$no_text,
78 'failhandle'=>$failhandle,
79 'notbuilt'=>{}, # indexes not built
80 'gli'=>$gli,
81 'disable_OAI'=>$disable_OAI
82 }, $class;
83
84 $self->{'gli'} = 0 unless defined $self->{'gli'};
85
86 # disable_OIA applies to greenstone 3 only and is only passed to &colcfg::write_build_cfg_xml (then cfgread4gs3::write_cfg_file) when writing the buildConfig.xml
87 $self->{'disable_OAI'} = 0 unless defined $self->{'disable_OAI'};
88
89 # Read in the collection configuration file.
90 my ($colcfgname);
91 ($colcfgname, $gs_mode) = &colcfg::get_collect_cfg_name($outhandle);
92 if ($gs_mode eq "gs2") {
93 $self->{'collect_cfg'} = &colcfg::read_collect_cfg ($colcfgname);
94 } elsif ($gs_mode eq "gs3") {
95 $self->{'collect_cfg'} = &colcfg::read_collection_cfg_xml ($colcfgname);
96
97 #this $self->{'collect_cfg_preserve'} is used for gs3 only and to be passed to &colcfg::write_build_cfg_xml in sub make_auxilary_files later in this basebuilder.pm, we use this preserve object because $self->{'collect_cfg'}->{'classify'} somewhat gets modified during the calling of &classify::load_classifiers.
98 $self->{'collect_cfg_preserve'} = &colcfg::read_collection_cfg_xml ($colcfgname);
99 }
100
101 # get the list of plugins for this collection
102 my $plugins = [];
103 if (defined $self->{'collect_cfg'}->{'plugin'}) {
104 $plugins = $self->{'collect_cfg'}->{'plugin'};
105 }
106
107 # load all the plugins
108
109 #build up the extra global options for the plugins
110 my @global_opts = ();
111 if (defined $self->{'collect_cfg'}->{'separate_cjk'} && $self->{'collect_cfg'}->{'separate_cjk'} =~ /^true$/i) {
112 push @global_opts, "-separate_cjk";
113 }
114 $self->{'pluginfo'} = &plugin::load_plugins ($plugins, $verbosity, $outhandle, $failhandle, \@global_opts, $keepold);
115
116 if (scalar(@{$self->{'pluginfo'}}) == 0) {
117 print $outhandle "No plugins were loaded.\n";
118 die "\n";
119 }
120
121 # get the list of classifiers for this collection
122 my $classifiers = [];
123 if (defined $self->{'collect_cfg'}->{'classify'}) {
124 $classifiers = $self->{'collect_cfg'}->{'classify'};
125 }
126
127 # load all the classifiers
128 $self->{'classifiers'} = &classify::load_classifiers ($classifiers, $build_dir, $outhandle);
129
130 # load up any dontgdbm fields
131 $self->{'dontgdbm'} = {};
132 if (defined ($self->{'collect_cfg'}->{'dontgdbm'})) {
133 foreach my $dg (@{$self->{'collect_cfg'}->{'dontgdbm'}}) {
134 $self->{'dontgdbm'}->{$dg} = 1;
135 }
136 }
137
138 $self->{'maxnumeric'} = 4;
139 return $self;
140}
141
142# stuff has been moved here from new, so we can use subclass methods
143sub init {
144 my $self = shift(@_);
145
146 $self->generate_index_list();
147 $self->generate_index_options();
148
149 # sort out subcollection indexes
150 if (defined $self->{'collect_cfg'}->{'indexsubcollections'}) {
151 my $indexes = $self->{'collect_cfg'}->{'indexes'};
152 $self->{'collect_cfg'}->{'indexes'} = [];
153 foreach my $subcollection (@{$self->{'collect_cfg'}->{'indexsubcollections'}}) {
154 foreach my $index (@$indexes) {
155 push (@{$self->{'collect_cfg'}->{'indexes'}}, "$index:$subcollection");
156 }
157 }
158 }
159
160 # sort out language subindexes
161 if (defined $self->{'collect_cfg'}->{'languages'}) {
162 my $indexes = $self->{'collect_cfg'}->{'indexes'};
163 $self->{'collect_cfg'}->{'indexes'} = [];
164 foreach my $language (@{$self->{'collect_cfg'}->{'languages'}}) {
165 foreach my $index (@$indexes) {
166 if (defined ($self->{'collect_cfg'}->{'indexsubcollections'})) {
167 push (@{$self->{'collect_cfg'}->{'indexes'}}, "$index:$language");
168 }
169 else { # add in an empty subcollection field
170 push (@{$self->{'collect_cfg'}->{'indexes'}}, "$index\:\:$language");
171 }
172 }
173 }
174 }
175
176 if (defined($self->{'collect_cfg'}->{'indexes'})) {
177 # make sure that the same index isn't specified more than once
178 my %tmphash = ();
179 my @tmparray = @{$self->{'collect_cfg'}->{'indexes'}};
180 $self->{'collect_cfg'}->{'indexes'} = [];
181 foreach my $i (@tmparray) {
182 if (!defined ($tmphash{$i})) {
183 push (@{$self->{'collect_cfg'}->{'indexes'}}, $i);
184 $tmphash{$i} = 1;
185 }
186 }
187 } else {
188 $self->{'collect_cfg'}->{'indexes'} = [];
189 }
190
191 # load up the document processor for building
192 # if a buildproc class has been created for this collection, use it
193 # otherwise, use the mg buildproc
194 my ($buildprocdir, $buildproctype);
195 my $collection = $self->{'collection'};
196 if (-e "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib/custombuildproc.pm") {
197 $buildprocdir = "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib";
198 $buildproctype = "custombuildproc";
199 } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/custombuildproc.pm") {
200 $buildprocdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
201 $buildproctype = "custombuildproc";
202 } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}buildproc.pm") {
203 $buildprocdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
204 $buildproctype = "${collection}buildproc";
205 } else {
206 $buildprocdir = "$ENV{'GSDLHOME'}/perllib";
207 $buildproctype = $self->default_buildproc();
208 }
209 require "$buildprocdir/$buildproctype.pm";
210
211 eval("\$self->{'buildproc'} = new $buildproctype(\$self->{'collection'}, " .
212 "\$self->{'source_dir'}, \$self->{'build_dir'}, \$self->{'keepold'}, \$self->{'verbosity'}, \$self->{'outhandle'})");
213 die "$@" if $@;
214
215 if (!$self->{'debug'} && !$self->{'keepold'}) {
216 # remove any old builds
217 &util::rm_r($self->{'build_dir'});
218 &util::mk_all_dir($self->{'build_dir'});
219
220 # make the text directory
221 my $textdir = "$self->{'build_dir'}/text";
222 &util::mk_all_dir($textdir);
223 }
224
225}
226
227sub deinit {
228 my $self = shift (@_);
229
230 &plugin::deinit($self->{'pluginfo'},$self->{'buildproc'});
231}
232
233sub set_sections_index_document_metadata {
234 my $self = shift (@_);
235 my ($index) = @_;
236
237 $self->{'buildproc'}->set_sections_index_document_metadata($index);
238}
239
240sub set_maxnumeric {
241 my $self = shift (@_);
242 my ($maxnumeric) = @_;
243
244 $self->{'maxnumeric'} = $maxnumeric;
245}
246sub set_strip_html {
247 my $self = shift (@_);
248 my ($strip) = @_;
249
250 $self->{'strip_html'} = $strip;
251 $self->{'buildproc'}->set_strip_html($strip);
252}
253
254sub compress_text {
255 my $self = shift (@_);
256 my ($textindex) = @_;
257
258 print STDERR "compress_text() should be implemented in subclass!!";
259 return;
260}
261
262
263sub build_indexes {
264 my $self = shift (@_);
265 my ($indexname) = @_;
266 my $outhandle = $self->{'outhandle'};
267
268 my $indexes = [];
269 if (defined $indexname && $indexname =~ /\w/) {
270 push @$indexes, $indexname;
271 } else {
272 $indexes = $self->{'collect_cfg'}->{'indexes'};
273 }
274
275 # create the mapping between the index descriptions
276 # and their directory names (includes subcolls and langs)
277 $self->{'index_mapping'} = $self->create_index_mapping ($indexes);
278
279 # build each of the indexes
280 foreach my $index (@$indexes) {
281 if ($self->want_built($index)) {
282 print $outhandle "\n*** building index $index in subdirectory " .
283 "$self->{'index_mapping'}->{$index}\n" if ($self->{'verbosity'} >= 1);
284 print STDERR "<Stage name='Index' source='$index'>\n" if $self->{'gli'};
285 $self->build_index($index);
286 } else {
287 print $outhandle "\n*** ignoring index $index\n" if ($self->{'verbosity'} >= 1);
288 }
289 }
290
291 $self->build_indexes_extra();
292
293}
294
295sub build_indexes_extra {
296 my $self = shift(@_);
297
298}
299
300sub build_index {
301 my $self = shift (@_);
302 my ($index) = @_;
303
304 print STDERR "build_index should be implemented in subclass\n";
305 return;
306}
307
308
309
310sub make_infodatabase {
311 my $self = shift (@_);
312 my $outhandle = $self->{'outhandle'};
313
314 print STDERR "BuildDir: $self->{'build_dir'}\n";
315
316 my $textdir = &util::filename_cat($self->{'build_dir'}, "text");
317 my $assocdir = &util::filename_cat($self->{'build_dir'}, "assoc");
318 &util::mk_all_dir ($textdir);
319 &util::mk_all_dir ($assocdir);
320
321 # get db name
322 my $dbext = ".bdb";
323 $dbext = ".ldb" if &util::is_little_endian();
324 my $fulldbname = &util::filename_cat ($textdir, "$self->{'collection'}$dbext");
325 $fulldbname =~ s/\//\\/g if ($ENV{'GSDLOS'} =~ /^windows$/i);
326
327 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
328 my $exe = &util::get_os_exe ();
329 my $txt2db_exe = &util::filename_cat($exedir, "txt2db$exe");
330
331 print $outhandle "\n*** creating the info database and processing associated files\n"
332 if ($self->{'verbosity'} >= 1);
333 print STDERR "<Stage name='CreateInfoData'>\n" if $self->{'gli'};
334
335 # init all the classifiers
336 &classify::init_classifiers ($self->{'classifiers'});
337
338
339 my $reconstructed_docs = undef;
340 if ($self->{'keepold'}) {
341 # reconstruct doc_obj metadata from gdbm for all docs
342 $reconstructed_docs = &classify::reconstruct_doc_objs_metadata($fulldbname);
343 }
344
345 # set up the document processor
346 my ($handle);
347 if ($self->{'debug'}) {
348 $handle = *STDOUT;
349 } else {
350 if (!-e "$txt2db_exe" || !open (PIPEOUT, "| txt2db$exe \"$fulldbname\"")) {
351 print STDERR "<FatalError name='NoRunText2DB'/>\n</Stage>\n" if $self->{'gli'};
352 die "builder::make_infodatabase - couldn't run $txt2db_exe\n";
353 }
354 $handle = *PIPEOUT;
355 }
356
357 $self->{'buildproc'}->set_output_handle ($handle);
358 $self->{'buildproc'}->set_mode ('infodb');
359 $self->{'buildproc'}->set_assocdir ($assocdir);
360 $self->{'buildproc'}->set_dontgdbm ($self->{'dontgdbm'});
361 $self->{'buildproc'}->set_classifiers ($self->{'classifiers'});
362 $self->{'buildproc'}->set_indexing_text (0);
363 $self->{'buildproc'}->set_store_text(1);
364
365 # make_infodatabase needs full reset even for incremental build
366 # as incremental works by reconstructing all docs from GDBM and
367 # then adding in the new ones
368 $self->{'buildproc'}->zero_reset();
369
370 if ($self->{'keepold'}) {
371 # create flat classify structure, ready for new docs to be added
372 foreach my $doc_obj ( @$reconstructed_docs ) {
373 print $outhandle " Adding reconstructed ", $doc_obj->get_OID(), " into classify structures\n";
374 $self->{'buildproc'}->process($doc_obj,undef);
375 }
376 }
377
378
379 # this has changed to only output collection meta if its
380 # not in the config file
381 $self->output_collection_meta($handle);
382 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
383 "", {}, $self->{'buildproc'}, $self->{'maxdocs'},0, $self->{'gli'});
384
385 # output classification information
386 &classify::output_classify_info ($self->{'classifiers'}, $handle,
387 $self->{'remove_empty_classifications'},
388 $self->{'gli'});
389
390 # Output classifier reverse lookup, used in incremental deletion
391 #&classify::print_reverse_lookup($handle);
392
393 #output doclist
394 my @doclist = $self->{'buildproc'}->get_doc_list();
395 my $docs = join (";",@doclist);
396 print $handle "[browselist]\n";
397 print $handle "<hastxt>0\n";
398 print $handle "<childtype>VList\n";
399 print $handle "<numleafdocs>" . ($#doclist+1) . "\n";
400 print $handle "<thistype>Invisible\n";
401 print $handle "<contains>$docs";
402 print $handle "\n" . ('-' x 70) . "\n";
403
404 close ($handle) if !$self->{'debug'};
405
406 print STDERR "</Stage>\n" if $self->{'gli'};
407}
408
409sub make_auxiliary_files {
410 my $self = shift (@_);
411 my ($index);
412 my $build_cfg = {};
413 # subclasses may have already defined stuff in here
414 if (defined $self->{'build_cfg'}) {
415 $build_cfg = $self->{'build_cfg'};
416 }
417
418 my $outhandle = $self->{'outhandle'};
419
420 print $outhandle "\n*** creating auxiliary files \n" if ($self->{'verbosity'} >= 1);
421 print STDERR "<Stage name='CreatingAuxilary'>\n" if $self->{'gli'};
422
423 # get the text directory
424 &util::mk_all_dir ($self->{'build_dir'});
425
426 # store the build date
427 $build_cfg->{'builddate'} = time;
428 $build_cfg->{'buildtype'} = $self->{'buildtype'};
429 $build_cfg->{'indexstem'} = $self->{'collection'};
430 $build_cfg->{'stemindexes'} = $self->{'stemindexes'};
431
432 # store the number of documents and number of bytes
433 $build_cfg->{'numdocs'} = $self->{'buildproc'}->get_num_docs();
434 $build_cfg->{'numsections'} = $self->{'buildproc'}->get_num_sections();
435 $build_cfg->{'numbytes'} = $self->{'buildproc'}->get_num_bytes();
436
437 # store the mapping between the index names and the directory names
438 # the index map is used to determine what indexes there are, so any that are not built should not be put into the map.
439 my @indexmap = ();
440 foreach my $index (@{$self->{'index_mapping'}->{'indexmaporder'}}) {
441 if (not defined ($self->{'notbuilt'}->{$index})) {
442 push (@indexmap, "$index\-\>$self->{'index_mapping'}->{'indexmap'}->{$index}");
443 }
444 }
445 $build_cfg->{'indexmap'} = \@indexmap if scalar (@indexmap);
446
447 my @subcollectionmap = ();
448 foreach my $subcollection (@{$self->{'index_mapping'}->{'subcollectionmaporder'}}) {
449 push (@subcollectionmap, "$subcollection\-\>" .
450 $self->{'index_mapping'}->{'subcollectionmap'}->{$subcollection});
451 }
452 $build_cfg->{'subcollectionmap'} = \@subcollectionmap if scalar (@subcollectionmap);
453
454 my @languagemap = ();
455 foreach my $language (@{$self->{'index_mapping'}->{'languagemaporder'}}) {
456 push (@languagemap, "$language\-\>" .
457 $self->{'index_mapping'}->{'languagemap'}->{$language});
458 }
459 $build_cfg->{'languagemap'} = \@languagemap if scalar (@languagemap);
460
461 my @notbuilt = ();
462 foreach my $nb (keys %{$self->{'notbuilt'}}) {
463 push (@notbuilt, $nb);
464 }
465 $build_cfg->{'notbuilt'} = \@notbuilt if scalar (@notbuilt);
466
467 $build_cfg->{'maxnumeric'} = $self->{'maxnumeric'};
468
469 $self->build_cfg_extra($build_cfg);
470
471 if ($gs_mode eq "gs2") {
472 &colcfg::write_build_cfg("$self->{'build_dir'}/build.cfg", $build_cfg);
473 }
474 if ($gs_mode eq "gs3") {
475
476 &colcfg::write_build_cfg_xml("$self->{'build_dir'}/buildConfig.xml", $build_cfg, $self->{'collect_cfg_preserve'}, $self->{'disable_OAI'});
477 }
478
479 print STDERR "</Stage>\n" if $self->{'gli'};
480}
481
482sub collect_specific {
483 my $self = shift (@_);
484}
485
486sub want_built {
487 my $self = shift (@_);
488 my ($index) = @_;
489
490 if (defined ($self->{'collect_cfg'}->{'dontbuild'})) {
491 foreach my $checkstr (@{$self->{'collect_cfg'}->{'dontbuild'}}) {
492 if ($index =~ /^$checkstr$/) {
493 $self->{'notbuilt'}->{$index} = 1;
494 return 0;
495 }
496 }
497 }
498
499 return 1;
500}
501
502sub create_index_mapping {
503 my $self = shift (@_);
504 my ($indexes) = @_;
505
506 print STDERR "create_index_mapping should be implemented in subclass\n";
507 my %mapping = ();
508 return \%mapping;
509}
510
511# returns a processed version of a field.
512# if the field has only one component the processed
513# version will contain the first character and next consonant
514# of that componant - otherwise it will contain the first
515# character of the first two components
516# only uses letdig (\w) characters now
517sub process_field {
518 my $self = shift (@_);
519 my ($field) = @_;
520
521 return "" unless (defined ($field) && $field =~ /\S/);
522
523 my ($a, $b);
524 my @components = split /,/, $field;
525 if (scalar @components >= 2) {
526 # pick the first letdig from the first two field names
527 ($a) = $components[0] =~ /^[^\w]*(\w)/;
528 ($b) = $components[1] =~ /^[^\w]*(\w)/;
529 } else {
530 # pick the first two letdig chars
531 ($a, $b) = $field =~ /^[^\w]*(\w)[^\w]*?(\w)/i;
532 }
533 # there may not have been any letdigs...
534 $a = 'a' unless defined $a;
535 $b = '0' unless defined $b;
536
537 return "$a$b";
538
539}
540
541sub get_next_version {
542 my $self = shift (@_);
543 my ($nameref) = @_;
544 my $num=0;
545 if ($$nameref =~ /(\d\d)$/) {
546 $num = $1; $num ++;
547 $$nameref =~ s/\d\d$/$num/;
548 } elsif ($$nameref =~ /(\d)$/) {
549 $num = $1;
550 if ($num == 9) {$$nameref =~ s/\d$/10/;}
551 else {$num ++; $$nameref =~ s/\d$/$num/;}
552 } else {
553 $$nameref =~ s/.$/0/;
554 }
555}
556
557# implement this in subclass if want to add extra stuff to build.cfg
558sub build_cfg_extra {
559 my $self = shift(@_);
560 my ($build_cfg) = @_;
561
562}
563
564# default is to output an empty [collection] entry
565sub output_collection_meta {
566 my $self = shift(@_);
567 my ($handle) = @_;
568
569 print $handle "[collection]\n". ('-' x 70) . "\n";;
570
571}
572
573sub print_stats {
574 my $self = shift (@_);
575
576 my $outhandle = $self->{'outhandle'};
577 my $indexing_text = $self->{'buildproc'}->get_indexing_text();
578 my $index = $self->{'buildproc'}->get_index();
579 my $num_bytes = $self->{'buildproc'}->get_num_bytes();
580 my $num_processed_bytes = $self->{'buildproc'}->get_num_processed_bytes();
581
582 if ($indexing_text) {
583 print $outhandle "Stats (Creating index $index)\n";
584 } else {
585 print $outhandle "Stats (Compressing text from $index)\n";
586 }
587 print $outhandle "Total bytes in collection: $num_bytes\n";
588 print $outhandle "Total bytes in $index: $num_processed_bytes\n";
589
590 if ($num_processed_bytes < 50 && ($indexing_text || !$self->{'no_text'})) {
591
592 if ($self->{'keepold'}) {
593 if ($num_processed_bytes == 0) {
594 if ($indexing_text) {
595 print $outhandle "No additional text was added to $index\n";
596 } elsif (!$self->{'no_text'}) {
597 print $outhandle "No additional text was compressed\n";
598 }
599 }
600 }
601 else {
602 print $outhandle "***************\n";
603 if ($indexing_text) {
604 print $outhandle "WARNING: There is very little or no text to process for $index\n";
605 } elsif (!$self->{'no_text'}) {
606 print $outhandle "WARNING: There is very little or no text to compress\n";
607 }
608 print $outhandle " Was this your intention?\n";
609 print $outhandle "***************\n";
610 }
611
612 }
613
614}
615
616
6171;
618
Note: See TracBrowser for help on using the repository browser.