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

Legend:
Removed from v.141  
changed lines
  Added in v.509

  ViewVC Help
Powered by ViewVC 1.1.26