36 |
config_file => 'name.conf', |
config_file => 'name.conf', |
37 |
code_page => 'ISO-8859-2', |
code_page => 'ISO-8859-2', |
38 |
low_mem => 1, |
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 |
Default is not to use C<low_mem> options (see L<MEMORY USAGE> below). |
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 |
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> |
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 |
|
|
143 |
$self->{'db'} = $db; |
$self->{'db'} = $db; |
144 |
} |
} |
145 |
|
|
146 |
|
$log->debug("filters defined: ",Dumper($self->{'filter'})); |
147 |
|
|
148 |
return $self; |
return $self; |
149 |
} |
} |
150 |
|
|
216 |
if (my $s = $self->{'start_mfn'}) { |
if (my $s = $self->{'start_mfn'}) { |
217 |
$log->info("skipping to MFN $s"); |
$log->info("skipping to MFN $s"); |
218 |
$startmfn = $s; |
$startmfn = $s; |
219 |
|
} else { |
220 |
|
$self->{'start_mfn'} = $startmfn; |
221 |
} |
} |
222 |
|
|
223 |
$maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn}); |
$maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn}); |
273 |
|
|
274 |
} |
} |
275 |
|
|
276 |
$self->{'current_mfn'} = $startmfn; |
$self->{'current_mfn'} = -1; |
277 |
$self->{'last_pcnt'} = 0; |
$self->{'last_pcnt'} = 0; |
278 |
|
|
279 |
$log->debug("max mfn: $maxmfn"); |
$log->debug("max mfn: $maxmfn"); |
296 |
|
|
297 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
298 |
|
|
299 |
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'}); |
300 |
|
|
301 |
|
if ($self->{'current_mfn'} == -1) { |
302 |
|
$self->{'current_mfn'} = $self->{'start_mfn'}; |
303 |
|
} else { |
304 |
|
$self->{'current_mfn'}++; |
305 |
|
} |
306 |
|
|
307 |
|
my $mfn = $self->{'current_mfn'}; |
308 |
|
|
309 |
if ($mfn > $self->{'max_mfn'}) { |
if ($mfn > $self->{'max_mfn'}) { |
310 |
$self->{'current_mfn'} = $self->{'max_mfn'}; |
$self->{'current_mfn'} = $self->{'max_mfn'}; |
321 |
} |
} |
322 |
} |
} |
323 |
|
|
324 |
|
=head2 mfn |
325 |
|
|
326 |
|
Returns current record number (MFN). |
327 |
|
|
328 |
|
print $webpac->mfn; |
329 |
|
|
330 |
|
=cut |
331 |
|
|
332 |
|
sub mfn { |
333 |
|
my $self = shift; |
334 |
|
return $self->{'current_mfn'}; |
335 |
|
} |
336 |
|
|
337 |
=head2 progress_bar |
=head2 progress_bar |
338 |
|
|
339 |
Draw progress bar on STDERR. |
Draw progress bar on STDERR. |
359 |
|
|
360 |
$self->{'last_pcnt'} ||= 1; |
$self->{'last_pcnt'} ||= 1; |
361 |
|
|
362 |
my $p = int($curr * 100 / $max); |
my $p = int($curr * 100 / $max) || 1; |
363 |
|
|
364 |
# reset on re-run |
# reset on re-run |
365 |
if ($p < $self->{'last_pcnt'}) { |
if ($p < $self->{'last_pcnt'}) { |
366 |
$self->{'last_pcnt'} = $p; |
$self->{'last_pcnt'} = $p; |
367 |
$self->{'last_t'} = time(); |
$self->{'last_t'} = time(); |
368 |
$self->{'last_curr'} = 1; |
$self->{'last_curr'} = undef; |
369 |
} |
} |
370 |
|
|
371 |
|
$self->{'last_t'} ||= time(); |
372 |
|
|
373 |
if ($p != $self->{'last_pcnt'}) { |
if ($p != $self->{'last_pcnt'}) { |
374 |
|
|
375 |
my $last_curr = $self->{'last_curr'} || $curr; |
my $last_curr = $self->{'last_curr'} || $curr; |
569 |
# iteration (for repeatable fields) |
# iteration (for repeatable fields) |
570 |
my $i = shift || 0; |
my $i = shift || 0; |
571 |
|
|
572 |
|
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
573 |
|
|
574 |
# FIXME remove for speedup? |
# FIXME remove for speedup? |
575 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
576 |
|
|
584 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
585 |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
586 |
|
|
587 |
|
my $filter_name; |
588 |
|
# remove filter{...} from beginning |
589 |
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
590 |
|
|
591 |
# do actual replacement of placeholders |
# do actual replacement of placeholders |
592 |
|
# repeatable fields |
593 |
$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; |
594 |
|
# non-repeatable fields |
595 |
|
$format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; |
596 |
|
|
597 |
if ($found) { |
if ($found) { |
598 |
$log->debug("format: $format"); |
$log->debug("format: $format"); |
600 |
my $eval = $self->fill_in($rec,$eval_code,$i); |
my $eval = $self->fill_in($rec,$eval_code,$i); |
601 |
return if (! $self->_eval($eval)); |
return if (! $self->_eval($eval)); |
602 |
} |
} |
603 |
|
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
604 |
|
$log->debug("filter '$filter_name' for $format"); |
605 |
|
$format = $self->{'filter'}->{$filter_name}->($format); |
606 |
|
return unless(defined($format)); |
607 |
|
$log->debug("filter result: $format"); |
608 |
|
} |
609 |
# do we have lookups? |
# do we have lookups? |
610 |
if ($format =~ /$LOOKUP_REGEX/o) { |
if ($format =~ /$LOOKUP_REGEX/o) { |
611 |
$log->debug("format '$format' has lookup"); |
$log->debug("format '$format' has lookup"); |
698 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
699 |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
700 |
|
|
701 |
|
my $filter_name; |
702 |
|
# remove filter{...} from beginning |
703 |
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
704 |
|
|
705 |
my $prefix; |
my $prefix; |
706 |
my $all_found=0; |
my $all_found=0; |
707 |
|
|
708 |
while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))?//s) { |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
709 |
|
|
710 |
my $del = $1 || ''; |
my $del = $1 || ''; |
711 |
$prefix ||= $del if ($all_found == 0); |
$prefix ||= $del if ($all_found == 0); |
712 |
|
|
713 |
|
# repeatable index |
714 |
|
my $r = $i; |
715 |
|
$r = 0 if (lc("$2") eq 's'); |
716 |
|
|
717 |
my $found = 0; |
my $found = 0; |
718 |
my $tmp = $self->get_data(\$rec,$2,$3,$i,\$found); |
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
719 |
|
|
720 |
if ($found) { |
if ($found) { |
721 |
push @out, $del; |
push @out, $del; |
739 |
} |
} |
740 |
|
|
741 |
if ($eval_code) { |
if ($eval_code) { |
742 |
my $eval = $self->fill_in($rec,$eval_code,$i); |
my $eval = $self->fill_in($rec,$eval_code,$i) || return; |
743 |
$log->debug("about to eval{",$eval,"} format: $out"); |
$log->debug("about to eval{$eval} format: $out"); |
744 |
return if (! $self->_eval($eval)); |
return if (! $self->_eval($eval)); |
745 |
} |
} |
746 |
|
|
747 |
|
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
748 |
|
$log->debug("about to filter{$filter_name} format: $out"); |
749 |
|
$out = $self->{'filter'}->{$filter_name}->($out); |
750 |
|
return unless(defined($out)); |
751 |
|
$log->debug("filter result: $out"); |
752 |
|
} |
753 |
|
|
754 |
return $out; |
return $out; |
755 |
} |
} |
816 |
return @arr; |
return @arr; |
817 |
} |
} |
818 |
|
|
819 |
|
=head2 sort_arr |
820 |
|
|
821 |
|
Sort array ignoring case and html in data |
822 |
|
|
823 |
|
my @sorted = $webpac->sort_arr(@unsorted); |
824 |
|
|
825 |
|
=cut |
826 |
|
|
827 |
|
sub sort_arr { |
828 |
|
my $self = shift; |
829 |
|
|
830 |
|
my $log = $self->_get_logger(); |
831 |
|
|
832 |
|
# FIXME add Schwartzian Transformation? |
833 |
|
|
834 |
|
my @sorted = sort { |
835 |
|
$a =~ s#<[^>]+/*>##; |
836 |
|
$b =~ s#<[^>]+/*>##; |
837 |
|
lc($b) cmp lc($a) |
838 |
|
} @_; |
839 |
|
$log->debug("sorted values: ",sub { join(", ",@sorted) }); |
840 |
|
|
841 |
|
return @sorted; |
842 |
|
} |
843 |
|
|
844 |
|
|
845 |
=head2 data_structure |
=head2 data_structure |
846 |
|
|
898 |
next if (! @v); |
next if (! @v); |
899 |
|
|
900 |
if ($tag->{'sort'}) { |
if ($tag->{'sort'}) { |
901 |
# very special sort, ignoring case and |
@v = $self->sort_arr(@v); |
|
# html |
|
|
@v = sort { |
|
|
$a =~ s#<[^>]+/*>##; |
|
|
$b =~ s#<[^>]+/*>##; |
|
|
lc($b) cmp lc($a) |
|
|
} @v; |
|
|
$log->warn("sort within tag is usually not what you want!"); |
|
|
$log->debug("sorted values: ",sub { join(", ",@v) }); |
|
902 |
} |
} |
903 |
|
|
904 |
# use format? |
# use format? |
934 |
# delimiter (or ,) |
# delimiter (or ,) |
935 |
my $d = $tag->{'delimiter'}; |
my $d = $tag->{'delimiter'}; |
936 |
# default delimiter |
# default delimiter |
937 |
$d ||= ", "; |
$d ||= " "; |
938 |
|
|
939 |
my $last = pop @{$row->{$type}}; |
my $last = pop @{$row->{$type}}; |
940 |
$d = "" if (! $last); |
$d = "" if (! $last); |
959 |
# post-sort all values in field |
# post-sort all values in field |
960 |
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
961 |
$log->warn("sort at field tag not implemented"); |
$log->warn("sort at field tag not implemented"); |
|
|
|
962 |
} |
} |
963 |
|
|
964 |
push @ds, $row; |
push @ds, $row; |
1106 |
|
|
1107 |
$log->debug("eval: ",$code," [",$ret,"]"); |
$log->debug("eval: ",$code," [",$ret,"]"); |
1108 |
|
|
1109 |
return $ret || 0; |
return $ret || undef; |
1110 |
} |
} |
1111 |
|
|
1112 |
=head2 _sort_by_order |
=head2 _sort_by_order |