4 |
|
|
5 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
6 |
use File::Temp qw/tempdir/; |
use File::Temp qw/tempdir/; |
|
use Data::Dumper; |
|
7 |
use lib './lib'; |
use lib './lib'; |
8 |
|
|
9 |
|
use WebPAC::Common 0.02; |
10 |
use WebPAC::Lookup; |
use WebPAC::Lookup; |
11 |
use WebPAC::Input::ISIS; |
use WebPAC::Input 0.03; |
12 |
use WebPAC::Store 0.03; |
use WebPAC::Store 0.03; |
13 |
use WebPAC::Normalize::XML; |
use WebPAC::Normalize; |
14 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
15 |
use WebPAC::Output::Estraier 0.02; |
use WebPAC::Validate; |
16 |
use YAML qw/LoadFile/; |
use YAML qw/LoadFile/; |
17 |
use LWP::Simple; |
use Getopt::Long; |
18 |
|
use File::Path; |
19 |
|
use Time::HiRes qw/time/; |
20 |
|
use File::Slurp; |
21 |
|
use MARC::Record 2.0; # need 2.0 for utf-8 encoding see marcpm.sf.net |
22 |
|
use MARC::Lint; |
23 |
|
use Data::Dump qw/dump/; |
24 |
|
|
25 |
my $limit = shift @ARGV; |
=head1 NAME |
26 |
|
|
27 |
my $config = LoadFile('conf/config.yml'); |
run.pl - start WebPAC indexing |
28 |
|
|
29 |
print "config = ",Dumper($config); |
B<this command will probably go away. Don't get used to it!> |
30 |
|
|
31 |
|
Options: |
32 |
|
|
33 |
|
=over 4 |
34 |
|
|
35 |
|
=item --offset 42 |
36 |
|
|
37 |
|
start loading (all) databases at offset 42 |
38 |
|
|
39 |
|
=item --limit 100 |
40 |
|
|
41 |
|
limit loading to 100 records |
42 |
|
|
43 |
|
=item --clean |
44 |
|
|
45 |
|
remove database and Hyper Estraier index before indexing |
46 |
|
|
47 |
|
=item --only=database_name/input_filter |
48 |
|
|
49 |
|
reindex just single database (legacy name is --one) |
50 |
|
|
51 |
|
C</input_filter> is optional part which can be C<name> |
52 |
|
or C<type> from input |
53 |
|
|
54 |
|
=item --config conf/config.yml |
55 |
|
|
56 |
|
path to YAML configuration file |
57 |
|
|
58 |
|
=item --stats |
59 |
|
|
60 |
|
disable indexing and dump statistics about field and subfield |
61 |
|
usage for each input |
62 |
|
|
63 |
|
=item --validate path/to/validation_file |
64 |
|
|
65 |
|
turn on extra validation of imput records, see L<WebPAC::Validation> |
66 |
|
|
67 |
|
=item --marc-normalize conf/normalize/mapping.pl |
68 |
|
|
69 |
|
This option specifies normalisation file for MARC creation |
70 |
|
|
71 |
|
=item --marc-output out/marc/test.marc |
72 |
|
|
73 |
|
Optional path to output file |
74 |
|
|
75 |
|
=item --marc-lint |
76 |
|
|
77 |
|
By default turned on if C<--marc-normalize> is used. You can disable lint |
78 |
|
messages with C<--no-arc-lint>. |
79 |
|
|
80 |
|
=back |
81 |
|
|
82 |
|
=cut |
83 |
|
|
84 |
|
my $offset; |
85 |
|
my $limit; |
86 |
|
|
87 |
|
my $clean = 0; |
88 |
|
my $config = 'conf/config.yml'; |
89 |
|
my $debug = 0; |
90 |
|
my $only_filter; |
91 |
|
my $stats = 0; |
92 |
|
my $validate_path; |
93 |
|
my ($marc_normalize, $marc_output); |
94 |
|
my $marc_lint = 1; |
95 |
|
|
96 |
|
GetOptions( |
97 |
|
"limit=i" => \$limit, |
98 |
|
"offset=i" => \$offset, |
99 |
|
"clean" => \$clean, |
100 |
|
"one=s" => \$only_filter, |
101 |
|
"only=s" => \$only_filter, |
102 |
|
"config" => \$config, |
103 |
|
"debug" => \$debug, |
104 |
|
"stats" => \$stats, |
105 |
|
"validate=s" => \$validate_path, |
106 |
|
"marc-normalize=s" => \$marc_normalize, |
107 |
|
"marc-output=s" => \$marc_output, |
108 |
|
"marc-lint!" => \$marc_lint, |
109 |
|
); |
110 |
|
|
111 |
|
$config = LoadFile($config); |
112 |
|
|
113 |
|
print "config = ",dump($config) if ($debug); |
114 |
|
|
115 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
116 |
|
|
117 |
|
my $log = _new WebPAC::Common()->_get_logger(); |
118 |
|
$log->info( "-" x 79 ); |
119 |
|
|
120 |
|
my $validate; |
121 |
|
$validate = new WebPAC::Validate( |
122 |
|
path => $validate_path, |
123 |
|
) if ($validate_path); |
124 |
|
|
125 |
|
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
126 |
|
if ($stats) { |
127 |
|
$log->debug("option --stats disables update of indexing engine..."); |
128 |
|
$use_indexer = undef; |
129 |
|
} else { |
130 |
|
$log->info("using $use_indexer indexing engine..."); |
131 |
|
} |
132 |
|
|
133 |
|
# disable indexing when creating marc |
134 |
|
$use_indexer = undef if ($marc_normalize); |
135 |
|
|
136 |
my $total_rows = 0; |
my $total_rows = 0; |
137 |
|
my $start_t = time(); |
138 |
|
|
139 |
|
my @links; |
140 |
|
my $indexer; |
141 |
|
|
142 |
|
my $lint = new MARC::Lint if ($marc_lint); |
143 |
|
|
144 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
145 |
|
|
146 |
|
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
147 |
|
next if ($only_database && $database !~ m/$only_database/i); |
148 |
|
|
149 |
|
if ($use_indexer) { |
150 |
|
my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration"); |
151 |
|
$indexer_config->{database} = $database; |
152 |
|
$indexer_config->{clean} = $clean; |
153 |
|
$indexer_config->{label} = $db_config->{name}; |
154 |
|
|
155 |
|
if ($use_indexer eq 'hyperestraier') { |
156 |
|
|
157 |
|
# open Hyper Estraier database |
158 |
|
use WebPAC::Output::Estraier '0.10'; |
159 |
|
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
160 |
|
|
161 |
|
} elsif ($use_indexer eq 'kinosearch') { |
162 |
|
|
163 |
|
# open KinoSearch |
164 |
|
use WebPAC::Output::KinoSearch; |
165 |
|
$indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path}); |
166 |
|
$indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } ); |
167 |
|
|
168 |
|
} else { |
169 |
|
$log->logdie("unknown use_indexer: $use_indexer"); |
170 |
|
} |
171 |
|
|
172 |
|
$log->logide("can't continue without valid indexer") unless ($indexer); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
# |
177 |
|
# now WebPAC::Store |
178 |
|
# |
179 |
my $abs_path = abs_path($0); |
my $abs_path = abs_path($0); |
180 |
$abs_path =~ s#/[^/]*$#/#; |
$abs_path =~ s#/[^/]*$#/#; |
181 |
|
|
182 |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
183 |
|
|
184 |
|
if ($clean) { |
185 |
|
$log->info("creating new database '$database' in $db_path"); |
186 |
|
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
187 |
|
} else { |
188 |
|
$log->info("working on database '$database' in $db_path"); |
189 |
|
} |
190 |
|
|
191 |
my $db = new WebPAC::Store( |
my $db = new WebPAC::Store( |
192 |
path => $db_path, |
path => $db_path, |
193 |
database => $database, |
database => $database, |
194 |
debug => 1, |
debug => $debug, |
195 |
); |
); |
196 |
|
|
|
my $log = $db->_get_logger; |
|
|
$log->info("working on $database in $db_path"); |
|
|
|
|
|
my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration"); |
|
|
$est_config->{database} = $database; |
|
|
|
|
|
$log->info("using HyperEstraier URL $est_config->{masterurl}"); |
|
|
|
|
|
my $est = new WebPAC::Output::Estraier( |
|
|
%{ $est_config }, |
|
|
); |
|
197 |
|
|
198 |
# |
# |
199 |
# now, iterate through input formats |
# now, iterate through input formats |
202 |
my @inputs; |
my @inputs; |
203 |
if (ref($db_config->{input}) eq 'ARRAY') { |
if (ref($db_config->{input}) eq 'ARRAY') { |
204 |
@inputs = @{ $db_config->{input} }; |
@inputs = @{ $db_config->{input} }; |
205 |
} else { |
} elsif ($db_config->{input}) { |
206 |
push @inputs, $db_config->{input}; |
push @inputs, $db_config->{input}; |
207 |
|
} else { |
208 |
|
$log->info("database $database doesn't have inputs defined"); |
209 |
} |
} |
210 |
|
|
211 |
|
my @supported_inputs = keys %{ $config->{webpac}->{inputs} }; |
212 |
|
|
213 |
foreach my $input (@inputs) { |
foreach my $input (@inputs) { |
214 |
|
|
215 |
|
next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i)); |
216 |
|
|
217 |
my $type = lc($input->{type}); |
my $type = lc($input->{type}); |
218 |
|
|
219 |
die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis'); |
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
220 |
|
|
221 |
my $lookup = new WebPAC::Lookup( |
my $lookup = new WebPAC::Lookup( |
222 |
lookup_file => $input->{lookup}, |
lookup_file => $input->{lookup}, |
223 |
); |
) if ($input->{lookup}); |
224 |
|
|
225 |
$log->info("working on input $input->{path} [$input->{type}]"); |
my $input_module = $config->{webpac}->{inputs}->{$type}; |
226 |
|
|
227 |
my $isis = new WebPAC::Input::ISIS( |
$log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module", |
228 |
|
$input->{lookup} ? "lookup '$input->{lookup}'" : "" |
229 |
|
); |
230 |
|
|
231 |
|
my $input_db = new WebPAC::Input( |
232 |
|
module => $input_module, |
233 |
code_page => $config->{webpac}->{webpac_encoding}, |
code_page => $config->{webpac}->{webpac_encoding}, |
234 |
limit_mfn => $input->{limit}, |
limit => $limit || $input->{limit}, |
235 |
|
offset => $offset, |
236 |
lookup => $lookup, |
lookup => $lookup, |
237 |
|
recode => $input->{recode}, |
238 |
|
stats => $stats, |
239 |
); |
); |
240 |
|
$log->logdie("can't create input using $input_module") unless ($input); |
241 |
|
|
242 |
my $maxmfn = $isis->open( |
my $maxmfn = $input_db->open( |
243 |
filename => $input->{path}, |
path => $input->{path}, |
244 |
code_page => $input->{encoding}, # database encoding |
code_page => $input->{encoding}, # database encoding |
245 |
|
%{ $input }, |
246 |
); |
); |
247 |
|
|
248 |
$log->info( Dumper($lookup->{_lookup_data}) ); |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
249 |
|
@{ $input->{normalize} } : ( $input->{normalize} ); |
250 |
|
|
251 |
my $n = new WebPAC::Normalize::XML( |
if ($marc_normalize) { |
252 |
# filter => { 'foo' => sub { shift } }, |
@norm_array = ( { |
253 |
db => $db, |
path => $marc_normalize, |
254 |
lookup_regex => $lookup->regex, |
output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc', |
255 |
lookup => $lookup, |
} ); |
256 |
prefix => $input->{name}, |
} |
|
); |
|
257 |
|
|
258 |
$n->open( |
foreach my $normalize (@norm_array) { |
259 |
tag => $input->{normalize}->{tag}, |
|
260 |
xml_file => $input->{normalize}->{path}, |
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
|
); |
|
261 |
|
|
262 |
for ( 0 ... $isis->size ) { |
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
263 |
|
|
264 |
my $row = $isis->fetch || next; |
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
265 |
|
|
266 |
my $mfn = $row->{'000'}->[0] || die "can't find MFN"; |
$log->info("Using $normalize_path for normalization..."); |
267 |
|
|
268 |
|
my $marc_fh; |
269 |
|
if (my $path = $normalize->{output}) { |
270 |
|
open($marc_fh, '>', $path) || |
271 |
|
$log->logdie("can't open MARC output $path: $!"); |
272 |
|
|
273 |
|
$log->info("Creating MARC export file $path", $marc_lint ? ' (with lint)' : '', "\n"); |
274 |
|
} |
275 |
|
|
276 |
|
# reset position in database |
277 |
|
$input_db->seek(1); |
278 |
|
|
279 |
|
foreach my $pos ( 0 ... $input_db->size ) { |
280 |
|
|
281 |
|
my $row = $input_db->fetch || next; |
282 |
|
|
283 |
|
my $mfn = $row->{'000'}->[0]; |
284 |
|
|
285 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
286 |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
287 |
|
$mfn = $pos; |
288 |
|
push @{ $row->{'000'} }, $pos; |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
|
if ($validate) { |
293 |
|
my @errors = $validate->validate_errors( $row ); |
294 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
my $ds = WebPAC::Normalize::data_structure( |
299 |
|
row => $row, |
300 |
|
rules => $rules, |
301 |
|
lookup => $lookup ? $lookup->lookup_hash : undef, |
302 |
|
marc_encoding => 'utf-8', |
303 |
|
); |
304 |
|
|
305 |
|
$db->save_ds( |
306 |
|
id => $mfn, |
307 |
|
ds => $ds, |
308 |
|
prefix => $input->{name}, |
309 |
|
) if ($ds && !$stats); |
310 |
|
|
311 |
|
$indexer->add( |
312 |
|
id => $input->{name} . "/" . $mfn, |
313 |
|
ds => $ds, |
314 |
|
type => $config->{$use_indexer}->{type}, |
315 |
|
) if ($indexer && $ds); |
316 |
|
|
317 |
|
if ($marc_fh) { |
318 |
|
my $marc = new MARC::Record; |
319 |
|
$marc->encoding( 'utf-8' ); |
320 |
|
my @marc_fields = WebPAC::Normalize::_get_marc_fields(); |
321 |
|
if (! @marc_fields) { |
322 |
|
$log->warn("MARC record $mfn is empty, skipping"); |
323 |
|
} else { |
324 |
|
$marc->add_fields( @marc_fields ); |
325 |
|
if ($marc_lint) { |
326 |
|
$lint->check_record( $marc ); |
327 |
|
my $err = join( "\n", $lint->warnings ); |
328 |
|
$log->error("MARC lint detected warning on MFN $mfn\n", |
329 |
|
"Original imput row: ",dump($row), "\n", |
330 |
|
"Normalized MARC row: ",dump(@marc_fields), "\n", |
331 |
|
"MARC lint warnings: ",$err,"\n" |
332 |
|
) if ($err); |
333 |
|
} |
334 |
|
print $marc_fh $marc->as_usmarc; |
335 |
|
} |
336 |
|
} |
337 |
|
|
338 |
my $ds = $n->data_structure($row); |
$total_rows++; |
339 |
|
} |
340 |
|
|
341 |
$est->add( |
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
342 |
id => $input->{name} . "#" . $mfn, |
|
343 |
ds => $ds, |
# close MARC file |
344 |
type => $config->{hyperestraier}->{type}, |
close($marc_fh) if ($marc_fh); |
|
); |
|
345 |
|
|
|
$total_rows++; |
|
346 |
} |
} |
347 |
|
|
348 |
}; |
} |
349 |
|
|
350 |
|
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
351 |
|
|
352 |
|
my $dt = time() - $start_t; |
353 |
|
$log->info("$total_rows records ", $indexer ? "indexed " : "", |
354 |
|
sprintf("in %.2f sec [%.2f rec/sec]", |
355 |
|
$dt, ($total_rows / $dt) |
356 |
|
) |
357 |
|
); |
358 |
|
|
359 |
|
# |
360 |
|
# add Hyper Estraier links to other databases |
361 |
|
# |
362 |
|
if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) { |
363 |
|
foreach my $link (@{ $db_config->{links} }) { |
364 |
|
if ($use_indexer eq 'hyperestraier') { |
365 |
|
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
366 |
|
push @links, { |
367 |
|
from => $database, |
368 |
|
to => $link->{to}, |
369 |
|
credit => $link->{credit}, |
370 |
|
}; |
371 |
|
} else { |
372 |
|
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
373 |
|
} |
374 |
|
} |
375 |
|
} |
376 |
|
|
|
$log->info("$total_rows records indexed"); |
|
377 |
} |
} |
378 |
|
|
379 |
|
foreach my $link (@links) { |
380 |
|
$log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]"); |
381 |
|
$indexer->add_link( %{ $link } ); |
382 |
|
} |