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

Legend:
Removed from v.141  
changed lines
  Added in v.539

  ViewVC Help
Powered by ViewVC 1.1.26