/[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 547 by dpavlin, Thu Jun 29 23:19:26 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  =back  =back
67    
68  =cut  =cut
# Line 74  my $clean = 0; Line 74  my $clean = 0;
74  my $config = 'conf/config.yml';  my $config = 'conf/config.yml';
75  my $debug = 0;  my $debug = 0;
76  my $only_filter;  my $only_filter;
 my $force_set = 0;  
77  my $stats = 0;  my $stats = 0;
78    my $validate_path;
79    
80  GetOptions(  GetOptions(
81          "limit=i" => \$limit,          "limit=i" => \$limit,
# Line 85  GetOptions( Line 85  GetOptions(
85          "only=s" => \$only_filter,          "only=s" => \$only_filter,
86          "config" => \$config,          "config" => \$config,
87          "debug" => \$debug,          "debug" => \$debug,
         "force-set" => \$force_set,  
88          "stats" => \$stats,          "stats" => \$stats,
89            "validate=s" => \$validate_path,
90  );  );
91    
92  $config = LoadFile($config);  $config = LoadFile($config);
# Line 98  die "no databases in config file!\n" unl Line 98  die "no databases in config file!\n" unl
98  my $log = _new WebPAC::Common()->_get_logger();  my $log = _new WebPAC::Common()->_get_logger();
99  $log->info( "-" x 79 );  $log->info( "-" x 79 );
100    
101    my $validate;
102    $validate = new WebPAC::Validate(
103            path => $validate_path,
104    ) if ($validate_path);
105    
106  my $use_indexer = $config->{use_indexer} || 'hyperestraier';  my $use_indexer = $config->{use_indexer} || 'hyperestraier';
107  if ($stats) {  if ($stats) {
108          $log->debug("option --stats disables update of indexing engine...");          $log->debug("option --stats disables update of indexing engine...");
# Line 109  if ($stats) { Line 114  if ($stats) {
114  my $total_rows = 0;  my $total_rows = 0;
115  my $start_t = time();  my $start_t = time();
116    
117    my @links;
118    my $indexer;
119    
120  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
121    
122          my ($only_database,$only_input) = split(m#/#, $only_filter);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
123          next if ($only_database && $database !~ m/$only_database/i);          next if ($only_database && $database !~ m/$only_database/i);
124    
         my $indexer;  
   
125          if ($use_indexer) {          if ($use_indexer) {
126                  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");
127                  $indexer_config->{database} = $database;                  $indexer_config->{database} = $database;
# Line 182  while (my ($database, $db_config) = each Line 188  while (my ($database, $db_config) = each
188    
189          foreach my $input (@inputs) {          foreach my $input (@inputs) {
190    
191                  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));
192    
193                  my $type = lc($input->{type});                  my $type = lc($input->{type});
194    
# Line 190  while (my ($database, $db_config) = each Line 196  while (my ($database, $db_config) = each
196    
197                  my $lookup = new WebPAC::Lookup(                  my $lookup = new WebPAC::Lookup(
198                          lookup_file => $input->{lookup},                          lookup_file => $input->{lookup},
199                  );                  ) if ($input->{lookup});
200    
201                  my $input_module = $config->{webpac}->{inputs}->{$type};                  my $input_module = $config->{webpac}->{inputs}->{$type};
202    
203                  $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",
204                            $input->{lookup} ? "lookup '$input->{lookup}'" : ""
205                    );
206    
207                  my $input_db = new WebPAC::Input(                  my $input_db = new WebPAC::Input(
208                          module => $input_module,                          module => $input_module,
# Line 210  while (my ($database, $db_config) = each Line 218  while (my ($database, $db_config) = each
218                  my $maxmfn = $input_db->open(                  my $maxmfn = $input_db->open(
219                          path => $input->{path},                          path => $input->{path},
220                          code_page => $input->{encoding},        # database encoding                          code_page => $input->{encoding},        # database encoding
221                            %{ $input },
222                  );                  );
223    
224                  my $n = new WebPAC::Normalize::XML(                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
225                  #       filter => { 'foo' => sub { shift } },                          @{ $input->{normalize} } : ( $input->{normalize} );
                         db => $db,  
                         lookup_regex => $lookup->regex,  
                         lookup => $lookup,  
                         prefix => $input->{name},  
                 );  
226    
227                  my $rules;                  foreach my $normalize (@norm_array) {
                 my $normalize_path = $input->{normalize}->{path};  
228    
229                  if ($force_set) {                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
                         my $new_norm_path = $normalize_path;  
                         $new_norm_path =~ s/\.xml$/.pl/;  
                         if (-e $new_norm_path) {  
                                 $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");  
                         }  
                 }  
230    
231                  if ($normalize_path =~ m/\.xml$/i) {                          $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
                         $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: $!";  
                 }  
232    
233                  foreach my $pos ( 0 ... $input_db->size ) {                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
234    
235                          my $row = $input_db->fetch || next;                          $log->info("Using $normalize_path for normalization...");
236    
237                          my $mfn = $row->{'000'}->[0];                          my $marc_fh;
238                            if (my $path = $normalize->{marc21}) {
239                                    open($marc_fh, '>', $path) ||
240                                            $log->logdie("can't open MARC output $path: $!");
241    
242                          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;  
243                          }                          }
244    
245                                                            # reset position in database
246                          my $ds;                          $input_db->seek(1);
247                          if ($n) {  
248                                  $ds = $n->data_structure($row);                          foreach my $pos ( 0 ... $input_db->size ) {
249                          } else {  
250                                  $ds = WebPAC::Normalize::Set::data_structure(                                  my $row = $input_db->fetch || next;
251    
252                                    my $mfn = $row->{'000'}->[0];
253    
254                                    if (! $mfn || $mfn !~ m#^\d+$#) {
255                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
256                                            $mfn = $pos;
257                                            push @{ $row->{'000'} }, $pos;
258                                    }
259    
260    
261                                    if ($validate) {
262                                            my @errors = $validate->validate_errors( $row );
263                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
264                                    }
265    
266                                            
267                                    my $ds = WebPAC::Normalize::data_structure(
268                                          row => $row,                                          row => $row,
269                                          rules => $rules,                                          rules => $rules,
270                                          lookup => $lookup->lookup_hash,                                          lookup => $lookup ? $lookup->lookup_hash : undef,
271                                            marc_encoding => 'utf-8',
272                                  );                                  );
273    
274                                  $db->save_ds(                                  $db->save_ds(
# Line 278  while (my ($database, $db_config) = each Line 276  while (my ($database, $db_config) = each
276                                          ds => $ds,                                          ds => $ds,
277                                          prefix => $input->{name},                                          prefix => $input->{name},
278                                  ) if ($ds && !$stats);                                  ) if ($ds && !$stats);
279    
280                                    $indexer->add(
281                                            id => $input->{name} . "/" . $mfn,
282                                            ds => $ds,
283                                            type => $config->{$use_indexer}->{type},
284                                    ) if ($indexer && $ds);
285    
286                                    if ($marc_fh) {
287                                            my $marc = new MARC::Record;
288                                            $marc->encoding( 'utf-8' );
289                                            $marc->add_fields( WebPAC::Normalize::_get_marc_fields() );
290                                            print $marc_fh $marc->as_usmarc;
291                                    }
292    
293                                    $total_rows++;
294                          }                          }
295    
296                          $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);  
297    
298                          $total_rows++;                          # close MARC file
299                  }                          close($marc_fh) if ($marc_fh);
300    
301                  $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);                  }
302    
303          };          }
304    
305          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
306    
# Line 308  while (my ($database, $db_config) = each Line 317  while (my ($database, $db_config) = each
317          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY') {
318                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
319                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
320                                  $log->info("adding link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");
321                                  $indexer->add_link(                                  push @links, {
322                                          from => $database,                                          from => $database,
323                                          to => $link->{to},                                          to => $link->{to},
324                                          credit => $link->{credit},                                          credit => $link->{credit},
325                                  );                                  };
326                          } else {                          } else {
327                                  $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}]");
328                          }                          }
# Line 322  while (my ($database, $db_config) = each Line 331  while (my ($database, $db_config) = each
331    
332  }  }
333    
334    foreach my $link (@links) {
335            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
336            $indexer->add_link( %{ $link } );
337    }

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

  ViewVC Help
Powered by ViewVC 1.1.26