/[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 536 by dpavlin, Mon Jun 26 16:39:51 2006 UTC revision 552 by dpavlin, Fri Jun 30 20:43:18 2006 UTC
# Line 19  use Getopt::Long; Line 19  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;
22    use MARC::Record 2.0;   # need 2.0 for utf-8 encoding see marcpm.sf.net
23    
24  =head1 NAME  =head1 NAME
25    
# Line 62  usage for each input Line 63  usage for each input
63    
64  turn on extra validation of imput records, see L<WebPAC::Validation>  turn on extra validation of imput records, see L<WebPAC::Validation>
65    
66    =item --marc-normalize conf/normalize/mapping.pl
67    
68    This option specifies normalisation file for MARC creation
69    
70    =item --marc-output out/marc/test.marc
71    
72    Optional path to output file
73    
74  =back  =back
75    
76  =cut  =cut
# Line 75  my $debug = 0; Line 84  my $debug = 0;
84  my $only_filter;  my $only_filter;
85  my $stats = 0;  my $stats = 0;
86  my $validate_path;  my $validate_path;
87    my ($marc_normalize, $marc_output);
88    
89  GetOptions(  GetOptions(
90          "limit=i" => \$limit,          "limit=i" => \$limit,
# Line 86  GetOptions( Line 96  GetOptions(
96          "debug" => \$debug,          "debug" => \$debug,
97          "stats" => \$stats,          "stats" => \$stats,
98          "validate=s" => \$validate_path,          "validate=s" => \$validate_path,
99            "marc-normalize=s" => \$marc_normalize,
100            "marc-output=s" => \$marc_output,
101  );  );
102    
103  $config = LoadFile($config);  $config = LoadFile($config);
# Line 110  if ($stats) { Line 122  if ($stats) {
122          $log->info("using $use_indexer indexing engine...");          $log->info("using $use_indexer indexing engine...");
123  }  }
124    
125    # disable indexing when creating marc
126    $use_indexer = undef if ($marc_normalize);
127    
128  my $total_rows = 0;  my $total_rows = 0;
129  my $start_t = time();  my $start_t = time();
130    
# Line 220  while (my ($database, $db_config) = each Line 235  while (my ($database, $db_config) = each
235                          %{ $input },                          %{ $input },
236                  );                  );
237    
238                  my $rules;                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
239                  my $normalize_path = $input->{normalize}->{path};                          @{ $input->{normalize} } : ( $input->{normalize} );
240    
241                    if ($marc_normalize) {
242                            @norm_array = ( {
243                                    path => $marc_normalize,
244                                    output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
245                            } );
246                    }
247    
248                    foreach my $normalize (@norm_array) {
249    
250                  $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
251    
252                  my $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 );
253    
254                  foreach my $pos ( 0 ... $input_db->size ) {                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
255    
256                          my $row = $input_db->fetch || next;                          $log->info("Using $normalize_path for normalization...");
257    
258                          my $mfn = $row->{'000'}->[0];                          my $marc_fh;
259                            if (my $path = $normalize->{output}) {
260                                    open($marc_fh, '>', $path) ||
261                                            $log->logdie("can't open MARC output $path: $!");
262    
263                          if (! $mfn || $mfn !~ m#^\d+$#) {                                  $log->info("Creating MARC export file $path\n");
                                 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");  
                                 $mfn = $pos;  
                                 push @{ $row->{'000'} }, $pos;  
264                          }                          }
265    
266                            # reset position in database
267                            $input_db->seek(1);
268    
269                          if ($validate) {                          foreach my $pos ( 0 ... $input_db->size ) {
270                                  my @errors = $validate->validate_errors( $row );  
271                                  $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);                                  my $row = $input_db->fetch || next;
272    
273                                    my $mfn = $row->{'000'}->[0];
274    
275                                    if (! $mfn || $mfn !~ m#^\d+$#) {
276                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
277                                            $mfn = $pos;
278                                            push @{ $row->{'000'} }, $pos;
279                                    }
280    
281    
282                                    if ($validate) {
283                                            my @errors = $validate->validate_errors( $row );
284                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
285                                    }
286    
287                                            
288                                    my $ds = WebPAC::Normalize::data_structure(
289                                            row => $row,
290                                            rules => $rules,
291                                            lookup => $lookup ? $lookup->lookup_hash : undef,
292                                            marc_encoding => 'utf-8',
293                                    );
294    
295                                    $db->save_ds(
296                                            id => $mfn,
297                                            ds => $ds,
298                                            prefix => $input->{name},
299                                    ) if ($ds && !$stats);
300    
301                                    $indexer->add(
302                                            id => $input->{name} . "/" . $mfn,
303                                            ds => $ds,
304                                            type => $config->{$use_indexer}->{type},
305                                    ) if ($indexer && $ds);
306    
307                                    if ($marc_fh) {
308                                            my $marc = new MARC::Record;
309                                            $marc->encoding( 'utf-8' );
310                                            $marc->add_fields( WebPAC::Normalize::_get_marc_fields() );
311                                            print $marc_fh $marc->as_usmarc;
312                                    }
313    
314                                    $total_rows++;
315                          }                          }
316    
317                                                            $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
                         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);  
318    
319                          $total_rows++;                          # close MARC file
320                  }                          close($marc_fh) if ($marc_fh);
321    
322                  $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);                  }
323    
324          };          }
325    
326          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
327    
# Line 283  while (my ($database, $db_config) = each Line 335  while (my ($database, $db_config) = each
335          #          #
336          # add Hyper Estraier links to other databases          # add Hyper Estraier links to other databases
337          #          #
338          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
339                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
340                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
341                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");

Legend:
Removed from v.536  
changed lines
  Added in v.552

  ViewVC Help
Powered by ViewVC 1.1.26