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

Legend:
Removed from v.255  
changed lines
  Added in v.508

  ViewVC Help
Powered by ViewVC 1.1.26