/[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 588 by dpavlin, Fri Jul 7 21:47:13 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 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;
22    use Data::Dump qw/dump/;
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    =item --marc-lint
75    
76    By default turned on if C<--marc-normalize> is used. You can disable lint
77    messages with C<--no-marc-lint>.
78    
79    =item --marc-dump
80    
81    Force dump or input and marc record for debugging.
82    
83  =back  =back
84    
85  =cut  =cut
# Line 75  my $debug = 0; Line 93  my $debug = 0;
93  my $only_filter;  my $only_filter;
94  my $stats = 0;  my $stats = 0;
95  my $validate_path;  my $validate_path;
96    my ($marc_normalize, $marc_output);
97    my $marc_lint = 1;
98    my $marc_dump = 0;
99    
100  GetOptions(  GetOptions(
101          "limit=i" => \$limit,          "limit=i" => \$limit,
# Line 83  GetOptions( Line 104  GetOptions(
104          "one=s" => \$only_filter,          "one=s" => \$only_filter,
105          "only=s" => \$only_filter,          "only=s" => \$only_filter,
106          "config" => \$config,          "config" => \$config,
107          "debug" => \$debug,          "debug+" => \$debug,
108          "stats" => \$stats,          "stats" => \$stats,
109          "validate=s" => \$validate_path,          "validate=s" => \$validate_path,
110            "marc-normalize=s" => \$marc_normalize,
111            "marc-output=s" => \$marc_output,
112            "marc-lint!" => \$marc_lint,
113            "marc-dump!" => \$marc_dump,
114  );  );
115    
116  $config = LoadFile($config);  $config = LoadFile($config);
117    
118  print "config = ",Dumper($config) if ($debug);  print "config = ",dump($config) if ($debug);
119    
120  die "no databases in config file!\n" unless ($config->{databases});  die "no databases in config file!\n" unless ($config->{databases});
121    
# Line 110  if ($stats) { Line 135  if ($stats) {
135          $log->info("using $use_indexer indexing engine...");          $log->info("using $use_indexer indexing engine...");
136  }  }
137    
138    # disable indexing when creating marc
139    $use_indexer = undef if ($marc_normalize);
140    
141  my $total_rows = 0;  my $total_rows = 0;
142  my $start_t = time();  my $start_t = time();
143    
# Line 193  while (my ($database, $db_config) = each Line 221  while (my ($database, $db_config) = each
221    
222                  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));
223    
224                  my $lookup = new WebPAC::Lookup(                  my $lookup;
225                          lookup_file => $input->{lookup},                  if ($input->{lookup}) {
226                  ) if ($input->{lookup});                          $lookup = new WebPAC::Lookup(
227                                    lookup_file => $input->{lookup},
228                            );
229                            delete( $input->{lookup} );
230                    }
231    
232                  my $input_module = $config->{webpac}->{inputs}->{$type};                  my $input_module = $config->{webpac}->{inputs}->{$type};
233    
# Line 205  while (my ($database, $db_config) = each Line 237  while (my ($database, $db_config) = each
237    
238                  my $input_db = new WebPAC::Input(                  my $input_db = new WebPAC::Input(
239                          module => $input_module,                          module => $input_module,
240                          code_page => $config->{webpac}->{webpac_encoding},                          encoding => $config->{webpac}->{webpac_encoding},
241                          limit => $limit || $input->{limit},                          limit => $limit || $input->{limit},
242                          offset => $offset,                          offset => $offset,
243                          lookup => $lookup,                          lookup_coderef => sub {
244                                    my $rec = shift || return;
245                                    $lookup->add( $rec );
246                            },
247                          recode => $input->{recode},                          recode => $input->{recode},
248                          stats => $stats,                          stats => $stats,
249                  );                  );
# Line 220  while (my ($database, $db_config) = each Line 255  while (my ($database, $db_config) = each
255                          %{ $input },                          %{ $input },
256                  );                  );
257    
258                  my $rules;                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
259                  my $normalize_path = $input->{normalize}->{path};                          @{ $input->{normalize} } : ( $input->{normalize} );
260    
261                  $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );                  if ($marc_normalize) {
262                            @norm_array = ( {
263                                    path => $marc_normalize,
264                                    output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
265                            } );
266                    }
267    
268                  my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";                  foreach my $normalize (@norm_array) {
269    
270                  foreach my $pos ( 0 ... $input_db->size ) {                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
271    
272                          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 );
273    
274                          my $mfn = $row->{'000'}->[0];                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
275    
276                          if (! $mfn || $mfn !~ m#^\d+$#) {                          $log->info("Using $normalize_path for normalization...");
277                                  $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");  
278                                  $mfn = $pos;                          my $marc = new WebPAC::Output::MARC(
279                                  push @{ $row->{'000'} }, $pos;                                  path => $normalize->{output},
280                          }                                  lint => $marc_lint,
281                                    dump => $marc_dump,
282                            ) if ($normalize->{output});
283    
284                            # reset position in database
285                            $input_db->seek(1);
286    
287                            foreach my $pos ( 0 ... $input_db->size ) {
288    
289                          if ($validate) {                                  my $row = $input_db->fetch || next;
290                                  my @errors = $validate->validate_errors( $row );  
291                                  $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);                                  my $mfn = $row->{'000'}->[0];
292    
293                                    if (! $mfn || $mfn !~ m#^\d+$#) {
294                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
295                                            $mfn = $pos;
296                                            push @{ $row->{'000'} }, $pos;
297                                    }
298    
299    
300                                    if ($validate) {
301                                            my @errors = $validate->validate_errors( $row );
302                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
303                                    }
304    
305                                    my $ds = WebPAC::Normalize::data_structure(
306                                            row => $row,
307                                            rules => $rules,
308                                            lookup => $lookup ? $lookup->lookup_hash : undef,
309                                            marc_encoding => 'utf-8',
310                                    );
311    
312                                    $db->save_ds(
313                                            id => $mfn,
314                                            ds => $ds,
315                                            prefix => $input->{name},
316                                    ) if ($ds && !$stats);
317    
318                                    $indexer->add(
319                                            id => $input->{name} . "/" . $mfn,
320                                            ds => $ds,
321                                            type => $config->{$use_indexer}->{type},
322                                    ) if ($indexer && $ds);
323    
324                                    if ($marc) {
325                                            my $i = 0;
326    
327                                            while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
328                                                    $marc->add(
329                                                            id => $mfn . ( $i ? "/$i" : '' ),
330                                                            fields => $fields,
331                                                            leader => WebPAC::Normalize::marc_leader(),
332                                                            row => $row,
333                                                    );
334                                                    $i++;
335                                            }
336    
337                                            $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
338                                    }
339    
340                                    $total_rows++;
341                          }                          }
342    
343                                                            $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,  
                         );  
344    
345                          $db->save_ds(                          # close MARC file
346                                  id => $mfn,                          $marc->finish if ($marc);
                                 ds => $ds,  
                                 prefix => $input->{name},  
                         ) if ($ds && !$stats);  
   
                         $indexer->add(  
                                 id => $input->{name} . "/" . $mfn,  
                                 ds => $ds,  
                                 type => $config->{$use_indexer}->{type},  
                         ) if ($indexer);  
347    
                         $total_rows++;  
348                  }                  }
349    
350                  $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);          }
   
         };  
351    
352          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
353    
# Line 283  while (my ($database, $db_config) = each Line 361  while (my ($database, $db_config) = each
361          #          #
362          # add Hyper Estraier links to other databases          # add Hyper Estraier links to other databases
363          #          #
364          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
365                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
366                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
367                                  $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.588

  ViewVC Help
Powered by ViewVC 1.1.26