/[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 210 by dpavlin, Mon Dec 5 17:47:04 2005 UTC revision 523 by dpavlin, Sun May 21 19:29:26 2006 UTC
# Line 7  use File::Temp qw/tempdir/; Line 7  use File::Temp qw/tempdir/;
7  use Data::Dumper;  use Data::Dumper;
8  use lib './lib';  use lib './lib';
9    
10    use WebPAC::Common 0.02;
11  use WebPAC::Lookup;  use WebPAC::Lookup;
12  use WebPAC::Input::ISIS;  use WebPAC::Input 0.03;
13  use WebPAC::Store 0.03;  use WebPAC::Store 0.03;
14  use WebPAC::Normalize::XML;  use WebPAC::Normalize::XML;
15    use WebPAC::Normalize::Set;
16  use WebPAC::Output::TT;  use WebPAC::Output::TT;
17  use WebPAC::Output::Estraier 0.02;  use WebPAC::Validate;
18  use YAML qw/LoadFile/;  use YAML qw/LoadFile/;
19  use LWP::Simple;  use Getopt::Long;
20    use File::Path;
21    use Time::HiRes qw/time/;
22    use File::Slurp;
23    
24  my $limit = shift @ARGV;  =head1 NAME
25    
26  my $config = LoadFile('conf/config.yml');  run.pl - start WebPAC indexing
27    
28  print "config = ",Dumper($config);  B<this command will probably go away. Don't get used to it!>
29    
30  die "no databases in config file!\n" unless ($config->{databases});  Options:
31    
32  while (my ($database, $db_config) = each %{ $config->{databases} }) {  =over 4
33    
34          my $type = lc($db_config->{input}->{type});  =item --offset 42
35    
36          die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');  start loading (all) databases at offset 42
37    
38          my $abs_path = abs_path($0);  =item --limit 100
         $abs_path =~ s#/[^/]*$#/#;  
39    
40          my $lookup = new WebPAC::Lookup(  limit loading to 100 records
                 lookup_file => $db_config->{input}->{lookup},  
         );  
41    
42          my $db_path = $config->{webpac}->{db_path} . '/' . $database;  =item --clean
43    
44    remove database and Hyper Estraier index before indexing
45    
46          my $log = $lookup->_get_logger;  =item --only=database_name/input_filter
         $log->info("working on $database in $db_path");  
47    
48          my $isis = new WebPAC::Input::ISIS(  reindex just single database (legacy name is --one)
                 code_page => $config->{webpac}->{webpac_encoding},  
                 limit_mfn => $db_config->{input}->{limit},  
         );  
49    
50          my $maxmfn = $isis->open(  C</input_filter> is optional part which can be C<name>
51                  filename => $db_config->{input}->{path},  or C<type> from input
                 code_page => $db_config->{input}->{encoding},   # database encoding  
         );  
52    
53          my $path = './db/';  =item --config conf/config.yml
54    
55          my $db = new WebPAC::Store(  path to YAML configuration file
                 path => $db_path,  
         );  
56    
57          my $n = new WebPAC::Normalize::XML(  =item --force-set
         #       filter => { 'foo' => sub { shift } },  
                 db => $db,  
                 lookup_regex => $lookup->regex,  
                 lookup => $lookup,  
         );  
58    
59          $n->open(  force conversion C<< normalize->path >> in C<config.yml> from
60                  tag => $db_config->{normalize}->{tag},  C<.xml> to C<.pl>
                 xml_file => $db_config->{normalize}->{path},  
         );  
61    
62          my $out = new WebPAC::Output::TT(  =item --stats
                 include_path => $config->{webpac}->{template_path},  
                 filters => { foo => sub { shift } },  
         );  
63    
64          my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");  disable indexing and dump statistics about field and subfield
65          $est_config->{database} = $database;  usage for each input
66    
67          $log->info("using HyperEstraier URL $est_config->{url}");  =item --validate path/to/validation_file
68    
69          my $est = new WebPAC::Output::Estraier(  turn on extra validation of imput records, see L<WebPAC::Validation>
70                  %{ $est_config },  
71          );  =back
72    
73          my $total_rows = 0;  =cut
74    
75          for ( 0 ... $isis->size ) {  my $offset;
76    my $limit;
77    
78                  my $row = $isis->fetch || next;  my $clean = 0;
79    my $config = 'conf/config.yml';
80    my $debug = 0;
81    my $only_filter;
82    my $force_set = 0;
83    my $stats = 0;
84    my $validate_path;
85    
86                  my $mfn = $row->{'000'}->[0] || die "can't find MFN";  GetOptions(
87            "limit=i" => \$limit,
88            "offset=i" => \$offset,
89            "clean" => \$clean,
90            "one=s" => \$only_filter,
91            "only=s" => \$only_filter,
92            "config" => \$config,
93            "debug" => \$debug,
94            "force-set" => \$force_set,
95            "stats" => \$stats,
96            "validate=s" => \$validate_path,
97    );
98    
99                  my $ds = $n->data_structure($row);  $config = LoadFile($config);
100    
101    print "config = ",Dumper($config) if ($debug);
102    
103    die "no databases in config file!\n" unless ($config->{databases});
104    
105    my $log = _new WebPAC::Common()->_get_logger();
106    $log->info( "-" x 79 );
107    
108    my $validate;
109    $validate = new WebPAC::Validate(
110            path => $validate_path,
111    ) if ($validate_path);
112    
113    my $use_indexer = $config->{use_indexer} || 'hyperestraier';
114    if ($stats) {
115            $log->debug("option --stats disables update of indexing engine...");
116            $use_indexer = undef;
117    } else {
118            $log->info("using $use_indexer indexing engine...");
119    }
120    
121    my $total_rows = 0;
122    my $start_t = time();
123    
124    while (my ($database, $db_config) = each %{ $config->{databases} }) {
125    
126            my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
127            next if ($only_database && $database !~ m/$only_database/i);
128    
129            my $indexer;
130    
131            if ($use_indexer) {
132                    my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
133                    $indexer_config->{database} = $database;
134                    $indexer_config->{clean} = $clean;
135                    $indexer_config->{label} = $db_config->{name};
136    
137                    if ($use_indexer eq 'hyperestraier') {
138    
139                            # open Hyper Estraier database
140                            use WebPAC::Output::Estraier '0.10';
141                            $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
142                    
143                    } elsif ($use_indexer eq 'kinosearch') {
144    
145                            # open KinoSearch
146                            use WebPAC::Output::KinoSearch;
147                            $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
148                            $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
149    
150                    } else {
151                            $log->logdie("unknown use_indexer: $use_indexer");
152                    }
153    
154                    $log->logide("can't continue without valid indexer") unless ($indexer);
155            }
156    
         #       print STDERR Dumper($row, $ds);  
157    
         #       my $html = $out->apply(  
         #               template => 'html_ffzg.tt',  
         #               data => $ds,  
         #       );  
158          #          #
159          #       # create test output          # now WebPAC::Store
160          #          #
161          #       my $file = sprintf('out/%02d.html', $mfn );          my $abs_path = abs_path($0);
162          #       open(my $fh, '>', $file) or die "can't open $file: $!";          $abs_path =~ s#/[^/]*$#/#;
163          #       print $fh $html;  
164          #       close($fh);          my $db_path = $config->{webpac}->{db_path} . '/' . $database;
165    
166            if ($clean) {
167                    $log->info("creating new database '$database' in $db_path");
168                    rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
169            } else {
170                    $log->info("working on database '$database' in $db_path");
171            }
172    
173            my $db = new WebPAC::Store(
174                    path => $db_path,
175                    database => $database,
176                    debug => $debug,
177            );
178    
179    
180          #          #
181          #       $html =~ s#\s*[\n\r]+\s*##gs;          # now, iterate through input formats
182          #          #
         #       print STDERR $html;  
183    
184                  $est->add(          my @inputs;
185                          id => $mfn,          if (ref($db_config->{input}) eq 'ARRAY') {
186                          ds => $ds,                  @inputs = @{ $db_config->{input} };
187                          type => $config->{hyperestraier}->{type},          } elsif ($db_config->{input}) {
188                    push @inputs, $db_config->{input};
189            } else {
190                    $log->info("database $database doesn't have inputs defined");
191            }
192    
193            my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
194    
195            foreach my $input (@inputs) {
196    
197                    next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
198    
199                    my $type = lc($input->{type});
200    
201                    die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
202    
203                    my $lookup = new WebPAC::Lookup(
204                            lookup_file => $input->{lookup},
205                    ) if ($input->{lookup});
206    
207                    my $input_module = $config->{webpac}->{inputs}->{$type};
208    
209                    $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
210                            $input->{lookup} ? "lookup '$input->{lookup}'" : ""
211                  );                  );
212    
213                  $total_rows++;                  my $input_db = new WebPAC::Input(
214                            module => $input_module,
215                            code_page => $config->{webpac}->{webpac_encoding},
216                            limit => $limit || $input->{limit},
217                            offset => $offset,
218                            lookup => $lookup,
219                            recode => $input->{recode},
220                            stats => $stats,
221                    );
222                    $log->logdie("can't create input using $input_module") unless ($input);
223    
224                    my $maxmfn = $input_db->open(
225                            path => $input->{path},
226                            code_page => $input->{encoding},        # database encoding
227                            %{ $input },
228                    );
229    
230                    my $n = new WebPAC::Normalize::XML(
231                    #       filter => { 'foo' => sub { shift } },
232                            db => $db,
233                            lookup_regex => $lookup ? $lookup->regex : undef,
234                            lookup => $lookup,
235                            prefix => $input->{name},
236                    );
237    
238                    my $rules;
239                    my $normalize_path = $input->{normalize}->{path};
240    
241                    if ($force_set) {
242                            my $new_norm_path = $normalize_path;
243                            $new_norm_path =~ s/\.xml$/.pl/;
244                            if (-e $new_norm_path) {
245                                    $log->debug("--force-set replaced $normalize_path with $new_norm_path");
246                                    $normalize_path = $new_norm_path;
247                            } else {
248                                    $log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path");
249                            }
250                    }
251    
252                    if ($normalize_path =~ m/\.xml$/i) {
253                            $n->open(
254                                    tag => $input->{normalize}->{tag},
255                                    xml_file => $normalize_path,
256                            );
257                    } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
258                            $n->open_yaml(
259                                    path => $normalize_path,
260                                    tag => $input->{normalize}->{tag},
261                            );
262                    } elsif ($normalize_path =~ m/\.(?:pl)$/i) {
263                            $n = undef;
264                            $log->info("using WebPAC::Normalize::Set to process $normalize_path");
265                            $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
266                    }
267    
268                    foreach my $pos ( 0 ... $input_db->size ) {
269    
270                            my $row = $input_db->fetch || next;
271    
272                            my $mfn = $row->{'000'}->[0];
273    
274                            if (! $mfn || $mfn !~ m#^\d+$#) {
275                                    $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
276                                    $mfn = $pos;
277                                    push @{ $row->{'000'} }, $pos;
278                            }
279    
280    
281                            if ($validate) {
282                                    my @errors = $validate->validate_errors( $row );
283                                    $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
284                            }
285    
286                                    
287                            my $ds;
288                            if ($n) {
289                                    $ds = $n->data_structure($row);
290                            } else {
291                                    $ds = WebPAC::Normalize::Set::data_structure(
292                                            row => $row,
293                                            rules => $rules,
294                                            lookup => $lookup ? $lookup->lookup_hash : undef,
295                                    );
296    
297                                    $db->save_ds(
298                                            id => $mfn,
299                                            ds => $ds,
300                                            prefix => $input->{name},
301                                    ) if ($ds && !$stats);
302                            }
303    
304                            $indexer->add(
305                                    id => $input->{name} . "/" . $mfn,
306                                    ds => $ds,
307                                    type => $config->{$use_indexer}->{type},
308                            ) if ($indexer);
309    
310                            $total_rows++;
311                    }
312    
313                    $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
314    
315          };          };
316    
317          $log->info("$total_rows records indexed");          eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
318    
319            my $dt = time() - $start_t;
320            $log->info("$total_rows records ", $indexer ? "indexed " : "",
321                    sprintf("in %.2f sec [%.2f rec/sec]",
322                            $dt, ($total_rows / $dt)
323                    )
324            );
325    
326            #
327            # add Hyper Estraier links to other databases
328            #
329            if (ref($db_config->{links}) eq 'ARRAY') {
330                    foreach my $link (@{ $db_config->{links} }) {
331                            if ($use_indexer eq 'hyperestraier') {
332                                    $log->info("adding link $database -> $link->{to} [$link->{credit}]");
333                                    $indexer->add_link(
334                                            from => $database,
335                                            to => $link->{to},
336                                            credit => $link->{credit},
337                                    );
338                            } else {
339                                    $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
340                            }
341                    }
342            }
343    
344  }  }
345    

Legend:
Removed from v.210  
changed lines
  Added in v.523

  ViewVC Help
Powered by ViewVC 1.1.26