--- trunk/run.pl 2006/08/25 12:31:06 619 +++ trunk/run.pl 2006/09/25 15:26:12 707 @@ -7,14 +7,14 @@ use lib './lib'; use WebPAC::Common 0.02; -use WebPAC::Lookup 0.03; -use WebPAC::Input 0.11; +use WebPAC::Parser 0.04; +use WebPAC::Input 0.13; use WebPAC::Store 0.03; use WebPAC::Normalize 0.11; use WebPAC::Output::TT; -use WebPAC::Validate; +use WebPAC::Validate 0.06; use WebPAC::Output::MARC; -use YAML qw/LoadFile/; +use WebPAC::Config; use Getopt::Long; use File::Path; use Time::HiRes qw/time/; @@ -60,8 +60,8 @@ =item --stats -disable indexing and dump statistics about field and subfield -usage for each input +disable indexing, modify_* in configuration and dump statistics about field +and subfield usage for each input =item --validate path/to/validation_file @@ -105,7 +105,7 @@ my $limit; my $clean = 0; -my $config = 'conf/config.yml'; +my $config_path; my $debug = 0; my $only_filter; my $stats = 0; @@ -119,21 +119,13 @@ my $log = _new WebPAC::Common()->_get_logger(); -my $hostname = `hostname`; -chomp($hostname); -$hostname =~ s/\..+$//; -if (-e "conf/$hostname.yml") { - $config = "conf/$hostname.yml"; - $log->info("using host configuration file: $config"); -} - GetOptions( "limit=i" => \$limit, "offset=i" => \$offset, "clean" => \$clean, "one=s" => \$only_filter, "only=s" => \$only_filter, - "config" => \$config, + "config" => \$config_path, "debug+" => \$debug, "stats" => \$stats, "validate=s" => \$validate_path, @@ -146,11 +138,11 @@ "merge" => \$merge, ); -$config = LoadFile($config); +my $config = new WebPAC::Config( path => $config_path ); #print "config = ",dump($config) if ($debug); -die "no databases in config file!\n" unless ($config->{databases}); +die "no databases in config file!\n" unless ($config->databases); $log->info( "-" x 79 ); @@ -171,7 +163,7 @@ ) if ($validate_path); -my $use_indexer = $config->{use_indexer} || 'hyperestraier'; +my $use_indexer = $config->use_indexer; if ($stats) { $log->debug("option --stats disables update of indexing engine..."); $use_indexer = undef; @@ -182,6 +174,10 @@ # disable indexing when creating marc $use_indexer = undef if ($marc_normalize); +# parse normalize files and create source files for lookup and normalization + +my $parser = new WebPAC::Parser( config => $config ); + my $total_rows = 0; my $start_t = time(); @@ -192,7 +188,16 @@ Proc::Queue::size($parallel); } -while (my ($database, $db_config) = each %{ $config->{databases} }) { +sub create_ds_config { + my ($db_config, $database, $input, $mfn) = @_; + my $c = dclone( $db_config ); + $c->{_} = $database || $log->logconfess("need database"); + $c->{_mfn} = $mfn || $log->logconfess("need mfn"); + $c->{input} = $input || $log->logconfess("need input"); + return $c; +} + +while (my ($database, $db_config) = each %{ $config->databases }) { my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); next if ($only_database && $database !~ m/$only_database/i); @@ -208,7 +213,11 @@ my $indexer; if ($use_indexer) { - my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); + + my $cfg_name = $use_indexer; + $cfg_name =~ s/\-.*$//; + + my $indexer_config = $config->get( $cfg_name ) || $log->logdie("can't find '$cfg_name' part in confguration"); $indexer_config->{database} = $database; $indexer_config->{clean} = $clean; $indexer_config->{label} = $db_config->{name}; @@ -222,6 +231,12 @@ use WebPAC::Output::Estraier '0.10'; $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); + } elsif ($use_indexer eq 'hyperestraier-native') { + + # open Hyper Estraier database + use WebPAC::Output::EstraierNative; + $indexer = new WebPAC::Output::EstraierNative( %{ $indexer_config } ); + } elsif ($use_indexer eq 'kinosearch') { # open KinoSearch @@ -270,7 +285,7 @@ my $abs_path = abs_path($0); $abs_path =~ s#/[^/]*$#/#; - my $db_path = $config->{webpac}->{db_path} . '/' . $database; + my $db_path = $config->get('webpac')->{db_path} . '/' . $database; if ($clean) { $log->info("creating new database '$database' in $db_path"); @@ -299,51 +314,91 @@ $log->info("database $database doesn't have inputs defined"); } - my @supported_inputs = keys %{ $config->{webpac}->{inputs} }; - foreach my $input (@inputs) { next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i)); my $type = lc($input->{type}); - 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(",", $config->webpac('inputs') ), " not '$type'!\n" unless (grep(/$type/, $config->webpac('inputs'))); - my $lookup; - if ($input->{lookup}) { - $lookup = new WebPAC::Lookup( - lookup_file => $input->{lookup}, - ); - delete( $input->{lookup} ); - } + my $input_module = $config->webpac('inputs')->{$type}; - my $input_module = $config->{webpac}->{inputs}->{$type}; + my @lookups = $parser->have_lookup_create($database, $input); $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module", - $input->{lookup} ? "lookup '$input->{lookup}'" : "" + @lookups ? " creating lookups: ".join(", ", @lookups) : "" ); + if ($stats) { + # disable modification of records if --stats is in use + delete($input->{modify_records}); + delete($input->{modify_file}); + } + my $input_db = new WebPAC::Input( module => $input_module, - encoding => $config->{webpac}->{webpac_encoding}, + encoding => $config->webpac('webpac_encoding'), limit => $limit || $input->{limit}, offset => $offset, - lookup_coderef => sub { - my $rec = shift || return; - $lookup->add( $rec ); - }, recode => $input->{recode}, stats => $stats, modify_records => $input->{modify_records}, + modify_file => $input->{modify_file}, ); $log->logdie("can't create input using $input_module") unless ($input); + if (defined( $input->{lookup} )) { + $log->warn("$database/", $input->{name}, " has depriciated lookup definition, removing it..."); + delete( $input->{lookup} ); + } + + my $lookup; + my $lookup_coderef; + + if (@lookups) { + + my $rules = $parser->lookup_create_rules($database, $input) || $log->logdie("no rules found for $database/$input"); + + $lookup_coderef = sub { + my $rec = shift || die "need rec!"; + my $mfn = $rec->{'000'}->[0] || die "need mfn in 000"; + + WebPAC::Normalize::data_structure( + row => $rec, + rules => $rules, + lookup => $lookup, + config => create_ds_config( $db_config, $database, $input, $mfn ), + ); + + warn "current lookup = ", dump($lookup) if ($lookup); + }; + + WebPAC::Normalize::_set_lookup( undef ); + + $log->debug("created lookup_coderef using:\n$rules"); + + }; + my $maxmfn = $input_db->open( path => $input->{path}, code_page => $input->{encoding}, # database encoding + lookup_coderef => $lookup_coderef, %{ $input }, ); + $log->debug("created following lookups: ", dump( WebPAC::Normalize::_get_lookup() ) ); + + my $report_fh; + if ($stats || $validate) { + my $path = "out/report/" . $database . '-' . $input->{name} . '.txt'; + open($report_fh, '>', $path) || $log->logdie("can't open $path: $!"); + + print $report_fh "Report for database '$database' input '$input->{name}' records ", + $offset || 1, "-", $limit || $input->{limit} || $maxmfn, "\n\n"; + $log->info("Generating report file $path"); + } + my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? @{ $input->{normalize} } : ( $input->{normalize} ); @@ -373,6 +428,10 @@ # reset position in database $input_db->seek(1); + # generate name of config key for indexer (strip everything after -) + my $indexer_config = $use_indexer; + $indexer_config =~ s/^(\w+)-?.*$/$1/g if ($indexer_config); + foreach my $pos ( 0 ... $input_db->size ) { my $row = $input_db->fetch || next; @@ -387,26 +446,18 @@ if ($validate) { - my @errors = $validate->validate_errors( $row ); - $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); + if ( my $errors = $validate->validate_errors( $row, $input_db->dump ) ) { + $log->error( "MFN $mfn validation error:\n", + $validate->report_error( $errors ) + ); + } } - my $ds_config = dclone($db_config); - - # default values -> database key - $ds_config->{_} = $database; - - # current mfn - $ds_config->{_mfn} = $mfn; - - # attach current input - $ds_config->{input} = $input; - my $ds = WebPAC::Normalize::data_structure( row => $row, rules => $rules, lookup => $lookup ? $lookup->lookup_hash : undef, - config => $ds_config, + config => create_ds_config( $db_config, $database, $input, $mfn ), marc_encoding => 'utf-8', ); @@ -419,7 +470,7 @@ $indexer->add( id => $input->{name} . "/" . $mfn, ds => $ds, - type => $config->{$use_indexer}->{type}, + type => $config->get($indexer_config)->{type}, ) if ($indexer && $ds); if ($marc) { @@ -441,11 +492,25 @@ $total_rows++; } - $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); + if ($validate) { + my $errors = $validate->report; + if ($errors) { + $log->info("validation errors:\n$errors\n" ); + print $report_fh "$errors\n" if ($report_fh); + } + } + + if ($stats) { + my $s = $input_db->stats; + $log->info("statistics of fields usage:\n$s"); + print $report_fh "Statistics of fields usage:\n$s" if ($report_fh); + } # close MARC file $marc->finish if ($marc); + # close report + close($report_fh) if ($report_fh) } }