source: gsdl/trunk/perllib/mgppbuilder.pm@ 15715

Last change on this file since 15715 was 15715, checked in by mdewsnip, 16 years ago

Added "use strict" and fixed various problems that it found.

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