source: main/trunk/greenstone2/perllib/mgppbuilder.pm@ 22820

Last change on this file since 22820 was 22820, checked in by davidb, 14 years ago

Need to call 'set_output_handle()' in additional places, to insure the more explict marking of the output stream to be ':utf8' is enforced

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