/[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 523 by dpavlin, Sun May 21 19:29:26 2006 UTC revision 558 by dpavlin, Sun Jul 2 10:27:06 2006 UTC
# Line 4  use strict; Line 4  use strict;
4    
5  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
6  use File::Temp qw/tempdir/;  use File::Temp qw/tempdir/;
 use Data::Dumper;  
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;
11  use WebPAC::Input 0.03;  use WebPAC::Input 0.03;
12  use WebPAC::Store 0.03;  use WebPAC::Store 0.03;
13  use WebPAC::Normalize::XML;  use WebPAC::Normalize;
 use WebPAC::Normalize::Set;  
14  use WebPAC::Output::TT;  use WebPAC::Output::TT;
15  use WebPAC::Validate;  use WebPAC::Validate;
16  use YAML qw/LoadFile/;  use YAML qw/LoadFile/;
# Line 20  use Getopt::Long; Line 18  use Getopt::Long;
18  use File::Path;  use File::Path;
19  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
20  use File::Slurp;  use File::Slurp;
21    use MARC::Record 2.0;   # need 2.0 for utf-8 encoding see marcpm.sf.net
22    use MARC::Lint;
23    use Data::Dump qw/dump/;
24    
25  =head1 NAME  =head1 NAME
26    
# Line 54  or C<type> from input Line 55  or C<type> from input
55    
56  path to YAML configuration file  path to YAML configuration file
57    
 =item --force-set  
   
 force conversion C<< normalize->path >> in C<config.yml> from  
 C<.xml> to C<.pl>  
   
58  =item --stats  =item --stats
59    
60  disable indexing and dump statistics about field and subfield  disable indexing and dump statistics about field and subfield
# Line 68  usage for each input Line 64  usage for each input
64    
65  turn on extra validation of imput records, see L<WebPAC::Validation>  turn on extra validation of imput records, see L<WebPAC::Validation>
66    
67    =item --marc-normalize conf/normalize/mapping.pl
68    
69    This option specifies normalisation file for MARC creation
70    
71    =item --marc-output out/marc/test.marc
72    
73    Optional path to output file
74    
75    =item --marc-lint
76    
77    By default turned on if C<--marc-normalize> is used. You can disable lint
78    messages with C<--no-marc-lint>.
79    
80  =back  =back
81    
82  =cut  =cut
# Line 79  my $clean = 0; Line 88  my $clean = 0;
88  my $config = 'conf/config.yml';  my $config = 'conf/config.yml';
89  my $debug = 0;  my $debug = 0;
90  my $only_filter;  my $only_filter;
 my $force_set = 0;  
91  my $stats = 0;  my $stats = 0;
92  my $validate_path;  my $validate_path;
93    my ($marc_normalize, $marc_output);
94    my $marc_lint = 1;
95    
96  GetOptions(  GetOptions(
97          "limit=i" => \$limit,          "limit=i" => \$limit,
# Line 91  GetOptions( Line 101  GetOptions(
101          "only=s" => \$only_filter,          "only=s" => \$only_filter,
102          "config" => \$config,          "config" => \$config,
103          "debug" => \$debug,          "debug" => \$debug,
         "force-set" => \$force_set,  
104          "stats" => \$stats,          "stats" => \$stats,
105          "validate=s" => \$validate_path,          "validate=s" => \$validate_path,
106            "marc-normalize=s" => \$marc_normalize,
107            "marc-output=s" => \$marc_output,
108            "marc-lint!" => \$marc_lint,
109  );  );
110    
111  $config = LoadFile($config);  $config = LoadFile($config);
112    
113  print "config = ",Dumper($config) if ($debug);  print "config = ",dump($config) if ($debug);
114    
115  die "no databases in config file!\n" unless ($config->{databases});  die "no databases in config file!\n" unless ($config->{databases});
116    
# Line 118  if ($stats) { Line 130  if ($stats) {
130          $log->info("using $use_indexer indexing engine...");          $log->info("using $use_indexer indexing engine...");
131  }  }
132    
133    # disable indexing when creating marc
134    $use_indexer = undef if ($marc_normalize);
135    
136  my $total_rows = 0;  my $total_rows = 0;
137  my $start_t = time();  my $start_t = time();
138    
139    my @links;
140    my $indexer;
141    
142    my $lint = new MARC::Lint if ($marc_lint);
143    
144  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
145    
146          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
147          next if ($only_database && $database !~ m/$only_database/i);          next if ($only_database && $database !~ m/$only_database/i);
148    
         my $indexer;  
   
149          if ($use_indexer) {          if ($use_indexer) {
150                  my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");                  my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
151                  $indexer_config->{database} = $database;                  $indexer_config->{database} = $database;
# Line 227  while (my ($database, $db_config) = each Line 245  while (my ($database, $db_config) = each
245                          %{ $input },                          %{ $input },
246                  );                  );
247    
248                  my $n = new WebPAC::Normalize::XML(                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
249                  #       filter => { 'foo' => sub { shift } },                          @{ $input->{normalize} } : ( $input->{normalize} );
                         db => $db,  
                         lookup_regex => $lookup ? $lookup->regex : undef,  
                         lookup => $lookup,  
                         prefix => $input->{name},  
                 );  
