source: main/trunk/greenstone2/perllib/basebuilder.pm

Last change on this file was 38299, checked in by davidb, 7 months ago

Away to have site name accessible in builderproc

  • Property svn:keywords set to Author Date Id Revision
File size: 27.2 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 arcinfo;
32use classify;
33use cfgread;
34use colcfg;
35use dbutil;
36use oaiinfo;
37use plugin;
38use util;
39use FileUtils;
40
41
42BEGIN {
43 # set autoflush on for STDERR and STDOUT so that mgpp
44 # doesn't get out of sync with plugins
45 STDOUT->autoflush(1);
46 STDERR->autoflush(1);
47}
48
49END {
50 STDOUT->autoflush(0);
51 STDERR->autoflush(0);
52}
53
54our $maxdocsize = 12000;
55
56# used to signify "gs2"(default) or "gs3"
57our $gs_mode = "gs2";
58
59sub new {
60 my ($class, $site, $collection, $source_dir, $build_dir, $verbosity,
61 $maxdocs, $debug, $keepold, $incremental, $incremental_mode,
62 $remove_empty_classifications,
63 $outhandle, $no_text, $failhandle, $gli) = @_;
64
65 $outhandle = *STDERR unless defined $outhandle;
66 $no_text = 0 unless defined $no_text;
67 $failhandle = *STDERR unless defined $failhandle;
68
69 # create a builder object
70 my $self = bless {'site'=>$site, # will be undef for Greenstone 2
71 'collection'=>$collection,
72 'source_dir'=>$source_dir,
73 'build_dir'=>$build_dir,
74 'verbosity'=>$verbosity,
75 'maxdocs'=>$maxdocs,
76 'debug'=>$debug,
77 'keepold'=>$keepold,
78 'incremental'=>$incremental,
79 'incremental_mode'=>$incremental_mode,
80 'remove_empty_classifications'=>$remove_empty_classifications,
81 'outhandle'=>$outhandle,
82 'no_text'=>$no_text,
83 'failhandle'=>$failhandle,
84 'notbuilt'=>{}, # indexes not built
85 'gli'=>$gli
86 }, $class;
87
88 $self->{'gli'} = 0 unless defined $self->{'gli'};
89
90 # Read in the collection configuration file.
91 if ((defined $site) && ($site ne "")) { # GS3
92 $gs_mode = "gs3";
93 }
94
95 my $colcfgname = &colcfg::get_collect_cfg_name($outhandle, $gs_mode);
96 $self->{'colcfgname'} = $colcfgname;
97 $self->{'collect_cfg'} = &colcfg::read_collection_cfg ($colcfgname, $gs_mode);
98
99 if ($gs_mode eq "gs3") {
100 # read it in again to save the original form for later writing out
101 # of buildConfig.xml
102 # we use this preserve object because $self->{'collect_cfg'}->{'classify'} somewhat gets modified during the calling of &classify::load_classifiers.
103 $self->{'collect_cfg_preserve'} = &colcfg::read_collection_cfg ($colcfgname, $gs_mode);
104 }
105
106 # get the database type for this collection from the collect.cfg file (may be undefined)
107 $self->{'infodbtype'} = $self->{'collect_cfg'}->{'infodbtype'} || &dbutil::get_default_infodb_type();
108
109
110 # load up any dontdb fields
111 $self->{'dontdb'} = {};
112 if (defined ($self->{'collect_cfg'}->{'dontgdbm'})) {
113 foreach my $dg (@{$self->{'collect_cfg'}->{'dontgdbm'}}) {
114 $self->{'dontdb'}->{$dg} = 1;
115 }
116 }
117
118 $self->{'maxnumeric'} = 4;
119 return $self;
120}
121
122# stuff has been moved here from new, so we can use subclass methods
123sub init {
124 my $self = shift(@_);
125
126 my $outhandle = $self->{'outhandle'};
127 my $failhandle = $self->{'failhandle'};
128
129 $self->generate_index_list();
130 my $indexes = $self->{'collect_cfg'}->{'indexes'};
131 if (defined $indexes) {
132 # sort out subcollection indexes
133 if (defined $self->{'collect_cfg'}->{'indexsubcollections'}) {
134 $self->{'collect_cfg'}->{'indexes'} = [];
135 foreach my $subcollection (@{$self->{'collect_cfg'}->{'indexsubcollections'}}) {
136 foreach my $index (@$indexes) {
137 push (@{$self->{'collect_cfg'}->{'indexes'}}, "$index:$subcollection");
138 }
139 }
140 }
141
142 # sort out language subindexes
143 if (defined $self->{'collect_cfg'}->{'languages'}) {
144 $indexes = $self->{'collect_cfg'}->{'indexes'};
145 $self->{'collect_cfg'}->{'indexes'} = [];
146 foreach my $language (@{$self->{'collect_cfg'}->{'languages'}}) {
147 foreach my $index (@$indexes) {
148 if (defined ($self->{'collect_cfg'}->{'indexsubcollections'})) {
149 push (@{$self->{'collect_cfg'}->{'indexes'}}, "$index:$language");
150 }
151 else { # add in an empty subcollection field
152 push (@{$self->{'collect_cfg'}->{'indexes'}}, "$index\:\:$language");
153 }
154 }
155 }
156 }
157 }
158
159 if (defined($self->{'collect_cfg'}->{'indexes'})) {
160 # make sure that the same index isn't specified more than once
161 my %tmphash = ();
162 my @tmparray = @{$self->{'collect_cfg'}->{'indexes'}};
163 $self->{'collect_cfg'}->{'indexes'} = [];
164 foreach my $i (@tmparray) {
165 if (!defined ($tmphash{$i})) {
166 push (@{$self->{'collect_cfg'}->{'indexes'}}, $i);
167 $tmphash{$i} = 1;
168 }
169 }
170 } else {
171 $self->{'collect_cfg'}->{'indexes'} = [];
172 }
173
174
175 # Prepare to work with the <collection>/etc/oai-inf.<db> that keeps track
176 # of the OAI identifiers with their time stamps and deleted status.
177 #
178 # At this stage of working with the oai info db, we don't care whether we have a
179 # manifest or are otherwise incremental, or whether we're doing removeold (full rebuild).
180 # Because we've already dealt with that during the import stage. From here on, we pretend
181 # we're incremental, since the oai info db should just do what archiveinfo contains.
182 # This is because "building is always incremental" where oai info db is concerned.
183
184 my $archivedir = $self->{'source_dir'};
185 my $oai_info = new oaiinfo($self->{'colcfgname'}, $self->{'collect_cfg'}->{'infodbtype'}, $self->{'verbosity'});
186 $oai_info->building_stage_before_indexing($archivedir);
187
188
189 # check incremental against whether builder can cope or not.
190 if ($self->{'incremental'} && !$self->is_incremental_capable()) {
191 print $outhandle "WARNING: The indexer used is not capable of incremental building. Reverting to -removeold\n";
192 $self->{'keepold'} = 0;
193 $self->{'incremental'} = 0;
194 $self->{'incremental_mode'} = "none";
195
196 }
197
198 # gs_version for plugins
199 my $gs_version = "2";
200 if ($gs_mode eq "gs3") {
201 $gs_version = "3";
202 }
203 # get the list of plugins for this collection
204 my $plugins = [];
205 if (defined $self->{'collect_cfg'}->{'plugin'}) {
206 $plugins = $self->{'collect_cfg'}->{'plugin'};
207 }
208
209 # load all the plugins
210
211 #build up the extra global options for the plugins
212 my @global_opts = ();
213 if (defined $self->{'collect_cfg'}->{'separate_cjk'} && $self->{'collect_cfg'}->{'separate_cjk'} =~ /^true$/i) {
214 push @global_opts, "-separate_cjk";
215 }
216 $self->{'pluginfo'} = &plugin::load_plugins ($plugins, $self->{'verbosity'}, $outhandle, $failhandle, \@global_opts, $self->{'incremental_mode'}, $gs_version, $self->{'site'});
217
218 if (scalar(@{$self->{'pluginfo'}}) == 0) {
219 print $outhandle "No plugins were loaded.\n";
220 die "\n";
221 }
222
223 # get the list of classifiers for this collection
224 my $classifiers = [];
225 if (defined $self->{'collect_cfg'}->{'classify'}) {
226 $classifiers = $self->{'collect_cfg'}->{'classify'};
227 }
228
229 # load all the classifiers
230 $self->{'classifiers'} = &classify::load_classifiers ($classifiers, $self->{'build_dir'}, $outhandle);
231
232 # load up the document processor for building
233 # if a buildproc class has been created for this collection, use it
234 # otherwise, use the default buildproc for the builder we are initialising
235 my $buildprocdir = undef;
236 my $buildproctype;
237
238 my $collection = $self->{'collection'};
239 if (-e "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib/custombuildproc.pm") {
240 $buildprocdir = "$ENV{'GSDLCOLLECTDIR'}/custom/${collection}/perllib";
241 $buildproctype = "custombuildproc";
242 } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/custombuildproc.pm") {
243 $buildprocdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
244 $buildproctype = "custombuildproc";
245 } elsif (-e "$ENV{'GSDLCOLLECTDIR'}/perllib/${collection}buildproc.pm") {
246 $buildprocdir = "$ENV{'GSDLCOLLECTDIR'}/perllib";
247 $buildproctype = "${collection}buildproc";
248 } else {
249 $buildproctype = $self->default_buildproc();
250 }
251 if (defined $buildprocdir) {
252 require "$buildprocdir/$buildproctype.pm";
253 }
254 else {
255 require "$buildproctype.pm";
256 }
257
258 eval("\$self->{'buildproc'} = new $buildproctype(\$self->{'collection'}, " .
259 "\$self->{'source_dir'}, \$self->{'build_dir'}, \$self->{'keepold'}, \$self->{'verbosity'}, \$self->{'outhandle'})");
260 die "$@" if $@;
261
262 # We call set_infodbtype() now so the buildproc knows the infodbtype for all phases of the build
263 $self->{'buildproc'}->set_infodbtype($self->{'infodbtype'});
264 $self->{'buildproc'}->set_site($self->{'site'});
265
266 $self->generate_index_options();
267
268 if (!$self->{'debug'} && !$self->{'keepold'}) {
269 # remove any old builds
270 &FileUtils::removeFilesRecursive($self->{'build_dir'});
271 &FileUtils::makeAllDirectories($self->{'build_dir'});
272
273 # make the text directory
274 my $textdir = "$self->{'build_dir'}/text";
275 &FileUtils::makeAllDirectories($textdir);
276 }
277
278 if ($self->{'incremental'}) {
279 # some classes may need to do some additional initialisation
280 $self->init_for_incremental_build();
281 }
282
283}
284
285sub is_incremental_capable
286{
287 # By default we return 'no' as the answer
288 # Safer to assume non-incremental to start with, and then override in
289 # inherited classes that are.
290
291 return 0;
292}
293
294# implement this in subclass if want to do additional initialisation for an
295# incremental build
296sub init_for_incremental_build {
297 my $self = shift (@_);
298}
299
300sub deinit {
301 my $self = shift (@_);
302
303 &plugin::deinit($self->{'pluginfo'},$self->{'buildproc'});
304}
305
306sub generate_index_options {
307 my $self = shift (@_);
308
309 my $separate_cjk = 0;
310
311 my $indexoptions = $self->{'collect_cfg'}->{'indexoptions'};
312 if (defined($indexoptions)) {
313
314 foreach my $option (@$indexoptions) {
315 if ($option =~ /separate_cjk/) {
316 $separate_cjk = 1;
317 }
318 }
319 }
320 # set this for building
321 $self->{'buildproc'}->set_separate_cjk($separate_cjk);
322 # record it for build.cfg
323 $self->{'separate_cjk'} = $separate_cjk;
324}
325
326sub set_sections_index_document_metadata {
327 my $self = shift (@_);
328 my ($index) = @_;
329
330 $self->{'buildproc'}->set_sections_index_document_metadata($index);
331}
332
333sub set_maxnumeric {
334 my $self = shift (@_);
335 my ($maxnumeric) = @_;
336
337 $self->{'maxnumeric'} = $maxnumeric;
338}
339sub set_strip_html {
340 my $self = shift (@_);
341 my ($strip) = @_;
342
343 $self->{'strip_html'} = $strip;
344 $self->{'buildproc'}->set_strip_html($strip);
345}
346
347sub set_store_metadata_coverage {
348 my $self = shift (@_);
349 my ($store_metadata_coverage) = @_;
350
351 $self->{'buildproc'}->set_store_metadata_coverage($store_metadata_coverage);
352}
353
354sub compress_text {
355 my $self = shift (@_);
356 my ($textindex) = @_;
357
358 print STDERR "compress_text() should be implemented in subclass!!";
359 return;
360}
361
362
363sub build_indexes {
364 my $self = shift (@_);
365 my ($indexname) = @_;
366 my $outhandle = $self->{'outhandle'};
367
368 $self->pre_build_indexes();
369
370 my $indexes = [];
371 if (defined $indexname && $indexname =~ /\w/) {
372 push @$indexes, $indexname;
373 } else {
374 $indexes = $self->{'collect_cfg'}->{'indexes'};
375 }
376
377 # create the mapping between the index descriptions
378 # and their directory names (includes subcolls and langs)
379 $self->{'index_mapping'} = $self->create_index_mapping ($indexes);
380
381 # build each of the indexes
382 foreach my $index (@$indexes) {
383 if ($self->want_built($index)) {
384 print $outhandle "\n*** building index $index in subdirectory " .
385 "$self->{'index_mapping'}->{$index}\n" if ($self->{'verbosity'} >= 1);
386 print STDERR "<Stage name='Index' source='$index'>\n" if $self->{'gli'};
387 $self->build_index($index);
388 } else {
389 print $outhandle "\n*** ignoring index $index\n" if ($self->{'verbosity'} >= 1);
390 }
391 }
392
393 $self->post_build_indexes();
394
395}
396
397# implement this in subclass if want to do extra stuff at before building
398# all the indexes
399sub pre_build_indexes {
400 my $self = shift(@_);
401 my ($indexname) = @_; # optional parameter
402}
403
404# implement this in subclass if want to do extra stuff at the end of building
405# all the indexes
406sub post_build_indexes {
407 my $self = shift(@_);
408}
409
410sub build_index {
411 my $self = shift (@_);
412 my ($index) = @_;
413
414 print STDERR "build_index should be implemented in subclass\n";
415 return;
416}
417
418# By default, builders do support make_infodatabase()
419sub supports_make_infodatabase {
420 return 1;
421}
422
423
424sub make_infodatabase {
425 my $self = shift (@_);
426 my $outhandle = $self->{'outhandle'};
427
428 print STDERR "BuildDir: $self->{'build_dir'}\n";
429
430 my $textdir = &FileUtils::filenameConcatenate($self->{'build_dir'}, "text");
431 my $assocdir = &FileUtils::filenameConcatenate($self->{'build_dir'}, "assoc");
432 &FileUtils::makeAllDirectories ($textdir);
433 &FileUtils::makeAllDirectories ($assocdir);
434
435 # Get info database file path
436 my $infodb_type = $self->{'infodbtype'};
437 my $infodb_file_path = &dbutil::get_infodb_file_path($infodb_type, $self->{'collection'}, $textdir);
438
439 print $outhandle "\n*** creating the info database and processing associated files\n"
440 if ($self->{'verbosity'} >= 1);
441 print STDERR "<Stage name='CreateInfoData'>\n" if $self->{'gli'};
442
443 # init all the classifiers
444 &classify::init_classifiers ($self->{'classifiers'});
445
446 my $reconstructed_docs = undef;
447 my $database_recs = undef;
448
449 if ($self->{'incremental'}) {
450 $database_recs = {};
451
452 &dbutil::read_infodb_file($infodb_type, $infodb_file_path, $database_recs);
453 }
454
455
456 # Important (for memory usage reasons) that we obtain the filehandle
457 # here for writing out to the database, rather than after
458 # $reconstructed_docs has been set up (assuming -incremental is on)
459 #
460 # This is because when we open a pipe to txt2db [using open()]
461 # this triggers a fork() followed by exec(). $reconstructed_docs
462 # can get very large, and so if we did the open() after this, it means
463 # the fork creates a clone of the *large* process image which (admittedly)
464 # is then quickly replaced in the execve() with the much smaller image for
465 # 'txt2db'. The trouble is, in that seismic second caused by
466 # the fork(), the system really does need to have all that memory available
467 # even though it isn't ultimately used. The result is an out of memory
468 # error.
469
470 my ($infodb_handle);
471 if ($self->{'debug'}) {
472 $infodb_handle = *STDOUT;
473 }
474 else {
475 $infodb_handle = &dbutil::open_infodb_write_handle($infodb_type, $infodb_file_path);
476 if (!defined($infodb_handle))
477 {
478 print STDERR "<FatalError name='NoRunText2DB'/>\n</Stage>\n" if $self->{'gli'};
479 die "builder::make_infodatabase - couldn't open infodb write handle\n";
480 }
481 }
482
483 # set up the document processor
484
485 $self->{'buildproc'}->set_output_handle ($infodb_handle);
486 $self->{'buildproc'}->set_mode ('infodb');
487 $self->{'buildproc'}->set_assocdir ($assocdir);
488 $self->{'buildproc'}->set_dontdb ($self->{'dontdb'});
489 $self->{'buildproc'}->set_classifiers ($self->{'classifiers'});
490 $self->{'buildproc'}->set_indexing_text (0);
491 $self->{'buildproc'}->set_store_text(1);
492
493 if ($self->{'incremental'}) {
494 # reconstruct doc_obj metadata from database for all docs
495 $reconstructed_docs
496 = &classify::reconstruct_doc_objs_metadata($infodb_type,
497 $infodb_file_path,
498 $database_recs);
499 }
500
501 # make_infodatabase needs full reset even for incremental build
502 # as incremental works by reconstructing all docs from the database and
503 # then adding in the new ones
504 $self->{'buildproc'}->zero_reset();
505
506 $self->{'buildproc'}->{'mdprefix_fields'} = {};
507
508 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
509 "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'},0, $self->{'gli'});
510
511 if ($self->{'incremental'}) {
512 # create flat classify structure, ready for new docs to be added
513 foreach my $doc_obj ( @$reconstructed_docs ) {
514 if (! defined $self->{'buildproc'}->{'dont_process_reconstructed'}->{$doc_obj->get_OID()}) {
515 print $outhandle " Adding reconstructed ", $doc_obj->get_OID(), " into classify structures\n";
516 $self->{'buildproc'}->process($doc_obj,undef);
517 }
518 }
519 }
520 # this has changed to only output collection meta if its
521 # not in the config file
522 $self->output_collection_meta($infodb_handle);
523
524 # output classification information
525 &classify::output_classify_info ($self->{'classifiers'}, $infodb_type, $infodb_handle,
526 $self->{'remove_empty_classifications'},
527 $self->{'gli'});
528
529 # Output classifier reverse lookup, used in incremental deletion
530 ####&classify::print_reverse_lookup($infodb_handle);
531
532 # output doclist
533 my @doc_list = $self->{'buildproc'}->get_doc_list();
534 my $browselist_infodb = { 'hastxt' => [ "0" ],
535 'childtype' => [ "VList" ],
536 'numleafdocs' => [ scalar(@doc_list) ],
537 'thistype' => [ "Invisible" ],
538 'contains' => [ join(";", @doc_list) ] };
539 &dbutil::write_infodb_entry($infodb_type, $infodb_handle, "browselist", $browselist_infodb);
540
541 &dbutil::close_infodb_write_handle($infodb_type, $infodb_handle) if !$self->{'debug'};
542
543 if ($infodb_type eq "gdbm-txtgz") {
544 my $gdb_infodb_file_path = &dbutil::get_infodb_file_path("gdbm", $self->{'collection'}, $textdir);
545 if (-e $gdb_infodb_file_path) {
546 &FileUtils::removeFiles($gdb_infodb_file_path);
547 }
548 }
549 print STDERR "</Stage>\n" if $self->{'gli'};
550}
551
552sub make_auxiliary_files {
553 my $self = shift (@_);
554 my ($index);
555 my $build_cfg = {};
556 # subclasses may have already defined stuff in here
557 if (defined $self->{'build_cfg'}) {
558 $build_cfg = $self->{'build_cfg'};
559 }
560
561 my $outhandle = $self->{'outhandle'};
562
563 print $outhandle "\n*** creating auxiliary files \n" if ($self->{'verbosity'} >= 1);
564 print STDERR "<Stage name='CreatingAuxilary'>\n" if $self->{'gli'};
565
566 # get the text directory
567 &FileUtils::makeAllDirectories ($self->{'build_dir'});
568
569 # store the build date
570 $build_cfg->{'builddate'} = time;
571 $build_cfg->{'buildtype'} = $self->{'buildtype'};
572 $build_cfg->{'indexstem'} = &util::get_dirsep_tail($self->{'collection'});
573 $build_cfg->{'stemindexes'} = $self->{'stemindexes'};
574 if ($self->{'separate_cjk'}) {
575 $build_cfg->{'separate_cjk'} = "true";
576 }
577
578 # store the number of documents and number of bytes
579 $build_cfg->{'numdocs'} = $self->{'buildproc'}->get_num_docs();
580 $build_cfg->{'numsections'} = $self->{'buildproc'}->get_num_sections();
581 $build_cfg->{'numbytes'} = $self->{'buildproc'}->get_num_bytes();
582
583 # store the mapping between the index names and the directory names
584 # the index map is used to determine what indexes there are, so any that are not built should not be put into the map.
585 my @indexmap = ();
586 foreach my $index (@{$self->{'index_mapping'}->{'indexmaporder'}}) {
587 if (not defined ($self->{'notbuilt'}->{$index})) {
588 push (@indexmap, "$index\-\>$self->{'index_mapping'}->{'indexmap'}->{$index}");
589 }
590 }
591
592 # store the number of indexes built to later determine whether search serviceracks get written out to buildConfig.xml
593 $build_cfg->{'num_indexes'} = scalar (@indexmap);
594
595 $build_cfg->{'indexmap'} = \@indexmap if scalar (@indexmap);
596
597 my @subcollectionmap = ();
598 foreach my $subcollection (@{$self->{'index_mapping'}->{'subcollectionmaporder'}}) {
599 push (@subcollectionmap, "$subcollection\-\>" .
600 $self->{'index_mapping'}->{'subcollectionmap'}->{$subcollection});
601 }
602 $build_cfg->{'subcollectionmap'} = \@subcollectionmap if scalar (@subcollectionmap);
603
604 my @languagemap = ();
605 foreach my $language (@{$self->{'index_mapping'}->{'languagemaporder'}}) {
606 push (@languagemap, "$language\-\>" .
607 $self->{'index_mapping'}->{'languagemap'}->{$language});
608 }
609 $build_cfg->{'languagemap'} = \@languagemap if scalar (@languagemap);
610
611 my @notbuilt = ();
612 foreach my $nb (keys %{$self->{'notbuilt'}}) {
613 push (@notbuilt, $nb);
614 }
615 $build_cfg->{'notbuilt'} = \@notbuilt if scalar (@notbuilt);
616
617 $build_cfg->{'maxnumeric'} = $self->{'maxnumeric'};
618
619 $build_cfg->{'infodbtype'} = $self->{'infodbtype'};
620
621 # write out the earliestDatestamp information needed for OAI
622 my $archivedir = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, "archives");
623 if(!-d $archivedir) {
624 $archivedir = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, "export");
625 }
626 my $earliestDatestampFile = &FileUtils::filenameConcatenate($archivedir, "earliestDatestamp");
627 my $earliestDatestamp = 0;
628 if (open(FIN,"<$earliestDatestampFile")) {
629 {
630 # slurp in file as a single line
631 local $/ = undef;
632 $earliestDatestamp = <FIN>;
633 #&unicode::ensure_utf8(\$earliestDatestamp); # turn any high bytes that aren't valid utf-8 into utf-8.
634 }
635 close(FIN);
636 }
637 else {
638 print $outhandle "Warning: unable to read collection's earliestDatestamp from $earliestDatestampFile.\n";
639 print $outhandle "Setting value to 0.\n";
640 }
641 $build_cfg->{'earliestdatestamp'} = $earliestDatestamp;
642
643 $self->build_cfg_extra($build_cfg);
644
645 if ($gs_mode eq "gs2") {
646 &colcfg::write_build_cfg(&FileUtils::filenameConcatenate($self->{'build_dir'},"build.cfg"), $build_cfg);
647 }
648 if ($gs_mode eq "gs3") {
649
650 &colcfg::write_build_cfg_xml(&FileUtils::filenameConcatenate($self->{'build_dir'}, "buildConfig.xml"), $build_cfg, $self->{'collect_cfg_preserve'});
651 }
652
653 print STDERR "</Stage>\n" if $self->{'gli'};
654}
655
656# implement this in subclass if want to add extra stuff to build.cfg
657sub build_cfg_extra {
658 my $self = shift(@_);
659 my ($build_cfg) = @_;
660
661}
662
663
664sub collect_specific {
665 my $self = shift (@_);
666}
667
668sub want_built {
669 my $self = shift (@_);
670 my ($index) = @_;
671
672 if (defined ($self->{'collect_cfg'}->{'dontbuild'})) {
673 foreach my $checkstr (@{$self->{'collect_cfg'}->{'dontbuild'}}) {
674 if ($index =~ /^$checkstr$/) {
675 $self->{'notbuilt'}->{$index} = 1;
676 return 0;
677 }
678 }
679 }
680
681 return 1;
682}
683
684sub create_index_mapping {
685 my $self = shift (@_);
686 my ($indexes) = @_;
687
688 print STDERR "create_index_mapping should be implemented in subclass\n";
689 my %mapping = ();
690 return \%mapping;
691}
692
693# returns a processed version of a field.
694# if the field has only one component the processed
695# version will contain the first character and next consonant
696# of that componant - otherwise it will contain the first
697# character of the first two components
698# only uses letdig (\w) characters now
699sub process_field {
700 my $self = shift (@_);
701 my ($field) = @_;
702
703 return "" unless (defined ($field) && $field =~ /\S/);
704
705 my ($a, $b);
706 my @components = split /,/, $field;
707 if (scalar @components >= 2) {
708 # pick the first letdig from the first two field names
709 ($a) = $components[0] =~ /^[^\w]*(\w)/;
710 ($b) = $components[1] =~ /^[^\w]*(\w)/;
711 } else {
712 # pick the first two letdig chars
713 ($a, $b) = $field =~ /^[^\w]*(\w)[^\w]*?(\w)/i;
714 }
715 # there may not have been any letdigs...
716 $a = 'a' unless defined $a;
717 $b = '0' unless defined $b;
718
719 my $newfield = "$a$b";
720 if ($newfield =~ /^\d\d$/) {
721 # digits only - Greenstone runtime doesn't like this.
722 $newfield = "a$a";
723 }
724 return $newfield;
725
726}
727
728sub get_next_version {
729 my $self = shift (@_);
730 my ($nameref) = @_;
731 my $num=0;
732 if ($$nameref =~ /(\d\d)$/) {
733 $num = $1; $num ++;
734 $$nameref =~ s/\d\d$/$num/;
735 } elsif ($$nameref =~ /(\d)$/) {
736 $num = $1;
737 if ($num == 9) {$$nameref =~ s/\d$/10/;}
738 else {$num ++; $$nameref =~ s/\d$/$num/;}
739 } else {
740 $$nameref =~ s/.$/0/;
741 }
742}
743
744
745
746sub get_collection_meta_sets
747{
748 my $self = shift(@_);
749 my $collection_infodb = shift(@_);
750
751 my $mdprefix_fields = $self->{'buildproc'}->{'mdprefix_fields'};
752 foreach my $prefix (keys %$mdprefix_fields)
753 {
754 push(@{$collection_infodb->{"metadataset"}}, $prefix);
755
756 foreach my $field (keys %{$mdprefix_fields->{$prefix}})
757 {
758 push(@{$collection_infodb->{"metadatalist-$prefix"}}, $field);
759
760 my $val = $mdprefix_fields->{$prefix}->{$field};
761 push(@{$collection_infodb->{"metadatafreq-$prefix-$field"}}, $val);
762 }
763 }
764}
765
766
767# default is to output the metadata sets (prefixes) used in collection
768sub output_collection_meta
769{
770 my $self = shift(@_);
771 my $infodb_handle = shift(@_);
772
773 my %collection_infodb = ();
774 $self->get_collection_meta_sets(\%collection_infodb);
775 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, "collection", \%collection_infodb);
776}
777
778# sometimes we need to read in an existing build.cfg - for example,
779# if doing each stage of building separately, or when doing incremental
780# building
781sub read_build_cfg {
782 my $self = shift(@_);
783
784 my $buildconfigfilename;
785
786 if ($gs_mode eq "gs2") {
787 $buildconfigfilename = "build.cfg";
788 } else {
789 $buildconfigfilename = "buildConfig.xml";
790 }
791
792 my $buildconfigfile = &FileUtils::filenameConcatenate($self->{'build_dir'}, $buildconfigfilename);
793
794 if (!-e $buildconfigfile) {
795 # try the index dir - but do we know where it is?? try here
796 $buildconfigfile = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, "index", $buildconfigfilename);
797 if (!-e $buildconfigfile) {
798 #we cant find a config file - just ignore the field list
799 return undef;
800 }
801 }
802 return &colcfg::read_building_cfg( $buildconfigfile, $gs_mode);
803
804}
805
806sub print_stats {
807 my $self = shift (@_);
808
809 my $outhandle = $self->{'outhandle'};
810 my $indexing_text = $self->{'buildproc'}->get_indexing_text();
811 my $index = $self->{'buildproc'}->get_index();
812 my $num_bytes = $self->{'buildproc'}->get_num_bytes();
813 my $num_processed_bytes = $self->{'buildproc'}->get_num_processed_bytes();
814
815 if ($indexing_text) {
816 print $outhandle "Stats (Creating index $index)\n";
817 } else {
818 print $outhandle "Stats (Compressing text from $index)\n";
819 }
820 print $outhandle "Total bytes in collection: $num_bytes\n";
821 print $outhandle "Total bytes in $index: $num_processed_bytes\n";
822
823 if ($num_processed_bytes < 50 && ($indexing_text || !$self->{'no_text'})) {
824
825 if ($self->{'incremental'}) {
826 if ($num_processed_bytes == 0) {
827 if ($indexing_text) {
828 print $outhandle "No additional text was added to $index\n";
829 } elsif (!$self->{'no_text'}) {
830 print $outhandle "No additional text was compressed\n";
831 }
832 }
833 }
834 else {
835 print $outhandle "***************\n";
836 if ($indexing_text) {
837 print $outhandle "WARNING: There is very little or no text to process for $index\n";
838 } elsif (!$self->{'no_text'}) {
839 print $outhandle "WARNING: There is very little or no text to compress\n";
840 }
841 print $outhandle " Was this your intention?\n";
842 print $outhandle "***************\n";
843 }
844
845 }
846
847}
848
849
8501;
851
Note: See TracBrowser for help on using the repository browser.