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; |
17 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
18 |
use Getopt::Long; |
use Getopt::Long; |
19 |
use File::Path; |
use File::Path; |
20 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
21 |
use File::Slurp; |
use File::Slurp; |
22 |
|
use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net |
23 |
|
|
24 |
=head1 NAME |
=head1 NAME |
25 |
|
|
43 |
|
|
44 |
remove database and Hyper Estraier index before indexing |
remove database and Hyper Estraier index before indexing |
45 |
|
|
46 |
=item --only=database_name |
=item --only=database_name/input_filter |
47 |
|
|
48 |
reindex just single database (legacy name is --one) |
reindex just single database (legacy name is --one) |
49 |
|
|
50 |
|
C</input_filter> is optional part which can be C<name> |
51 |
|
or C<type> from input |
52 |
|
|
53 |
=item --config conf/config.yml |
=item --config conf/config.yml |
54 |
|
|
55 |
path to YAML configuration file |
path to YAML configuration file |
56 |
|
|
57 |
=item --force-set |
=item --stats |
58 |
|
|
59 |
force conversion C<< normalize->path >> in C<config.yml> from |
disable indexing and dump statistics about field and subfield |
60 |
C<.xml> to C<.pl> |
usage for each input |
61 |
|
|
62 |
=item --stats |
=item --validate path/to/validation_file |
63 |
|
|
64 |
|
turn on extra validation of imput records, see L<WebPAC::Validation> |
65 |
|
|
66 |
|
=item --marc-normalize conf/normalize/mapping.pl |
67 |
|
|
68 |
|
This option specifies normalisation file for MARC creation |
69 |
|
|
70 |
dump statistics about used fields and subfields in each input |
=item --marc-output out/marc/test.marc |
71 |
|
|
72 |
|
Optional path to output file |
73 |
|
|
74 |
=back |
=back |
75 |
|
|
81 |
my $clean = 0; |
my $clean = 0; |
82 |
my $config = 'conf/config.yml'; |
my $config = 'conf/config.yml'; |
83 |
my $debug = 0; |
my $debug = 0; |
84 |
my $only_db_name; |
my $only_filter; |
|
my $force_set = 0; |
|
85 |
my $stats = 0; |
my $stats = 0; |
86 |
|
my $validate_path; |
87 |
|
my ($marc_normalize, $marc_output); |
88 |
|
|
89 |
GetOptions( |
GetOptions( |
90 |
"limit=i" => \$limit, |
"limit=i" => \$limit, |
91 |
"offset=i" => \$offset, |
"offset=i" => \$offset, |
92 |
"clean" => \$clean, |
"clean" => \$clean, |
93 |
"one=s" => \$only_db_name, |
"one=s" => \$only_filter, |
94 |
"only=s" => \$only_db_name, |
"only=s" => \$only_filter, |
95 |
"config" => \$config, |
"config" => \$config, |
96 |
"debug" => \$debug, |
"debug" => \$debug, |
|
"force-set" => \$force_set, |
|
97 |
"stats" => \$stats, |
"stats" => \$stats, |
98 |
|
"validate=s" => \$validate_path, |
99 |
|
"marc-normalize=s" => \$marc_normalize, |
100 |
|
"marc-output=s" => \$marc_output, |
101 |
); |
); |
102 |
|
|
103 |
$config = LoadFile($config); |
$config = LoadFile($config); |
107 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
108 |
|
|
109 |
my $log = _new WebPAC::Common()->_get_logger(); |
my $log = _new WebPAC::Common()->_get_logger(); |
110 |
|
$log->info( "-" x 79 ); |
111 |
|
|
112 |
|
my $validate; |
113 |
|
$validate = new WebPAC::Validate( |
114 |
|
path => $validate_path, |
115 |
|
) if ($validate_path); |
116 |
|
|
117 |
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
118 |
$log->info("using $use_indexer indexing engine..."); |
if ($stats) { |
119 |
|
$log->debug("option --stats disables update of indexing engine..."); |
120 |
|
$use_indexer = undef; |
121 |
|
} else { |
122 |
|
$log->info("using $use_indexer indexing engine..."); |
123 |
|
} |
124 |
|
|
125 |
|
# disable indexing when creating marc |
126 |
|
$use_indexer = undef if ($marc_normalize); |
127 |
|
|
128 |
my $total_rows = 0; |
my $total_rows = 0; |
129 |
my $start_t = time(); |
my $start_t = time(); |
130 |
|
|
131 |
|
my @links; |
132 |
|
my $indexer; |
133 |
|
|
134 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
135 |
|
|
136 |
next if ($only_db_name && $database !~ m/$only_db_name/i); |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
137 |
|
next if ($only_database && $database !~ m/$only_database/i); |
138 |
|
|
139 |
my $indexer; |
if ($use_indexer) { |
140 |
|
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
141 |
|
$indexer_config->{database} = $database; |
142 |
|
$indexer_config->{clean} = $clean; |
143 |
|
$indexer_config->{label} = $db_config->{name}; |
144 |
|
|
145 |
|
if ($use_indexer eq 'hyperestraier') { |
146 |
|
|
147 |
|
# open Hyper Estraier database |
148 |
|
use WebPAC::Output::Estraier '0.10'; |
149 |
|
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
150 |
|
|
151 |
|
} elsif ($use_indexer eq 'kinosearch') { |
152 |
|
|
153 |
|
# open KinoSearch |
154 |
|
use WebPAC::Output::KinoSearch; |
155 |
|
$indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path}); |
156 |
|
$indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } ); |
157 |
|
|
158 |
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
} else { |
159 |
$indexer_config->{database} = $database; |
$log->logdie("unknown use_indexer: $use_indexer"); |
160 |
$indexer_config->{clean} = $clean; |
} |
|
$indexer_config->{label} = $db_config->{name}; |
|
|
|
|
|
if ($use_indexer eq 'hyperestraier') { |
|
|
|
|
|
# open Hyper Estraier database |
|
|
use WebPAC::Output::Estraier '0.10'; |
|
|
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
|
|
|
|
|
} elsif ($use_indexer eq 'kinosearch') { |
|
|
|
|
|
# open KinoSearch |
|
|
use WebPAC::Output::KinoSearch; |
|
|
$indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path}); |
|
|
$indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } ); |
|
161 |
|
|
162 |
} else { |
$log->logide("can't continue without valid indexer") unless ($indexer); |
|
$log->logdie("unknown use_indexer: $use_indexer"); |
|
163 |
} |
} |
164 |
|
|
|
$log->logide("can't continue without valid indexer") unless ($indexer); |
|
165 |
|
|
166 |
# |
# |
167 |
# now WebPAC::Store |
# now WebPAC::Store |
172 |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
173 |
|
|
174 |
if ($clean) { |
if ($clean) { |
175 |
$log->info("creating new database $database in $db_path"); |
$log->info("creating new database '$database' in $db_path"); |
176 |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
177 |
} else { |
} else { |
178 |
$log->debug("working on $database in $db_path"); |
$log->info("working on database '$database' in $db_path"); |
179 |
} |
} |
180 |
|
|
181 |
my $db = new WebPAC::Store( |
my $db = new WebPAC::Store( |
202 |
|
|
203 |
foreach my $input (@inputs) { |
foreach my $input (@inputs) { |
204 |
|
|
205 |
|
next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i)); |
206 |
|
|
207 |
my $type = lc($input->{type}); |
my $type = lc($input->{type}); |
208 |
|
|
209 |
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
210 |
|
|
211 |
my $lookup = new WebPAC::Lookup( |
my $lookup = new WebPAC::Lookup( |
212 |
lookup_file => $input->{lookup}, |
lookup_file => $input->{lookup}, |
213 |
); |
) if ($input->{lookup}); |
214 |
|
|
215 |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
216 |
|
|
217 |
$log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'"); |
$log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module", |
218 |
|
$input->{lookup} ? "lookup '$input->{lookup}'" : "" |
219 |
|
); |
220 |
|
|
221 |
my $input_db = new WebPAC::Input( |
my $input_db = new WebPAC::Input( |
222 |
module => $input_module, |
module => $input_module, |
232 |
my $maxmfn = $input_db->open( |
my $maxmfn = $input_db->open( |
233 |
path => $input->{path}, |
path => $input->{path}, |
234 |
code_page => $input->{encoding}, # database encoding |
code_page => $input->{encoding}, # database encoding |
235 |
|
%{ $input }, |
236 |
); |
); |
237 |
|
|
238 |
my $n = new WebPAC::Normalize::XML( |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
239 |
# filter => { 'foo' => sub { shift } }, |
@{ $input->{normalize} } : ( $input->{normalize} ); |
|
db => $db, |
|
|
lookup_regex => $lookup->regex, |
|
|
lookup => $lookup, |
|
|
prefix => $input->{name}, |
|
|
); |
|
|
|
|
|
my $rules; |
|
|
my $normalize_path = $input->{normalize}->{path}; |
|
240 |
|
|
241 |
if ($force_set) { |
if ($marc_normalize) { |
242 |
my $new_norm_path = $normalize_path; |
@norm_array = ( { |
243 |
$new_norm_path =~ s/\.xml$/.pl/; |
path => $marc_normalize, |
244 |
if (-e $new_norm_path) { |
output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc', |
245 |
$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"); |
|
|
} |
|
246 |
} |
} |
247 |
|
|
248 |
if ($normalize_path =~ m/\.xml$/i) { |
foreach my $normalize (@norm_array) { |
249 |
$n->open( |
|
250 |
tag => $input->{normalize}->{tag}, |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
251 |
xml_file => $normalize_path, |
|
252 |
); |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
|
} 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: $!"; |
|
|
} |
|
253 |
|
|
254 |
foreach my $pos ( 0 ... $input_db->size ) { |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
255 |
|
|
256 |
my $row = $input_db->fetch || next; |
$log->info("Using $normalize_path for normalization..."); |
257 |
|
|
258 |
my $mfn = $row->{'000'}->[0]; |
my $marc_fh; |
259 |
|
if (my $path = $normalize->{output}) { |
260 |
|
open($marc_fh, '>', $path) || |
261 |
|
$log->logdie("can't open MARC output $path: $!"); |
262 |
|
|
263 |
if (! $mfn || $mfn !~ m#^\d+$#) { |
$log->info("Creating MARC export file $path\n"); |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
|
|
$mfn = $pos; |
|
|
push @{ $row->{'000'} }, $pos; |
|
264 |
} |
} |
265 |
|
|
266 |
|
# reset position in database |
267 |
my $ds; |
$input_db->seek(1); |
268 |
if ($n) { |
|
269 |
$ds = $n->data_structure($row); |
foreach my $pos ( 0 ... $input_db->size ) { |
270 |
} else { |
|
271 |
$ds = WebPAC::Normalize::Set::data_structure( |
my $row = $input_db->fetch || next; |
272 |
|
|
273 |
|
my $mfn = $row->{'000'}->[0]; |
274 |
|
|
275 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
276 |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
277 |
|
$mfn = $pos; |
278 |
|
push @{ $row->{'000'} }, $pos; |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
|
if ($validate) { |
283 |
|
my @errors = $validate->validate_errors( $row ); |
284 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
|
my $ds = WebPAC::Normalize::data_structure( |
289 |
row => $row, |
row => $row, |
290 |
rules => $rules, |
rules => $rules, |
291 |
lookup => $lookup->lookup_hash, |
lookup => $lookup ? $lookup->lookup_hash : undef, |
292 |
|
marc_encoding => 'utf-8', |
293 |
); |
); |
294 |
|
|
295 |
$db->save_ds( |
$db->save_ds( |
296 |
id => $mfn, |
id => $mfn, |
297 |
ds => $ds, |
ds => $ds, |
298 |
prefix => $input->{name}, |
prefix => $input->{name}, |
299 |
) if ($ds); |
) if ($ds && !$stats); |
300 |
|
|
301 |
|
$indexer->add( |
302 |
|
id => $input->{name} . "/" . $mfn, |
303 |
|
ds => $ds, |
304 |
|
type => $config->{$use_indexer}->{type}, |
305 |
|
) if ($indexer && $ds); |
306 |
|
|
307 |
|
if ($marc_fh) { |
308 |
|
my $marc = new MARC::Record; |
309 |
|
$marc->encoding( 'utf-8' ); |
310 |
|
$marc->add_fields( WebPAC::Normalize::_get_marc_fields() ); |
311 |
|
print $marc_fh $marc->as_usmarc; |
312 |
|
} |
313 |
|
|
314 |
|
$total_rows++; |
315 |
} |
} |
316 |
|
|
317 |
$indexer->add( |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
|
id => $input->{name} . "/" . $mfn, |
|
|
ds => $ds, |
|
|
type => $config->{$use_indexer}->{type}, |
|
|
); |
|
318 |
|
|
319 |
$total_rows++; |
# close MARC file |
320 |
} |
close($marc_fh) if ($marc_fh); |
321 |
|
|
322 |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
} |
323 |
|
|
324 |
}; |
} |
325 |
|
|
326 |
eval { $indexer->finish } if ($indexer->can('finish')); |
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
327 |
|
|
328 |
my $dt = time() - $start_t; |
my $dt = time() - $start_t; |
329 |
$log->info("$total_rows records indexed in " . |
$log->info("$total_rows records ", $indexer ? "indexed " : "", |
330 |
sprintf("%.2f sec [%.2f rec/sec]", |
sprintf("in %.2f sec [%.2f rec/sec]", |
331 |
$dt, ($total_rows / $dt) |
$dt, ($total_rows / $dt) |
332 |
) |
) |
333 |
); |
); |
335 |
# |
# |
336 |
# add Hyper Estraier links to other databases |
# add Hyper Estraier links to other databases |
337 |
# |
# |
338 |
if (ref($db_config->{links}) eq 'ARRAY') { |
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
339 |
foreach my $link (@{ $db_config->{links} }) { |
foreach my $link (@{ $db_config->{links} }) { |
340 |
if ($use_indexer eq 'hyperestraier') { |
if ($use_indexer eq 'hyperestraier') { |
341 |
$log->info("adding link $database -> $link->{to} [$link->{credit}]"); |
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
342 |
$indexer->add_link( |
push @links, { |
343 |
from => $database, |
from => $database, |
344 |
to => $link->{to}, |
to => $link->{to}, |
345 |
credit => $link->{credit}, |
credit => $link->{credit}, |
346 |
); |
}; |
347 |
} else { |
} else { |
348 |
$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}]"); |
349 |
} |
} |
352 |
|
|
353 |
} |
} |
354 |
|
|
355 |
|
foreach my $link (@links) { |
356 |
|
$log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]"); |
357 |
|
$indexer->add_link( %{ $link } ); |
358 |
|
} |