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

Last change on this file since 12270 was 12128, checked in by kjdon, 18 years ago

output_collection_meta now outputs an empty [collection] entry to gdbm database

  • Property svn:keywords set to Author Date Id Revision
File size: 17.9 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 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
349 "", {}, $self->{'buildproc'}, $self->{'maxdocs'},0, $self->{'gli'});
350
351 # output classification information
352 &classify::output_classify_info ($self->{'classifiers'}, $handle,
353 $self->{'remove_empty_classifications'},
354 $self->{'gli'});
355
356
357 #output doclist
358 my @doclist = $self->{'buildproc'}->get_doc_list();
359 my $docs = join (";",@doclist);
360 print $handle "[browselist]\n";
361 print $handle "<hastxt>0\n";
362 print $handle "<childtype>VList\n";
363 print $handle "<numleafdocs>" . ($#doclist+1) . "\n";
364 print $handle "<thistype>Invisible\n";
365 print $handle "<contains>$docs";
366 print $handle "\n" . ('-' x 70) . "\n";
367
368 close ($handle) if !$self->{'debug'};
369
370 print STDERR "</Stage>\n" if $self->{'gli'};
371}
372
373sub make_auxiliary_files {
374 my $self = shift (@_);
375 my ($index);
376 my $build_cfg = {};
377 # subclasses may have already defined stuff in here
378 if (defined $self->{'build_cfg'}) {
379 $build_cfg = $self->{'build_cfg'};
380 }
381
382 my $outhandle = $self->{'outhandle'};
383
384 print $outhandle "\n*** creating auxiliary files \n" if ($self->{'verbosity'} >= 1);
385 print STDERR "<Stage name='CreatingAuxilary'>\n" if $self->{'gli'};
386
387 # get the text directory
388 &util::mk_all_dir ($self->{'build_dir'});
389
390 # store the build date
391 $build_cfg->{'builddate'} = time;
392 $build_cfg->{'buildtype'} = $self->{'buildtype'};
393 $build_cfg->{'indexstem'} = $self->{'collection'};
394 # store the number of documents and number of bytes
395 $build_cfg->{'numdocs'} = $self->{'buildproc'}->get_num_docs();
396 $build_cfg->{'numbytes'} = $self->{'buildproc'}->get_num_bytes();
397
398
399 # store the mapping between the index names and the directory names
400 # the index map is used to determine what indexes there are, so any that are not built should not be put into the map.
401 my @indexmap = ();
402 foreach my $index (@{$self->{'index_mapping'}->{'indexmaporder'}}) {
403 if (not defined ($self->{'notbuilt'}->{$index})) {
404 push (@indexmap, "$index\-\>$self->{'index_mapping'}->{'indexmap'}->{$index}");
405 }
406 }
407 $build_cfg->{'indexmap'} = \@indexmap;
408
409 my @subcollectionmap = ();
410 foreach my $subcollection (@{$self->{'index_mapping'}->{'subcollectionmaporder'}}) {
411 push (@subcollectionmap, "$subcollection\-\>" .
412 $self->{'index_mapping'}->{'subcollectionmap'}->{$subcollection});
413 }
414 $build_cfg->{'subcollectionmap'} = \@subcollectionmap if scalar (@subcollectionmap);
415
416 my @languagemap = ();
417 foreach my $language (@{$self->{'index_mapping'}->{'languagemaporder'}}) {
418 push (@languagemap, "$language\-\>" .
419 $self->{'index_mapping'}->{'languagemap'}->{$language});
420 }
421 $build_cfg->{'languagemap'} = \@languagemap if scalar (@languagemap);
422
423 my @notbuilt = ();
424 foreach my $nb (keys %{$self->{'notbuilt'}}) {
425 push (@notbuilt, $nb);
426 }
427 $build_cfg->{'notbuilt'} = \@notbuilt if scalar (@notbuilt);
428
429 $build_cfg->{'maxnumeric'} = 4;
430 if (defined($self->{'collect_cfg'}->{'maxnumeric'}) &&
431 $self->{'collect_cfg'}->{'maxnumeric'} =~ /^\d+$/) {
432 $build_cfg->{'maxnumeric'} = $self->{'collect_cfg'}->{'maxnumeric'};
433 }
434
435 $self->build_cfg_extra($build_cfg);
436
437 $self->write_cfg_file($build_cfg);
438 print STDERR "</Stage>\n" if $self->{'gli'};
439}
440
441sub collect_specific {
442 my $self = shift (@_);
443}
444
445sub want_built {
446 my $self = shift (@_);
447 my ($index) = @_;
448
449 if (defined ($self->{'collect_cfg'}->{'dontbuild'})) {
450 foreach my $checkstr (@{$self->{'collect_cfg'}->{'dontbuild'}}) {
451 if ($index =~ /^$checkstr$/) {
452 $self->{'notbuilt'}->{$index} = 1;
453 return 0;
454 }
455 }
456 }
457
458 return 1;
459}
460
461sub create_index_mapping {
462 my $self = shift (@_);
463 my ($indexes) = @_;
464
465 print STDERR "create_index_mapping should be implemented in subclass\n";
466 my %mapping = ();
467 return \%mapping;
468}
469
470# returns a processed version of a field.
471# if the field has only one component the processed
472# version will contain the first character and next consonant
473# of that componant - otherwise it will contain the first
474# character of the first two components
475# only uses letdig (\w) characters now
476sub process_field {
477 my $self = shift (@_);
478 my ($field) = @_;
479
480 return "" unless (defined ($field) && $field =~ /\S/);
481
482 my ($a, $b);
483 my @components = split /,/, $field;
484 if (scalar @components >= 2) {
485 # pick the first letdig from the first two field names
486 ($a) = $components[0] =~ /^[^\w]*(\w)/;
487 ($b) = $components[1] =~ /^[^\w]*(\w)/;
488 } else {
489 # pick the first two letdig chars
490 ($a, $b) = $field =~ /^[^\w]*(\w)[^\w]*?(\w)/i;
491 }
492 # there may not have been any letdigs...
493 $a = 'a' unless defined $a;
494 $b = '0' unless defined $b;
495
496 return "$a$b";
497
498}
499
500sub get_next_version {
501 my $self = shift (@_);
502 my ($nameref) = @_;
503 my $num=0;
504 if ($$nameref =~ /(\d\d)$/) {
505 $num = $1; $num ++;
506 $$nameref =~ s/\d\d$/$num/;
507 } elsif ($$nameref =~ /(\d)$/) {
508 $num = $1;
509 if ($num == 9) {$$nameref =~ s/\d$/10/;}
510 else {$num ++; $$nameref =~ s/\d$/$num/;}
511 } else {
512 $$nameref =~ s/.$/0/;
513 }
514}
515
516# implement this in subclass if want to add extra stuff to build.cfg
517sub build_cfg_extra {
518 my $self = shift(@_);
519 my ($build_cfg) = @_;
520
521}
522
523sub write_cfg_file {
524 my $self = shift(@_);
525 my ($build_cfg) = @_;
526
527 # write out the build information
528 &cfgread::write_cfg_file("$self->{'build_dir'}/build.cfg", $build_cfg,
529 '^(builddate|buildtype|numdocs|numbytes|numwords|numsections|maxnumeric|indexstem)$',
530 '^(indexmap|subcollectionmap|languagemap|notbuilt)$');
531
532}
533
534# default is to output an empty [collection] entry
535sub output_collection_meta {
536 my $self = shift(@_);
537 my ($handle) = @_;
538
539 print $handle "[collection]\n". ('-' x 70) . "\n";;
540
541}
542
543sub print_stats {
544 my $self = shift (@_);
545
546 my $outhandle = $self->{'outhandle'};
547 my $indexing_text = $self->{'buildproc'}->get_indexing_text();
548 my $index = $self->{'buildproc'}->get_index();
549 my $num_bytes = $self->{'buildproc'}->get_num_bytes();
550 my $num_processed_bytes = $self->{'buildproc'}->get_num_processed_bytes();
551
552 if ($indexing_text) {
553 print $outhandle "Stats (Creating index $index)\n";
554 } else {
555 print $outhandle "Stats (Compressing text from $index)\n";
556 }
557 print $outhandle "Total bytes in collection: $num_bytes\n";
558 print $outhandle "Total bytes in $index: $num_processed_bytes\n";
559
560 if ($num_processed_bytes < 50 && ($indexing_text || !$self->{'no_text'})) {
561
562 if ($self->{'keepold'}) {
563 if ($num_processed_bytes == 0) {
564 if ($indexing_text) {
565 print $outhandle "No additional text was added to $index\n";
566 } elsif (!$self->{'no_text'}) {
567 print $outhandle "No additional text was compressed\n";
568 }
569 }
570 }
571 else {
572 print $outhandle "***************\n";
573 if ($indexing_text) {
574 print $outhandle "WARNING: There is very little or no text to process for $index\n";
575 } elsif (!$self->{'no_text'}) {
576 print $outhandle "WARNING: There is very little or no text to compress\n";
577 }
578 print $outhandle " Was this your intention?\n";
579 print $outhandle "***************\n";
580 }
581
582 }
583
584}
585
586
5871;
588
Note: See TracBrowser for help on using the repository browser.