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/;
23 use Storable qw/dclone/;
27 run.pl - start WebPAC indexing
29 B<this command will probably go away. Don't get used to it!>
37 start loading (all) databases at offset 42
41 limit loading to 100 records
45 remove database and Hyper Estraier index before indexing
47 =item --only=database_name/input_filter
49 reindex just single database (legacy name is --one)
51 C</input_filter> is optional part which can be C<name>
54 =item --config conf/config.yml
56 path to YAML configuration file
60 disable indexing and dump statistics about field and subfield
63 =item --validate path/to/validation_file
65 turn on extra validation of imput records, see L<WebPAC::Validation>
67 =item --marc-normalize conf/normalize/mapping.pl
69 This option specifies normalisation file for MARC creation
71 =item --marc-output out/marc/test.marc
73 Optional path to output file
77 By default turned on if C<--marc-normalize> is used. You can disable lint
78 messages with C<--no-marc-lint>.
82 Force dump or input and marc record for debugging.
92 my $config = 'conf/config.yml';
97 my ($marc_normalize, $marc_output);
102 "limit=i" => \$limit,
103 "offset=i" => \$offset,
105 "one=s" => \$only_filter,
106 "only=s" => \$only_filter,
107 "config" => \$config,
110 "validate=s" => \$validate_path,
111 "marc-normalize=s" => \$marc_normalize,
112 "marc-output=s" => \$marc_output,
113 "marc-lint!" => \$marc_lint,
114 "marc-dump!" => \$marc_dump,
117 $config = LoadFile($config);
119 print "config = ",dump($config) if ($debug);
121 die "no databases in config file!\n" unless ($config->{databases});
123 my $log = _new WebPAC::Common()->_get_logger();
124 $log->info( "-" x 79 );
127 $validate = new WebPAC::Validate(
128 path => $validate_path,
129 ) if ($validate_path);
131 my $use_indexer = $config->{use_indexer} || 'hyperestraier';
133 $log->debug("option --stats disables update of indexing engine...");
134 $use_indexer = undef;
136 $log->info("using $use_indexer indexing engine...");
139 # disable indexing when creating marc
140 $use_indexer = undef if ($marc_normalize);
143 my $start_t = time();
148 while (my ($database, $db_config) = each %{ $config->{databases} }) {
150 my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
151 next if ($only_database && $database !~ m/$only_database/i);
154 my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
155 $indexer_config->{database} = $database;
156 $indexer_config->{clean} = $clean;
157 $indexer_config->{label} = $db_config->{name};
159 if ($use_indexer eq 'hyperestraier') {
161 # open Hyper Estraier database
162 use WebPAC::Output::Estraier '0.10';
163 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
165 } elsif ($use_indexer eq 'kinosearch') {
168 use WebPAC::Output::KinoSearch;
169 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
170 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
173 $log->logdie("unknown use_indexer: $use_indexer");
176 $log->logide("can't continue without valid indexer") unless ($indexer);
183 my $abs_path = abs_path($0);
184 $abs_path =~ s#/[^/]*$#/#;
186 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
189 $log->info("creating new database '$database' in $db_path");
190 rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
192 $log->info("working on database '$database' in $db_path");
195 my $db = new WebPAC::Store(
197 database => $database,
203 # now, iterate through input formats
207 if (ref($db_config->{input}) eq 'ARRAY') {
208 @inputs = @{ $db_config->{input} };
209 } elsif ($db_config->{input}) {
210 push @inputs, $db_config->{input};
212 $log->info("database $database doesn't have inputs defined");
215 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
217 foreach my $input (@inputs) {
219 next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
221 my $type = lc($input->{type});
223 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
226 if ($input->{lookup}) {
227 $lookup = new WebPAC::Lookup(
228 lookup_file => $input->{lookup},
230 delete( $input->{lookup} );
233 my $input_module = $config->{webpac}->{inputs}->{$type};
235 $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
236 $input->{lookup} ? "lookup '$input->{lookup}'" : ""
239 my $input_db = new WebPAC::Input(
240 module => $input_module,
241 encoding => $config->{webpac}->{webpac_encoding},
242 limit => $limit || $input->{limit},
244 lookup_coderef => sub {
245 my $rec = shift || return;
246 $lookup->add( $rec );
248 recode => $input->{recode},
250 modify_records => $input->{modify_records},
252 $log->logdie("can't create input using $input_module") unless ($input);
254 my $maxmfn = $input_db->open(
255 path => $input->{path},
256 code_page => $input->{encoding}, # database encoding
260 my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
261 @{ $input->{normalize} } : ( $input->{normalize} );
263 if ($marc_normalize) {
265 path => $marc_normalize,
266 output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
270 foreach my $normalize (@norm_array) {
272 my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
274 $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
276 my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
278 $log->info("Using $normalize_path for normalization...");
280 my $marc = new WebPAC::Output::MARC(
281 path => $normalize->{output},
284 ) if ($normalize->{output});
286 # reset position in database
289 foreach my $pos ( 0 ... $input_db->size ) {
291 my $row = $input_db->fetch || next;
293 my $mfn = $row->{'000'}->[0];
295 if (! $mfn || $mfn !~ m#^\d+$#) {
296 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
298 push @{ $row->{'000'} }, $pos;
303 my @errors = $validate->validate_errors( $row );
304 $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
307 my $ds_config = dclone($db_config);
309 # default values -> database key
310 $ds_config->{_} = $database;
313 $ds_config->{_mfn} = $mfn;
315 # attach current input
316 $ds_config->{input} = $input;
318 my $ds = WebPAC::Normalize::data_structure(
321 lookup => $lookup ? $lookup->lookup_hash : undef,
322 config => $ds_config,
323 marc_encoding => 'utf-8',
329 prefix => $input->{name},
330 ) if ($ds && !$stats);
333 id => $input->{name} . "/" . $mfn,
335 type => $config->{$use_indexer}->{type},
336 ) if ($indexer && $ds);
341 while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
343 id => $mfn . ( $i ? "/$i" : '' ),
345 leader => WebPAC::Normalize::marc_leader(),
351 $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
357 $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
360 $marc->finish if ($marc);
366 eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
368 my $dt = time() - $start_t;
369 $log->info("$total_rows records ", $indexer ? "indexed " : "",
370 sprintf("in %.2f sec [%.2f rec/sec]",
371 $dt, ($total_rows / $dt)
376 # add Hyper Estraier links to other databases
378 if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
379 foreach my $link (@{ $db_config->{links} }) {
380 if ($use_indexer eq 'hyperestraier') {
381 $log->info("saving link $database -> $link->{to} [$link->{credit}]");
385 credit => $link->{credit},
388 $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
395 foreach my $link (@links) {
396 $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
397 $indexer->add_link( %{ $link } );