11 |
use WebPAC::Lookup; |
use WebPAC::Lookup; |
12 |
use WebPAC::Input 0.03; |
use WebPAC::Input 0.03; |
13 |
use WebPAC::Store 0.03; |
use WebPAC::Store 0.03; |
14 |
use WebPAC::Normalize::XML; |
use WebPAC::Normalize; |
|
use WebPAC::Normalize::Set; |
|
15 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
16 |
use WebPAC::Validate; |
use WebPAC::Validate; |
17 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
53 |
|
|
54 |
path to YAML configuration file |
path to YAML configuration file |
55 |
|
|
|
=item --force-set |
|
|
|
|
|
force conversion C<< normalize->path >> in C<config.yml> from |
|
|
C<.xml> to C<.pl> |
|
|
|
|
56 |
=item --stats |
=item --stats |
57 |
|
|
58 |
disable indexing and dump statistics about field and subfield |
disable indexing and dump statistics about field and subfield |
73 |
my $config = 'conf/config.yml'; |
my $config = 'conf/config.yml'; |
74 |
my $debug = 0; |
my $debug = 0; |
75 |
my $only_filter; |
my $only_filter; |
|
my $force_set = 0; |
|
76 |
my $stats = 0; |
my $stats = 0; |
77 |
my $validate_path; |
my $validate_path; |
78 |
|
|
84 |
"only=s" => \$only_filter, |
"only=s" => \$only_filter, |
85 |
"config" => \$config, |
"config" => \$config, |
86 |
"debug" => \$debug, |
"debug" => \$debug, |
|
"force-set" => \$force_set, |
|
87 |
"stats" => \$stats, |
"stats" => \$stats, |
88 |
"validate=s" => \$validate_path, |
"validate=s" => \$validate_path, |
89 |
); |
); |
113 |
my $total_rows = 0; |
my $total_rows = 0; |
114 |
my $start_t = time(); |
my $start_t = time(); |
115 |
|
|
116 |
|
my @links; |
117 |
|
my $indexer; |
118 |
|
|
119 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
120 |
|
|
121 |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
122 |
next if ($only_database && $database !~ m/$only_database/i); |
next if ($only_database && $database !~ m/$only_database/i); |
123 |
|
|
|
my $indexer; |
|
|
|
|
124 |
if ($use_indexer) { |
if ($use_indexer) { |
125 |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
126 |
$indexer_config->{database} = $database; |
$indexer_config->{database} = $database; |
199 |
|
|
200 |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
201 |
|
|
202 |
$log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module lookup '$input->{lookup}'"); |
$log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module", |
203 |
|
$input->{lookup} ? "lookup '$input->{lookup}'" : "" |
204 |
|
); |
205 |
|
|
206 |
my $input_db = new WebPAC::Input( |
my $input_db = new WebPAC::Input( |
207 |
module => $input_module, |
module => $input_module, |
217 |
my $maxmfn = $input_db->open( |
my $maxmfn = $input_db->open( |
218 |
path => $input->{path}, |
path => $input->{path}, |
219 |
code_page => $input->{encoding}, # database encoding |
code_page => $input->{encoding}, # database encoding |
220 |
|
%{ $input }, |
221 |
); |
); |
222 |
|
|
223 |
my $n = new WebPAC::Normalize::XML( |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
224 |
# filter => { 'foo' => sub { shift } }, |
@{ $input->{normalize} } : ( $input->{normalize} ); |
|
db => $db, |
|
|
lookup_regex => $lookup ? $lookup->regex : undef, |
|
|
lookup => $lookup, |
|
|
prefix => $input->{name}, |
|
|
); |
|
225 |
|
|
226 |
my $rules; |
foreach my $normalize (@norm_array) { |
|
my $normalize_path = $input->{normalize}->{path}; |
|
227 |
|
|
228 |
if ($force_set) { |
my $rules; |
229 |
my $new_norm_path = $normalize_path; |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
|
$new_norm_path =~ s/\.xml$/.pl/; |
|
|
if (-e $new_norm_path) { |
|
|
$log->debug("--force-set replaced $normalize_path with $new_norm_path"); |
|
|
$normalize_path = $new_norm_path; |
|
|
} else { |
|
|
$log->debug("--force-set failed on $new_norm_path, fallback to $normalize_path"); |
|
|
} |
|
|
} |
|
230 |
|
|
231 |
if ($normalize_path =~ m/\.xml$/i) { |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
|
$n->open( |
|
|
tag => $input->{normalize}->{tag}, |
|
|
xml_file => $normalize_path, |
|
|
); |
|
|
} elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) { |
|
|
$n->open_yaml( |
|
|
path => $normalize_path, |
|
|
tag => $input->{normalize}->{tag}, |
|
|
); |
|
|
} elsif ($normalize_path =~ m/\.(?:pl)$/i) { |
|
|
$n = undef; |
|
|
$log->info("using WebPAC::Normalize::Set to process $normalize_path"); |
|
|
$rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
|
|
} |
|
232 |
|
|
233 |
foreach my $pos ( 0 ... $input_db->size ) { |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
234 |
|
|
235 |
my $row = $input_db->fetch || next; |
$log->info("Using $normalize_path for normalization..."); |
236 |
|
|
237 |
my $mfn = $row->{'000'}->[0]; |
# reset position in database |
238 |
|
$input_db->seek(1); |
239 |
|
|
240 |
if (! $mfn || $mfn !~ m#^\d+$#) { |
foreach my $pos ( 0 ... $input_db->size ) { |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
|
|
$mfn = $pos; |
|
|
push @{ $row->{'000'} }, $pos; |
|
|
} |
|
241 |
|
|
242 |
|
my $row = $input_db->fetch || next; |
243 |
|
|
244 |
if ($validate) { |
my $mfn = $row->{'000'}->[0]; |
|
my @errors = $validate->validate_errors( $row ); |
|
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
|
|
} |
|
245 |
|
|
246 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
247 |
my $ds; |
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
248 |
if ($n) { |
$mfn = $pos; |
249 |
$ds = $n->data_structure($row); |
push @{ $row->{'000'} }, $pos; |
250 |
} else { |
} |
251 |
$ds = WebPAC::Normalize::Set::data_structure( |
|
252 |
|
|
253 |
|
if ($validate) { |
254 |
|
my @errors = $validate->validate_errors( $row ); |
255 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
my $ds = WebPAC::Normalize::data_structure( |
260 |
row => $row, |
row => $row, |
261 |
rules => $rules, |
rules => $rules, |
262 |
lookup => $lookup ? $lookup->lookup_hash : undef, |
lookup => $lookup ? $lookup->lookup_hash : undef, |
267 |
ds => $ds, |
ds => $ds, |
268 |
prefix => $input->{name}, |
prefix => $input->{name}, |
269 |
) if ($ds && !$stats); |
) if ($ds && !$stats); |
|
} |
|
270 |
|
|
271 |
$indexer->add( |
$indexer->add( |
272 |
id => $input->{name} . "/" . $mfn, |
id => $input->{name} . "/" . $mfn, |
273 |
ds => $ds, |
ds => $ds, |
274 |
type => $config->{$use_indexer}->{type}, |
type => $config->{$use_indexer}->{type}, |
275 |
) if ($indexer); |
) if ($indexer); |
276 |
|
|
277 |
|
$total_rows++; |
278 |
|
} |
279 |
|
|
280 |
$total_rows++; |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
|
} |
|
281 |
|
|
282 |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
}; |
283 |
|
|
284 |
}; |
} |
285 |
|
|
286 |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
287 |
|
|
298 |
if (ref($db_config->{links}) eq 'ARRAY') { |
if (ref($db_config->{links}) eq 'ARRAY') { |
299 |
foreach my $link (@{ $db_config->{links} }) { |
foreach my $link (@{ $db_config->{links} }) { |
300 |
if ($use_indexer eq 'hyperestraier') { |
if ($use_indexer eq 'hyperestraier') { |
301 |
$log->info("adding link $database -> $link->{to} [$link->{credit}]"); |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
302 |
$indexer->add_link( |
push @links, { |
303 |
from => $database, |
from => $database, |
304 |
to => $link->{to}, |
to => $link->{to}, |
305 |
credit => $link->{credit}, |
credit => $link->{credit}, |
306 |
); |
}; |
307 |
} else { |
} else { |
308 |
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
309 |
} |
} |
312 |
|
|
313 |
} |
} |
314 |
|
|
315 |
|
foreach my $link (@links) { |
316 |
|
$log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]"); |
317 |
|
$indexer->add_link( %{ $link } ); |
318 |
|
} |