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

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

  ViewVC Help
Powered by ViewVC 1.1.26