/[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 213 by dpavlin, Mon Dec 5 17:47:23 2005 UTC revision 431 by dpavlin, Mon Apr 17 15:10:04 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    Options:
28    
29    =over 4
30    
31    =item --offset 42
32    
33    start loading (all) databases at offset 42
34    
35    =item --limit 100
36    
37    limit loading to 100 records
38    
39    =item --clean
40    
41    remove database and Hyper Estraier index before indexing
42    
43    =item --only=database_name
44    
45    reindex just single database (legacy name is --one)
46    
47    =item --config conf/config.yml
48    
49    path to YAML configuration file
50    
51    =back
52    
53    =cut
54    
55    my $offset;
56    my $limit;
57    
58    my $clean = 0;
59    my $config = 'conf/config.yml';
60    my $debug = 0;
61    my $only_db_name;
62    
63    GetOptions(
64            "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    $config = LoadFile($config);
74    
75    print "config = ",Dumper($config) if ($debug);
76    
77  die "no databases in config file!\n" unless ($config->{databases});  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;  my $total_rows = 0;
85    my $start_t = time();
86    
87  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
88    
89          my $type = lc($db_config->{input}->{type});          next if ($only_db_name && $database !~ m/$only_db_name/i);
90    
91          die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');          my $indexer;
92    
93          my $abs_path = abs_path($0);          my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
94          $abs_path =~ s#/[^/]*$#/#;          $indexer_config->{database} = $database;
95            $indexer_config->{clean} = $clean;
96            $indexer_config->{label} = $db_config->{name};
97    
98            if ($use_indexer eq 'hyperestraier') {
99    
100                    # open Hyper Estraier database
101                    use WebPAC::Output::Estraier '0.10';
102                    $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
103            
104            } elsif ($use_indexer eq 'kinosearch') {
105    
106                    # open KinoSearch
107                    use WebPAC::Output::KinoSearch;
108                    $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
109    
110          my $lookup = new WebPAC::Lookup(          } else {
111                  lookup_file => $db_config->{input}->{lookup},                  $log->logdie("unknown use_indexer: $use_indexer");
112          );          }
113    
114          my $db_path = $config->{webpac}->{db_path} . '/' . $database;          $log->logide("can't continue without valid indexer") unless ($indexer);
115    
116            #
117            # now WebPAC::Store
118            #
119            my $abs_path = abs_path($0);
120            $abs_path =~ s#/[^/]*$#/#;
121    
122          my $log = $lookup->_get_logger;          my $db_path = $config->{webpac}->{db_path} . '/' . $database;
         $log->info("working on $database in $db_path");  
123    
124          my $path = './db/';          if ($clean) {
125                    $log->info("creating new database $database in $db_path");
126                    rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
127            } else {
128                    $log->debug("working on $database in $db_path");
129            }
130    
131          my $db = new WebPAC::Store(          my $db = new WebPAC::Store(
132                  path => $db_path,                  path => $db_path,
133                    database => $database,
134                    debug => $debug,
135          );          );
136    
         my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");  
         $est_config->{database} = $database;  
   
         $log->info("using HyperEstraier URL $est_config->{masterurl}");  
   
         my $est = new WebPAC::Output::Estraier(  
                 %{ $est_config },  
         );  
137    
138          #          #
139          # now, iterate through input formats          # now, iterate through input formats
# Line 67  while (my ($database, $db_config) = each Line 142  while (my ($database, $db_config) = each
142          my @inputs;          my @inputs;
143          if (ref($db_config->{input}) eq 'ARRAY') {          if (ref($db_config->{input}) eq 'ARRAY') {
144                  @inputs = @{ $db_config->{input} };                  @inputs = @{ $db_config->{input} };
145          } else {          } elsif ($db_config->{input}) {
146                  push @inputs, $db_config->{input};                  push @inputs, $db_config->{input};
147            } else {
148                    $log->info("database $database doesn't have inputs defined");
149          }          }
150    
151            my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
152    
153          foreach my $input (@inputs) {          foreach my $input (@inputs) {
                 $log->info("working on input $input->{path} [$input->{type}]");  
154    
155                  my $isis = new WebPAC::Input::ISIS(                  my $type = lc($input->{type});
156    
157                    die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
158    
159                    my $lookup = new WebPAC::Lookup(
160                            lookup_file => $input->{lookup},
161                    );
162    
163                    my $input_module = $config->{webpac}->{inputs}->{$type};
164    
165                    $log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'");
166    
167                    my $input_db = new WebPAC::Input(
168                            module => $input_module,
169                          code_page => $config->{webpac}->{webpac_encoding},                          code_page => $config->{webpac}->{webpac_encoding},
170                          limit_mfn => $input->{limit},                          limit => $limit || $input->{limit},
171                            offset => $offset,
172                            lookup => $lookup,
173                            recode => $input->{recode},
174                  );                  );
175                    $log->logdie("can't create input using $input_module") unless ($input);
176    
177                  my $maxmfn = $isis->open(                  my $maxmfn = $input_db->open(
178                          filename => $input->{path},                          path => $input->{path},
179                          code_page => $input->{encoding},        # database encoding                          code_page => $input->{encoding},        # database encoding
180                  );                  );
181    
# Line 89  while (my ($database, $db_config) = each Line 184  while (my ($database, $db_config) = each
184                          db => $db,                          db => $db,
185                          lookup_regex => $lookup->regex,                          lookup_regex => $lookup->regex,
186                          lookup => $lookup,                          lookup => $lookup,
187                            prefix => $input->{name},
188                  );                  );
189    
190                  $n->open(                  my $normalize_path = $input->{normalize}->{path};
191                          tag => $input->{normalize}->{tag},  
192                          xml_file => $input->{normalize}->{path},                  if ($normalize_path =~ m/\.xml$/i) {
193                  );                          $n->open(
194                                    tag => $input->{normalize}->{tag},
195                                    xml_file => $input->{normalize}->{path},
196                            );
197                    } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
198                            $n->open_yaml(
199                                    path => $normalize_path,
200                                    tag => $input->{normalize}->{tag},
201                            );
202                    }
203    
204                    foreach my $pos ( 0 ... $input_db->size ) {
205    
206                  for ( 0 ... $isis->size ) {                          my $row = $input_db->fetch || next;
207    
208                          my $row = $isis->fetch || next;                          my $mfn = $row->{'000'}->[0];
209    
210                          my $mfn = $row->{'000'}->[0] || die "can't find MFN";                          if (! $mfn || $mfn !~ m#^\d+$#) {
211                                    $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
212                                    $mfn = $pos;
213                                    push @{ $row->{'000'} }, $pos;
214                            }
215    
216                          my $ds = $n->data_structure($row);                          my $ds = $n->data_structure($row);
217    
218                          $est->add(                          $indexer->add(
219                                  id => $input->{name} . "#" . $mfn,                                  id => $input->{name} . "/" . $mfn,
220                                  ds => $ds,                                  ds => $ds,
221                                  type => $config->{hyperestraier}->{type},                                  type => $config->{$use_indexer}->{type},
222                          );                          );
223    
224                          $total_rows++;                          $total_rows++;
# Line 115  while (my ($database, $db_config) = each Line 226  while (my ($database, $db_config) = each
226    
227          };          };
228    
229          $log->info("$total_rows records indexed");          my $dt = time() - $start_t;
230            $log->info("$total_rows records indexed in " .
231                    sprintf("%.2f sec [%.2f rec/sec]",
232                            $dt, ($total_rows / $dt)
233                    )
234            );
235    
236            #
237            # add Hyper Estraier links to other databases
238            #
239            if (ref($db_config->{links}) eq 'ARRAY') {
240                    foreach my $link (@{ $db_config->{links} }) {
241                            if ($use_indexer eq 'hyperestraier') {
242                                    $log->info("adding link $database -> $link->{to} [$link->{credit}]");
243                                    $indexer->add_link(
244                                            from => $database,
245                                            to => $link->{to},
246                                            credit => $link->{credit},
247                                    );
248                            } else {
249                                    $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
250                            }
251                    }
252            }
253    
254  }  }
255    

Legend:
Removed from v.213  
changed lines
  Added in v.431

  ViewVC Help
Powered by ViewVC 1.1.26