/[webpac2]/branches/Sack/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 /branches/Sack/run.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 512 by dpavlin, Mon May 15 18:27:15 2006 UTC revision 585 by dpavlin, Wed Jul 5 19:52:45 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;
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;
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 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    =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 74  my $clean = 0; Line 91  my $clean = 0;
91  my $config = 'conf/config.yml';  my $config = 'conf/config.yml';
92  my $debug = 0;  my $debug = 0;
93  my $only_filter;  my $only_filter;
 my $force_set = 0;  
94  my $stats = 0;  my $stats = 0;
95    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 84  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,
         "force-set" => \$force_set,  
108          "stats" => \$stats,          "stats" => \$stats,
109            "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    
122  my $log = _new WebPAC::Common()->_get_logger();  my $log = _new WebPAC::Common()->_get_logger();
123  $log->info( "-" x 79 );  $log->info( "-" x 79 );
124    
125    my $validate;
126    $validate = new WebPAC::Validate(
127            path => $validate_path,
128    ) if ($validate_path);
129    
130  my $use_indexer = $config->{use_indexer} || 'hyperestraier';  my $use_indexer = $config->{use_indexer} || 'hyperestraier';
131  if ($stats) {  if ($stats) {
132          $log->debug("option --stats disables update of indexing engine...");          $log->debug("option --stats disables update of indexing engine...");
# Line 106  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    
144    my @links;
145    my $indexer;
146    
147  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
148    
149          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
150          next if ($only_database && $database !~ m/$only_database/i);          next if ($only_database && $database !~ m/$only_database/i);
151    
         my $indexer;  
   
152          if ($use_indexer) {          if ($use_indexer) {
153                  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");
154                  $indexer_config->{database} = $database;                  $indexer_config->{database} = $database;
# Line 190  while (my ($database, $db_config) = each Line 223  while (my ($database, $db_config) = each
223    
224                  my $lookup = new WebPAC::Lookup(                  my $lookup = new WebPAC::Lookup(
225                          lookup_file => $input->{lookup},                          lookup_file => $input->{lookup},
226                  );                  ) if ($input->{lookup});
227    
228                  my $input_module = $config->{webpac}->{inputs}->{$type};                  my $input_module = $config->{webpac}->{inputs}->{$type};
229    
230                  $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",
231                            $input->{lookup} ? "lookup '$input->{lookup}'" : ""
232                    );
233    
234                  my $input_db = new WebPAC::Input(                  my $input_db = new WebPAC::Input(
235                          module => $input_module,                          module => $input_module,
236                          code_page => $config->{webpac}->{webpac_encoding},                          encoding => $config->{webpac}->{webpac_encoding},
237                          limit => $limit || $input->{limit},                          limit => $limit || $input->{limit},
238                          offset => $offset,                          offset => $offset,
239                          lookup => $lookup,                          lookup_coderef => sub {
240                                    my $rec = shift || return;
241                                    $lookup->add( $rec );
242                            },
243                          recode => $input->{recode},                          recode => $input->{recode},
244                          stats => $stats,                          stats => $stats,
245                  );                  );
# Line 210  while (my ($database, $db_config) = each Line 248  while (my ($database, $db_config) = each
248                  my $maxmfn = $input_db->open(                  my $maxmfn = $input_db->open(
249                          path => $input->{path},                          path => $input->{path},
250                          code_page => $input->{encoding},        # database encoding                          code_page => $input->{encoding},        # database encoding
251                            %{ $input },
252                  );                  );
253    
254                  my $n = new WebPAC::Normalize::XML(                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
255                  #       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};  
256    
257                  if ($force_set) {                  if ($marc_normalize) {
258                          my $new_norm_path = $normalize_path;                          @norm_array = ( {
259                          $new_norm_path =~ s/\.xml$/.pl/;                                  path => $marc_normalize,
260                          if (-e $new_norm_path) {                                  output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
261                                  $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");  
                         }  
262                  }                  }
263    
264                  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: $!";  
                 }  
265    
266                  foreach my $pos ( 0 ... $input_db->size ) {                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
267    
268                          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 );
269    
270                          my $mfn = $row->{'000'}->[0];                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
271    
272                          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;  
                         }  
273    
274                                                            my $marc = new WebPAC::Output::MARC(
275                          my $ds;                                  path => $normalize->{output},
276                          if ($n) {                                  lint => $marc_lint,
277                                  $ds = $n->data_structure($row);                                  dump => $marc_dump,
278                          } else {                          ) if ($normalize->{output});
279                                  $ds = WebPAC::Normalize::Set::data_structure(  
280                            # reset position in database
281                            $input_db->seek(1);
282    
283                            foreach my $pos ( 0 ... $input_db->size ) {
284    
285                                    my $row = $input_db->fetch || next;
286    
287                                    my $mfn = $row->{'000'}->[0];
288    
289                                    if (! $mfn || $mfn !~ m#^\d+$#) {
290                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
291                                            $mfn = $pos;
292                                            push @{ $row->{'000'} }, $pos;
293                                    }
294    
295    
296                                    if ($validate) {
297                                            my @errors = $validate->validate_errors( $row );
298                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
299                                    }
300    
301                                    my $ds = WebPAC::Normalize::data_structure(
302                                          row => $row,                                          row => $row,
303                                          rules => $rules,                                          rules => $rules,
304                                          lookup => $lookup->lookup_hash,                                          lookup => $lookup ? $lookup->lookup_hash : undef,
305                                            marc_encoding => 'utf-8',
306                                  );                                  );
307    
308                                  $db->save_ds(                                  $db->save_ds(
# Line 278  while (my ($database, $db_config) = each Line 310  while (my ($database, $db_config) = each
310                                          ds => $ds,                                          ds => $ds,
311                                          prefix => $input->{name},                                          prefix => $input->{name},
312                                  ) if ($ds && !$stats);                                  ) if ($ds && !$stats);
313    
314                                    $indexer->add(
315                                            id => $input->{name} . "/" . $mfn,
316                                            ds => $ds,
317                                            type => $config->{$use_indexer}->{type},
318                                    ) if ($indexer && $ds);
319    
320                                    if ($marc) {
321                                            my $i = 0;
322    
323                                            while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
324                                                    $marc->add(
325                                                            id => $mfn . ( $i ? "/$i" : '' ),
326                                                            fields => $fields,
327                                                            leader => WebPAC::Normalize::marc_leader(),
328                                                            row => $row,
329                                                    );
330                                                    $i++;
331                                            }
332    
333                                            $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
334                                    }
335    
336                                    $total_rows++;
337                          }                          }
338    
339                          $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);  
340    
341                          $total_rows++;                          # close MARC file
342                  }                          $marc->finish if ($marc);
343    
344                  $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);                  }
345    
346          };          }
347    
348          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
349    
# Line 305  while (my ($database, $db_config) = each Line 357  while (my ($database, $db_config) = each
357          #          #
358          # add Hyper Estraier links to other databases          # add Hyper Estraier links to other databases
359          #          #
360          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
361                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
362                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
363                                  $log->info("adding link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");
364                                  $indexer->add_link(                                  push @links, {
365                                          from => $database,                                          from => $database,
366                                          to => $link->{to},                                          to => $link->{to},
367                                          credit => $link->{credit},                                          credit => $link->{credit},
368                                  );                                  };
369                          } else {                          } else {
370                                  $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}]");
371                          }                          }
# Line 322  while (my ($database, $db_config) = each Line 374  while (my ($database, $db_config) = each
374    
375  }  }
376    
377    foreach my $link (@links) {
378            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
379            $indexer->add_link( %{ $link } );
380    }

Legend:
Removed from v.512  
changed lines
  Added in v.585

  ViewVC Help
Powered by ViewVC 1.1.26