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

Last change on this file since 11997 was 11994, checked in by davidb, 18 years ago

Improved support for incremental addition: instead of having to run the
classifier pass of buildcol.pl from scratch (i.e. read in all documents
from the archives folder) so correct browse structures are formed -- a
simple to implement strategy, but not very efficient -- the first layer
of a classifier structure is now reconstructed from the GDBM file. Then
the new files in the archives directory are added, and then finally the
completed browser structure is formed.

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