6 use File::Temp qw/tempdir/;
10 use WebPAC::Common 0.02;
12 use WebPAC::Input 0.03;
13 use WebPAC::Store 0.03;
14 use WebPAC::Normalize;
15 use WebPAC::Output::TT;
17 use YAML qw/LoadFile/;
20 use Time::HiRes qw/time/;
22 use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net
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>
74 my $config = 'conf/config.yml';
82 "offset=i" => \$offset,
84 "one=s" => \$only_filter,
85 "only=s" => \$only_filter,
89 "validate=s" => \$validate_path,
92 $config = LoadFile($config);
94 print "config = ",Dumper($config) if ($debug);
96 die "no databases in config file!\n" unless ($config->{databases});
98 my $log = _new WebPAC::Common()->_get_logger();
99 $log->info( "-" x 79 );
102 $validate = new WebPAC::Validate(
103 path => $validate_path,
104 ) if ($validate_path);
106 my $use_indexer = $config->{use_indexer} || 'hyperestraier';
108 $log->debug("option --stats disables update of indexing engine...");
109 $use_indexer = undef;
111 $log->info("using $use_indexer indexing engine...");
115 my $start_t = time();
120 while (my ($database, $db_config) = each %{ $config->{databases} }) {
122 my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
123 next if ($only_database && $database !~ m/$only_database/i);
126 my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
127 $indexer_config->{database} = $database;
128 $indexer_config->{clean} = $clean;
129 $indexer_config->{label} = $db_config->{name};
131 if ($use_indexer eq 'hyperestraier') {
133 # open Hyper Estraier database
134 use WebPAC::Output::Estraier '0.10';
135 $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
137 } elsif ($use_indexer eq 'kinosearch') {
140 use WebPAC::Output::KinoSearch;
141 $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
142 $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
145 $log->logdie("unknown use_indexer: $use_indexer");
148 $log->logide("can't continue without valid indexer") unless ($indexer);
155 my $abs_path = abs_path($0);
156 $abs_path =~ s#/[^/]*$#/#;
158 my $db_path = $config->{webpac}->{db_path} . '/' . $database;
161 $log->info("creating new database '$database' in $db_path");
162 rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
164 $log->info("working on database '$database' in $db_path");
167 my $db = new WebPAC::Store(
169 database => $database,
175 # now, iterate through input formats
179 if (ref($db_config->{input}) eq 'ARRAY') {
180 @inputs = @{ $db_config->{input} };
181 } elsif ($db_config->{input}) {
182 push @inputs, $db_config->{input};
184 $log->info("database $database doesn't have inputs defined");
187 my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
189 foreach my $input (@inputs) {
191 next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
193 my $type = lc($input->{type});
195 die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
197 my $lookup = new WebPAC::Lookup(
198 lookup_file => $input->{lookup},
199 ) if ($input->{lookup});
201 my $input_module = $config->{webpac}->{inputs}->{$type};
203 $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
204 $input->{lookup} ? "lookup '$input->{lookup}'" : ""
207 my $input_db = new WebPAC::Input(
208 module => $input_module,
209 code_page => $config->{webpac}->{webpac_encoding},
210 limit => $limit || $input->{limit},
213 recode => $input->{recode},
216 $log->logdie("can't create input using $input_module") unless ($input);
218 my $maxmfn = $input_db->open(
219 path => $input->{path},
220 code_page => $input->{encoding}, # database encoding
224 my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
225 @{ $input->{normalize} } : ( $input->{normalize} );
227 foreach my $normalize (@norm_array) {
229 my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
231 $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
233 my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
235 $log->info("Using $normalize_path for normalization...");
238 if (my $path = $normalize->{marc21}) {
239 open($marc_fh, '>', $path) ||
240 $log->logdie("can't open MARC output $path: $!");
242 $log->info("Creating MARC export file $path\n");
245 # reset position in database
248 foreach my $pos ( 0 ... $input_db->size ) {
250 my $row = $input_db->fetch || next;
252 my $mfn = $row->{'000'}->[0];
254 if (! $mfn || $mfn !~ m#^\d+$#) {
255 $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
257 push @{ $row->{'000'} }, $pos;
262 my @errors = $validate->validate_errors( $row );
263 $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
267 my $ds = WebPAC::Normalize::data_structure(
270 lookup => $lookup ? $lookup->lookup_hash : undef,
271 marc_encoding => 'utf-8',
277 prefix => $input->{name},
278 ) if ($ds && !$stats);
281 id => $input->{name} . "/" . $mfn,
283 type => $config->{$use_indexer}->{type},
284 ) if ($indexer && $ds);
287 my $marc = new MARC::Record;
288 $marc->encoding( 'utf-8' );
289 $marc->add_fields( WebPAC::Normalize::_get_marc21_fields() );
290 print $marc_fh $marc->as_usmarc;
296 $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
299 close($marc_fh) if ($marc_fh);
305 eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
307 my $dt = time() - $start_t;
308 $log->info("$total_rows records ", $indexer ? "indexed " : "",
309 sprintf("in %.2f sec [%.2f rec/sec]",
310 $dt, ($total_rows / $dt)
315 # add Hyper Estraier links to other databases
317 if (ref($db_config->{links}) eq 'ARRAY') {
318 foreach my $link (@{ $db_config->{links} }) {
319 if ($use_indexer eq 'hyperestraier') {
320 $log->info("saving link $database -> $link->{to} [$link->{credit}]");
324 credit => $link->{credit},
327 $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
334 foreach my $link (@links) {
335 $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
336 $indexer->add_link( %{ $link } );