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::XML;
15 use WebPAC::Normalize::Set;
16 use WebPAC::Output::TT;
18 use YAML qw/LoadFile/;
21 use Time::HiRes qw/time/;
26 run.pl - start WebPAC indexing
28 B<this command will probably go away. Don't get used to it!>
36 start loading (all) databases at offset 42
40 limit loading to 100 records
44 remove database and Hyper Estraier index before indexing
46 =item --only=database_name/input_filter
48 reindex just single database (legacy name is --one)
50 C</input_filter> is optional part which can be C<name>
53 =item --config conf/config.yml
55 path to YAML configuration file
59 force conversion C<< normalize->path >> in C<config.yml> from
64 disable indexing and dump statistics about field and subfield
67 =item --validate path/to/validation_file
69 turn on extra validation of imput records, see L<WebPAC::Validation>
79 my $config = 'conf/config.yml';
88 "offset=i" => \$offset,
90 "one=s" => \$only_filter,
91 "only=s" => \$only_filter,
94 "force-set" => \$force_set,
96 "validate=s" => \$validate_path,
99 $config = LoadFile($config);
101 print "config = ",Dumper($config) if ($debug);
103 die "no databases in config file!\n" unless ($config->{databases});
105 my $log = _new WebPAC::Common()->_get_logger();
106 $log->info( "-" x 79 );
109 $validate = new WebPAC::Validate(
110 path => $validate_path,
111 ) if ($validate_path);
113 my $use_indexer = $config->{use_indexer} || 'hyperestraier';
115 $log->debug("option --stats disables update of indexing engine...");
116 $use_indexer = undef;
118 $log->info("using $use_indexer indexing engine...");
122 my $start_t = time();
127 while (my ($database, $db_config) = each %{ $config->{databases} }) {
129 my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
130 next if ($only_database && $database !~ m/$only_database/i);
133 my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
134 $indexer_config->{database} = $database;
135 $indexer_config->{clean} = $clean;
136 $indexer_config->{label} = $db_config->{name};
138 if ($use_indexer eq 'hyperestraier') {
140 # open Hyper Estraier database
141 use WebPAC::Output::Estraier '0.10';
142 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
144 } elsif ($use_indexer eq 'kinosearch') {
147 use WebPAC::Output::KinoSearch;
148 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
149 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
152 $log->logdie("unknown use_indexer: $use_indexer");
155 $log->logide("can't continue without valid indexer") unless ($indexer);
162 my $abs_path = abs_path($0);
163 $abs_path =~ s#/[^/]*$#/#;
165 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
168 $log->info("creating new database '$database' in $db_path");
169 rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
171 $log->info("working on database '$database' in $db_path");
174 my $db = new WebPAC::Store(
176 database => $database,
182 # now, iterate through input formats
186 if (ref($db_config->{input}) eq 'ARRAY') {
187 @inputs = @{ $db_config->{input} };
188 } elsif ($db_config->{input}) {
189 push @inputs, $db_config->{input};
191 $log->info("database $database doesn't have inputs defined");
194 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
196 foreach my $input (@inputs) {
198 next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
200 my $type = lc($input->{type});
202 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
204 my $lookup = new WebPAC::Lookup(
205 lookup_file => $input->{lookup},
206 ) if ($input->{lookup});
208 my $input_module = $config->{webpac}->{inputs}->{$type};
210 $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
211 $input->{lookup} ? "lookup '$input->{lookup}'" : ""
214 my $input_db = new WebPAC::Input(
215 module => $input_module,
216 code_page => $config->{webpac}->{webpac_encoding},
217 limit => $limit || $input->{limit},
220 recode => $input->{recode},
223 $log->logdie("can't create input using $input_module") unless ($input);
225 my $maxmfn = $input_db->open(
226 path => $input->{path},
227 code_page => $input->{encoding}, # database encoding
231 my $n = new WebPAC::Normalize::XML(
232 # filter => { 'foo' => sub { shift } },
234 lookup_regex => $lookup ? $lookup->regex : undef,
236 prefix => $input->{name},
240 my $normalize_path = $input->{normalize}->{path};
243 my $new_norm_path = $normalize_path;
244 $new_norm_path =~ s/\.xml$/.pl/;
245 if (-e $new_norm_path) {
246 $log->debug("--force-set replaced $normalize_path with $new_norm_path");
247 $normalize_path = $new_norm_path;
249 $log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path");
253 if ($normalize_path =~ m/\.xml$/i) {
255 tag => $input->{normalize}->{tag},
256 xml_file => $normalize_path,
258 } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
260 path => $normalize_path,
261 tag => $input->{normalize}->{tag},
263 } elsif ($normalize_path =~ m/\.(?:pl)$/i) {
265 $log->info("using WebPAC::Normalize::Set to process $normalize_path");
266 $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
269 foreach my $pos ( 0 ... $input_db->size ) {
271 my $row = $input_db->fetch || next;
273 my $mfn = $row->{'000'}->[0];
275 if (! $mfn || $mfn !~ m#^\d+$#) {
276 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
278 push @{ $row->{'000'} }, $pos;
283 my @errors = $validate->validate_errors( $row );
284 $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
290 $ds = $n->data_structure($row);
292 $ds = WebPAC::Normalize::Set::data_structure(
295 lookup => $lookup ? $lookup->lookup_hash : undef,
301 prefix => $input->{name},
302 ) if ($ds && !$stats);
306 id => $input->{name} . "/" . $mfn,
308 type => $config->{$use_indexer}->{type},
314 $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
318 eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
320 my $dt = time() - $start_t;
321 $log->info("$total_rows records ", $indexer ? "indexed " : "",
322 sprintf("in %.2f sec [%.2f rec/sec]",
323 $dt, ($total_rows / $dt)
328 # add Hyper Estraier links to other databases
330 if (ref($db_config->{links}) eq 'ARRAY') {
331 foreach my $link (@{ $db_config->{links} }) {
332 if ($use_indexer eq 'hyperestraier') {
333 $log->info("saving link $database -> $link->{to} [$link->{credit}]");
337 credit => $link->{credit},
340 $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
347 foreach my $link (@links) {
348 $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
349 $indexer->add_link( %{ $link } );