/[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 511 by dpavlin, Mon May 15 17:49:01 2006 UTC revision 552 by dpavlin, Fri Jun 30 20:43:18 2006 UTC
# Line 11  use WebPAC::Common 0.02; Line 11  use WebPAC::Common 0.02;
11  use WebPAC::Lookup;  use WebPAC::Lookup;
12  use WebPAC::Input 0.03;  use WebPAC::Input 0.03;
13  use WebPAC::Store 0.03;  use WebPAC::Store 0.03;
14  use WebPAC::Normalize::XML;  use WebPAC::Normalize;
 use WebPAC::Normalize::Set;  
15  use WebPAC::Output::TT;  use WebPAC::Output::TT;
16    use WebPAC::Validate;
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 MARC::Record 2.0;   # need 2.0 for utf-8 encoding see marcpm.sf.net
23    
24  =head1 NAME  =head1 NAME
25    
# Line 53  or C<type> from input Line 54  or C<type> from input
54    
55  path to YAML configuration file  path to YAML configuration file
56    
 =item --force-set  
   
 force conversion C<< normalize->path >> in C<config.yml> from  
 C<.xml> to C<.pl>  
   
57  =item --stats  =item --stats
58    
59  disable indexing and dump statistics about field and subfield  disable indexing and dump statistics about field and subfield
60  usage for each input  usage for each input
61    
62    =item --validate path/to/validation_file
63    
64    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 74  my $clean = 0; Line 82  my $clean = 0;
82  my $config = 'conf/config.yml';  my $config = 'conf/config.yml';
83  my $debug = 0;  my $debug = 0;
84  my $only_filter;  my $only_filter;
 my $force_set = 0;  
85  my $stats = 0;  my $stats = 0;
86    my $validate_path;
87    my ($marc_normalize, $marc_output);
88    
89  GetOptions(  GetOptions(
90          "limit=i" => \$limit,          "limit=i" => \$limit,
# Line 85  GetOptions( Line 94  GetOptions(
94          "only=s" => \$only_filter,          "only=s" => \$only_filter,
95          "config" => \$config,          "config" => \$config,
96          "debug" => \$debug,          "debug" => \$debug,
         "force-set" => \$force_set,  
97          "stats" => \$stats,          "stats" => \$stats,
98            "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 98  die "no databases in config file!\n" unl Line 109  die "no databases in config file!\n" unl
109  my $log = _new WebPAC::Common()->_get_logger();  my $log = _new WebPAC::Common()->_get_logger();
110  $log->info( "-" x 79 );  $log->info( "-" x 79 );
111    
112    my $validate;
113    $validate = new WebPAC::Validate(
114            path => $validate_path,
115    ) if ($validate_path);
116    
117  my $use_indexer = $config->{use_indexer} || 'hyperestraier';  my $use_indexer = $config->{use_indexer} || 'hyperestraier';
118  if ($stats) {  if ($stats) {
119          $log->debug("option --stats disables update of indexing engine...");          $log->debug("option --stats disables update of indexing engine...");
# Line 106  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    
131    my @links;
132    my $indexer;
133    
134  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
135    
136          my ($only_database,$only_input) = split(m#/#, $only_filter);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
137          next if ($only_database && $database !~ m/$only_database/i);          next if ($only_database && $database !~ m/$only_database/i);
138    
         my $indexer;  
   
139          if ($use_indexer) {          if ($use_indexer) {
140                  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");
141                  $indexer_config->{database} = $database;                  $indexer_config->{database} = $database;
# Line 182  while (my ($database, $db_config) = each Line 202  while (my ($database, $db_config) = each
202    
203          foreach my $input (@inputs) {          foreach my $input (@inputs) {
204    
205                  next if ($only_input && $input->{name} =~ m#$only_input#i || $input->{type} =~ m#$only_input#i);                  next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
206    
207                  my $type = lc($input->{type});                  my $type = lc($input->{type});
208    
# Line 190  while (my ($database, $db_config) = each Line 210  while (my ($database, $db_config) = each
210    
211                  my $lookup = new WebPAC::Lookup(                  my $lookup = new WebPAC::Lookup(
212                          lookup_file => $input->{lookup},                          lookup_file => $input->{lookup},
213                  );                  ) if ($input->{lookup});
214    
215                  my $input_module = $config->{webpac}->{inputs}->{$type};                  my $input_module = $config->{webpac}->{inputs}->{$type};
216    
217                  $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module lookup '$input->{lookup}'");                  $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
218                            $input->{lookup} ? "lookup '$input->{lookup}'" : ""
219                    );
220    
221                  my $input_db = new WebPAC::Input(                  my $input_db = new WebPAC::Input(
222                          module => $input_module,                          module => $input_module,
# Line 210  while (my ($database, $db_config) = each Line 232  while (my ($database, $db_config) = each
232                  my $maxmfn = $input_db->open(                  my $maxmfn = $input_db->open(
233                          path => $input->{path},                          path => $input->{path},
234                          code_page => $input->{encoding},        # database encoding                          code_page => $input->{encoding},        # database encoding
235                            %{ $input },
236                  );                  );
237    
238                  my $n = new WebPAC::Normalize::XML(                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
239                  #       filter => { 'foo' => sub { shift } },                          @{ $input->{normalize} } : ( $input->{normalize} );
                         db => $db,  
                         lookup_regex => $lookup->regex,  
                         lookup => $lookup,  
                         prefix => $input->{name},  
                 );  
   
                 my $rules;  
                 my $normalize_path = $input->{normalize}->{path};  
240    
241                  if ($force_set) {                  if ($marc_normalize) {
242                          my $new_norm_path = $normalize_path;                          @norm_array = ( {
243                          $new_norm_path =~ s/\.xml$/.pl/;                                  path => $marc_normalize,
244                          if (-e $new_norm_path) {                                  output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
245                                  $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");  
                         }  
246                  }                  }
247    
248                  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: $!";  
                 }  
249    
250                  foreach my $pos ( 0 ... $input_db->size ) {                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
251    
252                          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 );
253    
254                          my $mfn = $row->{'000'}->[0];                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
255    
256                          if (! $mfn || $mfn !~ m#^\d+$#) {                          $log->info("Using $normalize_path for normalization...");
257                                  $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");  
258                                  $mfn = $pos;                          my $marc_fh;
259                                  push @{ $row->{'000'} }, $pos;                          if (my $path = $normalize->{output}) {
260                                    open($marc_fh, '>', $path) ||
261                                            $log->logdie("can't open MARC output $path: $!");
262    
263                                    $log->info("Creating MARC export file $path\n");
264                          }                          }
265    
266                                                            # reset position in database
267                          my $ds;                          $input_db->seek(1);
268                          if ($n) {  
269                                  $ds = $n->data_structure($row);                          foreach my $pos ( 0 ... $input_db->size ) {
270                          } else {  
271                                  $ds = WebPAC::Normalize::Set::data_structure(                                  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,                                          row => $row,
290                                          rules => $rules,                                          rules => $rules,
291                                          lookup => $lookup->lookup_hash,                                          lookup => $lookup ? $lookup->lookup_hash : undef,
292                                            marc_encoding => 'utf-8',
293                                  );                                  );
294    
295                                  $db->save_ds(                                  $db->save_ds(
# Line 278  while (my ($database, $db_config) = each Line 297  while (my ($database, $db_config) = each
297                                          ds => $ds,                                          ds => $ds,
298                                          prefix => $input->{name},                                          prefix => $input->{name},
299                                  ) if ($ds && !$stats);                                  ) 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                          $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);  
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 305  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("adding link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");
342                                  $indexer->add_link(                                  push @links, {
343                                          from => $database,                                          from => $database,
344                                          to => $link->{to},                                          to => $link->{to},
345                                          credit => $link->{credit},                                          credit => $link->{credit},
346                                  );                                  };
347                          } else {                          } else {
348                                  $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}]");
349                          }                          }
# Line 322  while (my ($database, $db_config) = each Line 352  while (my ($database, $db_config) = each
352    
353  }  }
354    
355    foreach my $link (@links) {
356            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
357            $indexer->add_link( %{ $link } );
358    }

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

  ViewVC Help
Powered by ViewVC 1.1.26