/[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 492 by dpavlin, Sun May 14 13:25:36 2006 UTC revision 541 by dpavlin, Thu Jun 29 21:18:50 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 42  limit loading to 100 records Line 43  limit loading to 100 records
43    
44  remove database and Hyper Estraier index before indexing  remove database and Hyper Estraier index before indexing
45    
46  =item --only=database_name  =item --only=database_name/input_filter
47    
48  reindex just single database (legacy name is --one)  reindex just single database (legacy name is --one)
49    
50    C</input_filter> is optional part which can be C<name>
51    or C<type> from input
52    
53  =item --config conf/config.yml  =item --config conf/config.yml
54    
55  path to YAML configuration file  path to YAML configuration file
56    
57  =item --force-set  =item --stats
58    
59    disable indexing and dump statistics about field and subfield
60    usage for each input
61    
62  force conversion C<normalize->path> in C<config.yml> from  =item --validate path/to/validation_file
63  C<.xml> to C<.pl>  
64    turn on extra validation of imput records, see L<WebPAC::Validation>
65    
66  =back  =back
67    
# Line 65  my $limit; Line 73  my $limit;
73  my $clean = 0;  my $clean = 0;
74  my $config = 'conf/config.yml';  my $config = 'conf/config.yml';
75  my $debug = 0;  my $debug = 0;
76  my $only_db_name;  my $only_filter;
77  my $force_set = 0;  my $stats = 0;
78    my $validate_path;
79    
80  GetOptions(  GetOptions(
81          "limit=i" => \$limit,          "limit=i" => \$limit,
82          "offset=i" => \$offset,          "offset=i" => \$offset,
83          "clean" => \$clean,          "clean" => \$clean,
84          "one=s" => \$only_db_name,          "one=s" => \$only_filter,
85          "only=s" => \$only_db_name,          "only=s" => \$only_filter,
86          "config" => \$config,          "config" => \$config,
87          "debug" => \$debug,          "debug" => \$debug,
88          "force-set" => \$force_set,          "stats" => \$stats,
89            "validate=s" => \$validate_path,
90  );  );
91    
92  $config = LoadFile($config);  $config = LoadFile($config);
# Line 86  print "config = ",Dumper($config) if ($d Line 96  print "config = ",Dumper($config) if ($d
96  die "no databases in config file!\n" unless ($config->{databases});  die "no databases in config file!\n" unless ($config->{databases});
97    
98  my $log = _new WebPAC::Common()->_get_logger();  my $log = _new WebPAC::Common()->_get_logger();
99    $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  $log->info("using $use_indexer indexing engine...");  if ($stats) {
108            $log->debug("option --stats disables update of indexing engine...");
109            $use_indexer = undef;
110    } else {
111            $log->info("using $use_indexer indexing engine...");
112    }
113    
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          next if ($only_db_name && $database !~ m/$only_db_name/i);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
123            next if ($only_database && $database !~ m/$only_database/i);
124    
125          my $indexer;          if ($use_indexer) {
126                    my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
127                    $indexer_config->{database} = $database;
128                    $indexer_config->{clean} = $clean;
129                    $indexer_config->{label} = $db_config->{name};
130    
131                    if ($use_indexer eq 'hyperestraier') {
132    
133                            # open Hyper Estraier database
134                            use WebPAC::Output::Estraier '0.10';
135                            $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
136                    
137                    } elsif ($use_indexer eq 'kinosearch') {
138    
139                            # open KinoSearch
140                            use WebPAC::Output::KinoSearch;
141                            $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
142                            $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
143    
144          my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");                  } else {
145          $indexer_config->{database} = $database;                          $log->logdie("unknown use_indexer: $use_indexer");
146          $indexer_config->{clean} = $clean;                  }
         $indexer_config->{label} = $db_config->{name};  
   
         # important: clean database just once!  
         $clean = 0;  
   
         if ($use_indexer eq 'hyperestraier') {  
   
                 # open Hyper Estraier database  
                 use WebPAC::Output::Estraier '0.10';  
                 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );  
           
         } elsif ($use_indexer eq 'kinosearch') {  
   
                 # open KinoSearch  
                 use WebPAC::Output::KinoSearch;  
                 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});  
                 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );  
147    
148          } else {                  $log->logide("can't continue without valid indexer") unless ($indexer);
                 $log->logdie("unknown use_indexer: $use_indexer");  
149          }          }
150    
         $log->logide("can't continue without valid indexer") unless ($indexer);  
