/[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 430 by dpavlin, Mon Apr 17 15:09:54 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    my $use_indexer = $config->{use_indexer} || 'hyperestraier';
79    
80  my $total_rows = 0;  my $total_rows = 0;
81    my $start_t = time();
82    
83  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
84    
85          my $type = lc($db_config->{input}->{type});          next if ($only_db_name && $database !~ m/$only_db_name/i);
86    
87          die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');          my $log = _new WebPAC::Common()->_get_logger();
88    
89          my $abs_path = abs_path($0);          my $indexer;
         $abs_path =~ s#/[^/]*$#/#;  
90    
91          my $lookup = new WebPAC::Lookup(          if ($use_indexer eq 'hyperestraier') {
                 lookup_file => $db_config->{input}->{lookup},  
         );  
92    
93          my $db_path = $config->{webpac}->{db_path} . '/' . $database;                  #
94                    # open Hyper Estraier database
95                    #
96    
97                    use WebPAC::Output::Estraier '0.10';
98                    my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");
99                    $est_config->{database} = $database;
100                    $est_config->{clean} = $clean;
101                    $est_config->{label} = $db_config->{name};
102    
103          my $log = $lookup->_get_logger;                  $indexer = new WebPAC::Output::Estraier( %{ $est_config } );
104          $log->info("working on $database in $db_path");          } else {
105                    $log->logdie("unknown use_indexer: $use_indexer");
106            }
107    
108          my $path = './db/';          $log->logide("can't continue without valid indexer") unless ($indexer);
109    
110          my $db = new WebPAC::Store(          #
111                  path => $db_path,          # now WebPAC::Store
112          );          #
113            my $abs_path = abs_path($0);
114            $abs_path =~ s#/[^/]*$#/#;
115    
116          my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");          my $db_path = $config->{webpac}->{db_path} . '/' . $database;
         $est_config->{database} = $database;  
117    
118          $log->info("using HyperEstraier URL $est_config->{masterurl}");          if ($clean) {
119                    $log->info("creating new database $database in $db_path");
120                    rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
121            } else {
122                    $log->debug("working on $database in $db_path");
123            }
124    
125          my $est = new WebPAC::Output::Estraier(          my $db = new WebPAC::Store(
126                  %{ $est_config },                  path => $db_path,
127                    database => $database,
128                    debug => $debug,
129          );          );
130    
131    
132          #          #
133          # now, iterate through input formats          # now, iterate through input formats
134          #          #
# Line 67  while (my ($database, $db_config) = each Line 136  while (my ($database, $db_config) = each
136          my @inputs;          my @inputs;
137          if (ref($db_config->{input}) eq 'ARRAY') {          if (ref($db_config->{input}) eq 'ARRAY') {
138                  @inputs = @{ $db_config->{input} };                  @inputs = @{ $db_config->{input} };
139          } else {          } elsif ($db_config->{input}) {
140                  push @inputs, $db_config->{input};                  push @inputs, $db_config->{input};
141            } else {
142                    $log->info("database $database doesn't have inputs defined");
143          }          }
144    
145            my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
146    
147          foreach my $input (@inputs) {          foreach my $input (@inputs) {
                 $log->info("working on input $input->{path} [$input->{type}]");  
148    
149                  my $isis = new WebPAC::Input::ISIS(                  my $type = lc($input->{type});
150    
151                    die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
152    
153                    my $lookup = new WebPAC::Lookup(
154                            lookup_file => $input->{lookup},
155                    );
156    
157                    my $input_module = $config->{webpac}->{inputs}->{$type};
158    
159                    $log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'");
160    
161                    my $input_db = new WebPAC::Input(
162                            module => $input_module,
163                          code_page => $config->{webpac}->{webpac_encoding},                          code_page => $config->{webpac}->{webpac_encoding},
164                          limit_mfn => $input->{limit},                          limit => $limit || $input->{limit},
165                            offset => $offset,
166                            lookup => $lookup,
167                            recode => $input->{recode},
168                  );                  );
169                    $log->logdie("can't create input using $input_module") unless ($input);
170    
171                  my $maxmfn = $isis->open(                  my $maxmfn = $input_db->open(
172                          filename => $input->{path},                          path => $input->{path},
173                          code_page => $input->{encoding},        # database encoding                          code_page => $input->{encoding},        # database encoding
174                  );                  );
175    
# Line 89  while (my ($database, $db_config) = each Line 178  while (my ($database, $db_config) = each
178                          db => $db,                          db => $db,
179                          lookup_regex => $lookup->regex,                          lookup_regex => $lookup->regex,
180                          lookup => $lookup,                          lookup => $lookup,
181                            prefix => $input->{name},
182                  );                  );
183    
184                  $n->open(                  my $normalize_path = $input->{normalize}->{path};
185                          tag => $input->{normalize}->{tag},  
186                          xml_file => $input->{normalize}->{path},                  if ($normalize_path =~ m/\.xml$/i) {
187                  );                          $n->open(
188                                    tag => $input->{normalize}->{tag},
189                                    xml_file => $input->{normalize}->{path},
190                            );
191                    } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
192                            $n->open_yaml(
193                                    path => $normalize_path,
194                                    tag => $input->{normalize}->{tag},
195                            );
196                    }
197    
198                  for ( 0 ... $isis->size ) {                  foreach my $pos ( 0 ... $input_db->size ) {
199    
200                          my $row = $isis->fetch || next;                          my $row = $input_db->fetch || next;
201    
202                          my $mfn = $row->{'000'}->[0] || die "can't find MFN";                          my $mfn = $row->{'000'}->[0];
203    
204                            if (! $mfn || $mfn !~ m#^\d+$#) {
205                                    $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
206                                    $mfn = $pos;
207                                    push @{ $row->{'000'} }, $pos;
208                            }
209    
210                          my $ds = $n->data_structure($row);                          my $ds = $n->data_structure($row);
211    
212                          $est->add(                          $indexer->add(
213                                  id => $input->{name} . "#" . $mfn,                                  id => $input->{name} . "/" . $mfn,
214                                  ds => $ds,                                  ds => $ds,
215                                  type => $config->{hyperestraier}->{type},                                  type => $config->{$use_indexer}->{type},
216                          );                          );
217    
218                          $total_rows++;                          $total_rows++;
# Line 115  while (my ($database, $db_config) = each Line 220  while (my ($database, $db_config) = each
220    
221          };          };
222    
223          $log->info("$total_rows records indexed");          my $dt = time() - $start_t;
224            $log->info("$total_rows records indexed in " .
225                    sprintf("%.2f sec [%.2f rec/sec]",
226                            $dt, ($total_rows / $dt)
227                    )
228            );
229    
230            if ($use_indexer eq 'hyperestraier') {
231                    #
232                    # add Hyper Estraier links to other databases
233                    #
234                    if (ref($db_config->{links}) eq 'ARRAY') {
235                            foreach my $link (@{ $db_config->{links} }) {
236                                    $log->info("adding link $database -> $link->{to} [$link->{credit}]");
237                                    $indexer->add_link(
238                                            from => $database,
239                                            to => $link->{to},
240                                            credit => $link->{credit},
241                                    );
242                            }
243                    }
244            } else {
245                    $log->warn("links not implemented for $use_indexer");
246            }
247    
248  }  }
249    

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

  ViewVC Help
Powered by ViewVC 1.1.26