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

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

  ViewVC Help
Powered by ViewVC 1.1.26