root/main/trunk/greenstone2/perllib/basebuilder.pm @ 24754

Revision 24754, 25.5 KB (checked in by sjm84, 8 years ago)

Some changes to support the store_metadata_coverage option

  • Property svn:keywords set to Author Date Id Revision
Line 
1###########################################################################
2#
3# basebuilder.pm -- base class for collection builders
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package basebuilder;
27
28use strict;
29no strict 'refs'; # allow filehandles to be variables and viceversa
30
31use classify;
32use cfgread;
33use colcfg;
34use dbutil;
35use plugin;
36use util;
37
38
39BEGIN {
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
46END {
47    STDOUT->autoflush(0);
48    STDERR->autoflush(0);
49}
50
51our $maxdocsize = 12000;
52
53# used to signify "gs2"(default) or "gs3"
54our $gs_mode = "gs2";
55
56sub new {
57    my ($class, $site, $collection, $source_dir, $build_dir, $verbosity,
58    $maxdocs, $debug, $keepold, $incremental, $incremental_mode,
59    $remove_empty_classifications,
60    $outhandle, $no_text, $failhandle, $gli) = @_;
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
67    my $self = bless {'site'=>$site, # will be undef for Greenstone 2
68              'collection'=>$collection,
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,
76              'incremental_mode'=>$incremental_mode,
77              'remove_empty_classifications'=>$remove_empty_classifications,
78              'outhandle'=>$outhandle,
79              'no_text'=>$no_text,
80              'failhandle'=>$failhandle,
81              'notbuilt'=>{},    # indexes not built
82              'gli'=>$gli
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);
90    $self->{'collect_cfg'} = &colcfg::read_collection_cfg ($colcfgname, $gs_mode);
91
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);
97    }
98   
99    # get the database type for this collection from the collect.cfg file (may be undefined)
100    $self->{'infodbtype'} = $self->{'collect_cfg'}->{'infodbtype'} || &dbutil::get_default_infodb_type();
101
102
103    # load up any dontdb fields
104    $self->{'dontdb'} = {};
105    if (defined ($self->{'collect_cfg'}->{'dontgdbm'})) {
106    foreach my $dg (@{$self->{'collect_cfg'}->{'dontgdbm'}}) {
107        $self->{'dontdb'}->{$dg} = 1;
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
116sub init {
117    my $self = shift(@_);
118   
119    my $outhandle = $self->{'outhandle'};
120    my $failhandle = $self->{'failhandle'};
121
122    $self->generate_index_list();
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        }
132        }
133    }
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            }
147        }
148        }
149    }
150    }
151   
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
167    # check incremental against whether builder can cope or not.
168    if ($self->{'incremental'} && !$self->is_incremental_capable()) {
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
206    # load up the document processor for building
207    # if a buildproc class has been created for this collection, use it
208    # otherwise, use the default buildproc for the builder we are initialising
209    my $buildprocdir = undef;
210    my $buildproctype;
211
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    }
225    if (defined $buildprocdir) {
226    require "$buildprocdir/$buildproctype.pm";
227    }
228    else {
229    require "$buildproctype.pm";
230    }
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
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'});
238   
239   $self->generate_index_options();
240
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    }
250
251    if ($self->{'incremental'}) {
252    # some classes may need to do some additional initialisation
253    $self->init_for_incremental_build();
254    }
255   
256}
257
258sub 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
267# implement this in subclass if want to do additional initialisation for an
268# incremental build
269sub init_for_incremental_build {
270    my $self = shift (@_);
271}
272
273sub deinit {
274    my $self = shift (@_);
275   
276    &plugin::deinit($self->{'pluginfo'},$self->{'buildproc'});
277}
278
279sub 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 
297sub set_sections_index_document_metadata {
298    my $self = shift (@_);
299    my ($index) = @_;
300 
301    $self->{'buildproc'}->set_sections_index_document_metadata($index);
302}
303
304sub set_maxnumeric {
305    my $self = shift (@_);
306    my ($maxnumeric) = @_;
307
308    $self->{'maxnumeric'} = $maxnumeric;
309}
310sub 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
318sub 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
325sub 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
334sub build_indexes {
335    my $self = shift (@_);
336    my ($indexname) = @_;
337    my $outhandle = $self->{'outhandle'};
338
339    $self->pre_build_indexes();
340
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
364    $self->post_build_indexes();
365
366}
367
368# implement this in subclass if want to do extra stuff at before building
369# all the indexes
370sub pre_build_indexes {
371    my $self = shift(@_);
372    my ($indexname) = @_; # optional parameter
373}
374
375# implement this in subclass if want to do extra stuff at the end of building
376# all the indexes
377sub post_build_indexes {
378    my $self = shift(@_);   
379}
380
381sub 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
391sub 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
402    # Get info database file path
403    my $infodb_type = $self->{'infodbtype'};
404    my $infodb_file_path = &dbutil::get_infodb_file_path($infodb_type, $self->{'collection'}, $textdir);
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;
414    my $database_recs = undef;
415
416    if ($self->{'incremental'}) {
417    $database_recs = {};
418
419    &dbutil::read_infodb_file($infodb_type, $infodb_file_path, $database_recs);
420    }
421
422   
423    # Important (for memory usage reasons) that we obtain the filehandle
424    # here for writing out to the database, rather than after
425    # $reconstructed_docs has been set up (assuming -incremental is on)
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
432    # 'txt2db'.  The trouble is, in that seismic second caused by
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
437    my ($infodb_handle);
438    if ($self->{'debug'}) {
439    $infodb_handle = *STDOUT;
440    }
441    else {
442    $infodb_handle = &dbutil::open_infodb_write_handle($infodb_type, $infodb_file_path);
443    if (!defined($infodb_handle))
444    {
445        print STDERR "<FatalError name='NoRunText2DB'/>\n</Stage>\n" if $self->{'gli'};
446        die "builder::make_infodatabase - couldn't open infodb write handle\n";
447    }
448    }
449
450    if ($self->{'incremental'}) {
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
460    $self->{'buildproc'}->set_output_handle ($infodb_handle);
461    $self->{'buildproc'}->set_mode ('infodb');
462    $self->{'buildproc'}->set_assocdir ($assocdir);
463    $self->{'buildproc'}->set_dontdb ($self->{'dontdb'});
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
469    # as incremental works by reconstructing all docs from the database and
470    # then adding in the new ones
471    $self->{'buildproc'}->zero_reset();
472
473    $self->{'buildproc'}->{'mdprefix_fields'} = {};
474   
475    &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
476           "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'},0, $self->{'gli'});
477
478    if ($self->{'incremental'}) {
479    # create flat classify structure, ready for new docs to be added
480    foreach my $doc_obj ( @$reconstructed_docs ) {
481        if (! defined $self->{'buildproc'}->{'dont_process_reconstructed'}->{$doc_obj->get_OID()}) {
482        print $outhandle "  Adding reconstructed ", $doc_obj->get_OID(), " into classify structures\n";
483        $self->{'buildproc'}->process($doc_obj,undef);
484        }
485    }
486    }
487    # this has changed to only output collection meta if its
488    # not in the config file
489    $self->output_collection_meta($infodb_handle);
490   
491    # output classification information
492    &classify::output_classify_info ($self->{'classifiers'}, $infodb_type, $infodb_handle,
493                     $self->{'remove_empty_classifications'},
494                     $self->{'gli'});
495
496    # Output classifier reverse lookup, used in incremental deletion
497    ####&classify::print_reverse_lookup($infodb_handle);
498
499    # output doclist
500    my @doc_list = $self->{'buildproc'}->get_doc_list();
501    my $browselist_infodb = { 'hastxt' => [ "0" ],
502                  'childtype' => [ "VList" ],
503                  'numleafdocs' => [ scalar(@doc_list) ],
504                  'thistype' => [ "Invisible" ],
505                  'contains' => [ join(";", @doc_list) ] };
506    &dbutil::write_infodb_entry($infodb_type, $infodb_handle, "browselist", $browselist_infodb);
507
508    &dbutil::close_infodb_write_handle($infodb_type, $infodb_handle) if !$self->{'debug'};
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    }
516    print STDERR "</Stage>\n" if $self->{'gli'};
517}
518
519sub 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'};
539    $build_cfg->{'indexstem'} = &util::get_dirsep_tail($self->{'collection'});
540    $build_cfg->{'stemindexes'} = $self->{'stemindexes'};
541    if ($self->{'separate_cjk'}) {
542    $build_cfg->{'separate_cjk'} = "true";
543    }
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();
549
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
582    $build_cfg->{'infodbtype'} = $self->{'infodbtype'};
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");
588    }
589    my $earliestDatestampFile = &util::filename_cat ($archivedir, "earliestDatestamp");
590    my $earliestDatestamp = 0;
591    if (open(FIN,"<$earliestDatestampFile")) {
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    }
598    close(FIN);
599    }
600    else {
601    print $outhandle "Warning: unable to read collection's earliestDatestamp from $earliestDatestampFile.\n";
602    print $outhandle "Setting value to 0.\n";
603    }
604    $build_cfg->{'earliestdatestamp'} = $earliestDatestamp;
605   
606    $self->build_cfg_extra($build_cfg);
607
608    if ($gs_mode eq "gs2") {
609      &colcfg::write_build_cfg(&util::filename_cat($self->{'build_dir'},"build.cfg"), $build_cfg);
610    }
611    if ($gs_mode eq "gs3") {
612
613      &colcfg::write_build_cfg_xml(&util::filename_cat($self->{'build_dir'}, "buildConfig.xml"), $build_cfg, $self->{'collect_cfg_preserve'});
614    }   
615
616    print STDERR "</Stage>\n" if $self->{'gli'};
617}
618
619# implement this in subclass if want to add extra stuff to build.cfg
620sub build_cfg_extra {
621   my $self = shift(@_);
622   my ($build_cfg) = @_;
623   
624}
625
626
627sub collect_specific {
628    my $self = shift (@_);
629}
630
631sub 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
647sub 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
662sub 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   
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   
689}
690
691sub 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
708
709sub get_collection_meta_sets
710{
711    my $self = shift(@_);
712    my $collection_infodb = shift(@_);
713
714    my $mdprefix_fields = $self->{'buildproc'}->{'mdprefix_fields'};
715    foreach my $prefix (keys %$mdprefix_fields)
716    {   
717    push(@{$collection_infodb->{"metadataset"}}, $prefix);
718
719    foreach my $field (keys %{$mdprefix_fields->{$prefix}})
720    {
721        push(@{$collection_infodb->{"metadatalist-$prefix"}}, $field);
722
723        my $val = $mdprefix_fields->{$prefix}->{$field};
724        push(@{$collection_infodb->{"metadatafreq-$prefix-$field"}}, $val);
725    }
726    }
727}
728
729
730# default is to output the metadata sets (prefixes) used in collection
731sub output_collection_meta
732{
733    my $self = shift(@_);
734    my $infodb_handle = shift(@_);
735
736    my %collection_infodb = ();
737    $self->get_collection_meta_sets(\%collection_infodb);
738    &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, "collection", \%collection_infodb);
739}
740
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
744sub read_build_cfg {
745    my $self = shift(@_);
746
747    my $buildconfigfilename;
748   
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   
757    if (!-e $buildconfigfile) {
758    # try the index dir - but do we know where it is?? try here
759    $buildconfigfile  = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "index", $buildconfigfilename);
760    if (!-e $buildconfigfile) {
761        #we cant find a config file - just ignore the field list
762        return undef;
763    }
764    }
765    return &colcfg::read_building_cfg( $buildconfigfile, $gs_mode);
766   
767}
768
769sub 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   
788    if ($self->{'incremental'}) {
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 
8131;
814
Note: See TracBrowser for help on using the browser.