--- trunk/run.pl 2006/06/26 16:39:51 536 +++ trunk/run.pl 2006/06/29 23:19:26 547 @@ -19,6 +19,7 @@ use File::Path; use Time::HiRes qw/time/; use File::Slurp; +use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net =head1 NAME @@ -220,56 +221,86 @@ %{ $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 ); + foreach my $normalize (@norm_array) { - my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; + my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); - foreach my $pos ( 0 ... $input_db->size ) { + $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); - my $row = $input_db->fetch || next; + my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; - my $mfn = $row->{'000'}->[0]; + $log->info("Using $normalize_path for normalization..."); - if (! $mfn || $mfn !~ m#^\d+$#) { - $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); - $mfn = $pos; - push @{ $row->{'000'} }, $pos; + my $marc_fh; + if (my $path = $normalize->{marc21}) { + open($marc_fh, '>', $path) || + $log->logdie("can't open MARC output $path: $!"); + + $log->info("Creating MARC export file $path\n"); } + # 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 = WebPAC::Normalize::data_structure( + row => $row, + rules => $rules, + lookup => $lookup ? $lookup->lookup_hash : undef, + 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_fh) { + my $marc = new MARC::Record; + $marc->encoding( 'utf-8' ); + $marc->add_fields( WebPAC::Normalize::_get_marc_fields() ); + print $marc_fh $marc->as_usmarc; + } + + $total_rows++; } - - my $ds = WebPAC::Normalize::data_structure( - row => $row, - rules => $rules, - lookup => $lookup ? $lookup->lookup_hash : undef, - ); - - $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); + $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); - $total_rows++; - } + # close MARC file + close($marc_fh) if ($marc_fh); - $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); + } - }; + } eval { $indexer->finish } if ($indexer && $indexer->can('finish'));