--- trunk/run.pl 2006/06/26 16:39:51 536 +++ trunk/run.pl 2006/07/10 10:16:11 595 @@ -4,21 +4,23 @@ use Cwd qw/abs_path/; use File::Temp qw/tempdir/; -use Data::Dumper; use lib './lib'; use WebPAC::Common 0.02; -use WebPAC::Lookup; -use WebPAC::Input 0.03; +use WebPAC::Lookup 0.03; +use WebPAC::Input 0.07; use WebPAC::Store 0.03; -use WebPAC::Normalize; +use WebPAC::Normalize 0.11; use WebPAC::Output::TT; use WebPAC::Validate; +use WebPAC::Output::MARC; use YAML qw/LoadFile/; use Getopt::Long; use File::Path; use Time::HiRes qw/time/; use File::Slurp; +use Data::Dump qw/dump/; +use Storable qw/dclone/; =head1 NAME @@ -62,6 +64,23 @@ turn on extra validation of imput records, see L +=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 + +=item --marc-lint + +By default turned on if C<--marc-normalize> is used. You can disable lint +messages with C<--no-marc-lint>. + +=item --marc-dump + +Force dump or input and marc record for debugging. + =back =cut @@ -75,6 +94,9 @@ my $only_filter; my $stats = 0; my $validate_path; +my ($marc_normalize, $marc_output); +my $marc_lint = 1; +my $marc_dump = 0; GetOptions( "limit=i" => \$limit, @@ -83,14 +105,18 @@ "one=s" => \$only_filter, "only=s" => \$only_filter, "config" => \$config, - "debug" => \$debug, + "debug+" => \$debug, "stats" => \$stats, "validate=s" => \$validate_path, + "marc-normalize=s" => \$marc_normalize, + "marc-output=s" => \$marc_output, + "marc-lint!" => \$marc_lint, + "marc-dump!" => \$marc_dump, ); $config = LoadFile($config); -print "config = ",Dumper($config) if ($debug); +print "config = ",dump($config) if ($debug); die "no databases in config file!\n" unless ($config->{databases}); @@ -110,6 +136,9 @@ $log->info("using $use_indexer indexing engine..."); } +# disable indexing when creating marc +$use_indexer = undef if ($marc_normalize); + my $total_rows = 0; my $start_t = time(); @@ -193,9 +222,13 @@ die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); - my $lookup = new WebPAC::Lookup( - lookup_file => $input->{lookup}, - ) if ($input->{lookup}); + my $lookup; + if ($input->{lookup}) { + $lookup = new WebPAC::Lookup( + lookup_file => $input->{lookup}, + ); + delete( $input->{lookup} ); + } my $input_module = $config->{webpac}->{inputs}->{$type}; @@ -205,10 +238,13 @@ my $input_db = new WebPAC::Input( module => $input_module, - code_page => $config->{webpac}->{webpac_encoding}, + encoding => $config->{webpac}->{webpac_encoding}, limit => $limit || $input->{limit}, offset => $offset, - lookup => $lookup, + lookup_coderef => sub { + my $rec = shift || return; + $lookup->add( $rec ); + }, recode => $input->{recode}, stats => $stats, ); @@ -220,56 +256,111 @@ %{ $input }, ); - my $rules; - my $normalize_path = $input->{normalize}->{path}; + my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? + @{ $input->{normalize} } : ( $input->{normalize} ); - $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); + if ($marc_normalize) { + @norm_array = ( { + path => $marc_normalize, + output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc', + } ); + } - my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; + foreach my $normalize (@norm_array) { - foreach my $pos ( 0 ... $input_db->size ) { + my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); - 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 ); - my $mfn = $row->{'000'}->[0]; + my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; - if (! $mfn || $mfn !~ m#^\d+$#) { - $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); - $mfn = $pos; - push @{ $row->{'000'} }, $pos; - } + $log->info("Using $normalize_path for normalization..."); + + my $marc = new WebPAC::Output::MARC( + path => $normalize->{output}, + lint => $marc_lint, + dump => $marc_dump, + ) if ($normalize->{output}); + + # reset position in database + $input_db->seek(1); + + foreach my $pos ( 0 ... $input_db->size ) { + + my $row = $input_db->fetch || next; + my $mfn = $row->{'000'}->[0]; - if ($validate) { - my @errors = $validate->validate_errors( $row ); - $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); + if (! $mfn || $mfn !~ m#^\d+$#) { + $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); + $mfn = $pos; + push @{ $row->{'000'} }, $pos; + } + + + if ($validate) { + my @errors = $validate->validate_errors( $row ); + $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@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, + marc_encoding => 'utf-8', + ); + + $db->save_ds( + id => $mfn, + ds => $ds, + prefix => $input->{name}, + ) if ($ds && !$stats); + + $indexer->add( + id => $input->{name} . "/" . $mfn, + ds => $ds, + type => $config->{$use_indexer}->{type}, + ) if ($indexer && $ds); + + if ($marc) { + my $i = 0; + + while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) { + $marc->add( + id => $mfn . ( $i ? "/$i" : '' ), + fields => $fields, + leader => WebPAC::Normalize::marc_leader(), + row => $row, + ); + $i++; + } + + $log->info("Created $i instances of MFN $mfn\n") if ($i > 1); + } + + $total_rows++; } - - my $ds = WebPAC::Normalize::data_structure( - row => $row, - rules => $rules, - lookup => $lookup ? $lookup->lookup_hash : undef, - ); + $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); - $db->save_ds( - id => $mfn, - ds => $ds, - prefix => $input->{name}, - ) if ($ds && !$stats); - - $indexer->add( - id => $input->{name} . "/" . $mfn, - ds => $ds, - type => $config->{$use_indexer}->{type}, - ) if ($indexer); + # close MARC file + $marc->finish if ($marc); - $total_rows++; } - $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); - - }; + } eval { $indexer->finish } if ($indexer && $indexer->can('finish')); @@ -283,7 +374,7 @@ # # add Hyper Estraier links to other databases # - if (ref($db_config->{links}) eq 'ARRAY') { + if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { foreach my $link (@{ $db_config->{links} }) { if ($use_indexer eq 'hyperestraier') { $log->info("saving link $database -> $link->{to} [$link->{credit}]");