6 use File::Temp qw/tempdir/;
9 use WebPAC::Common 0.02;
11 use WebPAC::Input 0.03;
12 use WebPAC::Store 0.03;
13 use WebPAC::Normalize;
14 use WebPAC::Output::TT;
16 use YAML qw/LoadFile/;
19 use Time::HiRes qw/time/;
21 use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net
23 use Data::Dump qw/dump/;
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 my $lint = new MARC::Lint if ($marc_lint);
150 while (my ($database, $db_config) = each %{ $config->{databases} }) {
152 my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
153 next if ($only_database && $database !~ m/$only_database/i);
156 my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
157 $indexer_config->{database} = $database;
158 $indexer_config->{clean} = $clean;
159 $indexer_config->{label} = $db_config->{name};
161 if ($use_indexer eq 'hyperestraier') {
163 # open Hyper Estraier database
164 use WebPAC::Output::Estraier '0.10';
165 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
167 } elsif ($use_indexer eq 'kinosearch') {
170 use WebPAC::Output::KinoSearch;
171 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
172 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
175 $log->logdie("unknown use_indexer: $use_indexer");
178 $log->logide("can't continue without valid indexer") unless ($indexer);
185 my $abs_path = abs_path($0);
186 $abs_path =~ s#/[^/]*$#/#;
188 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
191 $log->info("creating new database '$database' in $db_path");
192 rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
194 $log->info("working on database '$database' in $db_path");
197 my $db = new WebPAC::Store(
199 database => $database,
205 # now, iterate through input formats
209 if (ref($db_config->{input}) eq 'ARRAY') {
210 @inputs = @{ $db_config->{input} };
211 } elsif ($db_config->{input}) {
212 push @inputs, $db_config->{input};
214 $log->info("database $database doesn't have inputs defined");
217 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
219 foreach my $input (@inputs) {
221 next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
223 my $type = lc($input->{type});
225 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
227 my $lookup = new WebPAC::Lookup(
228 lookup_file => $input->{lookup},
229 ) if ($input->{lookup});
231 my $input_module = $config->{webpac}->{inputs}->{$type};
233 $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
234 $input->{lookup} ? "lookup '$input->{lookup}'" : ""
237 my $input_db = new WebPAC::Input(
238 module => $input_module,
239 code_page => $config->{webpac}->{webpac_encoding},
240 limit => $limit || $input->{limit},
243 recode => $input->{recode},
246 $log->logdie("can't create input using $input_module") unless ($input);
248 my $maxmfn = $input_db->open(
249 path => $input->{path},
250 code_page => $input->{encoding}, # database encoding
254 my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
255 @{ $input->{normalize} } : ( $input->{normalize} );
257 if ($marc_normalize) {
259 path => $marc_normalize,
260 output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
264 foreach my $normalize (@norm_array) {
266 my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
268 $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
270 my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
272 $log->info("Using $normalize_path for normalization...");
275 if (my $path = $normalize->{output}) {
276 open($marc_fh, '>', $path) ||
277 $log->logdie("can't open MARC output $path: $!");
279 $log->info("Creating MARC export file $path", $marc_lint ? ' (with lint)' : '', "\n");
282 # reset position in database
285 foreach my $pos ( 0 ... $input_db->size ) {
287 my $row = $input_db->fetch || next;
289 my $mfn = $row->{'000'}->[0];
291 if (! $mfn || $mfn !~ m#^\d+$#) {
292 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
294 push @{ $row->{'000'} }, $pos;
299 my @errors = $validate->validate_errors( $row );
300 $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
303 my $ds = WebPAC::Normalize::data_structure(
306 lookup => $lookup ? $lookup->lookup_hash : undef,
307 marc_encoding => 'utf-8',
313 prefix => $input->{name},
314 ) if ($ds && !$stats);
317 id => $input->{name} . "/" . $mfn,
319 type => $config->{$use_indexer}->{type},
320 ) if ($indexer && $ds);
323 my $marc = new MARC::Record;
324 $marc->encoding( 'utf-8' );
325 my @marc_fields = WebPAC::Normalize::_get_marc_fields();
326 if (! @marc_fields) {
327 $log->warn("MARC record $mfn is empty, skipping");
329 $marc->add_fields( @marc_fields );
332 $lint->check_record( $marc );
333 my $err = join( "\n", $lint->warnings );
334 $log->error("MARC lint detected warning on MFN $mfn\n",
335 "Original imput row: ",dump($row), "\n",
336 "Normalized MARC row: ",dump(@marc_fields), "\n",
337 "MARC lint warnings: ",$err,"\n"
342 $log->info("MARC record on MFN $mfn\n",
343 "Original imput row: ",dump($row), "\n",
344 "Normalized MARC row: ",dump(@marc_fields), "\n",
348 print $marc_fh $marc->as_usmarc;
355 $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
358 close($marc_fh) if ($marc_fh);
364 eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
366 my $dt = time() - $start_t;
367 $log->info("$total_rows records ", $indexer ? "indexed " : "",
368 sprintf("in %.2f sec [%.2f rec/sec]",
369 $dt, ($total_rows / $dt)
374 # add Hyper Estraier links to other databases
376 if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
377 foreach my $link (@{ $db_config->{links} }) {
378 if ($use_indexer eq 'hyperestraier') {
379 $log->info("saving link $database -> $link->{to} [$link->{credit}]");
383 credit => $link->{credit},
386 $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
393 foreach my $link (@links) {
394 $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
395 $indexer->add_link( %{ $link } );