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