/[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 595 by dpavlin, Mon Jul 10 10:16:11 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    use Storable qw/dclone/;
24    
25  =head1 NAME  =head1 NAME
26    
# Line 62  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    =item --marc-dump
81    
82    Force dump or input and marc record for debugging.
83    
84  =back  =back
85    
86  =cut  =cut
# Line 75  my $debug = 0; Line 94  my $debug = 0;
94  my $only_filter;  my $only_filter;
95  my $stats = 0;  my $stats = 0;
96  my $validate_path;  my $validate_path;
97    my ($marc_normalize, $marc_output);
98    my $marc_lint = 1;
99    my $marc_dump = 0;
100    
101  GetOptions(  GetOptions(
102          "limit=i" => \$limit,          "limit=i" => \$limit,
# Line 83  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,
112            "marc-output=s" => \$marc_output,
113            "marc-lint!" => \$marc_lint,
114            "marc-dump!" => \$marc_dump,
115  );  );
116    
117  $config = LoadFile($config);  $config = LoadFile($config);
118    
119  print "config = ",Dumper($config) if ($debug);  print "config = ",dump($config) if ($debug);
120    
121  die "no databases in config file!\n" unless ($config->{databases});  die "no databases in config file!\n" unless ($config->{databases});
122    
# Line 110  if ($stats) { Line 136  if ($stats) {
136          $log->info("using $use_indexer indexing engine...");          $log->info("using $use_indexer indexing engine...");
137  }  }
138    
139    # disable indexing when creating marc
140    $use_indexer = undef if ($marc_normalize);
141    
142  my $total_rows = 0;  my $total_rows = 0;
143  my $start_t = time();  my $start_t = time();
144    
# Line 193  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 205  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                  );                  );
# Line 220  while (my ($database, $db_config) = each Line 256  while (my ($database, $db_config) = each
256                          %{ $input },                          %{ $input },
257                  );                  );
258    
259                  my $rules;                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
260                  my $normalize_path = $input->{normalize}->{path};                          @{ $input->{normalize} } : ( $input->{normalize} );
261    
262                  $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );                  if ($marc_normalize) {
263                            @norm_array = ( {
264                                    path => $marc_normalize,
265                                    output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
266                            } );
267                    }
268    
269                  my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";                  foreach my $normalize (@norm_array) {
270    
271                  foreach my $pos ( 0 ... $input_db->size ) {                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
272    
273                          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 );
274    
275                          my $mfn = $row->{'000'}->[0];                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
276    
277                          if (! $mfn || $mfn !~ m#^\d+$#) {                          $log->info("Using $normalize_path for normalization...");
278                                  $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");  
279                                  $mfn = $pos;                          my $marc = new WebPAC::Output::MARC(
280                                  push @{ $row->{'000'} }, $pos;                                  path => $normalize->{output},
281                          }                                  lint => $marc_lint,
282                                    dump => $marc_dump,
283                            ) if ($normalize->{output});
284    
285                            # reset position in database
286                            $input_db->seek(1);
287    
288                            foreach my $pos ( 0 ... $input_db->size ) {
289    
290                                    my $row = $input_db->fetch || next;
291    
292                                    my $mfn = $row->{'000'}->[0];
293    
294                          if ($validate) {                                  if (! $mfn || $mfn !~ m#^\d+$#) {
295                                  my @errors = $validate->validate_errors( $row );                                          $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
296                                  $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);                                          $mfn = $pos;
297                                            push @{ $row->{'000'} }, $pos;
298                                    }
299    
300    
301                                    if ($validate) {
302                                            my @errors = $validate->validate_errors( $row );
303                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
304                                    }
305    
306                                    my $ds_config = dclone($db_config);
307    
308                                    # default values -> database key
309                                    $ds_config->{_} = $database;
310    
311                                    # current mfn
312                                    $ds_config->{_mfn} = $mfn;
313    
314                                    # attach current input
315                                    $ds_config->{input} = $input;
316    
317                                    my $ds = WebPAC::Normalize::data_structure(
318                                            row => $row,
319                                            rules => $rules,
320                                            lookup => $lookup ? $lookup->lookup_hash : undef,
321                                            config => $ds_config,
322                                            marc_encoding => 'utf-8',
323                                    );
324    
325                                    $db->save_ds(
326                                            id => $mfn,
327                                            ds => $ds,
328                                            prefix => $input->{name},
329                                    ) if ($ds && !$stats);
330    
331                                    $indexer->add(
332                                            id => $input->{name} . "/" . $mfn,
333                                            ds => $ds,
334                                            type => $config->{$use_indexer}->{type},
335                                    ) if ($indexer && $ds);
336    
337                                    if ($marc) {
338                                            my $i = 0;
339    
340                                            while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
341                                                    $marc->add(
342                                                            id => $mfn . ( $i ? "/$i" : '' ),
343                                                            fields => $fields,
344                                                            leader => WebPAC::Normalize::marc_leader(),
345                                                            row => $row,
346                                                    );
347                                                    $i++;
348                                            }
349    
350                                            $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
351                                    }
352    
353                                    $total_rows++;
354                          }                          }
355    
356                                                            $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,  
                         );  
357    
358                          $db->save_ds(                          # close MARC file
359                                  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);  
360    
                         $total_rows++;  
361                  }                  }
362    
363                  $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);          }
   
         };  
364    
365          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
366    
# Line 283  while (my ($database, $db_config) = each Line 374  while (my ($database, $db_config) = each
374          #          #
375          # add Hyper Estraier links to other databases          # add Hyper Estraier links to other databases
376          #          #
377          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
378                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
379                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
380                                  $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.595

  ViewVC Help
Powered by ViewVC 1.1.26