/[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 595 by dpavlin, Mon Jul 10 10:16:11 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;
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 42  limit loading to 100 records Line 44  limit loading to 100 records
44    
45  remove database and Hyper Estraier index before indexing  remove database and Hyper Estraier index before indexing
46    
47  =item --only=database_name  =item --only=database_name/input_filter
48    
49  reindex just single database (legacy name is --one)  reindex just single database (legacy name is --one)
50    
51    C</input_filter> is optional part which can be C<name>
52    or C<type> from input
53    
54  =item --config conf/config.yml  =item --config conf/config.yml
55    
56  path to YAML configuration file  path to YAML configuration file
57    
58  =item --force-set  =item --stats
59    
60    disable indexing and dump statistics about field and subfield
61    usage for each input
62    
63    =item --validate path/to/validation_file
64    
65    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  force conversion C<normalize->path> in C<config.yml> from  Optional path to output file
74  C<.xml> to C<.pl>  
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    
# Line 65  my $limit; Line 91  my $limit;
91  my $clean = 0;  my $clean = 0;
92  my $config = 'conf/config.yml';  my $config = 'conf/config.yml';
93  my $debug = 0;  my $debug = 0;
94  my $only_db_name;  my $only_filter;
95  my $force_set = 0;  my $stats = 0;
96    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,
103          "offset=i" => \$offset,          "offset=i" => \$offset,
104          "clean" => \$clean,          "clean" => \$clean,
105          "one=s" => \$only_db_name,          "one=s" => \$only_filter,
106          "only=s" => \$only_db_name,          "only=s" => \$only_filter,
107          "config" => \$config,          "config" => \$config,
108          "debug" => \$debug,          "debug+" => \$debug,
109          "force-set" => \$force_set,          "stats" => \$stats,
110            "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    
123  my $log = _new WebPAC::Common()->_get_logger();  my $log = _new WebPAC::Common()->_get_logger();
124    $log->info( "-" x 79 );
125    
126    my $validate;
127    $validate = new WebPAC::Validate(
128            path => $validate_path,
129    ) if ($validate_path);
130    
131  my $use_indexer = $config->{use_indexer} || 'hyperestraier';  my $use_indexer = $config->{use_indexer} || 'hyperestraier';
132  $log->info("using $use_indexer indexing engine...");  if ($stats) {
133            $log->debug("option --stats disables update of indexing engine...");
134            $use_indexer = undef;
135    } else {
136            $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          next if ($only_db_name && $database !~ m/$only_db_name/i);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
151            next if ($only_database && $database !~ m/$only_database/i);
152    
153          my $indexer;          if ($use_indexer) {
154                    my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
155                    $indexer_config->{database} = $database;
156                    $indexer_config->{clean} = $clean;
157                    $indexer_config->{label} = $db_config->{name};
158    
159                    if ($use_indexer eq 'hyperestraier') {
160    
161                            # open Hyper Estraier database
162                            use WebPAC::Output::Estraier '0.10';
163                            $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
164                    
165                    } elsif ($use_indexer eq 'kinosearch') {
166    
167                            # open KinoSearch
168                            use WebPAC::Output::KinoSearch;
169                            $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
170                            $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
171    
172          my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");                  } else {
173          $indexer_config->{database} = $database;                          $log->logdie("unknown use_indexer: $use_indexer");
174          $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 } );  
175    
176          } else {                  $log->logide("can't continue without valid indexer") unless ($indexer);
                 $log->logdie("unknown use_indexer: $use_indexer");  
177          }          }
178    
         $log->logide("can't continue without valid indexer") unless ($indexer);  