151    
152          #          #
153          # now WebPAC::Store          # now WebPAC::Store
# Line 135  while (my ($database, $db_config) = each Line 158  while (my ($database, $db_config) = each
158          my $db_path = $config->{webpac}->{db_path} . '/' . $database;          my $db_path = $config->{webpac}->{db_path} . '/' . $database;
159    
160          if ($clean) {          if ($clean) {
161                  $log->info("creating new database $database in $db_path");                  $log->info("creating new database '$database' in $db_path");
162                  rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");                  rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
163          } else {          } else {
164                  $log->debug("working on $database in $db_path");                  $log->info("working on database '$database' in $db_path");
165          }          }
166    
167          my $db = new WebPAC::Store(          my $db = new WebPAC::Store(
# Line 165  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));
192    
193                  my $type = lc($input->{type});                  my $type = lc($input->{type});
194    
195                  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));
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->{path}' [$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 184  while (my ($database, $db_config) = each Line 211  while (my ($database, $db_config) = each
211                          offset => $offset,                          offset => $offset,
212                          lookup => $lookup,                          lookup => $lookup,
213                          recode => $input->{recode},                          recode => $input->{recode},
214                            stats => $stats,
215                  );                  );
216                  $log->logdie("can't create input using $input_module") unless ($input);                  $log->logdie("can't create input using $input_module") unless ($input);
217    
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 $rules;
230                          my $new_norm_path = $normalize_path;                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
                         $new_norm_path =~ s/\.xml$/.pl/;  
                         if (-e $new_norm_path) {  
                                 $log->info("--force-set replaced $normalize_path with $new_norm_path");  
                                 $normalize_path = $new_norm_path;  
                         } else {  
                                 $log->warn("--force-set failed on $new_norm_path, fallback to $normalize_path");  
                         }  
                 }  
231    
232                  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: $!";  
                 }  
233    
234                  foreach my $pos ( 0 ... $input_db->size ) {                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
235    
236                          my $row = $input_db->fetch || next;                          $log->info("Using $normalize_path for normalization...");
237    
238                          my $mfn = $row->{'000'}->[0];                          my $marc_fh;
239                            if (my $path = $normalize->{marc21}) {
240                                    open($marc_fh, '>', $path) ||
241                                            $log->logdie("can't open MARC output $path: $!");
242    
243                          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;  
244                          }                          }
245    
246                          my $ds = $n ? $n->data_structure($row) :                          # reset position in database
247                                  WebPAC::Normalize::Set::data_structure(                          $input_db->seek(1);
248    
249                            foreach my $pos ( 0 ... $input_db->size ) {
250    
251                                    my $row = $input_db->fetch || next;
252    
253                                    my $mfn = $row->{'000'}->[0];
254    
255                                    if (! $mfn || $mfn !~ m#^\d+$#) {
256                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
257                                            $mfn = $pos;
258                                            push @{ $row->{'000'} }, $pos;
259                                    }
260    
261    
262                                    if ($validate) {
263                                            my @errors = $validate->validate_errors( $row );
264                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
265                                    }
266    
267                                            
268                                    my $ds = WebPAC::Normalize::data_structure(
269                                          row => $row,                                          row => $row,
270                                          rules => $rules,                                          rules => $rules,
271                                          lookup => $lookup->lookup_hash,                                          lookup => $lookup ? $lookup->lookup_hash : undef,
272                                            marc_encoding => 'utf-8',
273                                  );                                  );
274    
275                          $indexer->add(                                  $db->save_ds(
276                                  id => $input->{name} . "/" . $mfn,                                          id => $mfn,
277                                  ds => $ds,                                          ds => $ds,
278                                  type => $config->{$use_indexer}->{type},                                          prefix => $input->{name},
279                          );                                  ) if ($ds && !$stats);
280    
281                                    $indexer->add(
282                                            id => $input->{name} . "/" . $mfn,
283                                            ds => $ds,
284                                            type => $config->{$use_indexer}->{type},
285                                    ) if ($indexer && $ds);
286    
287                                    if ($marc_fh) {
288                                            my $marc = new MARC::Record;
289                                            $marc->encoding( 'utf-8' );
290                                            $marc->add_fields( WebPAC::Normalize::_get_marc21_fields() );
291                                            print $marc_fh $marc->as_usmarc;
292                                    }
293    
294                                    $total_rows++;
295                            }
296    
297                            $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
298    
299                            # close MARC file
300                            close($marc_fh) if ($marc_fh);
301    
                         $total_rows++;  
302                  }                  }
303    
304          };          }
305    
306          eval { $indexer->finish } if ($indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
307    
308          my $dt = time() - $start_t;          my $dt = time() - $start_t;
309          $log->info("$total_rows records indexed in " .          $log->info("$total_rows records ", $indexer ? "indexed " : "",
310                  sprintf("%.2f sec [%.2f rec/sec]",                  sprintf("in %.2f sec [%.2f rec/sec]",
311                          $dt, ($total_rows / $dt)                          $dt, ($total_rows / $dt)
312                  )                  )
313          );          );
# Line 275  while (my ($database, $db_config) = each Line 318  while (my ($database, $db_config) = each
318          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY') {
319                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
320                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
321                                  $log->info("adding link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");
322                                  $indexer->add_link(                                  push @links, {
323                                          from => $database,                                          from => $database,
324                                          to => $link->{to},                                          to => $link->{to},
325                                          credit => $link->{credit},                                          credit => $link->{credit},
326                                  );                                  };
327                          } else {                          } else {
328                                  $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}]");
329                          }                          }
# Line 289  while (my ($database, $db_config) = each Line 332  while (my ($database, $db_config) = each
332    
333  }  }
334    
335    foreach my $link (@links) {
336            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
337            $indexer->add_link( %{ $link } );
338    }

Legend:
Removed from v.492  
changed lines
  Added in v.541

  ViewVC Help
Powered by ViewVC 1.1.26