/[webpac2]/trunk/run.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/run.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 712 by dpavlin, Tue Sep 26 10:23:04 2006 UTC revision 774 by dpavlin, Fri Nov 3 20:56:21 2006 UTC
# Line 7  use File::Temp qw/tempdir/; Line 7  use File::Temp qw/tempdir/;
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;
# Line 67  and subfield usage for each input Line 67  and subfield usage for each input
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
# Line 110  my $debug = 0; Line 102  my $debug = 0;
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;
# Line 129  GetOptions( Line 120  GetOptions(
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,
# Line 164  $validate = new WebPAC::Validate( Line 153  $validate = new WebPAC::Validate(
153    
154    
155  my $use_indexer = $config->use_indexer;  my $use_indexer = $config->use_indexer;
156    $stats ||= $validate;
157  if ($stats) {  if ($stats) {
158          $log->debug("option --stats disables update of indexing engine...");          $log->debug("disabled indexing for stats collection");
159          $use_indexer = undef;          $use_indexer = undef;
160  } else {  } else {
161          $log->info("using $use_indexer indexing engine...");          $log->info("using $use_indexer indexing engine...");
162  }  }
163    
 # disable indexing when creating marc  
 $use_indexer = undef if ($marc_normalize);  
   
164  # parse normalize files and create source files for lookup and normalization  # parse normalize files and create source files for lookup and normalization
165    
166  my $parser = new WebPAC::Parser( config => $config );  my $parser = new WebPAC::Parser( config => $config );
# Line 212  while (my ($database, $db_config) = each Line 199  while (my ($database, $db_config) = each
199          }          }
200    
201          my $indexer;          my $indexer;
202          if ($use_indexer) {          if ($use_indexer && $parser->have_rules( 'search', $database )) {
203    
204                  my $cfg_name = $use_indexer;                  my $cfg_name = $use_indexer;
205                  $cfg_name =~ s/\-.*$//;                  $cfg_name =~ s/\-.*$//;
# Line 285  while (my ($database, $db_config) = each Line 272  while (my ($database, $db_config) = each
272          my $abs_path = abs_path($0);          my $abs_path = abs_path($0);
273          $abs_path =~ s#/[^/]*$#/#;          $abs_path =~ s#/[^/]*$#/#;
274    
275          my $db_path = $config->get('webpac')->{db_path} . '/' . $database;          my $db_path = $config->webpac('db_path');
276    
277          if ($clean) {          if ($clean) {
278                  $log->info("creating new database '$database' in $db_path");                  $log->info("creating new database '$database' in $db_path");
# Line 296  while (my ($database, $db_config) = each Line 283  while (my ($database, $db_config) = each
283    
284          my $store = new WebPAC::Store(          my $store = new WebPAC::Store(
285                  path => $db_path,                  path => $db_path,
                 database => $database,  
286                  debug => $debug,                  debug => $debug,
287          );          );
288    
# Line 355  while (my ($database, $db_config) = each Line 341  while (my ($database, $db_config) = each
341                          delete( $input->{lookup} );                          delete( $input->{lookup} );
342                  }                  }
343    
                 my $lookup;  
344                  my $lookup_coderef;                  my $lookup_coderef;
345    
346                  if (@lookups) {                  if (@lookups) {
# Line 369  while (my ($database, $db_config) = each Line 354  while (my ($database, $db_config) = each
354                                  WebPAC::Normalize::data_structure(                                  WebPAC::Normalize::data_structure(
355                                          row => $rec,                                          row => $rec,
356                                          rules => $rules,                                          rules => $rules,
                                         lookup => $lookup,  
357                                          config => create_ds_config( $db_config, $database, $input, $mfn ),                                          config => create_ds_config( $db_config, $database, $input, $mfn ),
358                                  );                                  );
359    
360                                  warn "current lookup = ", dump($lookup) if ($lookup);                                  #warn "current lookup: ", dump(WebPAC::Normalize::_get_lookup());
361                          };                          };
362    
363                          WebPAC::Normalize::_set_lookup( undef );                          WebPAC::Normalize::_set_lookup( undef );
# Line 382  while (my ($database, $db_config) = each Line 366  while (my ($database, $db_config) = each
366    
367                  };                  };
368    
369                    my $lookup_jar;
370    
371                  my $maxmfn = $input_db->open(                  my $maxmfn = $input_db->open(
372                          path => $input->{path},                          path => $input->{path},
373                          code_page => $input->{encoding},        # database encoding                          code_page => $input->{encoding},        # database encoding
374                          lookup_coderef => $lookup_coderef,                          lookup_coderef => $lookup_coderef,
375                            lookup => $lookup_jar,
376                          %{ $input },                          %{ $input },
377                            load_row => sub {
378                                    my $a = shift;
379                                    return $store->load_row(
380                                            database => $database,
381                                            input => $input_name,
382                                            id => $a->{id},
383                                    );
384                            },
385                            save_row => sub {
386                                    my $a = shift;
387                                    return $store->save_row(
388                                            database => $database,
389                                            input => $input_name,
390                                            id => $a->{id},
391                                            row => $a->{row},
392                                    );
393                            },
394    
395                  );                  );
396    
397                  my $lookup_data = WebPAC::Normalize::_get_lookup();                  my $lookup_data = WebPAC::Normalize::_get_lookup();
398    
399                  if (defined( $lookup_data->{$database}->{$input_name} )) {                  if (defined( $lookup_data->{$database}->{$input_name} )) {
400                          $log->debug("created following lookups: ", dump( $lookup_data ));                          $log->debug("created following lookups: ", sub { dump( $lookup_data ) } );
401    
402                          foreach my $key (keys %{ $lookup_data->{$database}->{$input_name} }) {                          foreach my $key (keys %{ $lookup_data->{$database}->{$input_name} }) {
403                                  $store->save_lookup( $database, $input_name, $key, $lookup_data->{$database}->{$input_name}->{$key} );                                  $store->save_lookup(
404                                            database => $database,
405                                            input => $input_name,
406                                            key => $key,
407                                            data => $lookup_data->{$database}->{$input_name}->{$key},
408                                    );
409                          }                          }
410                  }                  }
411    
# Line 409  while (my ($database, $db_config) = each Line 419  while (my ($database, $db_config) = each
419                          $log->info("Generating report file $path");                          $log->info("Generating report file $path");
420                  }                  }
421    
422                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?                  my $marc;
423                          @{ $input->{normalize} } : ( $input->{normalize} );                  if ($parser->have_rules( 'marc', $database, $input_name )) {
424                            $marc = new WebPAC::Output::MARC(
425                  if ($marc_normalize) {                                  path => "out/marc/${database}-${input_name}.marc",
426                          @norm_array = ( {                                  lint => $marc_lint,
427                                  path => $marc_normalize,                                  dump => $marc_dump,
428                                  output => $marc_output || "out/marc/${database}-${input_name}.marc",                          );
                         } );  
429                  }                  }
430    
431                  foreach my $normalize (@norm_array) {                  my $rules = $parser->normalize_rules($database,$input_name) || $log->logdie("no normalize rules found for $database/$input_name");
432                    $log->debug("parsed normalize rules:\n$rules");
                         my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");  
433    
434                          $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );                  # reset position in database
435                    $input_db->seek(1);
436    
437                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";                  # generate name of config key for indexer (strip everything after -)
438                    my $indexer_config = $use_indexer;
439                          $log->info("Using $normalize_path for normalization...");                  $indexer_config =~ s/^(\w+)-?.*$/$1/g if ($indexer_config);
440    
441                    my $lookup_hash;
442                    my $depends = $parser->depends($database,$input_name);
443            
444                    if ($depends) {
445                            $log->debug("$database/$input_name depends on: ", dump($depends)) if ($depends);
446                            $log->logdie("parser->depends didn't return HASH") unless (ref($depends) eq 'HASH');
447    
448                            foreach my $db (keys %$depends) {
449                                    foreach my $i (keys %{$depends->{$db}}) {
450                                            foreach my $k (keys %{$depends->{$db}->{$i}}) {
451                                                    my $t = time();
452                                                    $log->debug("loading lookup $db/$i");
453                                                    $lookup_hash->{$db}->{$i}->{$k} = $store->load_lookup(
454                                                            database => $db,
455                                                            input => $i,
456                                                            key => $k,
457                                                    );
458                                                    $log->debug(sprintf("lookup $db/$i took %.2fs", time() - $t));
459                                            }
460                                    }
461                            }
462    
463                          my $marc = new WebPAC::Output::MARC(                          $log->debug("lookup_hash = ", sub { dump( $lookup_hash ) });
464                                  path => $normalize->{output},                  }
                                 lint => $marc_lint,  
                                 dump => $marc_dump,  
                         ) if ($normalize->{output});  