179    
180          #          #
181          # now WebPAC::Store          # now WebPAC::Store
# Line 135  while (my ($database, $db_config) = each Line 186  while (my ($database, $db_config) = each
186          my $db_path = $config->{webpac}->{db_path} . '/' . $database;          my $db_path = $config->{webpac}->{db_path} . '/' . $database;
187    
188          if ($clean) {          if ($clean) {
189                  $log->info("creating new database $database in $db_path");                  $log->info("creating new database '$database' in $db_path");
190                  rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");                  rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
191          } else {          } else {
192                  $log->debug("working on $database in $db_path");                  $log->info("working on database '$database' in $db_path");
193          }          }
194    
195          my $db = new WebPAC::Store(          my $db = new WebPAC::Store(
# Line 165  while (my ($database, $db_config) = each Line 216  while (my ($database, $db_config) = each
216    
217          foreach my $input (@inputs) {          foreach my $input (@inputs) {
218    
219                    next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
220    
221                  my $type = lc($input->{type});                  my $type = lc($input->{type});
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                  );                          $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    
235                  $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",
236                            $input->{lookup} ? "lookup '$input->{lookup}'" : ""
237                    );
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,
250                  );                  );
251                  $log->logdie("can't create input using $input_module") unless ($input);                  $log->logdie("can't create input using $input_module") unless ($input);
252    
253                  my $maxmfn = $input_db->open(                  my $maxmfn = $input_db->open(
254                          path => $input->{path},                          path => $input->{path},
255                          code_page => $input->{encoding},        # database encoding                          code_page => $input->{encoding},        # database encoding
256                            %{ $input },
257                  );                  );
258    
259                  my $n = new WebPAC::Normalize::XML(                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
260                  #       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};  
261    
262                  if ($force_set) {                  if ($marc_normalize) {
263                          my $new_norm_path = $normalize_path;                          @norm_array = ( {
264                          $new_norm_path =~ s/\.xml$/.pl/;                                  path => $marc_normalize,
265                          if (-e $new_norm_path) {                                  output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
266                                  $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");  
                         }  
267                  }                  }
268    
269                  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: $!";  
                 }  
270    
271                  foreach my $pos ( 0 ... $input_db->size ) {                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
272    
273                          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 );
274    
275                          my $mfn = $row->{'000'}->[0];                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
276    
277                          if (! $mfn || $mfn !~ m#^\d+$#) {                          $log->info("Using $normalize_path for normalization...");
278                                  $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");  
279                                  $mfn = $pos;                          my $marc = new WebPAC::Output::MARC(
280                                  push @{ $row->{'000'} }, $pos;                                  path => $normalize->{output},
281                          }                                  lint => $marc_lint,
282                                    dump => $marc_dump,
283                            ) if ($normalize->{output});
284    
285                            # reset position in database
286                            $input_db->seek(1);
287    
288                            foreach my $pos ( 0 ... $input_db->size ) {
289    
290                                    my $row = $input_db->fetch || next;
291    
292                                    my $mfn = $row->{'000'}->[0];
293    
294                                    if (! $mfn || $mfn !~ m#^\d+$#) {
295                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
296                                            $mfn = $pos;
297                                            push @{ $row->{'000'} }, $pos;
298                                    }
299    
300    
301                                    if ($validate) {
302                                            my @errors = $validate->validate_errors( $row );
303                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
304                                    }
305    
306                                    my $ds_config = dclone($db_config);
307    
308                          my $ds = $n ? $n->data_structure($row) :                                  # default values -> database key
309                                  WebPAC::Normalize::Set::data_structure(                                  $ds_config->{_} = $database;
310    
311                                    # current mfn
312                                    $ds_config->{_mfn} = $mfn;
313    
314                                    # attach current input
315                                    $ds_config->{input} = $input;
316    
317                                    my $ds = WebPAC::Normalize::data_structure(
318                                          row => $row,                                          row => $row,
319                                          rules => $rules,                                          rules => $rules,
320                                          lookup => $lookup->lookup_hash,                                          lookup => $lookup ? $lookup->lookup_hash : undef,
321                                            config => $ds_config,
322                                            marc_encoding => 'utf-8',
323                                  );                                  );
324    
325                          $indexer->add(                                  $db->save_ds(
326                                  id => $input->{name} . "/" . $mfn,                                          id => $mfn,
327                                  ds => $ds,                                          ds => $ds,
328                                  type => $config->{$use_indexer}->{type},                                          prefix => $input->{name},
329                          );                                  ) if ($ds && !$stats);
330    
331                                    $indexer->add(
332                                            id => $input->{name} . "/" . $mfn,
333                                            ds => $ds,
334                                            type => $config->{$use_indexer}->{type},
335                                    ) if ($indexer && $ds);
336    
337                                    if ($marc) {
338                                            my $i = 0;
339    
340                                            while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
341                                                    $marc->add(
342                                                            id => $mfn . ( $i ? "/$i" : '' ),
343                                                            fields => $fields,
344                                                            leader => WebPAC::Normalize::marc_leader(),
345                                                            row => $row,
346                                                    );
347                                                    $i++;
348                                            }
349    
350                                            $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
351                                    }
352    
353                                    $total_rows++;
354                            }
355    
356                            $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
357    
358                            # close MARC file
359                            $marc->finish if ($marc);
360    
                         $total_rows++;  
361                  }                  }
362    
363          };          }
364    
365          eval { $indexer->finish } if ($indexer->can('finish'));          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
366    
367          my $dt = time() - $start_t;          my $dt = time() - $start_t;
368          $log->info("$total_rows records indexed in " .          $log->info("$total_rows records ", $indexer ? "indexed " : "",
369                  sprintf("%.2f sec [%.2f rec/sec]",                  sprintf("in %.2f sec [%.2f rec/sec]",
370                          $dt, ($total_rows / $dt)                          $dt, ($total_rows / $dt)
371                  )                  )
372          );          );
# Line 272  while (my ($database, $db_config) = each Line 374  while (my ($database, $db_config) = each
374          #          #
375          # add Hyper Estraier links to other databases          # add Hyper Estraier links to other databases
376          #          #
377          if (ref($db_config->{links}) eq 'ARRAY') {          if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
378                  foreach my $link (@{ $db_config->{links} }) {                  foreach my $link (@{ $db_config->{links} }) {
379                          if ($use_indexer eq 'hyperestraier') {                          if ($use_indexer eq 'hyperestraier') {
380                                  $log->info("adding link $database -> $link->{to} [$link->{credit}]");                                  $log->info("saving link $database -> $link->{to} [$link->{credit}]");
381                                  $indexer->add_link(                                  push @links, {
382                                          from => $database,                                          from => $database,
383                                          to => $link->{to},                                          to => $link->{to},
384                                          credit => $link->{credit},                                          credit => $link->{credit},
385                                  );                                  };
386                          } else {                          } else {
387                                  $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}]");
388                          }                          }
# Line 289  while (my ($database, $db_config) = each Line 391  while (my ($database, $db_config) = each
391    
392  }  }
393    
394    foreach my $link (@links) {
395            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
396            $indexer->add_link( %{ $link } );
397    }

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

  ViewVC Help
Powered by ViewVC 1.1.26