/[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

Annotation of /trunk/run.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 510 - (hide annotations)
Mon May 15 17:38:22 2006 UTC (17 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 7589 byte(s)
added input filter to --only

1 dpavlin 74 #!/usr/bin/perl -w
2    
3     use strict;
4    
5     use Cwd qw/abs_path/;
6     use File::Temp qw/tempdir/;
7     use Data::Dumper;
8     use lib './lib';
9    
10 dpavlin 255 use WebPAC::Common 0.02;
11 dpavlin 74 use WebPAC::Lookup;
12 dpavlin 287 use WebPAC::Input 0.03;
13 dpavlin 209 use WebPAC::Store 0.03;
14 dpavlin 74 use WebPAC::Normalize::XML;
15 dpavlin 492 use WebPAC::Normalize::Set;
16 dpavlin 74 use WebPAC::Output::TT;
17 dpavlin 141 use YAML qw/LoadFile/;
18 dpavlin 301 use Getopt::Long;
19     use File::Path;
20 dpavlin 389 use Time::HiRes qw/time/;
21 dpavlin 492 use File::Slurp;
22 dpavlin 74
23 dpavlin 301 =head1 NAME
24 dpavlin 76
25 dpavlin 301 run.pl - start WebPAC indexing
26 dpavlin 141
27 dpavlin 301 B<this command will probably go away. Don't get used to it!>
28 dpavlin 141
29 dpavlin 301 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 dpavlin 510 =item --only=database_name/input_filter
46 dpavlin 335
47 dpavlin 423 reindex just single database (legacy name is --one)
48 dpavlin 335
49 dpavlin 510 C</input_filter> is optional part which can be C<name>
50     or C<type> from input
51    
52 dpavlin 301 =item --config conf/config.yml
53    
54     path to YAML configuration file
55    
56 dpavlin 492 =item --force-set
57    
58 dpavlin 508 force conversion C<< normalize->path >> in C<config.yml> from
59 dpavlin 492 C<.xml> to C<.pl>
60    
61 dpavlin 507 =item --stats
62    
63 dpavlin 509 disable indexing and dump statistics about field and subfield
64     usage for each input
65 dpavlin 507
66 dpavlin 301 =back
67    
68     =cut
69    
70     my $offset;
71     my $limit;
72    
73     my $clean = 0;
74     my $config = 'conf/config.yml';
75     my $debug = 0;
76 dpavlin 510 my $only_filter;
77 dpavlin 492 my $force_set = 0;
78 dpavlin 507 my $stats = 0;
79 dpavlin 301
80     GetOptions(
81     "limit=i" => \$limit,
82     "offset=i" => \$offset,
83     "clean" => \$clean,
84 dpavlin 510 "one=s" => \$only_filter,
85     "only=s" => \$only_filter,
86 dpavlin 301 "config" => \$config,
87     "debug" => \$debug,
88 dpavlin 492 "force-set" => \$force_set,
89 dpavlin 507 "stats" => \$stats,
90 dpavlin 301 );
91    
92     $config = LoadFile($config);
93    
94     print "config = ",Dumper($config) if ($debug);
95    
96 dpavlin 210 die "no databases in config file!\n" unless ($config->{databases});
97 dpavlin 431
98     my $log = _new WebPAC::Common()->_get_logger();
99 dpavlin 509 $log->info( "-" x 79 );
100 dpavlin 431
101 dpavlin 430 my $use_indexer = $config->{use_indexer} || 'hyperestraier';
102 dpavlin 509 if ($stats) {
103     $log->debug("option --stats disables update of indexing engine...");
104     $use_indexer = undef;
105     } else {
106     $log->info("using $use_indexer indexing engine...");
107     }
108 dpavlin 141
109 dpavlin 213 my $total_rows = 0;
110 dpavlin 389 my $start_t = time();
111 dpavlin 213
112 dpavlin 210 while (my ($database, $db_config) = each %{ $config->{databases} }) {
113 dpavlin 141
114 dpavlin 510 my ($only_database,$only_input) = split(m#/#, $only_filter);
115     next if ($only_database && $database !~ m/$only_database/i);
116 dpavlin 335
117 dpavlin 430 my $indexer;
118 dpavlin 255
119 dpavlin 509 if ($use_indexer) {
120     my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
121     $indexer_config->{database} = $database;
122     $indexer_config->{clean} = $clean;
123     $indexer_config->{label} = $db_config->{name};
124 dpavlin 431
125 dpavlin 509 if ($use_indexer eq 'hyperestraier') {
126 dpavlin 255
127 dpavlin 509 # open Hyper Estraier database
128     use WebPAC::Output::Estraier '0.10';
129     $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
130    
131     } elsif ($use_indexer eq 'kinosearch') {
132 dpavlin 430
133 dpavlin 509 # open KinoSearch
134     use WebPAC::Output::KinoSearch;
135     $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
136     $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
137 dpavlin 431
138 dpavlin 509 } else {
139     $log->logdie("unknown use_indexer: $use_indexer");
140     }
141    
142     $log->logide("can't continue without valid indexer") unless ($indexer);
143 dpavlin 430 }
144    
145    
146 dpavlin 255 #
147     # now WebPAC::Store
148     #
149 dpavlin 210 my $abs_path = abs_path($0);
150     $abs_path =~ s#/[^/]*$#/#;
151 dpavlin 141
152 dpavlin 210 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
153 dpavlin 74
154 dpavlin 301 if ($clean) {
155     $log->info("creating new database $database in $db_path");
156     rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
157     } else {
158 dpavlin 391 $log->debug("working on $database in $db_path");
159 dpavlin 301 }
160 dpavlin 255
161 dpavlin 210 my $db = new WebPAC::Store(
162     path => $db_path,
163 dpavlin 217 database => $database,
164 dpavlin 301 debug => $debug,
165 dpavlin 210 );
166 dpavlin 74
167 dpavlin 233
168 dpavlin 213 #
169     # now, iterate through input formats
170     #
171 dpavlin 74
172 dpavlin 213 my @inputs;
173     if (ref($db_config->{input}) eq 'ARRAY') {
174     @inputs = @{ $db_config->{input} };
175 dpavlin 255 } elsif ($db_config->{input}) {
176     push @inputs, $db_config->{input};
177 dpavlin 213 } else {
178 dpavlin 255 $log->info("database $database doesn't have inputs defined");
179 dpavlin 213 }
180 dpavlin 74
181 dpavlin 286 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
182    
183 dpavlin 213 foreach my $input (@inputs) {
184 dpavlin 233
185 dpavlin 510 next if ($only_input && $input->{name} =~ m#$only_input#i || $input->{type} =~ m#$only_input#i);
186    
187 dpavlin 233 my $type = lc($input->{type});
188    
189 dpavlin 286 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
190 dpavlin 233
191     my $lookup = new WebPAC::Lookup(
192     lookup_file => $input->{lookup},
193     );
194    
195 dpavlin 286 my $input_module = $config->{webpac}->{inputs}->{$type};
196 dpavlin 74
197 dpavlin 391 $log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'");
198 dpavlin 286
199 dpavlin 287 my $input_db = new WebPAC::Input(
200     module => $input_module,
201 dpavlin 213 code_page => $config->{webpac}->{webpac_encoding},
202 dpavlin 301 limit => $limit || $input->{limit},
203     offset => $offset,
204 dpavlin 251 lookup => $lookup,
205 dpavlin 416 recode => $input->{recode},
206 dpavlin 507 stats => $stats,
207 dpavlin 287 );
208 dpavlin 286 $log->logdie("can't create input using $input_module") unless ($input);
209 dpavlin 113
210 dpavlin 287 my $maxmfn = $input_db->open(
211 dpavlin 285 path => $input->{path},
212 dpavlin 213 code_page => $input->{encoding}, # database encoding
213     );
214 dpavlin 113
215 dpavlin 213 my $n = new WebPAC::Normalize::XML(
216     # filter => { 'foo' => sub { shift } },
217     db => $db,
218     lookup_regex => $lookup->regex,
219     lookup => $lookup,
220 dpavlin 221 prefix => $input->{name},
221 dpavlin 213 );
222 dpavlin 113
223 dpavlin 492 my $rules;
224 dpavlin 269 my $normalize_path = $input->{normalize}->{path};
225 dpavlin 210
226 dpavlin 492 if ($force_set) {
227     my $new_norm_path = $normalize_path;
228     $new_norm_path =~ s/\.xml$/.pl/;
229     if (-e $new_norm_path) {
230 dpavlin 493 $log->debug("--force-set replaced $normalize_path with $new_norm_path");
231 dpavlin 492 $normalize_path = $new_norm_path;
232     } else {
233 dpavlin 493 $log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path");
234 dpavlin 492 }
235     }
236    
237 dpavlin 269 if ($normalize_path =~ m/\.xml$/i) {
238     $n->open(
239     tag => $input->{normalize}->{tag},
240 dpavlin 492 xml_file => $normalize_path,
241 dpavlin 269 );
242     } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
243     $n->open_yaml(
244     path => $normalize_path,
245     tag => $input->{normalize}->{tag},
246     );
247 dpavlin 492 } elsif ($normalize_path =~ m/\.(?:pl)$/i) {
248     $n = undef;
249     $log->info("using WebPAC::Normalize::Set to process $normalize_path");
250     $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
251 dpavlin 269 }
252    
253 dpavlin 290 foreach my $pos ( 0 ... $input_db->size ) {
254 dpavlin 210
255 dpavlin 287 my $row = $input_db->fetch || next;
256 dpavlin 213
257 dpavlin 291 my $mfn = $row->{'000'}->[0];
258 dpavlin 213
259 dpavlin 291 if (! $mfn || $mfn !~ m#^\d+$#) {
260 dpavlin 290 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
261     $mfn = $pos;
262 dpavlin 291 push @{ $row->{'000'} }, $pos;
263 dpavlin 290 }
264    
265 dpavlin 508
266     my $ds;
267     if ($n) {
268     $ds = $n->data_structure($row);
269     } else {
270     $ds = WebPAC::Normalize::Set::data_structure(
271 dpavlin 492 row => $row,
272     rules => $rules,
273     lookup => $lookup->lookup_hash,
274     );
275 dpavlin 213
276 dpavlin 508 $db->save_ds(
277     id => $mfn,
278     ds => $ds,
279     prefix => $input->{name},
280 dpavlin 509 ) if ($ds && !$stats);
281 dpavlin 508 }
282    
283 dpavlin 430 $indexer->add(
284 dpavlin 291 id => $input->{name} . "/" . $mfn,
285 dpavlin 213 ds => $ds,
286 dpavlin 430 type => $config->{$use_indexer}->{type},
287 dpavlin 509 ) if ($indexer);
288 dpavlin 213
289     $total_rows++;
290     }
291    
292 dpavlin 507 $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
293    
294 dpavlin 210 };
295    
296 dpavlin 509 eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
297 dpavlin 434
298 dpavlin 389 my $dt = time() - $start_t;
299     $log->info("$total_rows records indexed in " .
300     sprintf("%.2f sec [%.2f rec/sec]",
301     $dt, ($total_rows / $dt)
302     )
303     );
304 dpavlin 255
305 dpavlin 431 #
306     # add Hyper Estraier links to other databases
307     #
308     if (ref($db_config->{links}) eq 'ARRAY') {
309     foreach my $link (@{ $db_config->{links} }) {
310     if ($use_indexer eq 'hyperestraier') {
311 dpavlin 430 $log->info("adding link $database -> $link->{to} [$link->{credit}]");
312     $indexer->add_link(
313     from => $database,
314     to => $link->{to},
315     credit => $link->{credit},
316     );
317 dpavlin 431 } else {
318     $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
319 dpavlin 430 }
320 dpavlin 255 }
321     }
322    
323 dpavlin 210 }
324    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26