/[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 558 by dpavlin, Sun Jul 2 10:27:06 2006 UTC revision 600 by dpavlin, Thu Jul 13 21:31:32 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::Lookup;  use WebPAC::Lookup 0.03;
11  use WebPAC::Input 0.03;  use WebPAC::Input 0.07;
12  use WebPAC::Store 0.03;  use WebPAC::Store 0.03;
13  use WebPAC::Normalize;  use WebPAC::Normalize 0.11;
14  use WebPAC::Output::TT;  use WebPAC::Output::TT;
15  use WebPAC::Validate;  use WebPAC::Validate;
16    use WebPAC::Output::MARC;
17  use YAML qw/LoadFile/;  use YAML qw/LoadFile/;
18  use Getopt::Long;  use Getopt::Long;
19  use File::Path;  use File::Path;
20  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
21  use File::Slurp;  use File::Slurp;
 use MARC::Record 2.0;   # need 2.0 for utf-8 encoding see marcpm.sf.net  
 use MARC::Lint;  
22  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
23    use Storable qw/dclone/;
24    
25  =head1 NAME  =head1 NAME
26    
# Line 77  Optional path to output file Line 77  Optional path to output file
77  By default turned on if C<--marc-normalize> is used. You can disable lint  By default turned on if C<--marc-normalize> is used. You can disable lint
78  messages with C<--no-marc-lint>.  messages with C<--no-marc-lint>.
79    
80    =item --marc-dump
81    
82    Force dump or input and marc record for debugging.
83    
84  =back  =back
85    
86  =cut  =cut
# Line 92  my $stats = 0; Line 96  my $stats = 0;
96  my $validate_path;  my $validate_path;
97  my ($marc_normalize, $marc_output);  my ($marc_normalize, $marc_output);
98  my $marc_lint = 1;  my $marc_lint = 1;
99    my $marc_dump = 0;
100    
101  GetOptions(  GetOptions(
102          "limit=i" => \$limit,          "limit=i" => \$limit,
# Line 100  GetOptions( Line 105  GetOptions(
105          "one=s" => \$only_filter,          "one=s" => \$only_filter,
106          "only=s" => \$only_filter,          "only=s" => \$only_filter,
107          "config" => \$config,          "config" => \$config,
108          "debug" => \$debug,          "debug+" => \$debug,
109          "stats" => \$stats,          "stats" => \$stats,
110          "validate=s" => \$validate_path,          "validate=s" => \$validate_path,
111          "marc-normalize=s" => \$marc_normalize,          "marc-normalize=s" => \$marc_normalize,
112          "marc-output=s" => \$marc_output,          "marc-output=s" => \$marc_output,
113          "marc-lint!" => \$marc_lint,          "marc-lint!" => \$marc_lint,
114            "marc-dump!" => \$marc_dump,
115  );  );
116    
117  $config = LoadFile($config);  $config = LoadFile($config);
# Line 139  my $start_t = time(); Line 145  my $start_t = time();
145  my @links;  my @links;
146  my $indexer;  my $indexer;
147    
 my $lint = new MARC::Lint if ($marc_lint);  
   
148  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
149    
150          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
# Line 218  while (my ($database, $db_config) = each Line 222  while (my ($database, $db_config) = each
222    
223                  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(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
224    
225                  my $lookup = new WebPAC::Lookup(                  my $lookup;
226                          lookup_file => $input->{lookup},                  if ($input->{lookup}) {
227                  ) if ($input->{lookup});                          $lookup = new WebPAC::Lookup(
228                                    lookup_file => $input->{lookup},
229                            );
230                            delete( $input->{lookup} );
231                    }
232    
233                  my $input_module = $config->{webpac}->{inputs}->{$type};                  my $input_module = $config->{webpac}->{inputs}->{$type};
234    
# Line 230  while (my ($database, $db_config) = each Line 238  while (my ($database, $db_config) = each
238    
239                  my $input_db = new WebPAC::Input(                  my $input_db = new WebPAC::Input(
240                          module => $input_module,                          module => $input_module,
241                          code_page => $config->{webpac}->{webpac_encoding},                          encoding => $config->{webpac}->{webpac_encoding},
242                          limit => $limit || $input->{limit},                          limit => $limit || $input->{limit},
243                          offset => $offset,                          offset => $offset,
244                          lookup => $lookup,                          lookup_coderef => sub {
245                                    my $rec = shift || return;
246                                    $lookup->add( $rec );
247                            },
248                          recode => $input->{recode},                          recode => $input->{recode},
249                          stats => $stats,                          stats => $stats,
250                            modify_records => $input->{modify_records},
251                  );                  );
252                  $log->logdie("can't create input using $input_module") unless ($input);                  $log->logdie("can't create input using $input_module") unless ($input);
253    
# Line 265  while (my ($database, $db_config) = each Line 277  while (my ($database, $db_config) = each
277    
278                          $log->info("Using $normalize_path for normalization...");                          $log->info("Using $normalize_path for normalization...");
279    
280                          my $marc_fh;                          my $marc = new WebPAC::Output::MARC(
281                          if (my $path = $normalize->{output}) {                                  path => $normalize->{output},
282                                  open($marc_fh, '>', $path) ||                                  lint => $marc_lint,
283                                          $log->logdie("can't open MARC output $path: $!");                                  dump => $marc_dump,
284                            ) if ($normalize->{output});
                                 $log->info("Creating MARC export file $path", $marc_lint ? ' (with lint)' : '', "\n");  
                         }  
285    
286                          # reset position in database                          # reset position in database
287                          $input_db->seek(1);                          $input_db->seek(1);
# Line 294  while (my ($database, $db_config) = each Line 304  while (my ($database, $db_config) = each
304                                          $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);                                          $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
305                                  }                                  }
306    
307                                                                            my $ds_config = dclone($db_config);
308    
309                                    # default values -> database key
310                                    $ds_config->{_} = $database;
311    
312                                    # current mfn
313                                    $ds_config->{_mfn} = $mfn;
314    
315                                    # attach current input
316                                    $ds_config->{input} = $input;
317    
318                                  my $ds = WebPAC::Normalize::data_structure(                                  my $ds = WebPAC::Normalize::data_structure(
319                                          row => $row,                                          row => $row,
320                                          rules => $rules,                                          rules => $rules,
321                                          lookup => $lookup ? $lookup->lookup_hash : undef,                                          lookup => $lookup ? $lookup->lookup_hash : undef,
322                                            config => $ds_config,
323                                          marc_encoding => 'utf-8',                                          marc_encoding => 'utf-8',
324                                  );                                  );
325    
# Line 314  while (my ($database, $db_config) = each Line 335  while (my ($database, $db_config) = each
335                                          type => $config->{$use_indexer}->{type},                                          type => $config->{$use_indexer}->{type},
336                                  ) if ($indexer && $ds);                                  ) if ($indexer && $ds);
337    
338                                  if ($marc_fh) {                                  if ($marc) {
339                                          my $marc = new MARC::Record;                                          my $i = 0;
340                                          $marc->encoding( 'utf-8' );  
341                                          my @marc_fields = WebPAC::Normalize::_get_marc_fields();                                          while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
342                                          if (! @marc_fields) {                                                  $marc->add(
343                                                  $log->warn("MARC record $mfn is empty, skipping");                                                          id => $mfn . ( $i ? "/$i" : '' ),
344                                          } else {                                                          fields => $fields,
345                                                  $marc->add_fields( @marc_fields );                                                          leader => WebPAC::Normalize::marc_leader(),
346                                                  if ($marc_lint) {                                                          row => $row,
347                                                          $lint->check_record( $marc );                                                  );
348                                                          my $err = join( "\n", $lint->warnings );                                                  $i++;
                                                         $log->error("MARC lint detected warning on MFN $mfn\n",  
                                                                 "Original imput row: ",dump($row), "\n",  
                                                                 "Normalized MARC row: ",dump(@marc_fields), "\n",  
                                                                 "MARC lint warnings: ",$err,"\n"  
                                                         ) if ($err);  
                                                 }  
                                                 print $marc_fh $marc->as_usmarc;  
349                                          }                                          }
350    
351                                            $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
352                                  }                                  }
353    
354                                  $total_rows++;                                  $total_rows++;
# Line 341  while (my ($database, $db_config) = each Line 357  while (my ($database, $db_config) = each
357                          $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);                          $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
358    
359                          # close MARC file                          # close MARC file
360                          close($marc_fh) if ($marc_fh);                          $marc->finish if ($marc);
361    
362                  }                  }
363    

Legend:
Removed from v.558  
changed lines
  Added in v.600

  ViewVC Help
Powered by ViewVC 1.1.26