--- trunk/run.pl 2006/05/22 19:34:45 529 +++ trunk/run.pl 2006/06/29 15:29:41 540 @@ -11,8 +11,7 @@ use WebPAC::Lookup; use WebPAC::Input 0.03; use WebPAC::Store 0.03; -use WebPAC::Normalize::XML; -use WebPAC::Normalize::Set; +use WebPAC::Normalize; use WebPAC::Output::TT; use WebPAC::Validate; use YAML qw/LoadFile/; @@ -20,6 +19,7 @@ use File::Path; use Time::HiRes qw/time/; use File::Slurp; +use MARC::Record; =head1 NAME @@ -54,11 +54,6 @@ path to YAML configuration file -=item --force-set - -force conversion C<< normalize->path >> in C from -C<.xml> to C<.pl> - =item --stats disable indexing and dump statistics about field and subfield @@ -79,7 +74,6 @@ my $config = 'conf/config.yml'; my $debug = 0; my $only_filter; -my $force_set = 0; my $stats = 0; my $validate_path; @@ -91,7 +85,6 @@ "only=s" => \$only_filter, "config" => \$config, "debug" => \$debug, - "force-set" => \$force_set, "stats" => \$stats, "validate=s" => \$validate_path, ); @@ -228,68 +221,51 @@ %{ $input }, ); - my $n = new WebPAC::Normalize::XML( - # filter => { 'foo' => sub { shift } }, - db => $db, - lookup_regex => $lookup ? $lookup->regex : undef, - lookup => $lookup, - prefix => $input->{name}, - ); + my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? + @{ $input->{normalize} } : ( $input->{normalize} ); - my $rules; - my $normalize_path = $input->{normalize}->{path}; + foreach my $normalize (@norm_array) { - if ($force_set) { - my $new_norm_path = $normalize_path; - $new_norm_path =~ s/\.xml$/.pl/; - if (-e $new_norm_path) { - $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"); - } - } + my $rules; + my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); - if ($normalize_path =~ m/\.xml$/i) { - $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: $!"; - } + $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); - foreach my $pos ( 0 ... $input_db->size ) { + my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; - my $row = $input_db->fetch || next; + $log->info("Using $normalize_path for normalization..."); - my $mfn = $row->{'000'}->[0]; + my $marc_fh; + if (my $path = $normalize->{marc21}) { + open($marc_fh, '>', $path) || + $log->logdie("can't open MARC output $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("Creating MARC export file $path\n"); } + # reset position in database + $input_db->seek(1); - if ($validate) { - my @errors = $validate->validate_errors( $row ); - $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); - } + foreach my $pos ( 0 ... $input_db->size ) { + + my $row = $input_db->fetch || next; + + my $mfn = $row->{'000'}->[0]; - - my $ds; - if ($n) { - $ds = $n->data_structure($row); - } else { - $ds = WebPAC::Normalize::Set::data_structure( + 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, @@ -300,20 +276,30 @@ 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->add_fields( WebPAC::Normalize::_get_marc21_fields() ); + print $marc_fh $marc->as_usmarc; + } + + $total_rows++; } - $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'));