root/main/trunk/greenstone2/perllib/mgppbuilder.pm @ 24460

Revision 24460, 28.3 KB (checked in by davidb, 9 years ago)

Code changes to support indexers that are provided through the extension mechanism

  • Property svn:keywords set to Author Date Id Revision
Line 
1###########################################################################
2#
3# mgppbuilder.pm -- MGBuilder object
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 mgppbuilder;
27
28use basebuilder;
29use colcfg;
30use plugin;
31use strict; no strict 'refs';
32use util;
33
34
35sub BEGIN {
36    @mgppbuilder::ISA = ('basebuilder');
37}
38
39
40
41our %level_map = ('document'=>'Doc',
42          'section'=>'Sec',
43          'paragraph'=>'Para',
44          'Doc'=>'_textdocument_',
45          'Sec'=>'_textsection_',
46          'Para'=>'_textparagraph_');
47
48our %wanted_index_files = ('td'=>1,
49               't'=>1,
50               'tl'=>1,
51               'ti'=>1,
52               'idb'=>1,
53               'ib1'=>1,
54               'ib2'=>1,
55               'ib3'=>1,
56               'ib4'=>1,
57               'ib5'=>1,
58               'ib6'=>1,
59               'ib7'=>1,
60               'i'=>1,
61               'il'=>1,
62               'w'=>1,
63               'wa'=>1);
64
65
66my $maxdocsize = $basebuilder::maxdocsize;
67
68sub new {
69    my $class = shift(@_);
70
71    my $self = new basebuilder (@_);
72    $self = bless $self, $class;
73
74    #$self->{'indexfieldmap'} = \%static_indexfield_map;
75
76    # get the levels (Section, Paragraph) for indexing and compression
77    $self->{'levels'} = {};
78    $self->{'levelorder'} = ();
79    if (defined $self->{'collect_cfg'}->{'levels'}) {
80        foreach my $level ( @{$self->{'collect_cfg'}->{'levels'}} ){
81        $level =~ tr/A-Z/a-z/;
82            $self->{'levels'}->{$level} = 1;
83        push (@{$self->{'levelorder'}}, $level);
84        }
85    } else { # default to document
86    $self->{'levels'}->{'document'} = 1;
87    push (@{$self->{'levelorder'}}, 'document');
88    }
89   
90    $self->{'buildtype'} = "mgpp";
91
92    return $self;
93}
94
95sub generate_index_list {
96    my $self  = shift (@_);
97   
98    # sort out the indexes
99    #indexes are specified with spaces, but we put them into one index
100    my $indexes = $self->{'collect_cfg'}->{'indexes'};
101    if (defined $indexes) {
102    $self->{'collect_cfg'}->{'indexes'} = [];
103
104    # remove any ex. from index spec but iff it is the only namespace in the metadata name
105    my @indexes_copy = @$indexes; # make a copy, as 'map' changes entry in array
106    #map { $_ =~ s/(^|,|;)ex\.([^.]+)$/$1$2/; } @indexes_copy; # No. Will replace metanames like flex.Image with fl.Image
107    map { $_ =~ s/(,|;)/$1 /g; } @indexes_copy; # introduce a space after every separator
108    map { $_ =~ s/(^| )ex\.([^.,:]+)(,|;|$)/$1$2$3/g; } @indexes_copy; # replace all <ex.> at start of metanames or <, ex.> when in a comma separated list
109    map { $_ =~ s/(,|:) /$1/g; } @indexes_copy; # remove space introduced after every separator
110    my $single_index = join(';', @indexes_copy).";";
111
112    push (@{$self->{'collect_cfg'}->{'indexes'}}, $single_index);
113    }
114}
115
116sub generate_index_options {
117    my $self = shift (@_);
118
119    $self->SUPER::generate_index_options();
120
121    $self->{'casefold'} = 0;
122    $self->{'stem'} = 0;
123    $self->{'accentfold'} = 0;
124   
125    if (!defined($self->{'collect_cfg'}->{'indexoptions'})) {
126    # just use default options
127    $self->{'casefold'} = 1;
128    $self->{'stem'} = 1;
129    $self->{'accentfold'} = 1;
130    } else {
131    foreach my $option (@{$self->{'collect_cfg'}->{'indexoptions'}}) {
132        if ($option =~ /stem/) {
133        $self->{'stem'} = 1;
134        } elsif ($option =~ /casefold/) {
135        $self->{'casefold'} = 1;
136        } elsif ($option =~ /accentfold/) {
137        $self->{'accentfold'} = 1;
138        }
139    }
140    }
141   
142    # now we record this for the build cfg
143    $self->{'stemindexes'} = 0;
144    if ($self->{'casefold'}) {
145    $self->{'stemindexes'} += 1;
146    }
147    if ($self->{'stem'}) {
148    $self->{'stemindexes'} += 2;
149    }
150    if ($self->{'accentfold'}) {
151    $self->{'stemindexes'} += 4;
152    }
153   
154}
155
156sub default_buildproc {
157    my $self  = shift (@_);
158
159    return "mgppbuildproc";
160}
161
162sub compress_text {
163
164    my $self = shift (@_);
165
166    # we don't do anything if we don't want compressed text
167    return if $self->{'no_text'};
168   
169    my ($textindex) = @_;
170
171    my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
172    my $exe = &util::get_os_exe ();
173    my $mgpp_passes_exe = &util::filename_cat($exedir, "mgpp_passes$exe");
174    my $mgpp_compression_dict_exe = &util::filename_cat($exedir, "mgpp_compression_dict$exe");
175    my $outhandle = $self->{'outhandle'};
176
177    my $maxnumeric = $self->{'maxnumeric'};
178   
179    &util::mk_all_dir (&util::filename_cat($self->{'build_dir'}, "text"));
180
181    my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
182    my $basefilename = &util::filename_cat("text",$collect_tail);
183    my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, $basefilename);
184   
185    my $osextra = "";
186    if ($ENV{'GSDLOS'} =~ /^windows$/i) {
187    $fulltextprefix =~ s@/@\\@g;
188    }
189    else {
190    $osextra = " -d /";
191    }
192
193
194    # define the section names and possibly the doc name for mgpasses
195    # the compressor doesn't need to know about paragraphs - never want to
196    # retrieve them
197   
198    # always use Doc and Sec levels
199    my $mgpp_passes_sections = "-J ". $level_map{"document"} ." -K " . $level_map{"section"} ." ";
200
201    print $outhandle "\n*** creating the compressed text\n" if ($self->{'verbosity'} >= 1);
202    print STDERR "<Stage name='CompressText'>\n" if $self->{'gli'};
203
204    # collect the statistics for the text
205    # -b $maxdocsize sets the maximum document size to be 12 meg
206    print $outhandle "\n    collecting text statistics (mgpp_passes -T1)\n"  if ($self->{'verbosity'} >= 1);
207    print STDERR "<Phase name='CollectTextStats'/>\n" if $self->{'gli'};
208
209    my ($handle);
210    if ($self->{'debug'}) {
211    $handle = *STDOUT;
212    }
213    else {
214    if (!-e "$mgpp_passes_exe" ||
215        !open($handle, "| mgpp_passes$exe  -M $maxnumeric $mgpp_passes_sections -f \"$fulltextprefix\" -T1 $osextra")) {
216        print STDERR "<FatalError name='NoRunMGPasses'>\n</Stage>\n" if $self->{'gli'};
217        die "mgppbuilder::compress_text - couldn't run $mgpp_passes_exe\n";
218    }
219    }
220   
221    my $db_level = "section";
222
223    $self->{'buildproc'}->set_output_handle ($handle);
224    $self->{'buildproc'}->set_mode ('text');
225    $self->{'buildproc'}->set_index ($textindex);
226    $self->{'buildproc'}->set_indexing_text (0);
227    #$self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
228    $self->{'buildproc'}->set_levels ($self->{'levels'});                     
229    $self->{'buildproc'}->set_db_level ($db_level);                       
230    $self->{'buildproc'}->reset();
231    &plugin::begin($self->{'pluginfo'}, $self->{'source_dir'},
232           $self->{'buildproc'}, $self->{'maxdocs'});
233    &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
234           "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
235    &plugin::end($self->{'pluginfo'});
236
237    close ($handle) unless $self->{'debug'};
238
239    $self->print_stats();
240
241    # create the compression dictionary
242    # the compression dictionary is built by assuming the stats are from a seed
243    # dictionary (-S), if a novel word is encountered it is spelled out (-H),
244    # and the resulting dictionary must be less than 5 meg with the most
245    # frequent words being put into the dictionary first (-2 -k 5120)
246    # note: these options are left over from mg version
247    if (!$self->{'debug'}) {
248    print $outhandle "\n    creating the compression dictionary\n"  if ($self->{'verbosity'} >= 1);
249    print STDERR "<Phase name='CreatingCompress'/>\n" if $self->{'gli'};
250    if (!-e "$mgpp_compression_dict_exe") {
251        print STDERR "<FatalError name='NoRunMGCompress'/>\n</Stage>\n" if $self->{'gli'};
252        die "mgppbuilder::compress_text - couldn't run $mgpp_compression_dict_exe\n";
253    }
254    system ("mgpp_compression_dict$exe -f \"$fulltextprefix\" -S -H -2 -k 5120 $osextra");
255
256    if (!$self->{'debug'}) {
257        if (!-e "$mgpp_passes_exe" ||
258        !open ($handle, "| mgpp_passes$exe  -M $maxnumeric $mgpp_passes_sections -f \"$fulltextprefix\" -T2 $osextra")) {
259        print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
260        die "mgppbuilder::compress_text - couldn't run $mgpp_passes_exe\n";
261        }
262    }
263    }
264    else {
265    print STDERR "<Phase name='SkipCreatingComp'/>\n" if $self->{'gli'};
266    }
267
268    $self->{'buildproc'}->set_output_handle ($handle);
269    $self->{'buildproc'}->reset();
270
271    # compress the text
272    print $outhandle "\n    compressing the text (mgpp_passes -T2)\n"  if ($self->{'verbosity'} >= 1);
273    print STDERR "<Phase name='CompressingText'/>\n" if $self->{'gli'};
274
275    &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
276           "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
277    close ($handle) unless $self->{'debug'};
278
279    $self->print_stats();
280    print STDERR "</Stage>\n" if $self->{'gli'};
281}
282
283
284sub post_build_indexes {
285    my $self = shift(@_);
286
287    #define the final field lists
288    $self->make_final_field_list();
289}   
290
291# creates directory names for each of the index descriptions
292sub create_index_mapping {
293    my $self = shift (@_);
294    my ($indexes) = @_;
295
296    my %mapping = ();
297
298    return \%mapping if !(scalar @$indexes);
299
300    $mapping{'indexmaporder'} = [];
301    $mapping{'subcollectionmaporder'} = [];
302    $mapping{'languagemaporder'} = [];
303   
304    # dirnames is used to check for collisions. Start this off
305    # with the manditory directory names
306    my %dirnames = ('text'=>'text',
307            'extra'=>'extra');
308    my %pnames = ('index' => {}, 'subcollection' => {}, 'languages' => {});
309
310    foreach my $index (@$indexes) {
311    my ($fields, $subcollection, $languages) = split (":", $index);
312   
313    # we only ever have one index, and its called 'idx'
314    my $pindex = 'idx';
315   
316    # next comes a processed version of the subcollection if there is one.
317    my $psub = $self->process_field ($subcollection);
318    $psub = lc ($psub);
319
320    # next comes a processed version of the language if there is one.
321    my $plang = $self->process_field ($languages);
322    $plang = lc ($plang);
323
324    my $dirname = $pindex . $psub . $plang;
325
326    # check to be sure all index names are unique
327    while (defined ($dirnames{$dirname})) {
328        $dirname = $self->make_unique (\%pnames, $index, \$pindex, \$psub, \$plang);
329    }
330
331    $mapping{$index} = $dirname;
332
333    # store the mapping orders as well as the maps
334    # also put index, subcollection and language fields into the mapping thing -
335    # (the full index name (eg text:subcol:lang) is not used on
336    # the query page) -these are used for collectionmeta later on
337    if (!defined $mapping{'indexmap'}{"$fields"}) {
338        $mapping{'indexmap'}{"$fields"} = $pindex;
339        push (@{$mapping{'indexmaporder'}}, "$fields");
340        if (!defined $mapping{"$fields"}) {
341        $mapping{"$fields"} = $pindex;
342        }   
343    }
344    if ($psub =~ /\w/ && !defined ($mapping{'subcollectionmap'}{$subcollection})) {
345        $mapping{'subcollectionmap'}{$subcollection} = $psub;
346        push (@{$mapping{'subcollectionmaporder'}}, $subcollection);
347        $mapping{$subcollection} = $psub;
348    }
349    if ($plang =~ /\w/ && !defined ($mapping{'languagemap'}{$languages})) {
350        $mapping{'languagemap'}{$languages} = $plang;
351        push (@{$mapping{'languagemaporder'}}, $languages);
352        $mapping{$languages} = $plang;
353    }
354    $dirnames{$dirname} = $index;
355    $pnames{'index'}->{$pindex} = "$fields";
356    $pnames{'subcollection'}->{$psub} = $subcollection;
357    $pnames{'languages'}->{$plang} = $languages;
358    }
359
360    return \%mapping;
361}
362
363sub make_unique {
364    my $self = shift (@_);
365    my ($namehash, $index, $indexref, $subref, $langref) = @_;
366    my ($fields, $subcollection, $languages) = split (":", $index);
367
368    if ($namehash->{'index'}->{$$indexref} ne "$fields") {
369    $self->get_next_version ($indexref);
370    } elsif ($namehash->{'subcollection'}->{$$subref} ne $subcollection) {
371    $self->get_next_version ($subref);
372    } elsif ($namehash->{'languages'}->{$$langref} ne $languages) {
373    $self->get_next_version ($langref);
374    }
375    return "$$indexref$$subref$$langref";
376}   
377
378
379sub build_index {
380    my $self = shift (@_);
381    my ($index) = @_;
382    my $outhandle = $self->{'outhandle'};
383
384    # get the full index directory path and make sure it exists
385    my $indexdir = $self->{'index_mapping'}->{$index};
386    &util::mk_all_dir (&util::filename_cat($self->{'build_dir'}, $indexdir));
387
388    my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
389    my $fullindexprefix = &util::filename_cat ($self->{'build_dir'},
390                           $indexdir,
391                           $collect_tail);
392    my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, "text",
393                           $collect_tail);
394
395    # get any os specific stuff
396    my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
397
398    my $exe = &util::get_os_exe ();
399    my $mgpp_passes_exe = &util::filename_cat($exedir, "mgpp_passes$exe");
400
401    # define the section names for mgpasses
402    my $mgpp_passes_sections = "-J ". $level_map{"document"} ." -K " . $level_map{"section"} ." ";
403    if ($self->{'levels'}->{'paragraph'}) {
404    $mgpp_passes_sections .= "-K " . $level_map{'paragraph'}. " ";
405    }
406
407    my $mgpp_perf_hash_build_exe =
408    &util::filename_cat($exedir, "mgpp_perf_hash_build$exe");
409    my $mgpp_weights_build_exe =
410    &util::filename_cat ($exedir, "mgpp_weights_build$exe");
411    my $mgpp_invf_dict_exe =
412    &util::filename_cat ($exedir, "mgpp_invf_dict$exe");
413    my $mgpp_stem_idx_exe =
414    &util::filename_cat ($exedir, "mgpp_stem_idx$exe");
415
416    my $maxnumeric = $self->{'maxnumeric'};
417
418    my $osextra = "";
419    if ($ENV{'GSDLOS'} =~ /^windows$/i) {
420    $fullindexprefix =~ s@/@\\@g;
421    } else {
422    $osextra = " -d /";
423    if ($outhandle ne "STDERR") {
424        # so mgpp_passes doesn't print to stderr if we redirect output
425        $osextra .= " 2>/dev/null";
426    }
427    }
428 
429    # get the index expression if this index belongs
430    # to a subcollection
431    my $indexexparr = [];
432    my $langarr = [];
433    # there may be subcollection info, and language info.
434    my ($fields, $subcollection, $language) = split (":", $index);
435    my @subcollections = ();
436    @subcollections = split /,/, $subcollection if (defined $subcollection);
437
438    foreach $subcollection (@subcollections) {
439    if (defined ($self->{'collect_cfg'}->{'subcollection'}->{$subcollection})) {
440        push (@$indexexparr, $self->{'collect_cfg'}->{'subcollection'}->{$subcollection});
441    }
442    }
443   
444    # add expressions for languages if this index belongs to
445    # a language subcollection - only put languages expressions for the
446    # ones we want in the index
447   
448    my @languages = ();
449    my $languagemetadata = "Language";
450    if (defined ($self->{'collect_cfg'}->{'languagemetadata'})) {
451    $languagemetadata = $self->{'collect_cfg'}->{'languagemetadata'};
452    }
453    @languages = split /,/, $language if (defined $language);
454    foreach my $language (@languages) {
455    my $not=0;
456    if ($language =~ s/^\!//) {
457        $not = 1;
458    }
459    if($not) {
460        push (@$langarr, "!$language");
461    } else {
462        push (@$langarr, "$language");
463    }
464    }
465
466    # Build index dictionary. Uses verbatim stem method
467    print $outhandle "\n    creating index dictionary (mgpp_passes -I1)\n"  if ($self->{'verbosity'} >= 1);
468    print STDERR "<Phase name='CreatingIndexDic'/>\n" if $self->{'gli'};
469    my ($handle);
470    if ($self->{'debug'}) {
471    $handle = *STDOUT;
472    }
473    else {
474    if (!-e "$mgpp_passes_exe" ||
475        !open($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fullindexprefix\" -I1 $osextra")) {
476        print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
477        die "mgppbuilder::build_index - couldn't run $mgpp_passes_exe\n";
478    }
479    }
480       
481    # db_level is always section
482    my $db_level = "section";
483
484    # set up the document processr
485    $self->{'buildproc'}->set_output_handle ($handle);
486    $self->{'buildproc'}->set_mode ('text');
487    $self->{'buildproc'}->set_index ($index, $indexexparr);
488    $self->{'buildproc'}->set_index_languages ($languagemetadata, $langarr) if (defined $language);
489    $self->{'buildproc'}->set_indexing_text (1);
490    #$self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
491    $self->{'buildproc'}->set_levels ($self->{'levels'});
492    $self->{'buildproc'}->set_db_level ($db_level);   
493   
494    $self->{'buildproc'}->reset();
495
496    &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
497           "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
498    close ($handle) unless $self->{'debug'};
499
500    $self->print_stats();
501
502    # now we check to see if the required files have been produced - if not we quit building this index so the whole process doesn't crap out.
503    # we check on the .id file - index dictionary
504    my $dict_file = "$fullindexprefix.id";
505    if (!-e $dict_file) {
506    print $outhandle "mgppbuilder::build_index - Couldn't create index $index\n";
507    print STDERR "<Warning name='NoIndex'/>\n</Stage>\n" if $self->{'gli'};
508    $self->{'notbuilt'}->{$index}=1;
509    return;
510    }
511
512    if (!$self->{'debug'}) {
513    # create the perfect hash function
514    if (!-e "$mgpp_perf_hash_build_exe") {
515        print STDERR "<FatalError name='NoRunMGHash'/>\n</Stage>\n" if $self->{'gli'};
516        die "mgppbuilder::build_index - couldn't run $mgpp_perf_hash_build_exe\n";
517    }
518    system ("mgpp_perf_hash_build$exe -f \"$fullindexprefix\" $osextra");
519
520    if (!-e "$mgpp_passes_exe" ||
521        !open ($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fullindexprefix\" -I2 $osextra")) {
522        print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
523        die "mgppbuilder::build_index - couldn't run $mgpp_passes_exe\n";
524    }
525    }
526   
527    # invert the text
528    print $outhandle "\n    inverting the text (mgpp_passes -I2)\n"  if ($self->{'verbosity'} >= 1);
529    print STDERR "<Phase name='InvertingText'/>\n" if $self->{'gli'};
530
531    $self->{'buildproc'}->set_output_handle ($handle);
532    $self->{'buildproc'}->reset();
533
534    &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
535           "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
536
537    $self->print_stats ();
538   
539    if (!$self->{'debug'}) {
540
541    close ($handle);
542   
543    # create the weights file
544    print $outhandle "\n    create the weights file\n"  if ($self->{'verbosity'} >= 1);
545    print STDERR "<Phase name='CreateTheWeights'/>\n" if $self->{'gli'};
546    if (!-e "$mgpp_weights_build_exe") {
547        print STDERR "<FatalError name='NoRunMGWeights'/>\n</Stage>\n" if $self->{'gli'};
548        die "mgppbuilder::build_index - couldn't run $mgpp_weights_build_exe\n";
549    }
550    system ("mgpp_weights_build$exe -f \"$fullindexprefix\" $osextra");
551
552    # create 'on-disk' stemmed dictionary
553    print $outhandle "\n    creating 'on-disk' stemmed dictionary\n"  if ($self->{'verbosity'} >= 1);
554    if (!-e "$mgpp_invf_dict_exe") {
555        print STDERR "<FatalError name='NoRunMGInvf'/>\n</Stage>\n" if $self->{'gli'};
556        die "mgppbuilder::build_index - couldn't run $mgpp_invf_dict_exe\n";
557    }
558    system ("mgpp_invf_dict$exe -f \"$fullindexprefix\" $osextra" );
559
560
561    # creates stem index files for the various stemming methods
562    print $outhandle "\n    creating stem indexes\n"  if ($self->{'verbosity'} >= 1);
563    print STDERR "<Phase name='CreatingStemIndx'/>\n" if $self->{'gli'};
564    if (!-e "$mgpp_stem_idx_exe") {
565        print STDERR "<FatalError name='NoRunMGStem'/>\n</Stage>\n" if $self->{'gli'};
566        die "mgppbuilder::build_index - couldn't run $mgpp_stem_idx_exe\n";
567    }
568    my $accent_folding_enabled = 1;
569    if ($self->{'accentfold'}) {
570        # the first time we do this, we test for accent folding enabled
571        if (system ("mgpp_stem_idx$exe -b 4096 -s4 -f \"$fullindexprefix\" $osextra") == 2) {
572        # accent folding has not been enabled in mgpp
573        $accent_folding_enabled = 0;
574        $self->{'stemindexes'} -= 4;
575        }
576    }
577    if ($self->{'casefold'}) {
578        system ("mgpp_stem_idx$exe -b 4096 -s1 -f \"$fullindexprefix\" $osextra");
579        if ($accent_folding_enabled && $self->{'accentfold'}) {
580        system ("mgpp_stem_idx$exe -b 4096 -s5 -f \"$fullindexprefix\" $osextra");
581        }
582    }
583    if ($self->{'stem'}) {
584        system ("mgpp_stem_idx$exe -b 4096 -s2 -f \"$fullindexprefix\" $osextra");
585        if ($accent_folding_enabled && $self->{'accentfold'}) {
586        system ("mgpp_stem_idx$exe -b 4096 -s6 -f \"$fullindexprefix\" $osextra");
587        }
588    }
589    if ($self->{'casefold'} && $self->{'stem'}) {
590        system ("mgpp_stem_idx$exe -b 4096 -s3 -f \"$fullindexprefix\" $osextra");
591        if ($accent_folding_enabled && $self->{'accentfold'}) {
592        system ("mgpp_stem_idx$exe -b 4096 -s7 -f \"$fullindexprefix\" $osextra");
593        }
594    }
595
596    # remove unwanted files
597    my $tmpdir = &util::filename_cat ($self->{'build_dir'}, $indexdir);
598    opendir (DIR, $tmpdir) || die
599        "mgppbuilder::build_index - couldn't read directory $tmpdir\n";
600    foreach my $file (readdir(DIR)) {
601        next if $file =~ /^\./;
602        my ($suffix) = $file =~ /\.([^\.]+)$/;
603        if (defined $suffix && !defined $wanted_index_files{$suffix}) {
604        # delete it!
605        print $outhandle "deleting $file\n" if $self->{'verbosity'} > 2;
606        #&util::rm (&util::filename_cat ($tmpdir, $file));
607        }
608    }
609    closedir (DIR);
610    }
611    print STDERR "</Stage>\n" if $self->{'gli'};
612}   
613
614
615sub get_collection_meta_indexes
616{
617    my $self = shift(@_);
618    my $collection_infodb = shift(@_);
619
620    # define the indexed field mapping if not already done so
621    # (i.e. if infodb called separately from build_index)
622    if (!defined $self->{'build_cfg'}) {
623    $self->read_final_field_list();
624    }
625
626    # first do the collection meta stuff - everything without a dot
627    my $collmetadefined = 0;
628    my $metadata_entry;
629    if (defined $self->{'collect_cfg'}->{'collectionmeta'}) {
630    $collmetadefined = 1;
631    }
632
633    #add the index field macros to [collection]
634    # eg <TI>Title
635    #    <SU>Subject
636    # these now come from collection meta. if that is not defined, uses the metadata name
637    my $collmeta = "";
638    if (defined $self->{'build_cfg'}->{'indexfields'}) {
639    foreach my $longfield (@{$self->{'build_cfg'}->{'indexfields'}}){
640        my $shortfield = $self->{'buildproc'}->{'indexfieldmap'}->{$longfield};
641        next if $shortfield eq 1;
642       
643        # we need to check if some coll meta has been defined - don't output
644        # any that have
645        $collmeta = ".$longfield";
646        if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{$collmeta}) {
647        if ($longfield eq "allfields") {
648            $collection_infodb->{$shortfield} = [ "_query:textallfields_" ];
649        } elsif ($longfield eq "text") {
650            $collection_infodb->{$shortfield} = [ "_query:texttextonly_" ];
651        } else {
652            $collection_infodb->{$shortfield} = [ $longfield ];
653        }
654        }
655    }
656    }
657
658    # now add the level names
659    my $level_entry = "";
660    foreach my $level (@{$self->{'collect_cfg'}->{'levels'}}) {
661    $collmeta = ".$level"; # based on the original specification
662    $level =~ tr/A-Z/a-z/; # make it lower case
663    my $levelid = $level_map{$level}; # find the actual value we used in the index
664    if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{$collmeta}) {
665        # use the default macro
666        $collection_infodb->{$levelid} = [ $level_map{$levelid} ];
667    }
668    }
669   
670    # now add subcoll meta
671    my $subcoll_entry = "";
672    my $shortname = "";
673    my $one_entry = "";
674    foreach my $subcoll (@{$self->{'index_mapping'}->{'subcollectionmaporder'}}) {
675    $shortname = $self->{'index_mapping'}->{$subcoll};
676    if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{".$subcoll"}) {
677        $collection_infodb->{$shortname} = [ $subcoll ];
678    }
679    }
680
681    # now add language meta
682    my $lang_entry = "";
683    foreach my $lang (@{$self->{'index_mapping'}->{'languagemaporder'}}) {
684    $shortname = $self->{'index_mapping'}->{$lang};
685    if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{".$lang"}) {
686        $collection_infodb->{$shortname} = [ $lang ];
687    }
688    }
689}
690
691
692# default is to output the metadata sets (prefixes) used in collection
693sub output_collection_meta
694{
695    my $self = shift(@_);
696    my $infodb_handle = shift(@_);
697
698    my %collection_infodb = ();
699    $self->get_collection_meta_sets(\%collection_infodb);
700    $self->get_collection_meta_indexes(\%collection_infodb);
701    &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, "collection", \%collection_infodb);
702}
703
704
705# at the end of building, we have an indexfieldmap with all the mappings,
706# plus some extras, and indexmap with any indexes in it that weren't
707# specified in the index definition.  We want to make an ordered list of
708# fields that are indexed, and a list of mappings that are used. This will
709# be used for the build.cfg file, and for collection meta definition we
710# store these in a build.cfg bit
711sub make_final_field_list {
712    my $self = shift (@_);
713   
714    $self->{'build_cfg'} = {};
715   
716    # store the indexfieldmap information
717    my @indexfieldmap = ();
718    my @indexfields = ();
719    my $specifiedfields = {};
720    my @specifiedfieldorder = ();
721
722    # go through the index definition and add each thing to a map, so we
723    # can easily check if it is already specified - when doing the
724    # metadata, we print out all the individual fields, but some may
725    # already be specified in the index definition, so we dont want to add
726    # those again.
727
728    my $field;
729    foreach $field (@{$self->{'collect_cfg'}->{'indexes'}}) {
730    # remove subcoll stuff
731    my $parts = $field;
732    $parts =~ s/:.*$//;
733    # *************
734    my @fs = split(';', $parts);
735    foreach my $f(@fs) {
736        if (!defined $specifiedfields->{$f}) {
737        $specifiedfields->{$f}=1;
738        push (@specifiedfieldorder, "$f");
739        }
740    }
741    }
742   
743    #add all fields bit
744    my $ifm = $self->{'buildproc'}->{'indexfieldmap'};
745   
746    foreach $field (@specifiedfieldorder) {
747    if ($field eq "metadata") {
748        foreach my $newfield (keys %{$self->{'buildproc'}->{'indexfields'}}) {
749        if (!defined $specifiedfields->{$newfield}) {
750            push (@indexfieldmap, "$newfield\-\>$self->{'buildproc'}->{'indexfieldmap'}->{$newfield}");
751            push (@indexfields, "$newfield");
752        }
753        }
754
755    } elsif ($field eq 'text') {
756        push (@indexfieldmap, "text\-\>TX");
757        push (@indexfields, "text");
758    } elsif ($field eq 'allfields') {
759        push (@indexfieldmap, "allfields\-\>ZZ");
760        push (@indexfields, "allfields");
761    } else {
762        # we only add in the ones that have been processed
763        if (defined $ifm->{$field}) {
764        push (@indexfieldmap, "$field\-\>$ifm->{$field}");
765        push (@indexfields, "$field");
766        }
767    }
768    }
769
770    if (scalar @indexfieldmap) {
771    $self->{'build_cfg'}->{'indexfieldmap'} = \@indexfieldmap;
772    }
773
774    if (scalar @indexfields) {
775    $self->{'build_cfg'}->{'indexfields'} = \@indexfields;
776    }
777}
778
779
780# recreate the field list from the build.cfg file, look first in building,
781# then in index to find it. if there is no build.cfg, we can't do the field
782# list (there is unlikely to be any index anyway.)
783sub read_final_field_list {
784    my $self = shift (@_);
785    $self->{'build_cfg'} = {};
786    my @indexfieldmap = ();
787    my @indexfields = ();
788    my @indexmap = ();
789
790    # we read the stuff in from the build.cfg file - if its there
791    my $buildcfg = $self->read_build_cfg();
792    return unless defined $buildcfg;
793
794    my $field;
795    if (defined $buildcfg->{'indexfields'}) {
796    foreach $field (@{$buildcfg->{'indexfields'}}) {
797        push (@indexfields, "$field");
798    }
799    }
800
801    if (defined $buildcfg->{'indexfieldmap'}) {
802    foreach $field (@{$buildcfg->{'indexfieldmap'}}) {
803        push (@indexfieldmap, "$field");
804        my ($f, $v) = $field =~ /^(.*)\-\>(.*)$/;
805        $self->{'buildproc'}->{'indexfieldmap'}->{$f} = $v;
806    }
807    }       
808
809    if (defined $buildcfg->{'indexmap'}) {
810    foreach $field (@{$buildcfg->{'indexmap'}}) {
811        push (@indexmap, "$field");
812    }
813    }       
814
815    $self->{'build_cfg'}->{'indexfieldmap'} = \@indexfieldmap;
816    $self->{'build_cfg'}->{'indexfields'} = \@indexfields;
817    $self->{'build_cfg'}->{'indexmap'} = \@indexmap;
818}
819
820
821sub build_cfg_extra {
822    my $self = shift (@_);
823    my ($build_cfg) = @_;
824
825    $build_cfg->{'numsections'} = $self->{'buildproc'}->get_num_sections();
826   
827    # store the level info
828    my @indexlevels = ();
829    my @levelmap = ();
830    foreach my $l (@{$self->{'levelorder'}}) {
831    push (@indexlevels, $level_map{$l});
832    push (@levelmap, "$l\-\>$level_map{$l}");
833    }
834    $build_cfg->{'indexlevels'} = \@indexlevels;
835    $build_cfg->{'levelmap'} = \@levelmap;
836
837    # text level (and database level) is always section
838    $build_cfg->{'textlevel'} = $level_map{'section'};
839   
840}
841
8421;
843
844
Note: See TracBrowser for help on using the browser.