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;
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
47 reindex just single database (legacy name is --one)
49 =item --config conf/config.yml
51 path to YAML configuration file
55 force conversion C<< normalize->path >> in C<config.yml> from
60 disable indexing and dump statistics about field and subfield
71 my $config = 'conf/config.yml';
79 "offset=i" => \$offset,
81 "one=s" => \$only_db_name,
82 "only=s" => \$only_db_name,
85 "force-set" => \$force_set,
89 $config = LoadFile($config);
91 print "config = ",Dumper($config) if ($debug);
93 die "no databases in config file!\n" unless ($config->{databases});
95 my $log = _new WebPAC::Common()->_get_logger();
96 $log->info( "-" x 79 );
98 my $use_indexer = $config->{use_indexer} || 'hyperestraier';
100 $log->debug("option --stats disables update of indexing engine...");
101 $use_indexer = undef;
103 $log->info("using $use_indexer indexing engine...");
107 my $start_t = time();
109 while (my ($database, $db_config) = each %{ $config->{databases} }) {
111 next if ($only_db_name && $database !~ m/$only_db_name/i);
116 my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
117 $indexer_config->{database} = $database;
118 $indexer_config->{clean} = $clean;
119 $indexer_config->{label} = $db_config->{name};
121 if ($use_indexer eq 'hyperestraier') {
123 # open Hyper Estraier database
124 use WebPAC::Output::Estraier '0.10';
125 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
127 } elsif ($use_indexer eq 'kinosearch') {
130 use WebPAC::Output::KinoSearch;
131 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
132 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
135 $log->logdie("unknown use_indexer: $use_indexer");
138 $log->logide("can't continue without valid indexer") unless ($indexer);
145 my $abs_path = abs_path($0);
146 $abs_path =~ s#/[^/]*$#/#;
148 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
151 $log->info("creating new database $database in $db_path");
152 rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
154 $log->debug("working on $database in $db_path");
157 my $db = new WebPAC::Store(
159 database => $database,
165 # now, iterate through input formats
169 if (ref($db_config->{input}) eq 'ARRAY') {
170 @inputs = @{ $db_config->{input} };
171 } elsif ($db_config->{input}) {
172 push @inputs, $db_config->{input};
174 $log->info("database $database doesn't have inputs defined");
177 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
179 foreach my $input (@inputs) {
181 my $type = lc($input->{type});
183 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
185 my $lookup = new WebPAC::Lookup(
186 lookup_file => $input->{lookup},
189 my $input_module = $config->{webpac}->{inputs}->{$type};
191 $log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'");
193 my $input_db = new WebPAC::Input(
194 module => $input_module,
195 code_page => $config->{webpac}->{webpac_encoding},
196 limit => $limit || $input->{limit},
199 recode => $input->{recode},
202 $log->logdie("can't create input using $input_module") unless ($input);
204 my $maxmfn = $input_db->open(
205 path => $input->{path},
206 code_page => $input->{encoding}, # database encoding
209 my $n = new WebPAC::Normalize::XML(
210 # filter => { 'foo' => sub { shift } },
212 lookup_regex => $lookup->regex,
214 prefix => $input->{name},
218 my $normalize_path = $input->{normalize}->{path};
221 my $new_norm_path = $normalize_path;
222 $new_norm_path =~ s/\.xml$/.pl/;
223 if (-e $new_norm_path) {
224 $log->debug("--force-set replaced $normalize_path with $new_norm_path");
225 $normalize_path = $new_norm_path;
227 $log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path");
231 if ($normalize_path =~ m/\.xml$/i) {
233 tag => $input->{normalize}->{tag},
234 xml_file => $normalize_path,
236 } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
238 path => $normalize_path,
239 tag => $input->{normalize}->{tag},
241 } elsif ($normalize_path =~ m/\.(?:pl)$/i) {
243 $log->info("using WebPAC::Normalize::Set to process $normalize_path");
244 $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
247 foreach my $pos ( 0 ... $input_db->size ) {
249 my $row = $input_db->fetch || next;
251 my $mfn = $row->{'000'}->[0];
253 if (! $mfn || $mfn !~ m#^\d+$#) {
254 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
256 push @{ $row->{'000'} }, $pos;
262 $ds = $n->data_structure($row);
264 $ds = WebPAC::Normalize::Set::data_structure(
267 lookup => $lookup->lookup_hash,
273 prefix => $input->{name},
274 ) if ($ds && !$stats);
278 id => $input->{name} . "/" . $mfn,
280 type => $config->{$use_indexer}->{type},
286 $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
290 eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
292 my $dt = time() - $start_t;
293 $log->info("$total_rows records indexed in " .
294 sprintf("%.2f sec [%.2f rec/sec]",
295 $dt, ($total_rows / $dt)
300 # add Hyper Estraier links to other databases
302 if (ref($db_config->{links}) eq 'ARRAY') {
303 foreach my $link (@{ $db_config->{links} }) {
304 if ($use_indexer eq 'hyperestraier') {
305 $log->info("adding link $database -> $link->{to} [$link->{credit}]");
309 credit => $link->{credit},
312 $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");