7 |
use lib './lib'; |
use lib './lib'; |
8 |
|
|
9 |
use WebPAC::Common 0.02; |
use WebPAC::Common 0.02; |
10 |
use WebPAC::Parser 0.04; |
use WebPAC::Parser 0.08; |
11 |
use WebPAC::Input 0.13; |
use WebPAC::Input 0.16; |
12 |
use WebPAC::Store 0.10; |
use WebPAC::Store 0.14; |
13 |
use WebPAC::Normalize 0.11; |
use WebPAC::Normalize 0.22; |
14 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
15 |
use WebPAC::Validate 0.06; |
use WebPAC::Validate 0.06; |
16 |
use WebPAC::Output::MARC; |
use WebPAC::Output::MARC; |
67 |
|
|
68 |
turn on extra validation of imput records, see L<WebPAC::Validation> |
turn on extra validation of imput records, see L<WebPAC::Validation> |
69 |
|
|
|
=item --marc-normalize conf/normalize/mapping.pl |
|
|
|
|
|
This option specifies normalisation file for MARC creation |
|
|
|
|
|
=item --marc-output out/marc/test.marc |
|
|
|
|
|
Optional path to output file |
|
|
|
|
70 |
=item --marc-lint |
=item --marc-lint |
71 |
|
|
72 |
By default turned on if C<--marc-normalize> is used. You can disable lint |
By default turned on if normalisation file has C<marc*> directives. You can disable lint |
73 |
messages with C<--no-marc-lint>. |
messages with C<--no-marc-lint>. |
74 |
|
|
75 |
=item --marc-dump |
=item --marc-dump |
102 |
my $only_filter; |
my $only_filter; |
103 |
my $stats = 0; |
my $stats = 0; |
104 |
my $validate_path; |
my $validate_path; |
|
my ($marc_normalize, $marc_output); |
|
105 |
my $marc_lint = 1; |
my $marc_lint = 1; |
106 |
my $marc_dump = 0; |
my $marc_dump = 0; |
107 |
my $parallel = 0; |
my $parallel = 0; |
120 |
"debug+" => \$debug, |
"debug+" => \$debug, |
121 |
"stats" => \$stats, |
"stats" => \$stats, |
122 |
"validate=s" => \$validate_path, |
"validate=s" => \$validate_path, |
|
"marc-normalize=s" => \$marc_normalize, |
|
|
"marc-output=s" => \$marc_output, |
|
123 |
"marc-lint!" => \$marc_lint, |
"marc-lint!" => \$marc_lint, |
124 |
"marc-dump!" => \$marc_dump, |
"marc-dump!" => \$marc_dump, |
125 |
"parallel=i" => \$parallel, |
"parallel=i" => \$parallel, |
135 |
|
|
136 |
$log->info( "-" x 79 ); |
$log->info( "-" x 79 ); |
137 |
|
|
138 |
|
my $log_file = 'log'; |
139 |
|
|
140 |
|
if (-e $log_file ) { # && -s $log_file > 5 * 1024 * 1024) { |
141 |
|
$log->info("moved old log with ", -s $log_file, " bytes to '${log_file}.old'"); |
142 |
|
rename $log_file, "${log_file}.old" || $log->logwarn("can't rename $log_file to ${log_file}.old: $!"); |
143 |
|
} |
144 |
|
|
145 |
my $estcmd_fh; |
my $estcmd_fh; |
146 |
my $estcmd_path = './estcmd-merge.sh'; |
my $estcmd_path = './estcmd-merge.sh'; |
159 |
|
|
160 |
|
|
161 |
my $use_indexer = $config->use_indexer; |
my $use_indexer = $config->use_indexer; |
162 |
|
$stats ||= $validate; |
163 |
if ($stats) { |
if ($stats) { |
164 |
$log->debug("option --stats disables update of indexing engine..."); |
$log->debug("disabled indexing for stats collection"); |
165 |
$use_indexer = undef; |
$use_indexer = undef; |
166 |
} else { |
} else { |
167 |
$log->info("using $use_indexer indexing engine..."); |
$log->info("using $use_indexer indexing engine..."); |
168 |
} |
} |
169 |
|
|
|
# disable indexing when creating marc |
|
|
$use_indexer = undef if ($marc_normalize); |
|
|
|
|
170 |
# parse normalize files and create source files for lookup and normalization |
# parse normalize files and create source files for lookup and normalization |
171 |
|
|
172 |
my $parser = new WebPAC::Parser( config => $config ); |
my $parser = new WebPAC::Parser( config => $config ); |
205 |
} |
} |
206 |
|
|
207 |
my $indexer; |
my $indexer; |
208 |
if ($use_indexer) { |
if ($use_indexer && $parser->have_rules( 'search', $database )) { |
209 |
|
|
210 |
my $cfg_name = $use_indexer; |
my $cfg_name = $use_indexer; |
211 |
$cfg_name =~ s/\-.*$//; |
$cfg_name =~ s/\-.*$//; |
278 |
my $abs_path = abs_path($0); |
my $abs_path = abs_path($0); |
279 |
$abs_path =~ s#/[^/]*$#/#; |
$abs_path =~ s#/[^/]*$#/#; |
280 |
|
|
281 |
my $db_path = $config->get('webpac')->{db_path} . '/' . $database; |
my $db_path = $config->webpac('db_path'); |
282 |
|
|
283 |
if ($clean) { |
if ($clean) { |
284 |
$log->info("creating new database '$database' in $db_path"); |
$log->info("creating new database '$database' in $db_path"); |
289 |
|
|
290 |
my $store = new WebPAC::Store( |
my $store = new WebPAC::Store( |
291 |
path => $db_path, |
path => $db_path, |
|
database => $database, |
|
292 |
debug => $debug, |
debug => $debug, |
293 |
); |
); |
294 |
|
|
347 |
delete( $input->{lookup} ); |
delete( $input->{lookup} ); |
348 |
} |
} |
349 |
|
|
|
my $lookup; |
|
350 |
my $lookup_coderef; |
my $lookup_coderef; |
351 |
|
|
352 |
if (@lookups) { |
if (@lookups) { |
360 |
WebPAC::Normalize::data_structure( |
WebPAC::Normalize::data_structure( |
361 |
row => $rec, |
row => $rec, |
362 |
rules => $rules, |
rules => $rules, |
|
lookup => $lookup, |
|
363 |
config => create_ds_config( $db_config, $database, $input, $mfn ), |
config => create_ds_config( $db_config, $database, $input, $mfn ), |
364 |
); |
); |
365 |
|
|
366 |
warn "current lookup = ", dump($lookup) if ($lookup); |
#warn "current lookup: ", dump(WebPAC::Normalize::_get_lookup()); |
367 |
}; |
}; |
368 |
|
|
369 |
WebPAC::Normalize::_set_lookup( undef ); |
WebPAC::Normalize::_set_lookup( undef ); |
372 |
|
|
373 |
}; |
}; |
374 |
|
|
375 |
|
my $lookup_jar; |
376 |
|
|
377 |
my $maxmfn = $input_db->open( |
my $maxmfn = $input_db->open( |
378 |
path => $input->{path}, |
path => $input->{path}, |
379 |
code_page => $input->{encoding}, # database encoding |
code_page => $input->{encoding}, # database encoding |
380 |
lookup_coderef => $lookup_coderef, |
lookup_coderef => $lookup_coderef, |
381 |
|
lookup => $lookup_jar, |
382 |
%{ $input }, |
%{ $input }, |
383 |
|
load_row => sub { |
384 |
|
my $a = shift; |
385 |
|
return $store->load_row( |
386 |
|
database => $database, |
387 |
|
input => $input_name, |
388 |
|
id => $a->{id}, |
389 |
|
); |
390 |
|
}, |
391 |
|
save_row => sub { |
392 |
|
my $a = shift; |
393 |
|
return $store->save_row( |
394 |
|
database => $database, |
395 |
|
input => $input_name, |
396 |
|
id => $a->{id}, |
397 |
|
row => $a->{row}, |
398 |
|
); |
399 |
|
}, |
400 |
|
|
401 |
); |
); |
402 |
|
|
403 |
my $lookup_data = WebPAC::Normalize::_get_lookup(); |
my $lookup_data = WebPAC::Normalize::_get_lookup(); |
404 |
|
|
405 |
if (defined( $lookup_data->{$database}->{$input_name} )) { |
if (defined( $lookup_data->{$database}->{$input_name} )) { |
406 |
$log->debug("created following lookups: ", dump( $lookup_data )); |
$log->debug("created following lookups: ", sub { dump( $lookup_data ) } ); |
407 |
|
|
408 |
foreach my $key (keys %{ $lookup_data->{$database}->{$input_name} }) { |
foreach my $key (keys %{ $lookup_data->{$database}->{$input_name} }) { |
409 |
$store->save_lookup( $database, $input_name, $key, $lookup_data->{$database}->{$input_name}->{$key} ); |
$store->save_lookup( |
410 |
|
database => $database, |
411 |
|
input => $input_name, |
412 |
|
key => $key, |
413 |
|
data => $lookup_data->{$database}->{$input_name}->{$key}, |
414 |
|
); |
415 |
} |
} |
416 |
} |
} |
417 |
|
|
425 |
$log->info("Generating report file $path"); |
$log->info("Generating report file $path"); |
426 |
} |
} |
427 |
|
|
428 |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
my $marc; |
429 |
@{ $input->{normalize} } : ( $input->{normalize} ); |
if ($parser->have_rules( 'marc', $database, $input_name )) { |
430 |
|
$marc = new WebPAC::Output::MARC( |
431 |
if ($marc_normalize) { |
path => "out/marc/${database}-${input_name}.marc", |
432 |
@norm_array = ( { |
lint => $marc_lint, |
433 |
path => $marc_normalize, |
dump => $marc_dump, |
434 |
output => $marc_output || "out/marc/${database}-${input_name}.marc", |
); |
|
} ); |
|
435 |
} |
} |
436 |
|
|
437 |
foreach my $normalize (@norm_array) { |
my $rules = $parser->normalize_rules($database,$input_name) || $log->logdie("no normalize rules found for $database/$input_name"); |
438 |
|
$log->debug("parsed normalize rules:\n$rules"); |
439 |
|
|
440 |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
# reset position in database |
441 |
|
$input_db->seek(1); |
442 |
|
|
443 |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
# generate name of config key for indexer (strip everything after -) |
444 |
|
my $indexer_config = $use_indexer; |
445 |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
$indexer_config =~ s/^(\w+)-?.*$/$1/g if ($indexer_config); |
446 |
|
|
447 |
$log->info("Using $normalize_path for normalization..."); |
my $lookup_hash; |
448 |
|
my $depends = $parser->depends($database,$input_name); |
449 |
|
|
450 |
|
if ($depends) { |
451 |
|
$log->debug("$database/$input_name depends on: ", dump($depends)) if ($depends); |
452 |
|
$log->logdie("parser->depends didn't return HASH") unless (ref($depends) eq 'HASH'); |
453 |
|
|
454 |
|
foreach my $db (keys %$depends) { |
455 |
|
foreach my $i (keys %{$depends->{$db}}) { |
456 |
|
foreach my $k (keys %{$depends->{$db}->{$i}}) { |
457 |
|
my $t = time(); |
458 |
|
$log->debug("loading lookup $db/$i"); |
459 |
|
$lookup_hash->{$db}->{$i}->{$k} = $store->load_lookup( |
460 |
|
database => $db, |
461 |
|
input => $i, |
462 |
|
key => $k, |
463 |
|
); |
464 |
|
$log->debug(sprintf("lookup $db/$i took %.2fs", time() - $t)); |
465 |
|
} |
466 |
|
} |
467 |
|
} |
468 |
|
|
469 |
my $marc = new WebPAC::Output::MARC( |
$log->debug("lookup_hash = ", sub { dump( $lookup_hash ) }); |
470 |
path => $normalize->{output}, |
} |
|
lint => $marc_lint, |
|
|
dump => $marc_dump, |
|
|
) if ($normalize->{output}); |
|
471 |
|
|
|
# reset position in database |
|
|
$input_db->seek(1); |
|
472 |
|
|
473 |
# generate name of config key for indexer (strip everything after -) |
foreach my $pos ( 0 ... $input_db->size ) { |
|
my $indexer_config = $use_indexer; |
|
|
$indexer_config =~ s/^(\w+)-?.*$/$1/g if ($indexer_config); |
|
474 |
|
|
475 |
foreach my $pos ( 0 ... $input_db->size ) { |
my $row = $input_db->fetch || next; |
476 |
|
|
477 |
my $row = $input_db->fetch || next; |
$total_rows++; |
478 |
|
|
479 |
my $mfn = $row->{'000'}->[0]; |
my $mfn = $row->{'000'}->[0]; |
480 |
|
|
481 |
if (! $mfn || $mfn !~ m#^\d+$#) { |
if (! $mfn || $mfn !~ m#^\d+$#) { |
482 |
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
483 |
$mfn = $pos; |
$mfn = $pos; |
484 |
push @{ $row->{'000'} }, $pos; |
push @{ $row->{'000'} }, $pos; |
485 |
} |
} |
486 |
|
|
487 |
|
|
488 |
if ($validate) { |
if ($validate) { |
489 |
if ( my $errors = $validate->validate_errors( $row, $input_db->dump ) ) { |
if ( my $errors = $validate->validate_rec( $row, $input_db->dump_ascii ) ) { |
490 |
$log->error( "MFN $mfn validation error:\n", |
$log->error( "MFN $mfn validation error:\n", |
491 |
$validate->report_error( $errors ) |
$validate->report_error( $errors ) |
492 |
); |
); |
|
} |
|
493 |
} |
} |
494 |
|
next; # validation doesn't create any output |
495 |
|
} |
496 |
|
|
497 |
my $ds = WebPAC::Normalize::data_structure( |
my $ds = WebPAC::Normalize::data_structure( |
498 |
row => $row, |
row => $row, |
499 |
rules => $rules, |
rules => $rules, |
500 |
lookup => $lookup ? $lookup->lookup_hash : undef, |
lookup => $lookup_hash, |
501 |
config => create_ds_config( $db_config, $database, $input, $mfn ), |
config => create_ds_config( $db_config, $database, $input, $mfn ), |
502 |
marc_encoding => 'utf-8', |
marc_encoding => 'utf-8', |
503 |
); |
load_row_coderef => sub { |
504 |
|
my ($database,$input,$mfn) = @_; |
505 |
$store->save_ds( |
return $store->load_row( |
506 |
id => $mfn, |
database => $database, |
507 |
ds => $ds, |
input => $input, |
508 |
prefix => $input_name, |
id => $mfn, |
509 |
) if ($ds && !$stats); |
); |
510 |
|
}, |
511 |
$indexer->add( |
); |
512 |
id => "${input_name}/${mfn}", |
|
513 |
ds => $ds, |
$log->debug("ds = ", sub { dump($ds) }) if ($ds); |
514 |
type => $config->get($indexer_config)->{type}, |
|
515 |
) if ($indexer && $ds); |
$store->save_ds( |
516 |
|
database => $database, |
517 |
if ($marc) { |
input => $input_name, |
518 |
my $i = 0; |
id => $mfn, |
519 |
|
ds => $ds, |
520 |
while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) { |
) if ($ds && !$stats); |
521 |
$marc->add( |
|
522 |
id => $mfn . ( $i ? "/$i" : '' ), |
$indexer->add( |
523 |
fields => $fields, |
id => "${input_name}/${mfn}", |
524 |
leader => WebPAC::Normalize::marc_leader(), |
ds => $ds, |
525 |
row => $row, |
type => $config->get($indexer_config)->{type}, |
526 |
); |
) if ($indexer && $ds); |
527 |
$i++; |
|
528 |
} |
if ($marc) { |
529 |
|
my $i = 0; |
530 |
$log->info("Created $i instances of MFN $mfn\n") if ($i > 1); |
|
531 |
|
while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) { |
532 |
|
$marc->add( |
533 |
|
id => $mfn . ( $i ? "/$i" : '' ), |
534 |
|
fields => $fields, |
535 |
|
leader => WebPAC::Normalize::marc_leader(), |
536 |
|
row => $row, |
537 |
|
); |
538 |
|
$i++; |
539 |
} |
} |
540 |
|
|
541 |
$total_rows++; |
$log->info("Created $i instances of MFN $mfn\n") if ($i > 1); |
542 |
} |
} |
543 |
|
} |
544 |
|
|
545 |
if ($validate) { |
if ($validate) { |
546 |
my $errors = $validate->report; |
my $errors = $validate->report; |
547 |
if ($errors) { |
if ($errors) { |
548 |
$log->info("validation errors:\n$errors\n" ); |
$log->info("validation errors:\n$errors\n" ); |
549 |
print $report_fh "$errors\n" if ($report_fh); |
print $report_fh "$errors\n" if ($report_fh); |
|
} |
|
550 |
} |
} |
551 |
|
} |
552 |
|
|
553 |
if ($stats) { |
if ($stats) { |
554 |
my $s = $input_db->stats; |
my $s = $input_db->stats; |
555 |
$log->info("statistics of fields usage:\n$s"); |
$log->info("statistics of fields usage:\n$s"); |
556 |
print $report_fh "Statistics of fields usage:\n$s" if ($report_fh); |
print $report_fh "Statistics of fields usage:\n$s" if ($report_fh); |
557 |
} |
} |
558 |
|
|
559 |
# close MARC file |
# close MARC file |
560 |
$marc->finish if ($marc); |
$marc->finish if ($marc); |
561 |
|
|
562 |
# close report |
# close report |
563 |
close($report_fh) if ($report_fh) |
close($report_fh) if ($report_fh) |
|
} |
|
564 |
|
|
565 |
} |
} |
566 |
|
|