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