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
66 my $config = 'conf/config.yml';
73 "offset=i" => \$offset,
75 "one=s" => \$only_db_name,
76 "only=s" => \$only_db_name,
79 "force-set" => \$force_set,
82 $config = LoadFile($config);
84 print "config = ",Dumper($config) if ($debug);
86 die "no databases in config file!\n" unless ($config->{databases});
88 my $log = _new WebPAC::Common()->_get_logger();
90 my $use_indexer = $config->{use_indexer} || 'hyperestraier';
91 $log->info("using $use_indexer indexing engine...");
96 while (my ($database, $db_config) = each %{ $config->{databases} }) {
98 next if ($only_db_name && $database !~ m/$only_db_name/i);
102 my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
103 $indexer_config->{database} = $database;
104 $indexer_config->{clean} = $clean;
105 $indexer_config->{label} = $db_config->{name};
107 if ($use_indexer eq 'hyperestraier') {
109 # open Hyper Estraier database
110 use WebPAC::Output::Estraier '0.10';
111 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
113 } elsif ($use_indexer eq 'kinosearch') {
116 use WebPAC::Output::KinoSearch;
117 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
118 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
121 $log->logdie("unknown use_indexer: $use_indexer");
124 $log->logide("can't continue without valid indexer") unless ($indexer);
129 my $abs_path = abs_path($0);
130 $abs_path =~ s#/[^/]*$#/#;
132 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
135 $log->info("creating new database $database in $db_path");
136 rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
138 $log->debug("working on $database in $db_path");
141 my $db = new WebPAC::Store(
143 database => $database,
149 # now, iterate through input formats
153 if (ref($db_config->{input}) eq 'ARRAY') {
154 @inputs = @{ $db_config->{input} };
155 } elsif ($db_config->{input}) {
156 push @inputs, $db_config->{input};
158 $log->info("database $database doesn't have inputs defined");
161 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
163 foreach my $input (@inputs) {
165 my $type = lc($input->{type});
167 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
169 my $lookup = new WebPAC::Lookup(
170 lookup_file => $input->{lookup},
173 my $input_module = $config->{webpac}->{inputs}->{$type};
175 $log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'");
177 my $input_db = new WebPAC::Input(
178 module => $input_module,
179 code_page => $config->{webpac}->{webpac_encoding},
180 limit => $limit || $input->{limit},
183 recode => $input->{recode},
185 $log->logdie("can't create input using $input_module") unless ($input);
187 my $maxmfn = $input_db->open(
188 path => $input->{path},
189 code_page => $input->{encoding}, # database encoding
192 my $n = new WebPAC::Normalize::XML(
193 # filter => { 'foo' => sub { shift } },
195 lookup_regex => $lookup->regex,
197 prefix => $input->{name},
201 my $normalize_path = $input->{normalize}->{path};
204 my $new_norm_path = $normalize_path;
205 $new_norm_path =~ s/\.xml$/.pl/;
206 if (-e $new_norm_path) {
207 $log->debug("--force-set replaced $normalize_path with $new_norm_path");
208 $normalize_path = $new_norm_path;
210 $log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path");
214 if ($normalize_path =~ m/\.xml$/i) {
216 tag => $input->{normalize}->{tag},
217 xml_file => $normalize_path,
219 } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
221 path => $normalize_path,
222 tag => $input->{normalize}->{tag},
224 } elsif ($normalize_path =~ m/\.(?:pl)$/i) {
226 $log->info("using WebPAC::Normalize::Set to process $normalize_path");
227 $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;
242 my $ds = $n ? $n->data_structure($row) :
243 WebPAC::Normalize::Set::data_structure(
246 lookup => $lookup->lookup_hash,
250 id => $input->{name} . "/" . $mfn,
252 type => $config->{$use_indexer}->{type},
260 eval { $indexer->finish } if ($indexer->can('finish'));
262 my $dt = time() - $start_t;
263 $log->info("$total_rows records indexed in " .
264 sprintf("%.2f sec [%.2f rec/sec]",
265 $dt, ($total_rows / $dt)
270 # add Hyper Estraier links to other databases
272 if (ref($db_config->{links}) eq 'ARRAY') {
273 foreach my $link (@{ $db_config->{links} }) {
274 if ($use_indexer eq 'hyperestraier') {
275 $log->info("adding link $database -> $link->{to} [$link->{credit}]");
279 credit => $link->{credit},
282 $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");