6 use File::Temp qw/tempdir/;
9 use WebPAC::Common 0.02;
10 use WebPAC::Lookup 0.03;
11 use WebPAC::Input 0.07;
12 use WebPAC::Store 0.03;
13 use WebPAC::Normalize 0.11;
14 use WebPAC::Output::TT;
16 use WebPAC::Output::MARC;
17 use YAML qw/LoadFile/;
20 use Time::HiRes qw/time/;
22 use Data::Dump qw/dump/;
26 run.pl - start WebPAC indexing
28 B<this command will probably go away. Don't get used to it!>
36 start loading (all) databases at offset 42
40 limit loading to 100 records
44 remove database and Hyper Estraier index before indexing
46 =item --only=database_name/input_filter
48 reindex just single database (legacy name is --one)
50 C</input_filter> is optional part which can be C<name>
53 =item --config conf/config.yml
55 path to YAML configuration file
59 disable indexing and dump statistics about field and subfield
62 =item --validate path/to/validation_file
64 turn on extra validation of imput records, see L<WebPAC::Validation>
66 =item --marc-normalize conf/normalize/mapping.pl
68 This option specifies normalisation file for MARC creation
70 =item --marc-output out/marc/test.marc
72 Optional path to output file
76 By default turned on if C<--marc-normalize> is used. You can disable lint
77 messages with C<--no-marc-lint>.
81 Force dump or input and marc record for debugging.
91 my $config = 'conf/config.yml';
96 my ($marc_normalize, $marc_output);
101 "limit=i" => \$limit,
102 "offset=i" => \$offset,
104 "one=s" => \$only_filter,
105 "only=s" => \$only_filter,
106 "config" => \$config,
109 "validate=s" => \$validate_path,
110 "marc-normalize=s" => \$marc_normalize,
111 "marc-output=s" => \$marc_output,
112 "marc-lint!" => \$marc_lint,
113 "marc-dump!" => \$marc_dump,
116 $config = LoadFile($config);
118 print "config = ",dump($config) if ($debug);
120 die "no databases in config file!\n" unless ($config->{databases});
122 my $log = _new WebPAC::Common()->_get_logger();
123 $log->info( "-" x 79 );
126 $validate = new WebPAC::Validate(
127 path => $validate_path,
128 ) if ($validate_path);
130 my $use_indexer = $config->{use_indexer} || 'hyperestraier';
132 $log->debug("option --stats disables update of indexing engine...");
133 $use_indexer = undef;
135 $log->info("using $use_indexer indexing engine...");
138 # disable indexing when creating marc
139 $use_indexer = undef if ($marc_normalize);
142 my $start_t = time();
147 while (my ($database, $db_config) = each %{ $config->{databases} }) {
149 my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
150 next if ($only_database && $database !~ m/$only_database/i);
153 my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
154 $indexer_config->{database} = $database;
155 $indexer_config->{clean} = $clean;
156 $indexer_config->{label} = $db_config->{name};
158 if ($use_indexer eq 'hyperestraier') {
160 # open Hyper Estraier database
161 use WebPAC::Output::Estraier '0.10';
162 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
164 } elsif ($use_indexer eq 'kinosearch') {
167 use WebPAC::Output::KinoSearch;
168 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
169 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
172 $log->logdie("unknown use_indexer: $use_indexer");
175 $log->logide("can't continue without valid indexer") unless ($indexer);
182 my $abs_path = abs_path($0);
183 $abs_path =~ s#/[^/]*$#/#;
185 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
188 $log->info("creating new database '$database' in $db_path");
189 rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
191 $log->info("working on database '$database' in $db_path");
194 my $db = new WebPAC::Store(
196 database => $database,
202 # now, iterate through input formats
206 if (ref($db_config->{input}) eq 'ARRAY') {
207 @inputs = @{ $db_config->{input} };
208 } elsif ($db_config->{input}) {
209 push @inputs, $db_config->{input};
211 $log->info("database $database doesn't have inputs defined");
214 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
216 foreach my $input (@inputs) {
218 next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
220 my $type = lc($input->{type});
222 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
225 if ($input->{lookup}) {
226 $lookup = new WebPAC::Lookup(
227 lookup_file => $input->{lookup},
229 delete( $input->{lookup} );
232 my $input_module = $config->{webpac}->{inputs}->{$type};
234 $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
235 $input->{lookup} ? "lookup '$input->{lookup}'" : ""
238 my $input_db = new WebPAC::Input(
239 module => $input_module,
240 encoding => $config->{webpac}->{webpac_encoding},
241 limit => $limit || $input->{limit},
243 lookup_coderef => sub {
244 my $rec = shift || return;
245 $lookup->add( $rec );
247 recode => $input->{recode},
250 $log->logdie("can't create input using $input_module") unless ($input);
252 my $maxmfn = $input_db->open(
253 path => $input->{path},
254 code_page => $input->{encoding}, # database encoding
258 my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
259 @{ $input->{normalize} } : ( $input->{normalize} );
261 if ($marc_normalize) {
263 path => $marc_normalize,
264 output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
268 foreach my $normalize (@norm_array) {
270 my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
272 $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
274 my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
276 $log->info("Using $normalize_path for normalization...");
278 my $marc = new WebPAC::Output::MARC(
279 path => $normalize->{output},
282 ) if ($normalize->{output});
284 # reset position in database
287 foreach my $pos ( 0 ... $input_db->size ) {
289 my $row = $input_db->fetch || next;
291 my $mfn = $row->{'000'}->[0];
293 if (! $mfn || $mfn !~ m#^\d+$#) {
294 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
296 push @{ $row->{'000'} }, $pos;
301 my @errors = $validate->validate_errors( $row );
302 $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
305 my $ds = WebPAC::Normalize::data_structure(
308 lookup => $lookup ? $lookup->lookup_hash : undef,
309 marc_encoding => 'utf-8',
315 prefix => $input->{name},
316 ) if ($ds && !$stats);
319 id => $input->{name} . "/" . $mfn,
321 type => $config->{$use_indexer}->{type},
322 ) if ($indexer && $ds);
327 while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
329 id => $mfn . ( $i ? "/$i" : '' ),
331 leader => WebPAC::Normalize::marc_leader(),
337 $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
343 $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
346 $marc->finish if ($marc);
352 eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
354 my $dt = time() - $start_t;
355 $log->info("$total_rows records ", $indexer ? "indexed " : "",
356 sprintf("in %.2f sec [%.2f rec/sec]",
357 $dt, ($total_rows / $dt)
362 # add Hyper Estraier links to other databases
364 if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
365 foreach my $link (@{ $db_config->{links} }) {
366 if ($use_indexer eq 'hyperestraier') {
367 $log->info("saving link $database -> $link->{to} [$link->{credit}]");
371 credit => $link->{credit},
374 $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
381 foreach my $link (@links) {
382 $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
383 $indexer->add_link( %{ $link } );