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

Revision 24404, 28.3 KB (checked in by ak19, 8 years ago)

Changes to perl code to do with removing the ex. prefix: ex. is only removed if it is the sole prefix (i.e. ex.dc.* prefixes are not removed).

  • 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 build_indexes_extra {
285    my $self = shift(@_);
286    #define the final field lists
287    $self->make_final_field_list();
288}   
289
290# creates directory names for each of the index descriptions
291sub create_index_mapping {
292    my $self = shift (@_);
293    my ($indexes) = @_;
294
295    my %mapping = ();
296
297    return \%mapping if !(scalar @$indexes);
298
299    $mapping{'indexmaporder'} = [];
300    $mapping{'subcollectionmaporder'} = [];
301    $mapping{'languagemaporder'} = [];
302   
303    # dirnames is used to check for collisions. Start this off
304    # with the manditory directory names
305    my %dirnames = ('text'=>'text',
306            'extra'=>'extra');
307    my %pnames = ('index' => {}, 'subcollection' => {}, 'languages' => {});
308
309    foreach my $index (@$indexes) {
310    my ($fields, $subcollection, $languages) = split (":", $index);
311   
312    # we only ever have one index, and its called 'idx'
313    my $pindex = 'idx';
314   
315    # next comes a processed version of the subcollection if there is one.
316    my $psub = $self->process_field ($subcollection);
317    $psub = lc ($psub);
318
319    # next comes a processed version of the language if there is one.
320    my $plang = $self->process_field ($languages);
321    $plang = lc ($plang);
322
323    my $dirname = $pindex . $psub . $plang;
324
325    # check to be sure all index names are unique
326    while (defined ($dirnames{$dirname})) {
327        $dirname = $self->make_unique (\%pnames, $index, \$pindex, \$psub, \$plang);
328    }
329
330    $mapping{$index} = $dirname;
331
332    # store the mapping orders as well as the maps
333    # also put index, subcollection and language fields into the mapping thing -
334    # (the full index name (eg text:subcol:lang) is not used on
335    # the query page) -these are used for collectionmeta later on
336    if (!defined $mapping{'indexmap'}{"$fields"}) {
337        $mapping{'indexmap'}{"$fields"} = $pindex;
338        push (@{$mapping{'indexmaporder'}}, "$fields");
339        if (!defined $mapping{"$fields"}) {
340        $mapping{"$fields"} = $pindex;
341        }   
342    }
343    if ($psub =~ /\w/ && !defined ($mapping{'subcollectionmap'}{$subcollection})) {
344        $mapping{'subcollectionmap'}{$subcollection} = $psub;
345        push (@{$mapping{'subcollectionmaporder'}}, $subcollection);
346        $mapping{$subcollection} = $psub;
347    }
348    if ($plang =~ /\w/ && !defined ($mapping{'languagemap'}{$languages})) {
349        $mapping{'languagemap'}{$languages} = $plang;
350        push (@{$mapping{'languagemaporder'}}, $languages);
351        $mapping{$languages} = $plang;
352    }
353    $dirnames{$dirname} = $index;
354    $pnames{'index'}->{$pindex} = "$fields";
355    $pnames{'subcollection'}->{$psub} = $subcollection;
356    $pnames{'languages'}->{$plang} = $languages;
357    }
358
359    return \%mapping;
360}
361
362sub make_unique {
363    my $self = shift (@_);
364    my ($namehash, $index, $indexref, $subref, $langref) = @_;
365    my ($fields, $subcollection, $languages) = split (":", $index);
366
367    if ($namehash->{'index'}->{$$indexref} ne "$fields") {
368    $self->get_next_version ($indexref);
369    } elsif ($namehash->{'subcollection'}->{$$subref} ne $subcollection) {
370    $self->get_next_version ($subref);
371    } elsif ($namehash->{'languages'}->{$$langref} ne $languages) {
372    $self->get_next_version ($langref);
373    }
374    return "$$indexref$$subref$$langref";
375}   
376
377
378sub build_index {
379    my $self = shift (@_);
380    my ($index) = @_;
381    my $outhandle = $self->{'outhandle'};
382
383    # get the full index directory path and make sure it exists
384    my $indexdir = $self->{'index_mapping'}->{$index};
385    &util::mk_all_dir (&util::filename_cat($self->{'build_dir'}, $indexdir));
386
387    my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
388    my $fullindexprefix = &util::filename_cat ($self->{'build_dir'},
389                           $indexdir,
390                           $collect_tail);
391    my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, "text",
392                           $collect_tail);
393
394    # get any os specific stuff
395    my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
396
397    my $exe = &util::get_os_exe ();
398    my $mgpp_passes_exe = &util::filename_cat($exedir, "mgpp_passes$exe");
399
400    # define the section names for mgpasses
401    my $mgpp_passes_sections = "-J ". $level_map{"document"} ." -K " . $level_map{"section"} ." ";
402    if ($self->{'levels'}->{'paragraph'}) {
403    $mgpp_passes_sections .= "-K " . $level_map{'paragraph'}. " ";
404    }
405
406    my $mgpp_perf_hash_build_exe =
407    &util::filename_cat($exedir, "mgpp_perf_hash_build$exe");
408    my $mgpp_weights_build_exe =
409    &util::filename_cat ($exedir, "mgpp_weights_build$exe");
410    my $mgpp_invf_dict_exe =
411    &util::filename_cat ($exedir, "mgpp_invf_dict$exe");
412    my $mgpp_stem_idx_exe =
413    &util::filename_cat ($exedir, "mgpp_stem_idx$exe");
414
415    my $maxnumeric = $self->{'maxnumeric'};
416
417    my $osextra = "";
418    if ($ENV{'GSDLOS'} =~ /^windows$/i) {
419    $fullindexprefix =~ s@/@\\@g;
420    } else {
421    $osextra = " -d /";
422    if ($outhandle ne "STDERR") {
423        # so mgpp_passes doesn't print to stderr if we redirect output
424        $osextra .= " 2>/dev/null";
425    }
426    }
427 
428    # get the index expression if this index belongs
429    # to a subcollection
430    my $indexexparr = [];
431    my $langarr = [];
432    # there may be subcollection info, and language info.
433    my ($fields, $subcollection, $language) = split (":", $index);
434    my @subcollections = ();
435    @subcollections = split /,/, $subcollection if (defined $subcollection);
436
437    foreach $subcollection (@subcollections) {
438    if (defined ($self->{'collect_cfg'}->{'subcollection'}->{$subcollection})) {
439        push (@$indexexparr, $self->{'collect_cfg'}->{'subcollection'}->{$subcollection});
440    }
441    }
442   
443    # add expressions for languages if this index belongs to
444    # a language subcollection - only put languages expressions for the
445    # ones we want in the index
446   
447    my @languages = ();
448    my $languagemetadata = "Language";
449    if (defined ($self->{'collect_cfg'}->{'languagemetadata'})) {
450    $languagemetadata = $self->{'collect_cfg'}->{'languagemetadata'};
451    }
452    @languages = split /,/, $language if (defined $language);
453    foreach my $language (@languages) {
454    my $not=0;
455    if ($language =~ s/^\!//) {
456        $not = 1;
457    }
458    if($not) {
459        push (@$langarr, "!$language");
460    } else {
461        push (@$langarr, "$language");
462    }
463    }
464
465    # Build index dictionary. Uses verbatim stem method
466    print $outhandle "\n    creating index dictionary (mgpp_passes -I1)\n"  if ($self->{'verbosity'} >= 1);
467    print STDERR "<Phase name='CreatingIndexDic'/>\n" if $self->{'gli'};
468    my ($handle);
469    if ($self->{'debug'}) {
470    $handle = *STDOUT;
471    }
472    else {
473    if (!-e "$mgpp_passes_exe" ||
474        !open($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fullindexprefix\" -I1 $osextra")) {
475        print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
476        die "mgppbuilder::build_index - couldn't run $mgpp_passes_exe\n";
477    }
478    }
479       
480    # db_level is always section
481    my $db_level = "section";
482
483    # set up the document processr
484    $self->{'buildproc'}->set_output_handle ($handle);
485    $self->{'buildproc'}->set_mode ('text');
486    $self->{'buildproc'}->set_index ($index, $indexexparr);
487    $self->{'buildproc'}->set_index_languages ($languagemetadata, $langarr) if (defined $language);
488    $self->{'buildproc'}->set_indexing_text (1);
489    #$self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
490    $self->{'buildproc'}->set_levels ($self->{'levels'});
491    $self->{'buildproc'}->set_db_level ($db_level);   
492   
493    $self->{'buildproc'}->reset();
494
495    &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
496           "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
497    close ($handle) unless $self->{'debug'};
498
499    $self->print_stats();
500
501    # 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.
502    # we check on the .id file - index dictionary
503    my $dict_file = "$fullindexprefix.id";
504    if (!-e $dict_file) {
505    print $outhandle "mgppbuilder::build_index - Couldn't create index $index\n";
506    print STDERR "<Warning name='NoIndex'/>\n</Stage>\n" if $self->{'gli'};
507    $self->{'notbuilt'}->{$index}=1;
508    return;
509    }
510
511    if (!$self->{'debug'}) {
512    # create the perfect hash function
513    if (!-e "$mgpp_perf_hash_build_exe") {
514        print STDERR "<FatalError name='NoRunMGHash'/>\n</Stage>\n" if $self->{'gli'};
515        die "mgppbuilder::build_index - couldn't run $mgpp_perf_hash_build_exe\n";
516    }
517    system ("mgpp_perf_hash_build$exe -f \"$fullindexprefix\" $osextra");
518
519    if (!-e "$mgpp_passes_exe" ||
520        !open ($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fullindexprefix\" -I2 $osextra")) {
521        print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
522        die "mgppbuilder::build_index - couldn't run $mgpp_passes_exe\n";
523    }
524    }
525   
526    # invert the text
527    print $outhandle "\n    inverting the text (mgpp_passes -I2)\n"  if ($self->{'verbosity'} >= 1);
528    print STDERR "<Phase name='InvertingText'/>\n" if $self->{'gli'};
529
530    $self->{'buildproc'}->set_output_handle ($handle);
531    $self->{'buildproc'}->reset();
532
533    &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
534           "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
535
536    $self->print_stats ();
537   
538    if (!$self->{'debug'}) {
539
540    close ($handle);
541   
542    # create the weights file
543    print $outhandle "\n    create the weights file\n"  if ($self->{'verbosity'} >= 1);
544    print STDERR "<Phase name='CreateTheWeights'/>\n" if $self->{'gli'};
545    if (!-e "$mgpp_weights_build_exe") {
546        print STDERR "<FatalError name='NoRunMGWeights'/>\n</Stage>\n" if $self->{'gli'};
547        die "mgppbuilder::build_index - couldn't run $mgpp_weights_build_exe\n";
548    }
549    system ("mgpp_weights_build$exe -f \"$fullindexprefix\" $osextra");
550
551    # create 'on-disk' stemmed dictionary
552    print $outhandle "\n    creating 'on-disk' stemmed dictionary\n"  if ($self->{'verbosity'} >= 1);
553    if (!-e "$mgpp_invf_dict_exe") {
554        print STDERR "<FatalError name='NoRunMGInvf'/>\n</Stage>\n" if $self->{'gli'};
555        die "mgppbuilder::build_index - couldn't run $mgpp_invf_dict_exe\n";
556    }
557    system ("mgpp_invf_dict$exe -f \"$fullindexprefix\" $osextra" );
558
559
560    # creates stem index files for the various stemming methods
561    print $outhandle "\n    creating stem indexes\n"  if ($self->{'verbosity'} >= 1);
562    print STDERR "<Phase name='CreatingStemIndx'/>\n" if $self->{'gli'};
563    if (!-e "$mgpp_stem_idx_exe") {
564        print STDERR "<FatalError name='NoRunMGStem'/>\n</Stage>\n" if $self->{'gli'};
565        die "mgppbuilder::build_index - couldn't run $mgpp_stem_idx_exe\n";
566    }
567    my $accent_folding_enabled = 1;
568    if ($self->{'accentfold'}) {
569        # the first time we do this, we test for accent folding enabled
570        if (system ("mgpp_stem_idx$exe -b 4096 -s4 -f \"$fullindexprefix\" $osextra") == 2) {
571        # accent folding has not been enabled in mgpp
572        $accent_folding_enabled = 0;
573        $self->{'stemindexes'} -= 4;
574        }
575    }
576    if ($self->{'casefold'}) {
577        system ("mgpp_stem_idx$exe -b 4096 -s1 -f \"$fullindexprefix\" $osextra");
578        if ($accent_folding_enabled && $self->{'accentfold'}) {
579        system ("mgpp_stem_idx$exe -b 4096 -s5 -f \"$fullindexprefix\" $osextra");
580        }
581    }
582    if ($self->{'stem'}) {
583        system ("mgpp_stem_idx$exe -b 4096 -s2 -f \"$fullindexprefix\" $osextra");
584        if ($accent_folding_enabled && $self->{'accentfold'}) {
585        system ("mgpp_stem_idx$exe -b 4096 -s6 -f \"$fullindexprefix\" $osextra");
586        }
587    }
588    if ($self->{'casefold'} && $self->{'stem'}) {
589        system ("mgpp_stem_idx$exe -b 4096 -s3 -f \"$fullindexprefix\" $osextra");
590        if ($accent_folding_enabled && $self->{'accentfold'}) {
591        system ("mgpp_stem_idx$exe -b 4096 -s7 -f \"$fullindexprefix\" $osextra");
592        }
593    }
594
595    # remove unwanted files
596    my $tmpdir = &util::filename_cat ($self->{'build_dir'}, $indexdir);
597    opendir (DIR, $tmpdir) || die
598        "mgppbuilder::build_index - couldn't read directory $tmpdir\n";
599    foreach my $file (readdir(DIR)) {
600        next if $file =~ /^\./;
601        my ($suffix) = $file =~ /\.([^\.]+)$/;
602        if (defined $suffix && !defined $wanted_index_files{$suffix}) {
603        # delete it!
604        print $outhandle "deleting $file\n" if $self->{'verbosity'} > 2;
605        #&util::rm (&util::filename_cat ($tmpdir, $file));
606        }
607    }
608    closedir (DIR);
609    }
610    print STDERR "</Stage>\n" if $self->{'gli'};
611}   
612
613
614sub get_collection_meta_indexes
615{
616    my $self = shift(@_);
617    my $collection_infodb = shift(@_);
618
619    # define the indexed field mapping if not already done so (ie if infodb called separately from build_index)
620    if (!defined $self->{'build_cfg'}) {
621    $self->read_final_field_list();
622    }
623
624    # first do the collection meta stuff - everything without a dot
625    my $collmetadefined = 0;
626    my $metadata_entry;
627    if (defined $self->{'collect_cfg'}->{'collectionmeta'}) {
628    $collmetadefined = 1;
629    }
630
631    #add the index field macros to [collection]
632    # eg <TI>Title
633    #    <SU>Subject
634    # these now come from collection meta. if that is not defined, uses the metadata name
635    my $collmeta = "";
636    if (defined $self->{'build_cfg'}->{'indexfields'}) {
637    foreach my $longfield (@{$self->{'build_cfg'}->{'indexfields'}}){
638        my $shortfield = $self->{'buildproc'}->{'indexfieldmap'}->{$longfield};
639        next if $shortfield eq 1;
640       
641        # we need to check if some coll meta has been defined - don't output
642        # any that have
643        $collmeta = ".$longfield";
644        if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{$collmeta}) {
645        if ($longfield eq "allfields") {
646            $collection_infodb->{$shortfield} = [ "_query:textallfields_" ];
647        } elsif ($longfield eq "text") {
648            $collection_infodb->{$shortfield} = [ "_query:texttextonly_" ];
649        } else {
650            $collection_infodb->{$shortfield} = [ $longfield ];
651        }
652        }
653    }
654    }
655
656    # now add the level names
657    my $level_entry = "";
658    foreach my $level (@{$self->{'collect_cfg'}->{'levels'}}) {
659    $collmeta = ".$level"; # based on the original specification
660    $level =~ tr/A-Z/a-z/; # make it lower case
661    my $levelid = $level_map{$level}; # find the actual value we used in the index
662    if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{$collmeta}) {
663        # use the default macro
664        $collection_infodb->{$levelid} = [ $level_map{$levelid} ];
665    }
666    }
667   
668    # now add subcoll meta
669    my $subcoll_entry = "";
670    my $shortname = "";
671    my $one_entry = "";
672    foreach my $subcoll (@{$self->{'index_mapping'}->{'subcollectionmaporder'}}) {
673    $shortname = $self->{'index_mapping'}->{$subcoll};
674    if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{".$subcoll"}) {
675        $collection_infodb->{$shortname} = [ $subcoll ];
676    }
677    }
678
679    # now add language meta
680    my $lang_entry = "";
681    foreach my $lang (@{$self->{'index_mapping'}->{'languagemaporder'}}) {
682    $shortname = $self->{'index_mapping'}->{$lang};
683    if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{".$lang"}) {
684        $collection_infodb->{$shortname} = [ $lang ];
685    }
686    }
687}
688
689
690# default is to output the metadata sets (prefixes) used in collection
691sub output_collection_meta
692{
693    my $self = shift(@_);
694    my $infodb_handle = shift(@_);
695
696    my %collection_infodb = ();
697    $self->get_collection_meta_sets(\%collection_infodb);
698    $self->get_collection_meta_indexes(\%collection_infodb);
699    &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, "collection", \%collection_infodb);
700}
701
702
703# at the end of building, we have an indexfieldmap with all the mappings,
704# plus some extras, and indexmap with any indexes in it that weren't
705# specified in the index definition.  we want to make an ordered list of
706# fields that are indexed, and a list of mappings that are used. this will
707# be used for the build.cfg file, and for collection meta definition we
708# store these in a build.cfg bit
709sub make_final_field_list {
710    my $self = shift (@_);
711   
712    $self->{'build_cfg'} = {};
713   
714    # store the indexfieldmap information
715    my @indexfieldmap = ();
716    my @indexfields = ();
717    my $specifiedfields = {};
718    my @specifiedfieldorder = ();
719
720    # go through the index definition and add each thing to a map, so we
721    # can easily check if it is already specified - when doing the
722    # metadata, we print out all the individual fields, but some may
723    # already be specified in the index definition, so we dont want to add
724    # those again.
725
726    my $field;
727    foreach $field (@{$self->{'collect_cfg'}->{'indexes'}}) {
728    # remove subcoll stuff
729    my $parts = $field;
730    $parts =~ s/:.*$//;
731    # *************
732    my @fs = split(';', $parts);
733    foreach my $f(@fs) {
734        if (!defined $specifiedfields->{$f}) {
735        $specifiedfields->{$f}=1;
736        push (@specifiedfieldorder, "$f");
737        }
738    }
739    }
740   
741    #add all fields bit
742    my $ifm = $self->{'buildproc'}->{'indexfieldmap'};
743   
744    foreach $field (@specifiedfieldorder) {
745    if ($field eq "metadata") {
746        foreach my $newfield (keys %{$self->{'buildproc'}->{'indexfields'}}) {
747        if (!defined $specifiedfields->{$newfield}) {
748            push (@indexfieldmap, "$newfield\-\>$self->{'buildproc'}->{'indexfieldmap'}->{$newfield}");
749            push (@indexfields, "$newfield");
750        }
751        }
752
753    } elsif ($field eq 'text') {
754        push (@indexfieldmap, "text\-\>TX");
755        push (@indexfields, "text");
756    } elsif ($field eq 'allfields') {
757        push (@indexfieldmap, "allfields\-\>ZZ");
758        push (@indexfields, "allfields");
759    } else {
760        # we only add in the ones that have been processed
761        if (defined $ifm->{$field}) {
762        push (@indexfieldmap, "$field\-\>$ifm->{$field}");
763        push (@indexfields, "$field");
764        }
765
766       
767    }
768    }
769
770    if (scalar @indexfieldmap) {
771    $self->{'build_cfg'}->{'indexfieldmap'} = \@indexfieldmap;
772    }
773    if (scalar @indexfields) {
774    $self->{'build_cfg'}->{'indexfields'} = \@indexfields;
775    }
776}
777
778
779# recreate the field list from the build.cfg file, look first in building,
780# then in index to find it. if there is no build.cfg, we can't do the field
781# list (there is unlikely to be any index anyway.)
782sub read_final_field_list {
783    my $self = shift (@_);
784    $self->{'build_cfg'} = {};
785    my @indexfieldmap = ();
786    my @indexfields = ();
787    my @indexmap = ();
788
789    # we read the stuff in from the build.cfg file - if its there
790    my $buildcfg = $self->read_build_cfg();
791    return unless defined $buildcfg;
792
793    my $field;
794    if (defined $buildcfg->{'indexfields'}) {
795    foreach $field (@{$buildcfg->{'indexfields'}}) {
796        push (@indexfields, "$field");
797    }
798    }
799
800    if (defined $buildcfg->{'indexfieldmap'}) {
801    foreach $field (@{$buildcfg->{'indexfieldmap'}}) {
802        push (@indexfieldmap, "$field");
803        my ($f, $v) = $field =~ /^(.*)\-\>(.*)$/;
804        $self->{'buildproc'}->{'indexfieldmap'}->{$f} = $v;
805    }
806    }       
807
808    if (defined $buildcfg->{'indexmap'}) {
809    foreach $field (@{$buildcfg->{'indexmap'}}) {
810        push (@indexmap, "$field");
811    }
812    }       
813
814    $self->{'build_cfg'}->{'indexfieldmap'} = \@indexfieldmap;
815    $self->{'build_cfg'}->{'indexfields'} = \@indexfields;
816    $self->{'build_cfg'}->{'indexmap'} = \@indexmap;
817}
818
819
820sub build_cfg_extra {
821    my $self = shift (@_);
822    my ($build_cfg) = @_;
823
824    $build_cfg->{'numsections'} = $self->{'buildproc'}->get_num_sections();
825   
826    # store the level info
827    my @indexlevels = ();
828    my @levelmap = ();
829    foreach my $l (@{$self->{'levelorder'}}) {
830    push (@indexlevels, $level_map{$l});
831    push (@levelmap, "$l\-\>$level_map{$l}");
832    }
833    $build_cfg->{'indexlevels'} = \@indexlevels;
834    $build_cfg->{'levelmap'} = \@levelmap;
835
836    # text level (and database level) is always section
837    $build_cfg->{'textlevel'} = $level_map{'section'};
838   
839}
840
8411;
842
843
Note: See TracBrowser for help on using the browser.