4 |
|
|
5 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
6 |
use File::Temp qw/tempdir/; |
use File::Temp qw/tempdir/; |
|
use Data::Dumper; |
|
7 |
use lib './lib'; |
use lib './lib'; |
8 |
|
|
9 |
use WebPAC::Common 0.02; |
use WebPAC::Common 0.02; |
10 |
use WebPAC::Lookup; |
use WebPAC::Lookup 0.03; |
11 |
use WebPAC::Input 0.03; |
use WebPAC::Input 0.07; |
12 |
use WebPAC::Store 0.03; |
use WebPAC::Store 0.03; |
13 |
use WebPAC::Normalize::XML; |
use WebPAC::Normalize 0.11; |
|
use WebPAC::Normalize::Set; |
|
14 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
15 |
|
use WebPAC::Validate; |
16 |
|
use WebPAC::Output::MARC; |
17 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
18 |
use Getopt::Long; |
use Getopt::Long; |
19 |
use File::Path; |
use File::Path; |
20 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
21 |
use File::Slurp; |
use File::Slurp; |
22 |
|
use Data::Dump qw/dump/; |
23 |
|
|
24 |
=head1 NAME |
=head1 NAME |
25 |
|
|
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 --stats |
58 |
|
|
59 |
|
disable indexing and dump statistics about field and subfield |
60 |
|
usage for each input |
61 |
|
|
62 |
force conversion C<< normalize->path >> in C<config.yml> from |
=item --validate path/to/validation_file |
|
C<.xml> to C<.pl> |
|
63 |
|
|
64 |
=item --stats |
turn on extra validation of imput records, see L<WebPAC::Validation> |
65 |
|
|
66 |
|
=item --marc-normalize conf/normalize/mapping.pl |
67 |
|
|
68 |
|
This option specifies normalisation file for MARC creation |
69 |
|
|
70 |
|
=item --marc-output out/marc/test.marc |
71 |
|
|
72 |
dump statistics about used fields and subfields in each input |
Optional path to output file |
73 |
|
|
74 |
|
=item --marc-lint |
75 |
|
|
76 |
|
By default turned on if C<--marc-normalize> is used. You can disable lint |
77 |
|
messages with C<--no-marc-lint>. |
78 |
|
|
79 |
|
=item --marc-dump |
80 |
|
|
81 |
|
Force dump or input and marc record for debugging. |
82 |
|
|
83 |
=back |
=back |
84 |
|
|
90 |
my $clean = 0; |
my $clean = 0; |
91 |
my $config = 'conf/config.yml'; |
my $config = 'conf/config.yml'; |
92 |
my $debug = 0; |
my $debug = 0; |
93 |
my $only_db_name; |
my $only_filter; |
|
my $force_set = 0; |
|
94 |
my $stats = 0; |
my $stats = 0; |
95 |
|
my $validate_path; |
96 |
|
my ($marc_normalize, $marc_output); |
97 |
|
my $marc_lint = 1; |
98 |
|
my $marc_dump = 0; |
99 |
|
|
100 |
GetOptions( |
GetOptions( |
101 |
"limit=i" => \$limit, |
"limit=i" => \$limit, |
102 |
"offset=i" => \$offset, |
"offset=i" => \$offset, |
103 |
"clean" => \$clean, |
"clean" => \$clean, |
104 |
"one=s" => \$only_db_name, |
"one=s" => \$only_filter, |
105 |
"only=s" => \$only_db_name, |
"only=s" => \$only_filter, |
106 |
"config" => \$config, |
"config" => \$config, |
107 |
"debug" => \$debug, |
"debug+" => \$debug, |
|
"force-set" => \$force_set, |
|
108 |
"stats" => \$stats, |
"stats" => \$stats, |
109 |
|
"validate=s" => \$validate_path, |
110 |
|
"marc-normalize=s" => \$marc_normalize, |
111 |
|
"marc-output=s" => \$marc_output, |
112 |
|
"marc-lint!" => \$marc_lint, |
113 |
|
"marc-dump!" => \$marc_dump, |
114 |
); |
); |
115 |
|
|
116 |
$config = LoadFile($config); |
$config = LoadFile($config); |
117 |
|
|
118 |
print "config = ",Dumper($config) if ($debug); |
print "config = ",dump($config) if ($debug); |
119 |
|
|
120 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
121 |
|
|
122 |
my $log = _new WebPAC::Common()->_get_logger(); |
my $log = _new WebPAC::Common()->_get_logger(); |
123 |
|
$log->info( "-" x 79 ); |
124 |
|
|
125 |
|
my $validate; |
126 |
|
$validate = new WebPAC::Validate( |
127 |
|
path => $validate_path, |
128 |
|
) if ($validate_path); |
129 |
|
|
130 |
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
131 |
$log->info("using $use_indexer indexing engine..."); |
if ($stats) { |
132 |
|
$log->debug("option --stats disables update of indexing engine..."); |
133 |
|
$use_indexer = undef; |
134 |
|
} else { |
135 |
|
$log->info("using $use_indexer indexing engine..."); |
136 |
|
} |
137 |
|
|
138 |
|
# disable indexing when creating marc |
139 |
|
$use_indexer = undef if ($marc_normalize); |
140 |
|
|
141 |
my $total_rows = 0; |
my $total_rows = 0; |
142 |
my $start_t = time(); |
my $start_t = time(); |
143 |
|
|
144 |
|
my @links; |
145 |
|
my $indexer; |
146 |
|
|
147 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
148 |
|
|
149 |
next if ($only_db_name && $database !~ m/$only_db_name/i); |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
150 |
|
next if ($only_database && $database !~ m/$only_database/i); |
151 |
|
|
152 |
my $indexer; |
if ($use_indexer) { |
153 |
|
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
154 |
|
$indexer_config->{database} = $database; |
155 |
|
$indexer_config->{clean} = $clean; |
156 |
|
$indexer_config->{label} = $db_config->{name}; |
157 |
|
|
158 |
|
if ($use_indexer eq 'hyperestraier') { |
159 |
|
|
160 |
|
# open Hyper Estraier database |
161 |
|
use WebPAC::Output::Estraier '0.10'; |
162 |
|
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
163 |
|
|
164 |
|
} elsif ($use_indexer eq 'kinosearch') { |
165 |
|
|
166 |
|
# open KinoSearch |
167 |
|
use WebPAC::Output::KinoSearch; |
168 |
|
$indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path}); |
169 |
|
$indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } ); |
170 |
|
|
171 |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
} else { |
172 |
$indexer_config->{database} = $database; |
$log->logdie("unknown use_indexer: $use_indexer"); |
173 |
$indexer_config->{clean} = $clean; |
} |
|
$indexer_config->{label} = $db_config->{name}; |
|
|
|
|
|
if ($use_indexer eq 'hyperestraier') { |
|
|
|
|
|
# open Hyper Estraier database |
|
|
use WebPAC::Output::Estraier '0.10'; |
|
|
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
|
|
|
|
|
} elsif ($use_indexer eq 'kinosearch') { |
|
|
|
|
|
# open KinoSearch |
|
|
use WebPAC::Output::KinoSearch; |
|
|
$indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path}); |
|
|
$indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } ); |
|
174 |
|
|
175 |
} else { |
$log->logide("can't continue without valid indexer") unless ($indexer); |
|
$log->logdie("unknown use_indexer: $use_indexer"); |
|
176 |
} |
} |
177 |
|
|
|
$log->logide("can't continue without valid indexer") unless ($indexer); |
|
178 |
|
|
179 |
# |
# |
180 |
# now WebPAC::Store |
# now WebPAC::Store |
185 |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
186 |
|
|
187 |
if ($clean) { |
if ($clean) { |
188 |
$log->info("creating new database $database in $db_path"); |
$log->info("creating new database '$database' in $db_path"); |
189 |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
190 |
} else { |
} else { |
191 |
$log->debug("working on $database in $db_path"); |
$log->info("working on database '$database' in $db_path"); |
192 |
} |
} |
193 |
|
|
194 |
my $db = new WebPAC::Store( |
my $db = new WebPAC::Store( |
215 |
|
|
216 |
foreach my $input (@inputs) { |
foreach my $input (@inputs) { |
217 |
|
|
218 |
|
next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i)); |
219 |
|
|
220 |
my $type = lc($input->{type}); |
my $type = lc($input->{type}); |
221 |
|
|
222 |
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)); |
223 |
|
|
224 |
my $lookup = new WebPAC::Lookup( |
my $lookup; |
225 |
lookup_file => $input->{lookup}, |
if ($input->{lookup}) { |
226 |
); |
$lookup = new WebPAC::Lookup( |
227 |
|
lookup_file => $input->{lookup}, |
228 |
|
); |
229 |
|
delete( $input->{lookup} ); |
230 |
|
} |
231 |
|
|
232 |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
233 |
|
|
234 |
$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", |
235 |
|
$input->{lookup} ? "lookup '$input->{lookup}'" : "" |
236 |
|
); |
237 |
|
|
238 |
my $input_db = new WebPAC::Input( |
my $input_db = new WebPAC::Input( |
239 |
module => $input_module, |
module => $input_module, |
240 |
code_page => $config->{webpac}->{webpac_encoding}, |
encoding => $config->{webpac}->{webpac_encoding}, |
241 |
limit => $limit || $input->{limit}, |
limit => $limit || $input->{limit}, |
242 |
offset => $offset, |
offset => $offset, |
243 |
lookup => $lookup, |
lookup_coderef => sub { |
244 |
|
my $rec = shift || return; |
245 |
|
$lookup->add( $rec ); |
246 |
|
}, |
247 |
recode => $input->{recode}, |
recode => $input->{recode}, |
248 |
stats => $stats, |
stats => $stats, |
249 |
); |
); |
252 |
my $maxmfn = $input_db->open( |
my $maxmfn = $input_db->open( |
253 |
path => $input->{path}, |
path => $input->{path}, |
254 |
code_page => $input->{encoding}, # database encoding |
code_page => $input->{encoding}, # database encoding |
255 |
|
%{ $input }, |
256 |
); |
); |
257 |
|
|
258 |
my $n = new WebPAC::Normalize::XML( |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
259 |
# filter => { 'foo' => sub { shift } }, |
@{ $input->{normalize} } : ( $input->{normalize} ); |
|
db => $db, |
|
|
lookup_regex => $lookup->regex, |
|
|
lookup => $lookup, |
|
|
prefix => $input->{name}, |
|
|
); |
|
|
|
|
|
my $rules; |
|
|
my $normalize_path = $input->{normalize}->{path}; |
|
260 |
|
|
261 |
if ($force_set) { |
if ($marc_normalize) { |
262 |
my $new_norm_path = $normalize_path; |
@norm_array = ( { |
263 |
$new_norm_path =~ s/\.xml$/.pl/; |
path => $marc_normalize, |
264 |
if (-e $new_norm_path) { |
output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc', |
265 |
$log->debug("--force-set replaced $normalize_path with $new_norm_path"); |
} ); |
|
$normalize_path = $new_norm_path; |
|
|
} else { |
|
|
$log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path"); |
|
|
} |
|
266 |
} |
} |
267 |
|
|
268 |
if ($normalize_path =~ m/\.xml$/i) { |
foreach my $normalize (@norm_array) { |
|
$n->open( |
|
|
tag => $input->{normalize}->{tag}, |
|
|
xml_file => $normalize_path, |
|
|
); |
|
|
} elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) { |
|
|
$n->open_yaml( |
|
|
path => $normalize_path, |
|
|
tag => $input->{normalize}->{tag}, |
|
|
); |
|
|
} elsif ($normalize_path =~ m/\.(?:pl)$/i) { |
|
|
$n = undef; |
|
|
$log->info("using WebPAC::Normalize::Set to process $normalize_path"); |
|
|
$rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
|
|
} |
|
269 |
|
|
270 |
foreach my $pos ( 0 ... $input_db->size ) { |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
271 |
|
|
272 |
my $row = $input_db->fetch || next; |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
273 |
|
|
274 |
my $mfn = $row->{'000'}->[0]; |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
275 |
|
|
276 |
if (! $mfn || $mfn !~ m#^\d+$#) { |
$log->info("Using $normalize_path for normalization..."); |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
|
|
$mfn = $pos; |
|
|
push @{ $row->{'000'} }, $pos; |
|
|
} |
|
277 |
|
|
278 |
|
my $marc = new WebPAC::Output::MARC( |
279 |
my $ds; |
path => $normalize->{output}, |
280 |
if ($n) { |
lint => $marc_lint, |
281 |
$ds = $n->data_structure($row); |
dump => $marc_dump, |
282 |
} else { |
) if ($normalize->{output}); |
283 |
$ds = WebPAC::Normalize::Set::data_structure( |
|
284 |
|
# reset position in database |
285 |
|
$input_db->seek(1); |
286 |
|
|
287 |
|
foreach my $pos ( 0 ... $input_db->size ) { |
288 |
|
|
289 |
|
my $row = $input_db->fetch || next; |
290 |
|
|
291 |
|
my $mfn = $row->{'000'}->[0]; |
292 |
|
|
293 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
294 |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
295 |
|
$mfn = $pos; |
296 |
|
push @{ $row->{'000'} }, $pos; |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
if ($validate) { |
301 |
|
my @errors = $validate->validate_errors( $row ); |
302 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
303 |
|
} |
304 |
|
|
305 |
|
my $ds = WebPAC::Normalize::data_structure( |
306 |
row => $row, |
row => $row, |
307 |
rules => $rules, |
rules => $rules, |
308 |
lookup => $lookup->lookup_hash, |
lookup => $lookup ? $lookup->lookup_hash : undef, |
309 |
|
marc_encoding => 'utf-8', |
310 |
); |
); |
311 |
|
|
312 |
$db->save_ds( |
$db->save_ds( |
313 |
id => $mfn, |
id => $mfn, |
314 |
ds => $ds, |
ds => $ds, |
315 |
prefix => $input->{name}, |
prefix => $input->{name}, |
316 |
) if ($ds); |
) if ($ds && !$stats); |
317 |
|
|
318 |
|
$indexer->add( |
319 |
|
id => $input->{name} . "/" . $mfn, |
320 |
|
ds => $ds, |
321 |
|
type => $config->{$use_indexer}->{type}, |
322 |
|
) if ($indexer && $ds); |
323 |
|
|
324 |
|
if ($marc) { |
325 |
|
my $i = 0; |
326 |
|
|
327 |
|
while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) { |
328 |
|
$marc->add( |
329 |
|
id => $mfn . ( $i ? "/$i" : '' ), |
330 |
|
fields => $fields, |
331 |
|
leader => WebPAC::Normalize::marc_leader(), |
332 |
|
row => $row, |
333 |
|
); |
334 |
|
$i++; |
335 |
|
} |
336 |
|
|
337 |
|
$log->info("Created $i instances of MFN $mfn\n") if ($i > 1); |
338 |
|
} |
339 |
|
|
340 |
|
$total_rows++; |
341 |
} |
} |
342 |
|
|
343 |
$indexer->add( |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
|
id => $input->{name} . "/" . $mfn, |
|
|
ds => $ds, |
|
|
type => $config->{$use_indexer}->{type}, |
|
|
); |
|
344 |
|
|
345 |
$total_rows++; |
# close MARC file |
346 |
} |
$marc->finish if ($marc); |
347 |
|
|
348 |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
} |
349 |
|
|
350 |
}; |
} |
351 |
|
|
352 |
eval { $indexer->finish } if ($indexer->can('finish')); |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
353 |
|
|
354 |
my $dt = time() - $start_t; |
my $dt = time() - $start_t; |
355 |
$log->info("$total_rows records indexed in " . |
$log->info("$total_rows records ", $indexer ? "indexed " : "", |
356 |
sprintf("%.2f sec [%.2f rec/sec]", |
sprintf("in %.2f sec [%.2f rec/sec]", |
357 |
$dt, ($total_rows / $dt) |
$dt, ($total_rows / $dt) |
358 |
) |
) |
359 |
); |
); |
361 |
# |
# |
362 |
# add Hyper Estraier links to other databases |
# add Hyper Estraier links to other databases |
363 |
# |
# |
364 |
if (ref($db_config->{links}) eq 'ARRAY') { |
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
365 |
foreach my $link (@{ $db_config->{links} }) { |
foreach my $link (@{ $db_config->{links} }) { |
366 |
if ($use_indexer eq 'hyperestraier') { |
if ($use_indexer eq 'hyperestraier') { |
367 |
$log->info("adding link $database -> $link->{to} [$link->{credit}]"); |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
368 |
$indexer->add_link( |
push @links, { |
369 |
from => $database, |
from => $database, |
370 |
to => $link->{to}, |
to => $link->{to}, |
371 |
credit => $link->{credit}, |
credit => $link->{credit}, |
372 |
); |
}; |
373 |
} else { |
} else { |
374 |
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
375 |
} |
} |
378 |
|
|
379 |
} |
} |
380 |
|
|
381 |
|
foreach my $link (@links) { |
382 |
|
$log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]"); |
383 |
|
$indexer->add_link( %{ $link } ); |
384 |
|
} |