9 |
use XML::Simple; |
use XML::Simple; |
10 |
use Template; |
use Template; |
11 |
use Log::Log4perl qw(get_logger :levels); |
use Log::Log4perl qw(get_logger :levels); |
12 |
|
use Time::HiRes qw(time); |
13 |
|
|
14 |
use Data::Dumper; |
use Data::Dumper; |
15 |
|
|
30 |
|
|
31 |
=head2 new |
=head2 new |
32 |
|
|
33 |
This will create new instance of WebPAC using configuration specified by C<config_file>. |
Create new instance of WebPAC using configuration specified by C<config_file>. |
34 |
|
|
35 |
my $webpac = new WebPAC( |
my $webpac = new WebPAC( |
36 |
config_file => 'name.conf', |
config_file => 'name.conf', |
37 |
[code_page => 'ISO-8859-2',] |
code_page => 'ISO-8859-2', |
38 |
|
low_mem => 1, |
39 |
|
filter => { |
40 |
|
'lower' => sub { lc($_[0]) }, |
41 |
|
}, |
42 |
); |
); |
43 |
|
|
44 |
Default C<code_page> is C<ISO-8859-2>. |
Default C<code_page> is C<ISO-8859-2>. |
45 |
|
|
46 |
It will also read configuration files |
Default is not to use C<low_mem> options (see L<MEMORY USAGE> below). |
47 |
|
|
48 |
|
There is optinal parametar C<filter> which specify different filters which |
49 |
|
can be applied using C<filter{name}> notation. |
50 |
|
Same filters can be used in Template Toolkit files. |
51 |
|
|
52 |
|
This method will also read configuration files |
53 |
C<global.conf> (used by indexer and Web font-end) |
C<global.conf> (used by indexer and Web font-end) |
54 |
and configuration file specified by C<config_file> |
and configuration file specified by C<config_file> |
55 |
which describes databases to be indexed. |
which describes databases to be indexed. |
70 |
my $self = {@_}; |
my $self = {@_}; |
71 |
bless($self, $class); |
bless($self, $class); |
72 |
|
|
73 |
|
$self->{'start_t'} = time(); |
74 |
|
|
75 |
my $log_file = $self->{'log'} || "log.conf"; |
my $log_file = $self->{'log'} || "log.conf"; |
76 |
Log::Log4perl->init($log_file); |
Log::Log4perl->init($log_file); |
77 |
|
|
113 |
# create Template toolkit instance |
# create Template toolkit instance |
114 |
$self->{'tt'} = Template->new( |
$self->{'tt'} = Template->new( |
115 |
INCLUDE_PATH => ($self->{'global_config_file'}->{'output_template'} || './output_template'), |
INCLUDE_PATH => ($self->{'global_config_file'}->{'output_template'} || './output_template'), |
116 |
# FILTERS => { |
FILTERS => $self->{'filter'}, |
|
# 'foo' => \&foo_filter, |
|
|
# }, |
|
117 |
EVAL_PERL => 1, |
EVAL_PERL => 1, |
118 |
); |
); |
119 |
|
|
120 |
|
# running with low_mem flag? well, use DBM::Deep then. |
121 |
|
if ($self->{'low_mem'}) { |
122 |
|
$log->info("running with low_mem which impacts performance (<32 Mb memory usage)"); |
123 |
|
|
124 |
|
my $db_file = "data.db"; |
125 |
|
|
126 |
|
if (-e $db_file) { |
127 |
|
unlink $db_file or $log->logdie("can't remove '$db_file' from last run"); |
128 |
|
$log->debug("removed '$db_file' from last run"); |
129 |
|
} |
130 |
|
|
131 |
|
require DBM::Deep; |
132 |
|
|
133 |
|
my $db = new DBM::Deep $db_file; |
134 |
|
|
135 |
|
$log->logdie("DBM::Deep error: $!") unless ($db); |
136 |
|
|
137 |
|
if ($db->error()) { |
138 |
|
$log->logdie("can't open '$db_file' under low_mem: ",$db->error()); |
139 |
|
} else { |
140 |
|
$log->debug("using file '$db_file' for DBM::Deep"); |
141 |
|
} |
142 |
|
|
143 |
|
$self->{'db'} = $db; |
144 |
|
} |
145 |
|
|
146 |
|
$log->debug("filters defined: ",Dumper($self->{'filter'})); |
147 |
|
|
148 |
return $self; |
return $self; |
149 |
} |
} |
150 |
|
|
151 |
=head2 open_isis |
=head2 open_isis |
152 |
|
|
153 |
Open CDS/ISIS database using OpenIsis module and read all records to memory. |
Open CDS/ISIS, WinISIS or IsisMarc database using IsisDB or OpenIsis module |
154 |
|
and read all records to memory. |
155 |
|
|
156 |
$webpac->open_isis( |
$webpac->open_isis( |
157 |
filename => '/data/ISIS/ISIS', |
filename => '/data/ISIS/ISIS', |
158 |
code_page => '852', |
code_page => '852', |
159 |
limit_mfn => '500', |
limit_mfn => 500, |
160 |
|
start_mfn => 6000, |
161 |
lookup => [ ... ], |
lookup => [ ... ], |
162 |
); |
); |
163 |
|
|
164 |
By default, ISIS code page is assumed to be C<852>. |
By default, ISIS code page is assumed to be C<852>. |
165 |
|
|
166 |
|
If optional parametar C<start_mfn> is set, this will be first MFN to read |
167 |
|
from database (so you can skip beginning of your database if you need to). |
168 |
|
|
169 |
If optional parametar C<limit_mfn> is set, it will read just 500 records |
If optional parametar C<limit_mfn> is set, it will read just 500 records |
170 |
from database in example above. |
from database in example above. |
171 |
|
|
193 |
$log->logcroak("need filename") if (! $arg->{'filename'}); |
$log->logcroak("need filename") if (! $arg->{'filename'}); |
194 |
my $code_page = $arg->{'code_page'} || '852'; |
my $code_page = $arg->{'code_page'} || '852'; |
195 |
|
|
196 |
use OpenIsis; |
$log->logdie("can't find database ",$arg->{'filename'}) unless (glob($arg->{'filename'}.'.*')); |
197 |
|
|
198 |
|
# store data in object |
199 |
|
$self->{'isis_filename'} = $arg->{'filename'}; |
200 |
|
$self->{'isis_code_page'} = $code_page; |
201 |
|
|
202 |
#$self->{'isis_code_page'} = $code_page; |
#$self->{'isis_code_page'} = $code_page; |
203 |
|
|
205 |
my $cp = Text::Iconv->new($code_page,$self->{'code_page'}); |
my $cp = Text::Iconv->new($code_page,$self->{'code_page'}); |
206 |
|
|
207 |
$log->info("reading ISIS database '",$arg->{'filename'},"'"); |
$log->info("reading ISIS database '",$arg->{'filename'},"'"); |
208 |
|
$log->debug("isis code page: $code_page"); |
209 |
|
|
210 |
|
my $use_openisis = 1; |
211 |
|
|
212 |
|
eval { use IsisDB 0.06; }; |
213 |
|
$use_openisis = 0 unless ($@); |
214 |
|
|
215 |
|
my ($isis_db,$maxmfn); |
216 |
|
|
217 |
my $isis_db = OpenIsis::open($arg->{'filename'}); |
if ($use_openisis) { |
218 |
|
use OpenIsis; |
219 |
|
$isis_db = OpenIsis::open($arg->{'filename'}); |
220 |
|
$maxmfn = OpenIsis::maxRowid( $isis_db ) || 1; |
221 |
|
} else { |
222 |
|
$isis_db = new IsisDB( |
223 |
|
isisdb => $arg->{'filename'}, |
224 |
|
include_deleted => 1, |
225 |
|
hash_filter => sub { |
226 |
|
my $l = shift || return; |
227 |
|
$l = $cp->convert($l); |
228 |
|
return $l; |
229 |
|
}, |
230 |
|
); |
231 |
|
$maxmfn = $isis_db->{'maxmfn'}; |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
my $startmfn = 1; |
236 |
|
|
237 |
|
if (my $s = $self->{'start_mfn'}) { |
238 |
|
$log->info("skipping to MFN $s"); |
239 |
|
$startmfn = $s; |
240 |
|
} else { |
241 |
|
$self->{'start_mfn'} = $startmfn; |
242 |
|
} |
243 |
|
|
244 |
my $maxmfn = OpenIsis::maxRowid( $isis_db ) || 1; |
$maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn}); |
245 |
|
|
246 |
$maxmfn = $self->{limit_mfn} if ($self->{limit_mfn}); |
$log->info("processing ",($maxmfn-$startmfn)." records using ",( $use_openisis ? 'OpenIsis' : 'IsisDB')); |
247 |
|
|
|
$log->info("processing $maxmfn records..."); |
|
248 |
|
|
249 |
# read database |
# read database |
250 |
for (my $mfn = 1; $mfn <= $maxmfn; $mfn++) { |
for (my $mfn = $startmfn; $mfn <= $maxmfn; $mfn++) { |
251 |
|
|
252 |
|
|
253 |
|
$log->debug("mfn: $mfn\n"); |
254 |
|
|
255 |
|
my $rec; |
256 |
|
|
257 |
# read record |
if ($use_openisis) { |
258 |
my $row = OpenIsis::read( $isis_db, $mfn ); |
|
259 |
foreach my $k (keys %{$row}) { |
# read record using OpenIsis |
260 |
if ($k ne "mfn") { |
my $row = OpenIsis::read( $isis_db, $mfn ); |
261 |
foreach my $l (@{$row->{$k}}) { |
foreach my $k (keys %{$row}) { |
262 |
$l = $cp->convert($l); |
if ($k ne "mfn") { |
263 |
# has subfields? |
foreach my $l (@{$row->{$k}}) { |
264 |
my $val; |
$l = $cp->convert($l); |
265 |
if ($l =~ m/\^/) { |
# has subfields? |
266 |
foreach my $t (split(/\^/,$l)) { |
my $val; |
267 |
next if (! $t); |
if ($l =~ m/\^/) { |
268 |
$val->{substr($t,0,1)} = substr($t,1); |
foreach my $t (split(/\^/,$l)) { |
269 |
|
next if (! $t); |
270 |
|
$val->{substr($t,0,1)} = substr($t,1); |
271 |
|
} |
272 |
|
} else { |
273 |
|
$val = $l; |
274 |
} |
} |
|
} else { |
|
|
$val = $l; |
|
|
} |
|
275 |
|
|
276 |
push @{$self->{'data'}->{$mfn}->{$k}}, $val; |
push @{$rec->{$k}}, $val; |
277 |
|
} |
278 |
|
} else { |
279 |
|
push @{$rec->{'000'}}, $mfn; |
280 |
} |
} |
|
} else { |
|
|
push @{$self->{'data'}->{$mfn}->{'000'}}, $mfn; |
|
281 |
} |
} |
282 |
|
|
283 |
|
} else { |
284 |
|
$rec = $isis_db->to_hash($mfn); |
285 |
|
} |
286 |
|
|
287 |
|
$log->confess("record $mfn empty?") unless ($rec); |
288 |
|
|
289 |
|
# store |
290 |
|
if ($self->{'low_mem'}) { |
291 |
|
$self->{'db'}->put($mfn, $rec); |
292 |
|
} else { |
293 |
|
$self->{'data'}->{$mfn} = $rec; |
294 |
} |
} |
295 |
|
|
296 |
# create lookup |
# create lookup |
|
my $rec = $self->{'data'}->{$mfn}; |
|
297 |
$self->create_lookup($rec, @{$arg->{'lookup'}}); |
$self->create_lookup($rec, @{$arg->{'lookup'}}); |
298 |
|
|
299 |
|
$self->progress_bar($mfn,$maxmfn); |
300 |
|
|
301 |
} |
} |
302 |
|
|
303 |
$self->{'current_mfn'} = 1; |
$self->{'current_mfn'} = -1; |
304 |
|
$self->{'last_pcnt'} = 0; |
305 |
|
|
306 |
|
$log->debug("max mfn: $maxmfn"); |
307 |
|
|
308 |
# store max mfn and return it. |
# store max mfn and return it. |
309 |
return $self->{'max_mfn'} = $maxmfn; |
return $self->{'max_mfn'} = $maxmfn; |
323 |
|
|
324 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
325 |
|
|
326 |
my $mfn = $self->{'current_mfn'}++ || $log->logconfess("it seems that you didn't load database!"); |
$log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'}); |
327 |
|
|
328 |
|
if ($self->{'current_mfn'} == -1) { |
329 |
|
$self->{'current_mfn'} = $self->{'start_mfn'}; |
330 |
|
} else { |
331 |
|
$self->{'current_mfn'}++; |
332 |
|
} |
333 |
|
|
334 |
|
my $mfn = $self->{'current_mfn'}; |
335 |
|
|
336 |
if ($mfn > $self->{'max_mfn'}) { |
if ($mfn > $self->{'max_mfn'}) { |
337 |
$self->{'current_mfn'} = $self->{'max_mfn'}; |
$self->{'current_mfn'} = $self->{'max_mfn'}; |
339 |
return; |
return; |
340 |
} |
} |
341 |
|
|
342 |
return $self->{'data'}->{$mfn}; |
$self->progress_bar($mfn,$self->{'max_mfn'}); |
343 |
|
|
344 |
|
if ($self->{'low_mem'}) { |
345 |
|
return $self->{'db'}->get($mfn); |
346 |
|
} else { |
347 |
|
return $self->{'data'}->{$mfn}; |
348 |
|
} |
349 |
|
} |
350 |
|
|
351 |
|
=head2 mfn |
352 |
|
|
353 |
|
Returns current record number (MFN). |
354 |
|
|
355 |
|
print $webpac->mfn; |
356 |
|
|
357 |
|
=cut |
358 |
|
|
359 |
|
sub mfn { |
360 |
|
my $self = shift; |
361 |
|
return $self->{'current_mfn'}; |
362 |
|
} |
363 |
|
|
364 |
|
=head2 progress_bar |
365 |
|
|
366 |
|
Draw progress bar on STDERR. |
367 |
|
|
368 |
|
$webpac->progress_bar($current, $max); |
369 |
|
|
370 |
|
=cut |
371 |
|
|
372 |
|
sub progress_bar { |
373 |
|
my $self = shift; |
374 |
|
|
375 |
|
my ($curr,$max) = @_; |
376 |
|
|
377 |
|
my $log = $self->_get_logger(); |
378 |
|
|
379 |
|
$log->logconfess("no current value!") if (! $curr); |
380 |
|
$log->logconfess("no maximum value!") if (! $max); |
381 |
|
|
382 |
|
if ($curr > $max) { |
383 |
|
$max = $curr; |
384 |
|
$log->debug("overflow to $curr"); |
385 |
|
} |
386 |
|
|
387 |
|
$self->{'last_pcnt'} ||= 1; |
388 |
|
|
389 |
|
my $p = int($curr * 100 / $max) || 1; |
390 |
|
|
391 |
|
# reset on re-run |
392 |
|
if ($p < $self->{'last_pcnt'}) { |
393 |
|
$self->{'last_pcnt'} = $p; |
394 |
|
$self->{'start_t'} = time(); |
395 |
|
} |
396 |
|
|
397 |
|
if ($p != $self->{'last_pcnt'}) { |
398 |
|
|
399 |
|
my $t = time(); |
400 |
|
my $rate = ($curr / ($t - $self->{'start_t'} || 1)); |
401 |
|
my $eta = ($max-$curr) / ($rate || 1); |
402 |
|
printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$curr,"=" x ($p/3)."$p%>", $max, $rate, $self->fmt_time($eta)); |
403 |
|
$self->{'last_pcnt'} = $p; |
404 |
|
$self->{'last_curr'} = $curr; |
405 |
|
} |
406 |
|
print STDERR "\n" if ($p == 100); |
407 |
|
} |
408 |
|
|
409 |
|
=head2 fmt_time |
410 |
|
|
411 |
|
Format time (in seconds) for display. |
412 |
|
|
413 |
|
print $webpac->fmt_time(time()); |
414 |
|
|
415 |
|
This method is called by L<progress_bar> to display remaining time. |
416 |
|
|
417 |
|
=cut |
418 |
|
|
419 |
|
sub fmt_time { |
420 |
|
my $self = shift; |
421 |
|
|
422 |
|
my $t = shift || 0; |
423 |
|
my $out = ""; |
424 |
|
|
425 |
|
my ($ss,$mm,$hh) = gmtime($t); |
426 |
|
$out .= "${hh}h" if ($hh); |
427 |
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
428 |
|
$out .= " " if ($hh == 0); |
429 |
|
return $out; |
430 |
} |
} |
431 |
|
|
432 |
=head2 open_import_xml |
=head2 open_import_xml |
488 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
489 |
|
|
490 |
foreach my $i (@_) { |
foreach my $i (@_) { |
491 |
if ($i->{'eval'}) { |
$log->logconfess("need key") unless defined($i->{'key'}); |
492 |
my $eval = $self->fill_in($rec,$i->{'eval'}); |
$log->logconfess("need val") unless defined($i->{'val'}); |
493 |
my $key = $self->fill_in($rec,$i->{'key'}); |
|
494 |
my @val = $self->fill_in($rec,$i->{'val'}); |
if (defined($i->{'eval'})) { |
495 |
if ($key && @val && eval $eval) { |
# eval first, so we can skip fill_in for key and val |
496 |
|
my $eval = $self->fill_in($rec,$i->{'eval'}) || next; |
497 |
|
if ($self->_eval($eval)) { |
498 |
|
my $key = $self->fill_in($rec,$i->{'key'}) || next; |
499 |
|
my @val = $self->fill_in($rec,$i->{'val'}) || next; |
500 |
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
501 |
push @{$self->{'lookup'}->{$key}}, @val; |
push @{$self->{'lookup'}->{$key}}, @val; |
502 |
} |
} |
503 |
} else { |
} else { |
504 |
my $key = $self->fill_in($rec,$i->{'key'}); |
my $key = $self->fill_in($rec,$i->{'key'}) || next; |
505 |
my @val = $self->fill_in($rec,$i->{'val'}); |
my @val = $self->fill_in($rec,$i->{'val'}) || next; |
506 |
if ($key && @val) { |
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
507 |
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
push @{$self->{'lookup'}->{$key}}, @val; |
|
push @{$self->{'lookup'}->{$key}}, @val; |
|
|
} |
|
508 |
} |
} |
509 |
} |
} |
510 |
} |
} |
535 |
|
|
536 |
if ($$rec->{$f}) { |
if ($$rec->{$f}) { |
537 |
return '' if (! $$rec->{$f}->[$i]); |
return '' if (! $$rec->{$f}->[$i]); |
538 |
|
no strict 'refs'; |
539 |
if ($sf && $$rec->{$f}->[$i]->{$sf}) { |
if ($sf && $$rec->{$f}->[$i]->{$sf}) { |
540 |
$$found++ if (defined($$found)); |
$$found++ if (defined($$found)); |
541 |
return $$rec->{$f}->[$i]->{$sf}; |
return $$rec->{$f}->[$i]->{$sf}; |
576 |
This function B<does not> perform parsing of format to inteligenty skip |
This function B<does not> perform parsing of format to inteligenty skip |
577 |
delimiters before fields which aren't used. |
delimiters before fields which aren't used. |
578 |
|
|
579 |
|
This method will automatically decode UTF-8 string to local code page |
580 |
|
if needed. |
581 |
|
|
582 |
=cut |
=cut |
583 |
|
|
584 |
sub fill_in { |
sub fill_in { |
591 |
# iteration (for repeatable fields) |
# iteration (for repeatable fields) |
592 |
my $i = shift || 0; |
my $i = shift || 0; |
593 |
|
|
594 |
|
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
595 |
|
|
596 |
# FIXME remove for speedup? |
# FIXME remove for speedup? |
597 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
598 |
|
|
599 |
|
if (utf8::is_utf8($format)) { |
600 |
|
$format = $self->_x($format); |
601 |
|
} |
602 |
|
|
603 |
my $found = 0; |
my $found = 0; |
604 |
|
|
605 |
my $eval_code; |
my $eval_code; |
606 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
607 |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
608 |
|
|
609 |
|
my $filter_name; |
610 |
|
# remove filter{...} from beginning |
611 |
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
612 |
|
|
613 |
# do actual replacement of placeholders |
# do actual replacement of placeholders |
614 |
|
# repeatable fields |
615 |
$format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; |
$format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; |
616 |
|
# non-repeatable fields |
617 |
|
$format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; |
618 |
|
|
619 |
if ($found) { |
if ($found) { |
620 |
$log->debug("format: $format"); |
$log->debug("format: $format"); |
622 |
my $eval = $self->fill_in($rec,$eval_code,$i); |
my $eval = $self->fill_in($rec,$eval_code,$i); |
623 |
return if (! $self->_eval($eval)); |
return if (! $self->_eval($eval)); |
624 |
} |
} |
625 |
|
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
626 |
|
$log->debug("filter '$filter_name' for $format"); |
627 |
|
$format = $self->{'filter'}->{$filter_name}->($format); |
628 |
|
return unless(defined($format)); |
629 |
|
$log->debug("filter result: $format"); |
630 |
|
} |
631 |
# do we have lookups? |
# do we have lookups? |
632 |
if ($format =~ /$LOOKUP_REGEX/o) { |
if ($format =~ /$LOOKUP_REGEX/o) { |
633 |
$log->debug("format '$format' has lookup"); |
$log->debug("format '$format' has lookup"); |
720 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
721 |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
722 |
|
|
723 |
|
my $filter_name; |
724 |
|
# remove filter{...} from beginning |
725 |
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
726 |
|
|
727 |
my $prefix; |
my $prefix; |
728 |
my $all_found=0; |
my $all_found=0; |
729 |
|
|
730 |
while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))?//s) { |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
731 |
|
|
732 |
my $del = $1 || ''; |
my $del = $1 || ''; |
733 |
$prefix ||= $del if ($all_found == 0); |
$prefix ||= $del if ($all_found == 0); |
734 |
|
|
735 |
|
# repeatable index |
736 |
|
my $r = $i; |
737 |
|
$r = 0 if (lc("$2") eq 's'); |
738 |
|
|
739 |
my $found = 0; |
my $found = 0; |
740 |
my $tmp = $self->get_data(\$rec,$2,$3,$i,\$found); |
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
741 |
|
|
742 |
if ($found) { |
if ($found) { |
743 |
push @out, $del; |
push @out, $del; |
761 |
} |
} |
762 |
|
|
763 |
if ($eval_code) { |
if ($eval_code) { |
764 |
my $eval = $self->fill_in($rec,$eval_code,$i); |
my $eval = $self->fill_in($rec,$eval_code,$i) || return; |
765 |
$log->debug("about to eval{",$eval,"} format: $out"); |
$log->debug("about to eval{$eval} format: $out"); |
766 |
return if (! $self->_eval($eval)); |
return if (! $self->_eval($eval)); |
767 |
} |
} |
768 |
|
|
769 |
|
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
770 |
|
$log->debug("about to filter{$filter_name} format: $out"); |
771 |
|
$out = $self->{'filter'}->{$filter_name}->($out); |
772 |
|
return unless(defined($out)); |
773 |
|
$log->debug("filter result: $out"); |
774 |
|
} |
775 |
|
|
776 |
return $out; |
return $out; |
777 |
} |
} |
838 |
return @arr; |
return @arr; |
839 |
} |
} |
840 |
|
|
841 |
|
=head2 sort_arr |
842 |
|
|
843 |
|
Sort array ignoring case and html in data |
844 |
|
|
845 |
|
my @sorted = $webpac->sort_arr(@unsorted); |
846 |
|
|
847 |
|
=cut |
848 |
|
|
849 |
|
sub sort_arr { |
850 |
|
my $self = shift; |
851 |
|
|
852 |
|
my $log = $self->_get_logger(); |
853 |
|
|
854 |
|
# FIXME add Schwartzian Transformation? |
855 |
|
|
856 |
|
my @sorted = sort { |
857 |
|
$a =~ s#<[^>]+/*>##; |
858 |
|
$b =~ s#<[^>]+/*>##; |
859 |
|
lc($b) cmp lc($a) |
860 |
|
} @_; |
861 |
|
$log->debug("sorted values: ",sub { join(", ",@sorted) }); |
862 |
|
|
863 |
|
return @sorted; |
864 |
|
} |
865 |
|
|
866 |
|
|
867 |
=head2 data_structure |
=head2 data_structure |
868 |
|
|
872 |
my @ds = $webpac->data_structure($rec); |
my @ds = $webpac->data_structure($rec); |
873 |
|
|
874 |
This method will also set C<$webpac->{'currnet_filename'}> if there is |
This method will also set C<$webpac->{'currnet_filename'}> if there is |
875 |
<filename> tag in C<import_xml>. |
<filename> tag in C<import_xml> and C<$webpac->{'headline'}> if there is |
876 |
|
<headline> tag. |
877 |
|
|
878 |
=cut |
=cut |
879 |
|
|
886 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
887 |
|
|
888 |
undef $self->{'currnet_filename'}; |
undef $self->{'currnet_filename'}; |
889 |
|
undef $self->{'headline'}; |
890 |
|
|
891 |
my @sorted_tags; |
my @sorted_tags; |
892 |
if ($self->{tags_by_order}) { |
if ($self->{tags_by_order}) { |
919 |
} |
} |
920 |
next if (! @v); |
next if (! @v); |
921 |
|
|
922 |
|
if ($tag->{'sort'}) { |
923 |
|
@v = $self->sort_arr(@v); |
924 |
|
} |
925 |
|
|
926 |
# use format? |
# use format? |
927 |
if ($tag->{'format_name'}) { |
if ($tag->{'format_name'}) { |
928 |
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
929 |
} |
} |
930 |
|
|
|
# does tag have type? |
|
|
if ($tag->{'type'}) { |
|
|
push @{$row->{$tag->{'type'}}}, @v; |
|
|
} else { |
|
|
push @{$row->{'display'}}, @v; |
|
|
push @{$row->{'swish'}}, @v; |
|
|
} |
|
|
|
|
931 |
if ($field eq 'filename') { |
if ($field eq 'filename') { |
932 |
$self->{'current_filename'} = join('',@v); |
$self->{'current_filename'} = join('',@v); |
933 |
$log->debug("filename: ",$self->{'current_filename'}); |
$log->debug("filename: ",$self->{'current_filename'}); |
934 |
|
} elsif ($field eq 'headline') { |
935 |
|
$self->{'headline'} .= join('',@v); |
936 |
|
$log->debug("headline: ",$self->{'headline'}); |
937 |
|
next; # don't return headline in data_structure! |
938 |
|
} |
939 |
|
|
940 |
|
# delimiter will join repeatable fields |
941 |
|
if ($tag->{'delimiter'}) { |
942 |
|
@v = ( join($tag->{'delimiter'}, @v) ); |
943 |
} |
} |
944 |
|
|
945 |
|
# default types |
946 |
|
my @types = qw(display swish); |
947 |
|
# override by type attribute |
948 |
|
@types = ( $tag->{'type'} ) if ($tag->{'type'}); |
949 |
|
|
950 |
|
foreach my $type (@types) { |
951 |
|
# append to previous line? |
952 |
|
$log->debug("type: $type ",sub { join(" ",@v) }, $row->{'append'} || 'no append'); |
953 |
|
if ($tag->{'append'}) { |
954 |
|
|
955 |
|
# I will delimit appended part with |
956 |
|
# delimiter (or ,) |
957 |
|
my $d = $tag->{'delimiter'}; |
958 |
|
# default delimiter |
959 |
|
$d ||= " "; |
960 |
|
|
961 |
|
my $last = pop @{$row->{$type}}; |
962 |
|
$d = "" if (! $last); |
963 |
|
$last .= $d . join($d, @v); |
964 |
|
push @{$row->{$type}}, $last; |
965 |
|
|
966 |
|
} else { |
967 |
|
push @{$row->{$type}}, @v; |
968 |
|
} |
969 |
|
} |
970 |
|
|
971 |
|
|
972 |
} |
} |
973 |
|
|
974 |
if ($row) { |
if ($row) { |
978 |
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
979 |
$row->{'name'} = $name ? $self->_x($name) : $field; |
$row->{'name'} = $name ? $self->_x($name) : $field; |
980 |
|
|
981 |
|
# post-sort all values in field |
982 |
|
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
983 |
|
$log->warn("sort at field tag not implemented"); |
984 |
|
} |
985 |
|
|
986 |
push @ds, $row; |
push @ds, $row; |
987 |
|
|
988 |
$log->debug("row $field: ",sub { Dumper($row) }); |
$log->debug("row $field: ",sub { Dumper($row) }); |
1023 |
return $out; |
return $out; |
1024 |
} |
} |
1025 |
|
|
1026 |
|
=head2 output_file |
1027 |
|
|
1028 |
|
Create output from in-memory data structure using Template Toolkit template |
1029 |
|
to a file. |
1030 |
|
|
1031 |
|
$webpac->output_file( |
1032 |
|
file => 'out.txt', |
1033 |
|
template => 'text.tt', |
1034 |
|
data => @ds |
1035 |
|
); |
1036 |
|
|
1037 |
|
=cut |
1038 |
|
|
1039 |
|
sub output_file { |
1040 |
|
my $self = shift; |
1041 |
|
|
1042 |
|
my $args = {@_}; |
1043 |
|
|
1044 |
|
my $log = $self->_get_logger(); |
1045 |
|
|
1046 |
|
my $file = $args->{'file'} || $log->logconfess("need file name"); |
1047 |
|
|
1048 |
|
$log->debug("creating file ",$file); |
1049 |
|
|
1050 |
|
open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!"); |
1051 |
|
print $fh $self->output( |
1052 |
|
template => $args->{'template'}, |
1053 |
|
data => $args->{'data'}, |
1054 |
|
) || $log->logdie("print: $!"); |
1055 |
|
close($fh) || $log->logdie("close: $!"); |
1056 |
|
} |
1057 |
|
|
1058 |
=head2 apply_format |
=head2 apply_format |
1059 |
|
|
1060 |
Apply format specified in tag with C<format_name="name"> and |
Apply format specified in tag with C<format_name="name"> and |
1128 |
|
|
1129 |
$log->debug("eval: ",$code," [",$ret,"]"); |
$log->debug("eval: ",$code," [",$ret,"]"); |
1130 |
|
|
1131 |
return $ret || 0; |
return $ret || undef; |
1132 |
} |
} |
1133 |
|
|
1134 |
=head2 _sort_by_order |
=head2 _sort_by_order |
1198 |
also use method names, and not only classes (which are just few) |
also use method names, and not only classes (which are just few) |
1199 |
to filter logging. |
to filter logging. |
1200 |
|
|
1201 |
|
|
1202 |
|
=head1 MEMORY USAGE |
1203 |
|
|
1204 |
|
C<low_mem> options is double-edged sword. If enabled, WebPAC |
1205 |
|
will run on memory constraint machines (which doesn't have enough |
1206 |
|
physical RAM to create memory structure for whole source database). |
1207 |
|
|
1208 |
|
If your machine has 512Mb or more of RAM and database is around 10000 records, |
1209 |
|
memory shouldn't be an issue. If you don't have enough physical RAM, you |
1210 |
|
might consider using virtual memory (if your operating system is handling it |
1211 |
|
well, like on FreeBSD or Linux) instead of dropping to L<DBD::Deep> to handle |
1212 |
|
parsed structure of ISIS database (this is what C<low_mem> option does). |
1213 |
|
|
1214 |
|
Hitting swap at end of reading source database is probably o.k. However, |
1215 |
|
hitting swap before 90% will dramatically decrease performance and you will |
1216 |
|
be better off with C<low_mem> and using rest of availble memory for |
1217 |
|
operating system disk cache (Linux is particuallary good about this). |
1218 |
|
However, every access to database record will require disk access, so |
1219 |
|
generation phase will be slower 10-100 times. |
1220 |
|
|
1221 |
|
Parsed structures are essential - you just have option to trade RAM memory |
1222 |
|
(which is fast) for disk space (which is slow). Be sure to have planty of |
1223 |
|
disk space if you are using C<low_mem> and thus L<DBD::Deep>. |
1224 |
|
|
1225 |
|
However, when WebPAC is running on desktop machines (or laptops :-), it's |
1226 |
|
highly undesireable for system to start swapping. Using C<low_mem> option can |
1227 |
|
reduce WecPAC memory usage to around 64Mb for same database with lookup |
1228 |
|
fields and sorted indexes which stay in RAM. Performance will suffer, but |
1229 |
|
memory usage will really be minimal. It might be also more confortable to |
1230 |
|
run WebPAC reniced on those machines. |
1231 |
|
|
1232 |
=cut |
=cut |
1233 |
|
|
1234 |
1; |
1; |