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

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

  ViewVC Help
Powered by ViewVC 1.1.26