465    
                         # reset position in database  
                         $input_db->seek(1);  
466    
467                          # 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);  
468    
469                          foreach my $pos ( 0 ... $input_db->size ) {                          my $row = $input_db->fetch || next;
470    
471                                  my $row = $input_db->fetch || next;                          $total_rows++;
472    
473                                  my $mfn = $row->{'000'}->[0];                          my $mfn = $row->{'000'}->[0];
474    
475                                  if (! $mfn || $mfn !~ m#^\d+$#) {                          if (! $mfn || $mfn !~ m#^\d+$#) {
476                                          $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");
477                                          $mfn = $pos;                                  $mfn = $pos;
478                                          push @{ $row->{'000'} }, $pos;                                  push @{ $row->{'000'} }, $pos;
479                                  }                          }
480    
481    
482                                  if ($validate) {                          if ($validate) {
483                                          if ( my $errors = $validate->validate_errors( $row, $input_db->dump ) ) {                                  if ( my $errors = $validate->validate_rec( $row, $input_db->dump_ascii ) ) {
484                                                  $log->error( "MFN $mfn validation error:\n",                                          $log->error( "MFN $mfn validation error:\n",
485                                                          $validate->report_error( $errors )                                                  $validate->report_error( $errors )
486                                                  );                                          );
                                         }  
487                                  }                                  }
488                                    next;   # validation doesn't create any output
489                            }
490    
491                                  my $ds = WebPAC::Normalize::data_structure(                          my $ds = WebPAC::Normalize::data_structure(
492                                          row => $row,                                  row => $row,
493                                          rules => $rules,                                  rules => $rules,
494                                          lookup => $lookup ? $lookup->lookup_hash : undef,                                  lookup => $lookup_hash,
495                                          config => create_ds_config( $db_config, $database, $input, $mfn ),                                  config => create_ds_config( $db_config, $database, $input, $mfn ),
496                                          marc_encoding => 'utf-8',                                  marc_encoding => 'utf-8',
497                                  );                                  load_row_coderef => sub {
498                                            my ($database,$input,$mfn) = @_;
499                                  $store->save_ds(                                          return $store->load_row(
500                                          id => $mfn,                                                  database => $database,
501                                          ds => $ds,                                                  input => $input,
502                                          prefix => $input_name,                                                  id => $mfn,
503                                  ) if ($ds && !$stats);                                          );
504                                    },
505                                  $indexer->add(                          );
506                                          id => "${input_name}/${mfn}",  
507                                          ds => $ds,                          $log->debug("ds = ", sub { dump($ds) }) if ($ds);
508                                          type => $config->get($indexer_config)->{type},  
509                                  ) if ($indexer && $ds);                          $store->save_ds(
510                                    database => $database,
511                                  if ($marc) {                                  input => $input_name,
512                                          my $i = 0;                                  id => $mfn,
513                                    ds => $ds,
514                                          while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {                          ) if ($ds && !$stats);
515                                                  $marc->add(  
516                                                          id => $mfn . ( $i ? "/$i" : '' ),                          $indexer->add(
517                                                          fields => $fields,                                  id => "${input_name}/${mfn}",
518                                                          leader => WebPAC::Normalize::marc_leader(),                                  ds => $ds,
519                                                          row => $row,                                  type => $config->get($indexer_config)->{type},
520                                                  );                          ) if ($indexer && $ds);
521                                                  $i++;  
522                                          }                          if ($marc) {
523                                    my $i = 0;
524                                          $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);  
525                                    while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
526                                            $marc->add(
527                                                    id => $mfn . ( $i ? "/$i" : '' ),
528                                                    fields => $fields,
529                                                    leader => WebPAC::Normalize::marc_leader(),
530                                                    row => $row,
531                                            );
532                                            $i++;
533                                  }                                  }
534    
535                                  $total_rows++;                                  $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
536                          }                          }
537                    }
538    
539                          if ($validate) {                  if ($validate) {
540                                  my $errors = $validate->report;                          my $errors = $validate->report;
541                                  if ($errors) {                          if ($errors) {
542                                          $log->info("validation errors:\n$errors\n" );                                  $log->info("validation errors:\n$errors\n" );
543                                          print $report_fh "$errors\n" if ($report_fh);                                  print $report_fh "$errors\n" if ($report_fh);
                                 }  
544                          }                          }
545                    }
546    
547                          if ($stats) {                  if ($stats) {
548                                  my $s = $input_db->stats;                          my $s = $input_db->stats;
549                                  $log->info("statistics of fields usage:\n$s");                          $log->info("statistics of fields usage:\n$s");
550                                  print $report_fh "Statistics of fields usage:\n$s" if ($report_fh);                          print $report_fh "Statistics of fields usage:\n$s" if ($report_fh);
551                          }                  }
552    
553                          # close MARC file                  # close MARC file
554                          $marc->finish if ($marc);                  $marc->finish if ($marc);
555    
556                          # close report                  # close report
557                          close($report_fh) if ($report_fh)                  close($report_fh) if ($report_fh)
                 }  
558    
559          }          }
560    

Legend:
Removed from v.712  
changed lines
  Added in v.774

  ViewVC Help
Powered by ViewVC 1.1.26