250    
251                  my $rules;                  if ($marc_normalize) {
252                  my $normalize_path = $input->{normalize}->{path};                          @norm_array = ( {
253                                    path => $marc_normalize,
254                  if ($force_set) {                                  output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
255                          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");  
                         }  
256                  }                  }
257    
258                  if ($normalize_path =~ m/\.xml$/i) {                  foreach my $normalize (@norm_array) {
                         $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: $!";  
                 }  
259    
260                  foreach my $pos ( 0 ... $input_db->size ) {                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
261    
262                          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 );
263    
264                          my $mfn = $row->{'000'}->[0];                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
265    
266                          if (! $mfn || $mfn !~ m#^\d+$#) {                          $log->info("Using $normalize_path for normalization...");
                                 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");  
                                 $mfn = $pos;  
                                 push @{ $row->{'000'} }, $pos;  
                         }  
267    
268                            my $marc_fh;
269                            if (my $path = $normalize->{output}) {
270                                    open($marc_fh, '>', $path) ||
271                                            $log->logdie("can't open MARC output $path: $!");
272    
273                          if ($validate) {                                  $log->info("Creating MARC export file $path", $marc_lint ? ' (with lint)' : '', "\n");
                                 my @errors = $validate->validate_errors( $row );  
                                 $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);  
274                          }                          }
275    
276                                                            # reset position in database
277                          my $ds;                          $input_db->seek(1);
278                          if ($n) {  
279                                  $ds = $n->data_structure($row);                          foreach my $pos ( 0 ... $input_db->size ) {
280                          } else {  
281                                  $ds = WebPAC::Normalize::Set::data_structure(                                  my $row = $input_db->fetch || next;
282    
283                                    my $mfn = $row->{'000'}->[0];
284    
285                                    if (! $mfn || $mfn !~ m#^\d+$#) {
286                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
287                                            $mfn = $pos;
288                                            push @{ $row->{'000'} }, $pos;
289                                    }
290    
291    
292                                    if ($validate) {
293                                            my @errors = $validate->validate_errors( $row );
294                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
295                                    }
296    
297                                            
298                                    my $ds = WebPAC::Normalize::data_structure(
299                                          row => $row,                                          row => $row,
300                                          rules => $rules,                                          rules => $rules,
301                                          lookup => $lookup ? $lookup->lookup_hash : undef,                                          lookup => $lookup ? $lookup->lookup_hash : undef,
302                                            marc_encoding => 'utf-8',
303                                  );                                  );
304    
305                                  $db->save_ds(                                  $db->save_ds(
# Line 299  while (my ($database, $db_config) = each Line 307  while (my ($database, $db_config) = each
307                                          ds => $ds,                                          ds => $ds,
308                                          prefix => $input->{name},                                          prefix => $input->{name},
309                                  ) if ($ds && !$stats);                                  ) if ($ds && !$stats);
310    
311                                    $indexer->add(
312                                            id => $input->{name} . "/" . $mfn,
313                                            ds => $ds,
314                                            type => $config->{$use_indexer}->{type},
315                                    ) if ($indexer && $ds);
316    
317                                    if ($marc_fh) {
318                                            my $marc = new MARC::Record;
319                                            $marc->encoding( 'utf-8' );
320                                            my @marc_fields = WebPAC::Normalize::_get_marc_fields();
321                                            if (! @marc_fields) {
322                                                    $log->warn("MARC record $mfn is empty, skipping");
323                                            } else {
324                                                    $marc->add_fields( @marc_fields );
325                                                    if ($marc_lint) {
326                                                            $lint->check_record( $marc );
327                                                            my $err = join( "\n", $lint->warnings );
328                                                            $log->error("MARC lint detected warning on MFN $mfn\n",
329                                                                    "Original imput row: ",dump($row), "\n",
330                                                                    "Normalized MARC row: ",dump(@marc_fields), "\n",
331                                                                    "MARC lint warnings: ",$err,"\n"
332                                                            ) if ($err);
333                                                    }
334                                                    print $marc_fh $marc->as_usmarc;
335                                            }
336                                    }
337    
338                                    $total_rows++;
339                          }                          }
340    
341                          $indexer->add(                          $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
                                 id => $input->{name} . "/" . $mfn,  
                                 ds => $ds,  
                                 type => $config->{$use_indexer}->{type},  
                         ) if ($indexer);  
342    
343                          $total_rows++;                          # close MARC file
344                  }                          close($marc_fh) if ($marc_fh);
345    
346                  $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);                  }
347    
348          };          }
349    
350          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
351    
# Line 326  while (my ($database, $db_config) = each Line 359  while (my ($database, $db_config) = each
359          #          #
360          # add Hyper Estraier links to other databases          # add Hyper Estraier links to other databases
361          #          #
362          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
363                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
364                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
365                                  $log->info("adding link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");
366                                  $indexer->add_link(                                  push @links, {
367                                          from => $database,                                          from => $database,
368                                          to => $link->{to},                                          to => $link->{to},
369                                          credit => $link->{credit},                                          credit => $link->{credit},
370                                  );                                  };
371                          } else {                          } else {
372                                  $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");                                  $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
373                          }                          }
# Line 343  while (my ($database, $db_config) = each Line 376  while (my ($database, $db_config) = each
376    
377  }  }
378    
379    foreach my $link (@links) {
380            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
381            $indexer->add_link( %{ $link } );
382    }

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

  ViewVC Help
Powered by ViewVC 1.1.26