6 use File::Temp qw/tempdir/;
10 use WebPAC::Common 0.02;
12 use WebPAC::Input 0.03;
13 use WebPAC::Store 0.03;
14 use WebPAC::Normalize;
15 use WebPAC::Output::TT;
17 use YAML qw/LoadFile/;
20 use Time::HiRes qw/time/;
25 run.pl - start WebPAC indexing
27 B<this command will probably go away. Don't get used to it!>
35 start loading (all) databases at offset 42
39 limit loading to 100 records
43 remove database and Hyper Estraier index before indexing
45 =item --only=database_name/input_filter
47 reindex just single database (legacy name is --one)
49 C</input_filter> is optional part which can be C<name>
52 =item --config conf/config.yml
54 path to YAML configuration file
58 disable indexing and dump statistics about field and subfield
61 =item --validate path/to/validation_file
63 turn on extra validation of imput records, see L<WebPAC::Validation>
73 my $config = 'conf/config.yml';
81 "offset=i" => \$offset,
83 "one=s" => \$only_filter,
84 "only=s" => \$only_filter,
88 "validate=s" => \$validate_path,
91 $config = LoadFile($config);
93 print "config = ",Dumper($config) if ($debug);
95 die "no databases in config file!\n" unless ($config->{databases});
97 my $log = _new WebPAC::Common()->_get_logger();
98 $log->info( "-" x 79 );
101 $validate = new WebPAC::Validate(
102 path => $validate_path,
103 ) if ($validate_path);
105 my $use_indexer = $config->{use_indexer} || 'hyperestraier';
107 $log->debug("option --stats disables update of indexing engine...");
108 $use_indexer = undef;
110 $log->info("using $use_indexer indexing engine...");
114 my $start_t = time();
119 while (my ($database, $db_config) = each %{ $config->{databases} }) {
121 my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
122 next if ($only_database && $database !~ m/$only_database/i);
125 my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
126 $indexer_config->{database} = $database;
127 $indexer_config->{clean} = $clean;
128 $indexer_config->{label} = $db_config->{name};
130 if ($use_indexer eq 'hyperestraier') {
132 # open Hyper Estraier database
133 use WebPAC::Output::Estraier '0.10';
134 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
136 } elsif ($use_indexer eq 'kinosearch') {
139 use WebPAC::Output::KinoSearch;
140 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
141 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
144 $log->logdie("unknown use_indexer: $use_indexer");
147 $log->logide("can't continue without valid indexer") unless ($indexer);
154 my $abs_path = abs_path($0);
155 $abs_path =~ s#/[^/]*$#/#;
157 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
160 $log->info("creating new database '$database' in $db_path");
161 rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
163 $log->info("working on database '$database' in $db_path");
166 my $db = new WebPAC::Store(
168 database => $database,
174 # now, iterate through input formats
178 if (ref($db_config->{input}) eq 'ARRAY') {
179 @inputs = @{ $db_config->{input} };
180 } elsif ($db_config->{input}) {
181 push @inputs, $db_config->{input};
183 $log->info("database $database doesn't have inputs defined");
186 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
188 foreach my $input (@inputs) {
190 next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
192 my $type = lc($input->{type});
194 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
196 my $lookup = new WebPAC::Lookup(
197 lookup_file => $input->{lookup},
198 ) if ($input->{lookup});
200 my $input_module = $config->{webpac}->{inputs}->{$type};
202 $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
203 $input->{lookup} ? "lookup '$input->{lookup}'" : ""
206 my $input_db = new WebPAC::Input(
207 module => $input_module,
208 code_page => $config->{webpac}->{webpac_encoding},
209 limit => $limit || $input->{limit},
212 recode => $input->{recode},
215 $log->logdie("can't create input using $input_module") unless ($input);
217 my $maxmfn = $input_db->open(
218 path => $input->{path},
219 code_page => $input->{encoding}, # database encoding
224 my $normalize_path = $input->{normalize}->{path};
226 $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
228 my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
230 foreach my $pos ( 0 ... $input_db->size ) {
232 my $row = $input_db->fetch || next;
234 my $mfn = $row->{'000'}->[0];
236 if (! $mfn || $mfn !~ m#^\d+$#) {
237 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
239 push @{ $row->{'000'} }, $pos;
244 my @errors = $validate->validate_errors( $row );
245 $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
249 my $ds = WebPAC::Normalize::data_structure(
252 lookup => $lookup ? $lookup->lookup_hash : undef,
258 prefix => $input->{name},
259 ) if ($ds && !$stats);
262 id => $input->{name} . "/" . $mfn,
264 type => $config->{$use_indexer}->{type},
270 $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
274 eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
276 my $dt = time() - $start_t;
277 $log->info("$total_rows records ", $indexer ? "indexed " : "",
278 sprintf("in %.2f sec [%.2f rec/sec]",
279 $dt, ($total_rows / $dt)
284 # add Hyper Estraier links to other databases
286 if (ref($db_config->{links}) eq 'ARRAY') {
287 foreach my $link (@{ $db_config->{links} }) {
288 if ($use_indexer eq 'hyperestraier') {
289 $log->info("saving link $database -> $link->{to} [$link->{credit}]");
293 credit => $link->{credit},
296 $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
303 foreach my $link (@links) {
304 $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
305 $indexer->add_link( %{ $link } );