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

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

Removed a couple of references to gdbm.

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