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::XML; |
15 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
16 |
use WebPAC::Output::Estraier 0.02; |
use WebPAC::Output::Estraier '0.10'; |
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 |
|
|
22 |
my $limit = shift @ARGV; |
=head1 NAME |
23 |
|
|
24 |
my $config = LoadFile('conf/config.yml'); |
run.pl - start WebPAC indexing |
25 |
|
|
26 |
print "config = ",Dumper($config); |
B<this command will probably go away. Don't get used to it!> |
27 |
|
|
28 |
|
Options: |
29 |
|
|
30 |
|
=over 4 |
31 |
|
|
32 |
|
=item --offset 42 |
33 |
|
|
34 |
|
start loading (all) databases at offset 42 |
35 |
|
|
36 |
|
=item --limit 100 |
37 |
|
|
38 |
|
limit loading to 100 records |
39 |
|
|
40 |
|
=item --clean |
41 |
|
|
42 |
|
remove database and Hyper Estraier index before indexing |
43 |
|
|
44 |
|
=item --only=database_name |
45 |
|
|
46 |
|
reindex just single database (legacy name is --one) |
47 |
|
|
48 |
|
=item --config conf/config.yml |
49 |
|
|
50 |
|
path to YAML configuration file |
51 |
|
|
52 |
|
=back |
53 |
|
|
54 |
|
=cut |
55 |
|
|
56 |
|
my $offset; |
57 |
|
my $limit; |
58 |
|
|
59 |
|
my $clean = 0; |
60 |
|
my $config = 'conf/config.yml'; |
61 |
|
my $debug = 0; |
62 |
|
my $only_db_name; |
63 |
|
|
64 |
|
GetOptions( |
65 |
|
"limit=i" => \$limit, |
66 |
|
"offset=i" => \$offset, |
67 |
|
"clean" => \$clean, |
68 |
|
"one=s" => \$only_db_name, |
69 |
|
"only=s" => \$only_db_name, |
70 |
|
"config" => \$config, |
71 |
|
"debug" => \$debug, |
72 |
|
); |
73 |
|
|
74 |
|
$config = LoadFile($config); |
75 |
|
|
76 |
|
print "config = ",Dumper($config) if ($debug); |
77 |
|
|
78 |
die "no databases in config file!\n" unless ($config->{databases}); |
die "no databases in config file!\n" unless ($config->{databases}); |
79 |
|
|
80 |
my $total_rows = 0; |
my $total_rows = 0; |
81 |
|
my $start_t = time(); |
82 |
|
|
83 |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
while (my ($database, $db_config) = each %{ $config->{databases} }) { |
84 |
|
|
85 |
my $type = lc($db_config->{input}->{type}); |
next if ($only_db_name && $database !~ m/$only_db_name/i); |
86 |
|
|
87 |
|
my $log = _new WebPAC::Common()->_get_logger(); |
88 |
|
|
89 |
die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis'); |
# |
90 |
|
# open Hyper Estraier database |
91 |
|
# |
92 |
|
|
93 |
|
my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration"); |
94 |
|
$est_config->{database} = $database; |
95 |
|
$est_config->{clean} = $clean; |
96 |
|
$est_config->{label} = $db_config->{name}; |
97 |
|
|
98 |
|
my $est = new WebPAC::Output::Estraier( %{ $est_config } ); |
99 |
|
|
100 |
|
# |
101 |
|
# now WebPAC::Store |
102 |
|
# |
103 |
my $abs_path = abs_path($0); |
my $abs_path = abs_path($0); |
104 |
$abs_path =~ s#/[^/]*$#/#; |
$abs_path =~ s#/[^/]*$#/#; |
105 |
|
|
|
my $lookup = new WebPAC::Lookup( |
|
|
lookup_file => $db_config->{input}->{lookup}, |
|
|
); |
|
|
|
|
106 |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
my $db_path = $config->{webpac}->{db_path} . '/' . $database; |
107 |
|
|
108 |
|
if ($clean) { |
109 |
my $log = $lookup->_get_logger; |
$log->info("creating new database $database in $db_path"); |
110 |
$log->info("working on $database in $db_path"); |
rmtree( $db_path ) || $log->warn("can't remove $db_path: $!"); |
111 |
|
} else { |
112 |
|
$log->debug("working on $database in $db_path"); |
113 |
|
} |
114 |
|
|
115 |
my $db = new WebPAC::Store( |
my $db = new WebPAC::Store( |
116 |
path => $db_path, |
path => $db_path, |
117 |
|
database => $database, |
118 |
|
debug => $debug, |
119 |
); |
); |
120 |
|
|
|
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 }, |
|
|
); |
|
121 |
|
|
122 |
# |
# |
123 |
# now, iterate through input formats |
# now, iterate through input formats |
126 |
my @inputs; |
my @inputs; |
127 |
if (ref($db_config->{input}) eq 'ARRAY') { |
if (ref($db_config->{input}) eq 'ARRAY') { |
128 |
@inputs = @{ $db_config->{input} }; |
@inputs = @{ $db_config->{input} }; |
129 |
} else { |
} elsif ($db_config->{input}) { |
130 |
push @inputs, $db_config->{input}; |
push @inputs, $db_config->{input}; |
131 |
|
} else { |
132 |
|
$log->info("database $database doesn't have inputs defined"); |
133 |
} |
} |
134 |
|
|
135 |
|
my @supported_inputs = keys %{ $config->{webpac}->{inputs} }; |
136 |
|
|
137 |
foreach my $input (@inputs) { |
foreach my $input (@inputs) { |
|
$log->info("working on input $input->{path} [$input->{type}]"); |
|
138 |
|
|
139 |
my $isis = new WebPAC::Input::ISIS( |
my $type = lc($input->{type}); |
140 |
|
|
141 |
|
die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs)); |
142 |
|
|
143 |
|
my $lookup = new WebPAC::Lookup( |
144 |
|
lookup_file => $input->{lookup}, |
145 |
|
); |
146 |
|
|
147 |
|
my $input_module = $config->{webpac}->{inputs}->{$type}; |
148 |
|
|
149 |
|
$log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'"); |
150 |
|
|
151 |
|
my $input_db = new WebPAC::Input( |
152 |
|
module => $input_module, |
153 |
code_page => $config->{webpac}->{webpac_encoding}, |
code_page => $config->{webpac}->{webpac_encoding}, |
154 |
limit_mfn => $input->{limit}, |
limit => $limit || $input->{limit}, |
155 |
|
offset => $offset, |
156 |
|
lookup => $lookup, |
157 |
|
recode => $input->{recode}, |
158 |
); |
); |
159 |
|
$log->logdie("can't create input using $input_module") unless ($input); |
160 |
|
|
161 |
my $maxmfn = $isis->open( |
my $maxmfn = $input_db->open( |
162 |
filename => $input->{path}, |
path => $input->{path}, |
163 |
code_page => $input->{encoding}, # database encoding |
code_page => $input->{encoding}, # database encoding |
164 |
); |
); |
165 |
|
|
168 |
db => $db, |
db => $db, |
169 |
lookup_regex => $lookup->regex, |
lookup_regex => $lookup->regex, |
170 |
lookup => $lookup, |
lookup => $lookup, |
171 |
|
prefix => $input->{name}, |
172 |
); |
); |
173 |
|
|
174 |
$n->open( |
my $normalize_path = $input->{normalize}->{path}; |
175 |
tag => $input->{normalize}->{tag}, |
|
176 |
xml_file => $input->{normalize}->{path}, |
if ($normalize_path =~ m/\.xml$/i) { |
177 |
); |
$n->open( |
178 |
|
tag => $input->{normalize}->{tag}, |
179 |
|
xml_file => $input->{normalize}->{path}, |
180 |
|
); |
181 |
|
} elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) { |
182 |
|
$n->open_yaml( |
183 |
|
path => $normalize_path, |
184 |
|
tag => $input->{normalize}->{tag}, |
185 |
|
); |
186 |
|
} |
187 |
|
|
188 |
for ( 0 ... $isis->size ) { |
foreach my $pos ( 0 ... $input_db->size ) { |
189 |
|
|
190 |
my $row = $isis->fetch || next; |
my $row = $input_db->fetch || next; |
191 |
|
|
192 |
my $mfn = $row->{'000'}->[0] || die "can't find MFN"; |
my $mfn = $row->{'000'}->[0]; |
193 |
|
|
194 |
|
if (! $mfn || $mfn !~ m#^\d+$#) { |
195 |
|
$log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos"); |
196 |
|
$mfn = $pos; |
197 |
|
push @{ $row->{'000'} }, $pos; |
198 |
|
} |
199 |
|
|
200 |
my $ds = $n->data_structure($row); |
my $ds = $n->data_structure($row); |
201 |
|
|
202 |
$est->add( |
$est->add( |
203 |
id => $input->{name} . "#" . $mfn, |
id => $input->{name} . "/" . $mfn, |
204 |
ds => $ds, |
ds => $ds, |
205 |
type => $config->{hyperestraier}->{type}, |
type => $config->{hyperestraier}->{type}, |
206 |
); |
); |
210 |
|
|
211 |
}; |
}; |
212 |
|
|
213 |
$log->info("$total_rows records indexed"); |
my $dt = time() - $start_t; |
214 |
|
$log->info("$total_rows records indexed in " . |
215 |
|
sprintf("%.2f sec [%.2f rec/sec]", |
216 |
|
$dt, ($total_rows / $dt) |
217 |
|
) |
218 |
|
); |
219 |
|
|
220 |
|
# |
221 |
|
# add Hyper Estraier links to other databases |
222 |
|
# |
223 |
|
if (ref($db_config->{links}) eq 'ARRAY') { |
224 |
|
foreach my $link (@{ $db_config->{links} }) { |
225 |
|
$log->info("adding link $database -> $link->{to} [$link->{credit}]"); |
226 |
|
$est->add_link( |
227 |
|
from => $database, |
228 |
|
to => $link->{to}, |
229 |
|
credit => $link->{credit}, |
230 |
|
); |
231 |
|
} |
232 |
|
} |
233 |
|
|
234 |
} |
} |
235 |
|
|