14 |
use WebPAC::Normalize::XML; |
use WebPAC::Normalize::XML; |
15 |
use WebPAC::Normalize::Set; |
use WebPAC::Normalize::Set; |
16 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
17 |
|
use WebPAC::Validate; |
18 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
19 |
use Getopt::Long; |
use Getopt::Long; |
20 |
use File::Path; |
use File::Path; |
43 |
|
|
44 |
remove database and Hyper Estraier index before indexing |
remove database and Hyper Estraier index before indexing |
45 |
|
|
46 |
=item --only=database_name |
=item --only=database_name/input_filter |
47 |
|
|
48 |
reindex just single database (legacy name is --one) |
reindex just single database (legacy name is --one) |
49 |
|
|
50 |
|
C</input_filter> is optional part which can be C<name> |
51 |
|
or C<type> from input |
52 |
|
|
53 |
=item --config conf/config.yml |
=item --config conf/config.yml |
54 |
|
|
55 |
path to YAML configuration file |
path to YAML configuration file |
56 |
|
|
57 |
=item --force-set |
=item --force-set |
58 |
|
|
59 |
force conversion C<normalize->path> in C<config.yml> from |
force conversion C<< normalize->path >> in C<config.yml> from |
60 |
C<.xml> to C<.pl> |
C<.xml> to C<.pl> |
61 |
|
|
62 |
|
=item --stats |
63 |
|
|
64 |
|
disable indexing and dump statistics about field and subfield |
65 |
|
usage for each input |
66 |
|
|
67 |
|
=item --validate path/to/validation_file |
68 |
|
|
69 |
|
turn on extra validation of imput records, see L<WebPAC::Validation> |
70 |
|
|
71 |
=back |
=back |
72 |
|
|
73 |
=cut |
=cut |
78 |
my $clean = 0; |
my $clean = 0; |
79 |
my $config = 'conf/config.yml'; |
my $config = 'conf/config.yml'; |
80 |
my $debug = 0; |
my $debug = 0; |
81 |
my $only_db_name; |
my $only_filter; |
82 |
my $force_set = 0; |
my $force_set = 0; |
83 |
|
my $stats = 0; |
84 |
|
my $validate_path; |
85 |
|
|
86 |
GetOptions( |
GetOptions( |
87 |
"limit=i" => \$limit, |
"limit=i" => \$limit, |
88 |
"offset=i" => \$offset, |
"offset=i" => \$offset, |
89 |
"clean" => \$clean, |
"clean" => \$clean, |
90 |
"one=s" => \$only_db_name, |
"one=s" => \$only_filter, |
91 |
"only=s" => \$only_db_name, |
"only=s" => \$only_filter, |
92 |
"config" => \$config, |
"config" => \$config, |
93 |
"debug" => \$debug, |
"debug" => \$debug, |
94 |
"force-set" => \$force_set, |
"force-set" => \$force_set, |
95 |
|
"stats" => \$stats, |
96 |
|
"validate=s" => \$validate_path, |
97 |
); |
); |
98 |
|
|
99 |
$config = LoadFile($config); |
$config = LoadFile($config); |
103 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
104 |
|
|
105 |
my $log = _new WebPAC::Common()->_get_logger(); |
my $log = _new WebPAC::Common()->_get_logger(); |
106 |
|
$log->info( "-" x 79 ); |
107 |
|
|
108 |
|
my $validate; |
109 |
|
$validate = new WebPAC::Validate( |
110 |
|
path => $validate_path, |
111 |
|
) if ($validate_path); |
112 |
|
|
113 |
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
114 |
$log->info("using $use_indexer indexing engine..."); |
if ($stats) { |
115 |
|
$log->debug("option --stats disables update of indexing engine..."); |
116 |
|
$use_indexer = undef; |
117 |
|
} else { |
118 |
|
$log->info("using $use_indexer indexing engine..."); |
119 |
|
} |
120 |
|
|
121 |
my $total_rows = 0; |
my $total_rows = 0; |
122 |
my $start_t = time(); |
my $start_t = time(); |
123 |
|
|
124 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
125 |
|
|
126 |
next if ($only_db_name && $database !~ m/$only_db_name/i); |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
127 |
|
next if ($only_database && $database !~ m/$only_database/i); |
128 |
|
|
129 |
my $indexer; |
my $indexer; |
130 |
|
|
131 |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
if ($use_indexer) { |
132 |
$indexer_config->{database} = $database; |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
133 |
$indexer_config->{clean} = $clean; |
$indexer_config->{database} = $database; |
134 |
$indexer_config->{label} = $db_config->{name}; |
$indexer_config->{clean} = $clean; |
135 |
|
$indexer_config->{label} = $db_config->{name}; |
136 |
# important: clean database just once! |
|
137 |
$clean = 0; |
if ($use_indexer eq 'hyperestraier') { |
138 |
|
|
139 |
if ($use_indexer eq 'hyperestraier') { |
# open Hyper Estraier database |
140 |
|
use WebPAC::Output::Estraier '0.10'; |
141 |
# open Hyper Estraier database |
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
142 |
use WebPAC::Output::Estraier '0.10'; |
|
143 |
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
} elsif ($use_indexer eq 'kinosearch') { |
144 |
|
|
145 |
} elsif ($use_indexer eq 'kinosearch') { |
# open KinoSearch |
146 |
|
use WebPAC::Output::KinoSearch; |
147 |
# open KinoSearch |
$indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path}); |
148 |
use WebPAC::Output::KinoSearch; |
$indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } ); |
|
$indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path}); |
|
|
$indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } ); |
|
149 |
|
|
150 |
} else { |
} else { |
151 |
$log->logdie("unknown use_indexer: $use_indexer"); |
$log->logdie("unknown use_indexer: $use_indexer"); |
152 |
|
} |
153 |
|
|
154 |
|
$log->logide("can't continue without valid indexer") unless ($indexer); |
155 |
} |
} |
156 |
|
|
|
$log->logide("can't continue without valid indexer") unless ($indexer); |
|
157 |
|
|
158 |
# |
# |
159 |
# now WebPAC::Store |
# now WebPAC::Store |
164 |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
165 |
|
|
166 |
if ($clean) { |
if ($clean) { |
167 |
$log->info("creating new database $database in $db_path"); |
$log->info("creating new database '$database' in $db_path"); |
168 |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
169 |
} else { |
} else { |
170 |
$log->debug("working on $database in $db_path"); |
$log->info("working on database '$database' in $db_path"); |
171 |
} |
} |
172 |
|
|
173 |
my $db = new WebPAC::Store( |
my $db = new WebPAC::Store( |
194 |
|
|
195 |
foreach my $input (@inputs) { |
foreach my $input (@inputs) { |
196 |
|
|
197 |
|
next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i)); |
198 |
|
|
199 |
my $type = lc($input->{type}); |
my $type = lc($input->{type}); |
200 |
|
|
201 |
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
206 |
|
|
207 |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
208 |
|
|
209 |
$log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'"); |
$log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module lookup '$input->{lookup}'"); |
210 |
|
|
211 |
my $input_db = new WebPAC::Input( |
my $input_db = new WebPAC::Input( |
212 |
module => $input_module, |
module => $input_module, |
215 |
offset => $offset, |
offset => $offset, |
216 |
lookup => $lookup, |
lookup => $lookup, |
217 |
recode => $input->{recode}, |
recode => $input->{recode}, |
218 |
|
stats => $stats, |
219 |
); |
); |
220 |
$log->logdie("can't create input using $input_module") unless ($input); |
$log->logdie("can't create input using $input_module") unless ($input); |
221 |
|
|
239 |
my $new_norm_path = $normalize_path; |
my $new_norm_path = $normalize_path; |
240 |
$new_norm_path =~ s/\.xml$/.pl/; |
$new_norm_path =~ s/\.xml$/.pl/; |
241 |
if (-e $new_norm_path) { |
if (-e $new_norm_path) { |
242 |
$log->info("--force-set replaced $normalize_path with $new_norm_path"); |
$log->debug("--force-set replaced $normalize_path with $new_norm_path"); |
243 |
$normalize_path = $new_norm_path; |
$normalize_path = $new_norm_path; |
244 |
} else { |
} else { |
245 |
$log->warn("--force-set failed on $new_norm_path, fallback to $normalize_path"); |
$log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path"); |
246 |
} |
} |
247 |
} |
} |
248 |
|
|
274 |
push @{ $row->{'000'} }, $pos; |
push @{ $row->{'000'} }, $pos; |
275 |
} |
} |
276 |
|
|
277 |
my $ds = $n ? $n->data_structure($row) : |
|
278 |
WebPAC::Normalize::Set::data_structure( |
if ($validate) { |
279 |
|
my @errors = $validate->validate_errors( $row ); |
280 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
my $ds; |
285 |
|
if ($n) { |
286 |
|
$ds = $n->data_structure($row); |
287 |
|
} else { |
288 |
|
$ds = WebPAC::Normalize::Set::data_structure( |
289 |
row => $row, |
row => $row, |
290 |
rules => $rules, |
rules => $rules, |
291 |
lookup => $lookup->lookup_hash, |
lookup => $lookup->lookup_hash, |
292 |
); |
); |
293 |
|
|
294 |
|
$db->save_ds( |
295 |
|
id => $mfn, |
296 |
|
ds => $ds, |
297 |
|
prefix => $input->{name}, |
298 |
|
) if ($ds && !$stats); |
299 |
|
} |
300 |
|
|
301 |
$indexer->add( |
$indexer->add( |
302 |
id => $input->{name} . "/" . $mfn, |
id => $input->{name} . "/" . $mfn, |
303 |
ds => $ds, |
ds => $ds, |
304 |
type => $config->{$use_indexer}->{type}, |
type => $config->{$use_indexer}->{type}, |
305 |
); |
) if ($indexer); |
306 |
|
|
307 |
$total_rows++; |
$total_rows++; |
308 |
} |
} |
309 |
|
|
310 |
|
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
311 |
|
|
312 |
}; |
}; |
313 |
|
|
314 |
eval { $indexer->finish } if ($indexer->can('finish')); |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
315 |
|
|
316 |
my $dt = time() - $start_t; |
my $dt = time() - $start_t; |
317 |
$log->info("$total_rows records indexed in " . |
$log->info("$total_rows records ", $indexer ? "indexed " : "", |
318 |
sprintf("%.2f sec [%.2f rec/sec]", |
sprintf("in %.2f sec [%.2f rec/sec]", |
319 |
$dt, ($total_rows / $dt) |
$dt, ($total_rows / $dt) |
320 |
) |
) |
321 |
); |
); |