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

Last change on this file since 15073 was 15003, checked in by davidb, 16 years ago

With the new ability to group collections within a top-level collection, import.pl and buildcol.pl commands can now be given collection names such as "mygroup/demo". Some of the building code -- mostly involved with generating the index files and GDBM files -- had to be updated to correctly handle such compound collection names

  • Property svn:keywords set to Author Date Id Revision
File size: 28.6 KB
Line 
1###########################################################################
2#
3# mgppbuilder.pm -- MGBuilder object
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package mgppbuilder;
27
28use basebuilder;
29use classify;
30use cfgread;
31use colcfg;
32use plugin;
33use util;
34use FileHandle;
35
36sub BEGIN {
37 @mgppbuilder::ISA = ('basebuilder');
38}
39
40
41
42our %level_map = ('document'=>'Doc',
43 'section'=>'Sec',
44 'paragraph'=>'Para',
45 'Doc'=>'_textdocument_',
46 'Sec'=>'_textsection_',
47 'Para'=>'_textparagraph_');
48
49our %wanted_index_files = ('td'=>1,
50 't'=>1,
51 'tl'=>1,
52 'ti'=>1,
53 'idb'=>1,
54 'ib1'=>1,
55 'ib2'=>1,
56 'ib3'=>1,
57 'ib4'=>1,
58 'ib5'=>1,
59 'ib6'=>1,
60 'ib7'=>1,
61 'i'=>1,
62 'il'=>1,
63 'w'=>1,
64 'wa'=>1);
65
66# change this so a user can add their own ones in via a file or cfg
67#add AND, OR, NOT NEAR to this list - these cannot be used as field names
68#also add the level names (Doc, Sec, Para)
69our %static_indexfield_map = ('Title'=>'TI',
70 'TI'=>1,
71 'Subject'=>'SU',
72 'SU'=>1,
73 'Creator'=>'CR',
74 'CR'=>1,
75 'Organization'=>'ORG',
76 'ORG'=>1,
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,
87 'Coverage'=>'CO',
88 'CO'=>1,
89 'allfields'=>'ZZ',
90 'ZZ'=>1,
91 'text'=>'TX',
92 'TX'=>1,
93 'AND'=>1,
94 'OR'=>1,
95 'NOT'=>1,
96 'NEAR'=>1,
97 'Doc'=>1,
98 'Sec'=>1,
99 'Para'=>1);
100
101my $maxdocsize = $basebuilder::maxdocsize;
102
103sub new {
104 my $class = shift(@_);
105
106 my $self = new basebuilder (@_);
107 $self = bless $self, $class;
108
109 $self->{'indexfieldmap'} = \%static_indexfield_map;
110
111 # get the levels (Section, Paragraph) for indexing and compression
112 $self->{'levels'} = {};
113 $self->{'levelorder'} = ();
114 if (defined $self->{'collect_cfg'}->{'levels'}) {
115 foreach my $level ( @{$self->{'collect_cfg'}->{'levels'}} ){
116 $level =~ tr/A-Z/a-z/;
117 $self->{'levels'}->{$level} = 1;
118 push (@{$self->{'levelorder'}}, $level);
119 }
120 } else { # default to document
121 $self->{'levels'}->{'document'} = 1;
122 push (@{$self->{'levelorder'}}, 'document');
123 }
124
125 $self->{'buildtype'} = "mgpp";
126
127 return $self;
128}
129
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'} = [];
137 push (@{$self->{'collect_cfg'}->{'indexes'}}, join(';', @$indexes).";");
138}
139
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 }
175
176}
177
178sub default_buildproc {
179 my $self = shift (@_);
180
181 return "mgppbuildproc";
182}
183
184sub compress_text {
185
186 my $self = shift (@_);
187
188 # we don't do anything if we don't want compressed text
189 return if $self->{'no_text'};
190
191 my ($textindex) = @_;
192
193 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
194 my $exe = &util::get_os_exe ();
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");
197 my $outhandle = $self->{'outhandle'};
198
199 my $maxnumeric = $self->{'maxnumeric'};
200
201 &util::mk_all_dir (&util::filename_cat($self->{'build_dir'}, "text"));
202
203 my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
204 my $basefilename = &util::filename_cat("text",$collect_tail);
205 my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, $basefilename);
206
207 my $osextra = "";
208 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
209 $fulltextprefix =~ s@/@\\@g;
210 }
211 else {
212 $osextra = " -d /";
213 }
214
215
216 # define the section names and possibly the doc name for mgpasses
217 # the compressor doesn't need to know about paragraphs - never want to
218 # retrieve them
219
220 # always use Doc and Sec levels
221 my $mgpp_passes_sections = "-J ". $level_map{"document"} ." -K " . $level_map{"section"} ." ";
222
223 print $outhandle "\n*** creating the compressed text\n" if ($self->{'verbosity'} >= 1);
224 print STDERR "<Stage name='CompressText'>\n" if $self->{'gli'};
225
226 # collect the statistics for the text
227 # -b $maxdocsize sets the maximum document size to be 12 meg
228 print $outhandle "\n collecting text statistics (mgpp_passes -T1)\n" if ($self->{'verbosity'} >= 1);
229 print STDERR "<Phase name='CollectTextStats'/>\n" if $self->{'gli'};
230
231 my ($handle);
232 if ($self->{'debug'}) {
233 $handle = STDOUT;
234 } else {
235 if (!-e "$mgpp_passes_exe" ||
236 !open (PIPEOUT, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fulltextprefix\" -T1 $osextra")) {
237 print STDERR "<FatalError name='NoRunMGPasses'>\n</Stage>\n" if $self->{'gli'};
238 die "mgppbuilder::compress_text - couldn't run $mgpp_passes_exe\n";
239 }
240 $handle = mgppbuilder::PIPEOUT;
241 }
242
243 my $gdbm_level = "section";
244
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);
249 $self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
250 $self->{'buildproc'}->set_levels ($self->{'levels'});
251 $self->{'buildproc'}->set_gdbm_level ($gdbm_level);
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'},
256 "", {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
257 &plugin::end($self->{'pluginfo'});
258 close (PIPEOUT);
259
260 close ($handle) unless $self->{'debug'};
261
262 $self->print_stats();
263
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)
269 # note: these options are left over from mg version
270 if (!$self->{'debug'}) {
271 print $outhandle "\n creating the compression dictionary\n" if ($self->{'verbosity'} >= 1);
272 print STDERR "<Phase name='CreatingCompress'/>\n" if $self->{'gli'};
273 if (!-e "$mgpp_compression_dict_exe") {
274 print STDERR "<FatalError name='NoRunMGCompress'/>\n</Stage>\n" if $self->{'gli'};
275 die "mgppbuilder::compress_text - couldn't run $mgpp_compression_dict_exe\n";
276 }
277 system ("mgpp_compression_dict$exe -f \"$fulltextprefix\" -S -H -2 -k 5120 $osextra");
278
279 if (!$self->{'debug'}) {
280 if (!-e "$mgpp_passes_exe" ||
281 !open ($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fulltextprefix\" -T2 $osextra")) {
282 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
283 die "mgppbuilder::compress_text - couldn't run $mgpp_passes_exe\n";
284 }
285 }
286 }
287 else {
288 print STDERR "<Phase name='SkipCreatingComp'/>\n" if $self->{'gli'};
289 }
290
291 $self->{'buildproc'}->reset();
292 # compress the text
293 print $outhandle "\n compressing the text (mgpp_passes -T2)\n" if ($self->{'verbosity'} >= 1);
294 print STDERR "<Phase name='CompressingText'/>\n" if $self->{'gli'};
295
296 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
297 "", {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
298 close ($handle) unless $self->{'debug'};
299
300 $self->print_stats();
301 print STDERR "</Stage>\n" if $self->{'gli'};
302}
303
304
305sub build_indexes_extra {
306 my $self = shift(@_);
307 #define the final field lists
308 $self->make_final_field_list();
309}
310
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 = ();
317
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');
326 my %pnames = ('index' => {}, 'subcollection' => {}, 'languages' => {});
327
328 foreach my $index (@$indexes) {
329 my ($fields, $subcollection, $languages) = split (":", $index);
330
331 # we only ever have one index, and its called 'idx'
332 my $pindex = 'idx';
333
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
349 $mapping{$index} = $dirname;
350
351 # store the mapping orders as well as the maps
352 # also put index, subcollection and language fields into the mapping thing -
353 # (the full index name (eg text:subcol:lang) is not used on
354 # the query page) -these are used for collectionmeta later on
355 if (!defined $mapping{'indexmap'}{"$fields"}) {
356 $mapping{'indexmap'}{"$fields"} = $pindex;
357 push (@{$mapping{'indexmaporder'}}, "$fields");
358 if (!defined $mapping{"$fields"}) {
359 $mapping{"$fields"} = $pindex;
360 }
361 }
362 if ($psub =~ /\w/ && !defined ($mapping{'subcollectionmap'}{$subcollection})) {
363 $mapping{'subcollectionmap'}{$subcollection} = $psub;
364 push (@{$mapping{'subcollectionmaporder'}}, $subcollection);
365 $mapping{$subcollection} = $psub;
366 }
367 if ($plang =~ /\w/ && !defined ($mapping{'languagemap'}{$languages})) {
368 $mapping{'languagemap'}{$languages} = $plang;
369 push (@{$mapping{'languagemaporder'}}, $languages);
370 $mapping{$languages} = $plang;
371 }
372 $dirnames{$dirname} = $index;
373 $pnames{'index'}->{$pindex} = "$fields";
374 $pnames{'subcollection'}->{$psub} = $subcollection;
375 $pnames{'languages'}->{$plang} = $languages;
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) = @_;
400 my $outhandle = $self->{'outhandle'};
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));
405
406 my $collect_tail = &util::get_dirsep_tail($self->{'collection'});
407 my $fullindexprefix = &util::filename_cat ($self->{'build_dir'},
408 $indexdir,
409 $collect_tail);
410 my $fulltextprefix = &util::filename_cat ($self->{'build_dir'}, "text",
411 $collect_tail);
412
413 # get any os specific stuff
414 my $exedir = "$ENV{'GSDLHOME'}/bin/$ENV{'GSDLOS'}";
415
416 my $exe = &util::get_os_exe ();
417 my $mgpp_passes_exe = &util::filename_cat($exedir, "mgpp_passes$exe");
418
419 # define the section names for mgpasses
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'}. " ";
423 }
424
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");
433
434 my $maxnumeric = $self->{'maxnumeric'};
435
436 my $osextra = "";
437 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
438 $fullindexprefix =~ s@/@\\@g;
439 } else {
440 $osextra = " -d /";
441 if ($outhandle ne "STDERR") {
442 # so mgpp_passes doesn't print to stderr if we redirect output
443 $osextra .= " 2>/dev/null";
444 }
445 }
446
447 # get the index expression if this index belongs
448 # to a subcollection
449 my $indexexparr = [];
450 my $langarr = [];
451 # there may be subcollection info, and language info.
452 my ($fields, $subcollection, $language) = split (":", $index);
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
463 # a language subcollection - only put languages expressions for the
464 # ones we want in the index
465
466 my @languages = ();
467 my $language_metadata = "Language";
468 if (defined ($self->{'collect_cfg'}->{'language_metadata'})) {
469 $language_metadata = $self->{'collect_cfg'}->{'language_metadata'};
470 }
471 @languages = split /,/, $language if (defined $language);
472 foreach my $language (@languages) {
473 my $not=0;
474 if ($language =~ s/^\!//) {
475 $not = 1;
476 }
477 if($not) {
478 push (@$langarr, "!$language");
479 } else {
480 push (@$langarr, "$language");
481 }
482 }
483
484 # Build index dictionary. Uses verbatim stem method
485 print $outhandle "\n creating index dictionary (mgpp_passes -I1)\n" if ($self->{'verbosity'} >= 1);
486 print STDERR "<Phase name='CreatingIndexDic'/>\n" if $self->{'gli'};
487 my ($handle);
488 if ($self->{'debug'}) {
489 $handle = STDOUT;
490 } else {
491 if (!-e "$mgpp_passes_exe" ||
492 !open (PIPEOUT, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fullindexprefix\" -I1 $osextra")) {
493 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
494 die "mgppbuilder::build_index - couldn't run $mgpp_passes_exe\n";
495 }
496 $handle = mgppbuilder::PIPEOUT;
497 }
498
499 # gdbm_level is always section
500 my $gdbm_level = "section";
501
502 # set up the document processr
503 $self->{'buildproc'}->set_output_handle ($handle);
504 $self->{'buildproc'}->set_mode ('text');
505 $self->{'buildproc'}->set_index ($index, $indexexparr);
506 $self->{'buildproc'}->set_index_languages ($language_metadata, $langarr) if (defined $language);
507 $self->{'buildproc'}->set_indexing_text (1);
508 $self->{'buildproc'}->set_indexfieldmap ($self->{'indexfieldmap'});
509 $self->{'buildproc'}->set_levels ($self->{'levels'});
510 $self->{'buildproc'}->set_gdbm_level ($gdbm_level);
511
512 $self->{'buildproc'}->reset();
513 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
514 "", {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
515 close ($handle) unless $self->{'debug'};
516
517 $self->print_stats();
518
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";
524 print STDERR "<Warning name='NoIndex'/>\n</Stage>\n" if $self->{'gli'};
525 $self->{'notbuilt'}->{$index}=1;
526 return;
527 }
528
529 if (!$self->{'debug'}) {
530 # create the perfect hash function
531 if (!-e "$mgpp_perf_hash_build_exe") {
532 print STDERR "<FatalError name='NoRunMGHash'/>\n</Stage>\n" if $self->{'gli'};
533 die "mgppbuilder::build_index - couldn't run $mgpp_perf_hash_build_exe\n";
534 }
535 system ("mgpp_perf_hash_build$exe -f \"$fullindexprefix\" $osextra");
536
537 if (!-e "$mgpp_passes_exe" ||
538 !open ($handle, "| mgpp_passes$exe -M $maxnumeric $mgpp_passes_sections -f \"$fullindexprefix\" -I2 $osextra")) {
539 print STDERR "<FatalError name='NoRunMGPasses'/>\n</Stage>\n" if $self->{'gli'};
540 die "mgppbuilder::build_index - couldn't run $mgpp_passes_exe\n";
541 }
542 }
543
544 # invert the text
545 print $outhandle "\n inverting the text (mgpp_passes -I2)\n" if ($self->{'verbosity'} >= 1);
546 print STDERR "<Phase name='InvertingText'/>\n" if $self->{'gli'};
547 $self->{'buildproc'}->reset();
548 &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
549 "", {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
550
551 $self->print_stats ();
552
553 if (!$self->{'debug'}) {
554
555 close ($handle);
556
557 # create the weights file
558 print $outhandle "\n create the weights file\n" if ($self->{'verbosity'} >= 1);
559 print STDERR "<Phase name='CreateTheWeights'/>\n" if $self->{'gli'};
560 if (!-e "$mgpp_weights_build_exe") {
561 print STDERR "<FatalError name='NoRunMGWeights'/>\n</Stage>\n" if $self->{'gli'};
562 die "mgppbuilder::build_index - couldn't run $mgpp_weights_build_exe\n";
563 }
564 system ("mgpp_weights_build$exe -f \"$fullindexprefix\" $osextra");
565
566 # create 'on-disk' stemmed dictionary
567 print $outhandle "\n creating 'on-disk' stemmed dictionary\n" if ($self->{'verbosity'} >= 1);
568 if (!-e "$mgpp_invf_dict_exe") {
569 print STDERR "<FatalError name='NoRunMGInvf'/>\n</Stage>\n" if $self->{'gli'};
570 die "mgppbuilder::build_index - couldn't run $mgpp_invf_dict_exe\n";
571 }
572 system ("mgpp_invf_dict$exe -f \"$fullindexprefix\" $osextra" );
573
574
575 # creates stem index files for the various stemming methods
576 print $outhandle "\n creating stem indexes\n" if ($self->{'verbosity'} >= 1);
577 print STDERR "<Phase name='CreatingStemIndx'/>\n" if $self->{'gli'};
578 if (!-e "$mgpp_stem_idx_exe") {
579 print STDERR "<FatalError name='NoRunMGStem'/>\n</Stage>\n" if $self->{'gli'};
580 die "mgppbuilder::build_index - couldn't run $mgpp_stem_idx_exe\n";
581 }
582 my $accent_folding_enabled = 1;
583 if ($self->{'accentfold'}) {
584 # the first time we do this, we test for accent folding enabled
585 if (system ("mgpp_stem_idx$exe -b 4096 -s4 -f \"$fullindexprefix\" $osextra") == 2) {
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
610 # remove unwanted files
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";
614 foreach my $file (readdir(DIR)) {
615 next if $file =~ /^\./;
616 my ($suffix) = $file =~ /\.([^\.]+)$/;
617 if (defined $suffix && !defined $wanted_index_files{$suffix}) {
618 # delete it!
619 print $outhandle "deleting $file\n" if $self->{'verbosity'} > 2;
620 #&util::rm (&util::filename_cat ($tmpdir, $file));
621 }
622 }
623 closedir (DIR);
624 }
625 print STDERR "</Stage>\n" if $self->{'gli'};
626}
627
628# now only outputs stuff if you can't generate it from collectionmeta - e.g. if someone has specified 'metadata' as an index.
629sub output_collection_meta {
630 my $self = shift(@_);
631 my ($handle) = @_;
632
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
638 # do the collection info
639 $self->output_collection_meta_start($handle);
640 $self->output_collection_meta_sets($handle);
641
642 # first do the collection meta stuff - everything without a dot
643 my $collmetadefined = 0;
644 my $metadata_entry;
645 if (defined $self->{'collect_cfg'}->{'collectionmeta'}) {
646 $collmetadefined = 1;
647 }
648
649 #add the index field macros to [collection]
650 # eg <TI>Title
651 # <SU>Subject
652 # these now come from collection meta. if that is not defined, usses the metadata name
653 my $field_entry="";
654 my $collmeta = "";
655 foreach my $longfield (@{$self->{'build_cfg'}->{'indexfields'}}){
656 my $shortfield = $self->{'buildproc'}->{'indexfieldmap'}->{$longfield};
657 next if $shortfield eq 1;
658
659 # we need to check if some coll meta has been defined - don't output
660 # any that have
661 $collmeta = ".$longfield";
662 if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{$collmeta}) {
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 }
670 }
671 }
672 print $handle $field_entry;
673
674 # now add the level names
675 my $level_entry = "";
676 foreach my $level (@{$self->{'collect_cfg'}->{'levels'}}) {
677 $collmeta = ".$level"; # based on the original specification
678 $level =~ tr/A-Z/a-z/; # make it lower case
679 my $levelid = $level_map{$level}; # find the actual value we used in the index
680 if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{$collmeta}) {
681 # use the default macro
682 $level_entry .= "<$levelid>" . $level_map{$levelid} . "\n";
683 }
684 }
685 print $handle $level_entry;
686
687 # now add subcoll meta
688 my $subcoll_entry = "";
689 my $shortname = "";
690 my $one_entry = "";
691 foreach my $subcoll (@{$self->{'index_mapping'}->{'subcollectionmaporder'}}) {
692 $shortname = $self->{'index_mapping'}->{$subcoll};
693 if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{".$subcoll"}) {
694 $subcoll_entry .= "<$shortname>$subcoll\n";
695 }
696 }
697 print $handle $subcoll_entry;
698
699 # now add language meta
700 my $lang_entry = "";
701 foreach my $lang (@{$self->{'index_mapping'}->{'languagemaporder'}}) {
702 $shortname = $self->{'index_mapping'}->{$lang};
703 if (!$collmetadefined || !defined $self->{'collect_cfg'}->{'collectionmeta'}->{".$lang"}) {
704 $lang_entry .= "<$shortname>$lang\n";
705 }
706 }
707 print $handle "$lang_entry\n";
708
709 $self->output_collection_meta_end($handle);
710}
711
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
718sub make_final_field_list {
719 my $self = shift (@_);
720
721 $self->{'build_cfg'} = {};
722
723 # store the indexfieldmap information
724 my @indexfieldmap = ();
725 my @indexfields = ();
726 my $specifiedfields = {};
727 my @specifiedfieldorder = ();
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
735 my $field;
736 foreach $field (@{$self->{'collect_cfg'}->{'indexes'}}) {
737 # remove subcoll stuff
738 my $parts = $field;
739 $parts =~ s/:.*$//;
740 # *************
741 my @fs = split(';', $parts);
742 foreach my $f(@fs) {
743 if (!defined $specifiedfields->{$f}) {
744 $specifiedfields->{$f}=1;
745 push (@specifiedfieldorder, "$f");
746 }
747 }
748 }
749
750 #add all fields bit
751 foreach $field (@specifiedfieldorder) {
752 if ($field eq "metadata") {
753 foreach my $newfield (keys %{$self->{'buildproc'}->{'indexfields'}}) {
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 {
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
775
776 }
777 }
778
779 $self->{'build_cfg'}->{'indexfieldmap'} = \@indexfieldmap;
780 $self->{'build_cfg'}->{'indexfields'} = \@indexfields;
781
782}
783
784
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.)
788sub read_final_field_list {
789 my $self = shift (@_);
790 $self->{'build_cfg'} = {};
791 my @indexfieldmap = ();
792 my @indexfields = ();
793 my @indexmap = ();
794
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
800 my $buildconfigfile = &util::filename_cat($self->{'build_dir'}, "build.cfg");
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 }
810
811 my $buildcfg = &colcfg::read_build_cfg( $buildconfigfile);
812 my $field;
813 if (defined $buildcfg->{'indexfields'}) {
814 foreach $field (@{$buildcfg->{'indexfields'}}) {
815 push (@indexfields, "$field");
816 }
817 }
818
819 if (defined $buildcfg->{'indexfieldmap'}) {
820 foreach $field (@{$buildcfg->{'indexfieldmap'}}) {
821 push (@indexfieldmap, "$field");
822 my ($f, $v) = $field =~ /^(.*)\-\>(.*)$/;
823 $self->{'buildproc'}->{'indexfieldmap'}->{$f} = $v;
824 }
825 }
826
827 if (defined $buildcfg->{'indexmap'}) {
828 foreach $field (@{$buildcfg->{'indexmap'}}) {
829 push (@indexmap, "$field");
830 }
831 }
832
833 $self->{'build_cfg'}->{'indexfieldmap'} = \@indexfieldmap;
834 $self->{'build_cfg'}->{'indexfields'} = \@indexfields;
835 $self->{'build_cfg'}->{'indexmap'} = \@indexmap;
836}
837
838
839sub build_cfg_extra {
840 my $self = shift (@_);
841 my ($build_cfg) = @_;
842
843 $build_cfg->{'numsections'} = $self->{'buildproc'}->get_num_sections();
844
845 # store the level info
846 my @indexlevels = ();
847 my @levelmap = ();
848 foreach my $l (@{$self->{'levelorder'}}) {
849 push (@indexlevels, $level_map{$l});
850 push (@levelmap, "$l\-\>$level_map{$l}");
851 }
852 $build_cfg->{'indexlevels'} = \@indexlevels;
853 $build_cfg->{'levelmap'} = \@levelmap;
854
855 # text level (and gdbm level) is always section
856 $build_cfg->{'textlevel'} = $level_map{'section'};
857
858}
859
8601;
861
862
Note: See TracBrowser for help on using the repository browser.