/[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 600 by dpavlin, Thu Jul 13 21:31:32 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::XML;  use WebPAC::Normalize 0.11;
 use WebPAC::Normalize::Set;  
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 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    =item --marc-dump
81    
82    Force dump or input and marc record for debugging.
83    
84  =back  =back
85    
86  =cut  =cut
# Line 79  my $clean = 0; Line 92  my $clean = 0;
92  my $config = 'conf/config.yml';  my $config = 'conf/config.yml';
93  my $debug = 0;  my $debug = 0;
94  my $only_filter;  my $only_filter;
 my $force_set = 0;  
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 90  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,
         "force-set" => \$force_set,  
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 118  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    
145    my @links;
146    my $indexer;
147    
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);
151          next if ($only_database && $database !~ m/$only_database/i);          next if ($only_database && $database !~ m/$only_database/i);
152    
         my $indexer;  
   
153          if ($use_indexer) {          if ($use_indexer) {
154                  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");
155                  $indexer_config->{database} = $database;                  $indexer_config->{database} = $database;
# Line 200  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 212  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 227  while (my ($database, $db_config) = each Line 257  while (my ($database, $db_config) = each
257                          %{ $input },                          %{ $input },
258                  );                  );
259    
260                  my $n = new WebPAC::Normalize::XML(                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
261                  #       filter => { 'foo' => sub { shift } },                          @{ $input->{normalize} } : ( $input->{normalize} );
                         db => $db,  
                         lookup_regex => $lookup ? $lookup->regex : undef,  
                         lookup => $lookup,  
                         prefix => $input->{name},  
                 );  
   
                 my $rules;  
                 my $normalize_path = $input->{normalize}->{path};  
262    
263                  if ($force_set) {                  if ($marc_normalize) {
264                          my $new_norm_path = $normalize_path;                          @norm_array = ( {
265                          $new_norm_path =~ s/\.xml$/.pl/;                                  path => $marc_normalize,
266                          if (-e $new_norm_path) {                                  output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
267                                  $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");  
                         }  
268                  }                  }
269    
270                  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: $!";  
                 }  
271    
272                  foreach my $pos ( 0 ... $input_db->size ) {                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
273    
274                          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 );
275    
276                          my $mfn = $row->{'000'}->[0];                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
277    
278                          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;  
                         }  
279    
280                            my $marc = new WebPAC::Output::MARC(
281                                    path => $normalize->{output},
282                                    lint => $marc_lint,
283                                    dump => $marc_dump,
284                            ) if ($normalize->{output});
285    
286                          if ($validate) {                          # reset position in database
287                                  my @errors = $validate->validate_errors( $row );                          $input_db->seek(1);
                                 $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);  
                         }  
288    
289                                                            foreach my $pos ( 0 ... $input_db->size ) {
290                          my $ds;  
291                          if ($n) {                                  my $row = $input_db->fetch || next;
292                                  $ds = $n->data_structure($row);  
293                          } else {                                  my $mfn = $row->{'000'}->[0];
294                                  $ds = WebPAC::Normalize::Set::data_structure(  
295                                    if (! $mfn || $mfn !~ m#^\d+$#) {
296                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
297                                            $mfn = $pos;
298                                            push @{ $row->{'000'} }, $pos;
299                                    }
300    
301    
302                                    if ($validate) {
303                                            my @errors = $validate->validate_errors( $row );
304                                            $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(
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',
324                                  );                                  );
325    
326                                  $db->save_ds(                                  $db->save_ds(
# Line 299  while (my ($database, $db_config) = each Line 328  while (my ($database, $db_config) = each
328                                          ds => $ds,                                          ds => $ds,
329                                          prefix => $input->{name},                                          prefix => $input->{name},
330                                  ) if ($ds && !$stats);                                  ) if ($ds && !$stats);
331    
332                                    $indexer->add(
333                                            id => $input->{name} . "/" . $mfn,
334                                            ds => $ds,
335                                            type => $config->{$use_indexer}->{type},
336                                    ) if ($indexer && $ds);
337    
338                                    if ($marc) {
339                                            my $i = 0;
340    
341                                            while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
342                                                    $marc->add(
343                                                            id => $mfn . ( $i ? "/$i" : '' ),
344                                                            fields => $fields,
345                                                            leader => WebPAC::Normalize::marc_leader(),
346                                                            row => $row,
347                                                    );
348                                                    $i++;
349                                            }
350    
351                                            $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
352                                    }
353    
354                                    $total_rows++;
355                          }                          }
356    
357                          $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);  
358    
359                          $total_rows++;                          # close MARC file
360                  }                          $marc->finish if ($marc);
361    
362                  $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);                  }
363    
364          };          }
365    
366          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
367    
# Line 326  while (my ($database, $db_config) = each Line 375  while (my ($database, $db_config) = each
375          #          #
376          # add Hyper Estraier links to other databases          # add Hyper Estraier links to other databases
377          #          #
378          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
379                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
380                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
381                                  $log->info("adding link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");
382                                  $indexer->add_link(                                  push @links, {
383                                          from => $database,                                          from => $database,
384                                          to => $link->{to},                                          to => $link->{to},
385                                          credit => $link->{credit},                                          credit => $link->{credit},
386                                  );                                  };
387                          } else {                          } else {
388                                  $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}]");
389                          }                          }
# Line 343  while (my ($database, $db_config) = each Line 392  while (my ($database, $db_config) = each
392    
393  }  }
394    
395    foreach my $link (@links) {
396            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
397            $indexer->add_link( %{ $link } );
398    }

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

  ViewVC Help
Powered by ViewVC 1.1.26