7 |
use Data::Dumper; |
use Data::Dumper; |
8 |
use lib './lib'; |
use lib './lib'; |
9 |
|
|
10 |
|
use WebPAC::Common 0.02; |
11 |
use WebPAC::Lookup; |
use WebPAC::Lookup; |
12 |
use WebPAC::Input::ISIS; |
use WebPAC::Input 0.03; |
13 |
use WebPAC::Store 0.03; |
use WebPAC::Store 0.03; |
14 |
use WebPAC::Normalize::XML; |
use WebPAC::Normalize; |
15 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
16 |
use WebPAC::Output::Estraier 0.02; |
use WebPAC::Validate; |
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 MARC::Record; |
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 |
|
Options: |
31 |
|
|
32 |
|
=over 4 |
33 |
|
|
34 |
|
=item --offset 42 |
35 |
|
|
36 |
|
start loading (all) databases at offset 42 |
37 |
|
|
38 |
|
=item --limit 100 |
39 |
|
|
40 |
|
limit loading to 100 records |
41 |
|
|
42 |
|
=item --clean |
43 |
|
|
44 |
|
remove database and Hyper Estraier index before indexing |
45 |
|
|
46 |
|
=item --only=database_name/input_filter |
47 |
|
|
48 |
|
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 |
54 |
|
|
55 |
|
path to YAML configuration file |
56 |
|
|
57 |
|
=item --stats |
58 |
|
|
59 |
|
disable indexing and dump statistics about field and subfield |
60 |
|
usage for each input |
61 |
|
|
62 |
|
=item --validate path/to/validation_file |
63 |
|
|
64 |
|
turn on extra validation of imput records, see L<WebPAC::Validation> |
65 |
|
|
66 |
|
=back |
67 |
|
|
68 |
|
=cut |
69 |
|
|
70 |
|
my $offset; |
71 |
|
my $limit; |
72 |
|
|
73 |
|
my $clean = 0; |
74 |
|
my $config = 'conf/config.yml'; |
75 |
|
my $debug = 0; |
76 |
|
my $only_filter; |
77 |
|
my $stats = 0; |
78 |
|
my $validate_path; |
79 |
|
|
80 |
|
GetOptions( |
81 |
|
"limit=i" => \$limit, |
82 |
|
"offset=i" => \$offset, |
83 |
|
"clean" => \$clean, |
84 |
|
"one=s" => \$only_filter, |
85 |
|
"only=s" => \$only_filter, |
86 |
|
"config" => \$config, |
87 |
|
"debug" => \$debug, |
88 |
|
"stats" => \$stats, |
89 |
|
"validate=s" => \$validate_path, |
90 |
|
); |
91 |
|
|
92 |
|
$config = LoadFile($config); |
93 |
|
|
94 |
|
print "config = ",Dumper($config) if ($debug); |
95 |
|
|
96 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
97 |
|
|
98 |
|
my $log = _new WebPAC::Common()->_get_logger(); |
99 |
|
$log->info( "-" x 79 ); |
100 |
|
|
101 |
|
my $validate; |
102 |
|
$validate = new WebPAC::Validate( |
103 |
|
path => $validate_path, |
104 |
|
) if ($validate_path); |
105 |
|
|
106 |
|
my $use_indexer = $config->{use_indexer} || 'hyperestraier'; |
107 |
|
if ($stats) { |
108 |
|
$log->debug("option --stats disables update of indexing engine..."); |
109 |
|
$use_indexer = undef; |
110 |
|
} else { |
111 |
|
$log->info("using $use_indexer indexing engine..."); |
112 |
|
} |
113 |
|
|
114 |
my $total_rows = 0; |
my $total_rows = 0; |
115 |
|
my $start_t = time(); |
116 |
|
|
117 |
|
my @links; |
118 |
|
my $indexer; |
119 |
|
|
120 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
121 |
|
|
122 |
my $type = lc($db_config->{input}->{type}); |
my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter); |
123 |
|
next if ($only_database && $database !~ m/$only_database/i); |
124 |
|
|
125 |
|
if ($use_indexer) { |
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}; |
130 |
|
|
131 |
|
if ($use_indexer eq 'hyperestraier') { |
132 |
|
|
133 |
|
# open Hyper Estraier database |
134 |
|
use WebPAC::Output::Estraier '0.10'; |
135 |
|
$indexer = new WebPAC::Output::Estraier( %{ $indexer_config } ); |
136 |
|
|
137 |
|
} elsif ($use_indexer eq 'kinosearch') { |
138 |
|
|
139 |
|
# open 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 } ); |
143 |
|
|
144 |
|
} else { |
145 |
|
$log->logdie("unknown use_indexer: $use_indexer"); |
146 |
|
} |
147 |
|
|
148 |
|
$log->logide("can't continue without valid indexer") unless ($indexer); |
149 |
|
} |
150 |
|
|
|
die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis'); |
|
151 |
|
|
152 |
|
# |
153 |
|
# now WebPAC::Store |
154 |
|
# |
155 |
my $abs_path = abs_path($0); |
my $abs_path = abs_path($0); |
156 |
$abs_path =~ s#/[^/]*$#/#; |
$abs_path =~ s#/[^/]*$#/#; |
157 |
|
|
|
my $lookup = new WebPAC::Lookup( |
|
|
lookup_file => $db_config->{input}->{lookup}, |
|
|
); |
|
|
|
|
158 |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
159 |
|
|
160 |
|
if ($clean) { |
161 |
my $log = $lookup->_get_logger; |
$log->info("creating new database '$database' in $db_path"); |
162 |
$log->info("working on $database in $db_path"); |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
163 |
|
} else { |
164 |
|
$log->info("working on database '$database' in $db_path"); |
165 |
|
} |
166 |
|
|
167 |
my $db = new WebPAC::Store( |
my $db = new WebPAC::Store( |
168 |
path => $db_path, |
path => $db_path, |
169 |
|
database => $database, |
170 |
|
debug => $debug, |
171 |
); |
); |
172 |
|
|
|
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 }, |
|
|
); |
|
173 |
|
|
174 |
# |
# |
175 |
# now, iterate through input formats |
# now, iterate through input formats |
178 |
my @inputs; |
my @inputs; |
179 |
if (ref($db_config->{input}) eq 'ARRAY') { |
if (ref($db_config->{input}) eq 'ARRAY') { |
180 |
@inputs = @{ $db_config->{input} }; |
@inputs = @{ $db_config->{input} }; |
181 |
} else { |
} elsif ($db_config->{input}) { |
182 |
push @inputs, $db_config->{input}; |
push @inputs, $db_config->{input}; |
183 |
|
} else { |
184 |
|
$log->info("database $database doesn't have inputs defined"); |
185 |
} |
} |
186 |
|
|
187 |
|
my @supported_inputs = keys %{ $config->{webpac}->{inputs} }; |
188 |
|
|
189 |
foreach my $input (@inputs) { |
foreach my $input (@inputs) { |
|
$log->info("working on input $input->{path} [$input->{type}]"); |
|
190 |
|
|
191 |
my $isis = new WebPAC::Input::ISIS( |
next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i)); |
|
code_page => $config->{webpac}->{webpac_encoding}, |
|
|
limit_mfn => $input->{limit}, |
|
|
); |
|
192 |
|
|
193 |
my $maxmfn = $isis->open( |
my $type = lc($input->{type}); |
194 |
filename => $input->{path}, |
|
195 |
code_page => $input->{encoding}, # database encoding |
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
196 |
|
|
197 |
|
my $lookup = new WebPAC::Lookup( |
198 |
|
lookup_file => $input->{lookup}, |
199 |
|
) if ($input->{lookup}); |
200 |
|
|
201 |
|
my $input_module = $config->{webpac}->{inputs}->{$type}; |
202 |
|
|
203 |
|
$log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module", |
204 |
|
$input->{lookup} ? "lookup '$input->{lookup}'" : "" |
205 |
); |
); |
206 |
|
|
207 |
my $n = new WebPAC::Normalize::XML( |
my $input_db = new WebPAC::Input( |
208 |
# filter => { 'foo' => sub { shift } }, |
module => $input_module, |
209 |
db => $db, |
code_page => $config->{webpac}->{webpac_encoding}, |
210 |
lookup_regex => $lookup->regex, |
limit => $limit || $input->{limit}, |
211 |
|
offset => $offset, |
212 |
lookup => $lookup, |
lookup => $lookup, |
213 |
|
recode => $input->{recode}, |
214 |
|
stats => $stats, |
215 |
); |
); |
216 |
|
$log->logdie("can't create input using $input_module") unless ($input); |
217 |
|
|
218 |
$n->open( |
my $maxmfn = $input_db->open( |
219 |
tag => $input->{normalize}->{tag}, |
path => $input->{path}, |
220 |
xml_file => $input->{normalize}->{path}, |
code_page => $input->{encoding}, # database encoding |
221 |
|
%{ $input }, |
222 |
); |
); |
223 |
|
|
224 |
for ( 0 ... $isis->size ) { |
my @norm_array = ref($input->{normalize}) eq 'ARRAY' ? |
225 |
|
@{ $input->{normalize} } : ( $input->{normalize} ); |
226 |
|
|
227 |
|
foreach my $normalize (@norm_array) { |
228 |
|
|
229 |
|
my $rules; |
230 |
|
my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config"); |
231 |
|
|
232 |
|
$log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i ); |
233 |
|
|
234 |
|
my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!"; |
235 |
|
|
236 |
|
$log->info("Using $normalize_path for normalization..."); |
237 |
|
|
238 |
|
my $marc_fh; |
239 |
|
if (my $path = $normalize->{marc21}) { |
240 |
|
open($marc_fh, '>', $path) || |
241 |
|
$log->logdie("can't open MARC output $path: $!"); |
242 |
|
|
243 |
|
$log->info("Creating MARC export file $path\n"); |
244 |
|
} |
245 |
|
|
246 |
my $row = $isis->fetch || next; |
# reset position in database |
247 |
|
$input_db->seek(1); |
248 |
|
|
249 |
my $mfn = $row->{'000'}->[0] || die "can't find MFN"; |
foreach my $pos ( 0 ... $input_db->size ) { |
250 |
|
|
251 |
my $ds = $n->data_structure($row); |
my $row = $input_db->fetch || next; |
252 |
|
|
253 |
$est->add( |
my $mfn = $row->{'000'}->[0]; |
254 |
id => $input->{name} . "#" . $mfn, |
|
255 |
ds => $ds, |
if (! $mfn || $mfn !~ m#^\d+$#) { |
256 |
type => $config->{hyperestraier}->{type}, |
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
257 |
); |
$mfn = $pos; |
258 |
|
push @{ $row->{'000'} }, $pos; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
|
if ($validate) { |
263 |
|
my @errors = $validate->validate_errors( $row ); |
264 |
|
$log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
my $ds = WebPAC::Normalize::data_structure( |
269 |
|
row => $row, |
270 |
|
rules => $rules, |
271 |
|
lookup => $lookup ? $lookup->lookup_hash : undef, |
272 |
|
); |
273 |
|
|
274 |
|
$db->save_ds( |
275 |
|
id => $mfn, |
276 |
|
ds => $ds, |
277 |
|
prefix => $input->{name}, |
278 |
|
) if ($ds && !$stats); |
279 |
|
|
280 |
|
$indexer->add( |
281 |
|
id => $input->{name} . "/" . $mfn, |
282 |
|
ds => $ds, |
283 |
|
type => $config->{$use_indexer}->{type}, |
284 |
|
) if ($indexer && $ds); |
285 |
|
|
286 |
|
if ($marc_fh) { |
287 |
|
my $marc = new MARC::Record; |
288 |
|
$marc->add_fields( WebPAC::Normalize::_get_marc21_fields() ); |
289 |
|
print $marc_fh $marc->as_usmarc; |
290 |
|
} |
291 |
|
|
292 |
|
$total_rows++; |
293 |
|
} |
294 |
|
|
295 |
|
$log->info("statistics of fields usage:\n", $input_db->stats) if ($stats); |
296 |
|
|
297 |
|
# close MARC file |
298 |
|
close($marc_fh) if ($marc_fh); |
299 |
|
|
|
$total_rows++; |
|
300 |
} |
} |
301 |
|
|
302 |
}; |
} |
303 |
|
|
304 |
|
eval { $indexer->finish } if ($indexer && $indexer->can('finish')); |
305 |
|
|
306 |
|
my $dt = time() - $start_t; |
307 |
|
$log->info("$total_rows records ", $indexer ? "indexed " : "", |
308 |
|
sprintf("in %.2f sec [%.2f rec/sec]", |
309 |
|
$dt, ($total_rows / $dt) |
310 |
|
) |
311 |
|
); |
312 |
|
|
313 |
|
# |
314 |
|
# add Hyper Estraier links to other databases |
315 |
|
# |
316 |
|
if (ref($db_config->{links}) eq 'ARRAY') { |
317 |
|
foreach my $link (@{ $db_config->{links} }) { |
318 |
|
if ($use_indexer eq 'hyperestraier') { |
319 |
|
$log->info("saving link $database -> $link->{to} [$link->{credit}]"); |
320 |
|
push @links, { |
321 |
|
from => $database, |
322 |
|
to => $link->{to}, |
323 |
|
credit => $link->{credit}, |
324 |
|
}; |
325 |
|
} else { |
326 |
|
$log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]"); |
327 |
|
} |
328 |
|
} |
329 |
|
} |
330 |
|
|
|
$log->info("$total_rows records indexed"); |
|
331 |
} |
} |
332 |
|
|
333 |
|
foreach my $link (@links) { |
334 |
|
$log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]"); |
335 |
|
$indexer->add_link( %{ $link } ); |
336 |
|
} |