169 |
# read database |
# read database |
170 |
for (my $mfn = 1; $mfn <= $maxmfn; $mfn++) { |
for (my $mfn = 1; $mfn <= $maxmfn; $mfn++) { |
171 |
|
|
172 |
|
|
173 |
|
$log->debug("mfn: $mfn\n"); |
174 |
|
|
175 |
# read record |
# read record |
176 |
my $row = OpenIsis::read( $isis_db, $mfn ); |
my $row = OpenIsis::read( $isis_db, $mfn ); |
177 |
foreach my $k (keys %{$row}) { |
foreach my $k (keys %{$row}) { |
198 |
} |
} |
199 |
|
|
200 |
# create lookup |
# create lookup |
201 |
my $rec = $self->{'data'}->{$mfn}; |
my $rec = $self->{'data'}->{$mfn} || $log->confess("record $mfn empty?"); |
202 |
$self->create_lookup($rec, @{$arg->{'lookup'}}); |
$self->create_lookup($rec, @{$arg->{'lookup'}}); |
203 |
|
|
204 |
|
$self->progress_bar($mfn,$maxmfn); |
205 |
|
|
206 |
} |
} |
207 |
|
|
208 |
$self->{'current_mfn'} = 1; |
$self->{'current_mfn'} = 1; |
209 |
|
$self->{'last_pcnt'} = 0; |
210 |
|
|
211 |
|
$log->debug("max mfn: $maxmfn"); |
212 |
|
|
213 |
# store max mfn and return it. |
# store max mfn and return it. |
214 |
return $self->{'max_mfn'} = $maxmfn; |
return $self->{'max_mfn'} = $maxmfn; |
236 |
return; |
return; |
237 |
} |
} |
238 |
|
|
239 |
|
$self->progress_bar($mfn,$self->{'max_mfn'}); |
240 |
|
|
241 |
return $self->{'data'}->{$mfn}; |
return $self->{'data'}->{$mfn}; |
242 |
} |
} |
243 |
|
|
244 |
|
=head2 progress_bar |
245 |
|
|
246 |
|
Draw progress bar on STDERR. |
247 |
|
|
248 |
|
$webpac->progress_bar($current, $max); |
249 |
|
|
250 |
|
=cut |
251 |
|
|
252 |
|
sub progress_bar { |
253 |
|
my $self = shift; |
254 |
|
|
255 |
|
my ($curr,$max) = @_; |
256 |
|
|
257 |
|
my $log = $self->_get_logger(); |
258 |
|
|
259 |
|
$log->logconfess("no current value!") if (! $curr); |
260 |
|
$log->logconfess("no maximum value!") if (! $max); |
261 |
|
|
262 |
|
if ($curr > $max) { |
263 |
|
$max = $curr; |
264 |
|
$log->debug("overflow to $curr"); |
265 |
|
} |
266 |
|
|
267 |
|
$self->{'last_pcnt'} ||= 1; |
268 |
|
|
269 |
|
$self->{'last_pcnt'} = $curr if ($curr < $self->{'last_pcnt'}); |
270 |
|
|
271 |
|
my $p = int($curr * 100 / $max); |
272 |
|
if ($p != $self->{'last_pcnt'}) { |
273 |
|
printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$curr,$max,"=" x ($p/2).">", $p ); |
274 |
|
$self->{'last_pcnt'} = $p; |
275 |
|
} |
276 |
|
print STDERR "\n" if ($p == 100); |
277 |
|
} |
278 |
|
|
279 |
=head2 open_import_xml |
=head2 open_import_xml |
280 |
|
|
281 |
Read file from C<import_xml/> directory and parse it. |
Read file from C<import_xml/> directory and parse it. |
299 |
|
|
300 |
$self->{'tag'} = $type2tag{$type_base}; |
$self->{'tag'} = $type2tag{$type_base}; |
301 |
|
|
302 |
$log->debug("using type '",$self->{'type'},"' tag <",$self->{'tag'},">") if ($self->{'debug'}); |
$log->info("using type '",$self->{'type'},"' tag <",$self->{'tag'},">"); |
303 |
|
|
304 |
my $f = "./import_xml/".$self->{'type'}.".xml"; |
my $f = "./import_xml/".$self->{'type'}.".xml"; |
305 |
$log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f"); |
$log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f"); |
306 |
|
|
307 |
$log->debug("reading '$f'") if ($self->{'debug'}); |
$log->info("reading '$f'"); |
308 |
|
|
309 |
|
$self->{'import_xml_file'} = $f; |
310 |
|
|
311 |
$self->{'import_xml'} = XMLin($f, |
$self->{'import_xml'} = XMLin($f, |
312 |
ForceArray => [ $self->{'tag'}, 'config', 'format' ], |
ForceArray => [ $self->{'tag'}, 'config', 'format' ], |
313 |
); |
); |
314 |
|
|
315 |
|
$log->debug("import xml is ",sub { Dumper($self->{'import_xml'}) }); |
316 |
|
|
317 |
} |
} |
318 |
|
|
319 |
=head2 create_lookup |
=head2 create_lookup |
335 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
336 |
|
|
337 |
foreach my $i (@_) { |
foreach my $i (@_) { |
338 |
if ($i->{'eval'}) { |
$log->logconfess("need key") unless defined($i->{'key'}); |
339 |
my $eval = $self->fill_in($rec,$i->{'eval'}); |
$log->logconfess("need val") unless defined($i->{'val'}); |
340 |
my $key = $self->fill_in($rec,$i->{'key'}); |
|
341 |
my @val = $self->fill_in($rec,$i->{'val'}); |
if (defined($i->{'eval'})) { |
342 |
if ($key && @val && eval $eval) { |
# eval first, so we can skip fill_in for key and val |
343 |
|
my $eval = $self->fill_in($rec,$i->{'eval'}) || next; |
344 |
|
if ($self->_eval($eval)) { |
345 |
|
my $key = $self->fill_in($rec,$i->{'key'}) || next; |
346 |
|
my @val = $self->fill_in($rec,$i->{'val'}) || next; |
347 |
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
348 |
push @{$self->{'lookup'}->{$key}}, @val; |
push @{$self->{'lookup'}->{$key}}, @val; |
349 |
} |
} |
350 |
} else { |
} else { |
351 |
my $key = $self->fill_in($rec,$i->{'key'}); |
my $key = $self->fill_in($rec,$i->{'key'}) || next; |
352 |
my @val = $self->fill_in($rec,$i->{'val'}); |
my @val = $self->fill_in($rec,$i->{'val'}) || next; |
353 |
if ($key && @val) { |
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
354 |
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
push @{$self->{'lookup'}->{$key}}, @val; |
|
push @{$self->{'lookup'}->{$key}}, @val; |
|
|
} |
|
355 |
} |
} |
356 |
} |
} |
357 |
} |
} |
382 |
|
|
383 |
if ($$rec->{$f}) { |
if ($$rec->{$f}) { |
384 |
return '' if (! $$rec->{$f}->[$i]); |
return '' if (! $$rec->{$f}->[$i]); |
385 |
|
no strict 'refs'; |
386 |
if ($sf && $$rec->{$f}->[$i]->{$sf}) { |
if ($sf && $$rec->{$f}->[$i]->{$sf}) { |
387 |
$$found++ if (defined($$found)); |
$$found++ if (defined($$found)); |
388 |
return $$rec->{$f}->[$i]->{$sf}; |
return $$rec->{$f}->[$i]->{$sf}; |
423 |
This function B<does not> perform parsing of format to inteligenty skip |
This function B<does not> perform parsing of format to inteligenty skip |
424 |
delimiters before fields which aren't used. |
delimiters before fields which aren't used. |
425 |
|
|
426 |
|
This method will automatically decode UTF-8 string to local code page |
427 |
|
if needed. |
428 |
|
|
429 |
=cut |
=cut |
430 |
|
|
431 |
sub fill_in { |
sub fill_in { |
441 |
# FIXME remove for speedup? |
# FIXME remove for speedup? |
442 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
443 |
|
|
444 |
|
if (utf8::is_utf8($format)) { |
445 |
|
$format = $self->_x($format); |
446 |
|
} |
447 |
|
|
448 |
my $found = 0; |
my $found = 0; |
449 |
|
|
450 |
my $eval_code; |
my $eval_code; |
542 |
|
|
543 |
$i = 0 if (! $i); |
$i = 0 if (! $i); |
544 |
|
|
545 |
my $format = $self->{'utf2cp'}->convert($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
546 |
|
|
547 |
my @out; |
my @out; |
548 |
|
|
664 |
my @ds = $webpac->data_structure($rec); |
my @ds = $webpac->data_structure($rec); |
665 |
|
|
666 |
This method will also set C<$webpac->{'currnet_filename'}> if there is |
This method will also set C<$webpac->{'currnet_filename'}> if there is |
667 |
<filename> tag in C<import_xml>. |
<filename> tag in C<import_xml> and C<$webpac->{'headline'}> if there is |
668 |
|
<headline> tag. |
669 |
|
|
670 |
=cut |
=cut |
671 |
|
|
678 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
679 |
|
|
680 |
undef $self->{'currnet_filename'}; |
undef $self->{'currnet_filename'}; |
681 |
|
undef $self->{'headline'}; |
682 |
|
|
683 |
my @sorted_tags; |
my @sorted_tags; |
684 |
if ($self->{tags_by_order}) { |
if ($self->{tags_by_order}) { |
711 |
} |
} |
712 |
next if (! @v); |
next if (! @v); |
713 |
|
|
714 |
|
# use format? |
715 |
|
if ($tag->{'format_name'}) { |
716 |
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
717 |
|
} |
718 |
|
|
719 |
|
if ($field eq 'filename') { |
720 |
|
$self->{'current_filename'} = join('',@v); |
721 |
|
$log->debug("filename: ",$self->{'current_filename'}); |
722 |
|
} elsif ($field eq 'headline') { |
723 |
|
$self->{'headline'} .= join('',@v); |
724 |
|
$log->debug("headline: ",$self->{'headline'}); |
725 |
|
next; # don't return headline in data_structure! |
726 |
|
} |
727 |
|
|
728 |
# does tag have type? |
# does tag have type? |
729 |
if ($tag->{'type'}) { |
if ($tag->{'type'}) { |
730 |
push @{$row->{$tag->{'type'}}}, @v; |
push @{$row->{$tag->{'type'}}}, @v; |
733 |
push @{$row->{'swish'}}, @v; |
push @{$row->{'swish'}}, @v; |
734 |
} |
} |
735 |
|
|
|
if ($field eq 'filename') { |
|
|
$self->{'current_filename'} = join('',@v); |
|
|
$log->debug("filename: ",$self->{'current_filename'}); |
|
|
} |
|
736 |
|
|
737 |
} |
} |
738 |
|
|
739 |
if ($row) { |
if ($row) { |
740 |
$row->{'tag'} = $field; |
$row->{'tag'} = $field; |
741 |
|
|
742 |
|
# TODO: name_sigular, name_plural |
743 |
|
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
744 |
|
$row->{'name'} = $name ? $self->_x($name) : $field; |
745 |
|
|
746 |
push @ds, $row; |
push @ds, $row; |
747 |
|
|
748 |
$log->debug("row $field: ",sub { Dumper($row) }); |
$log->debug("row $field: ",sub { Dumper($row) }); |
783 |
return $out; |
return $out; |
784 |
} |
} |
785 |
|
|
786 |
|
=head2 output_file |
787 |
|
|
788 |
|
Create output from in-memory data structure using Template Toolkit template |
789 |
|
to a file. |
790 |
|
|
791 |
|
$webpac->output_file( |
792 |
|
file => 'out.txt', |
793 |
|
template => 'text.tt', |
794 |
|
data => @ds |
795 |
|
); |
796 |
|
|
797 |
|
=cut |
798 |
|
|
799 |
|
sub output_file { |
800 |
|
my $self = shift; |
801 |
|
|
802 |
|
my $args = {@_}; |
803 |
|
|
804 |
|
my $log = $self->_get_logger(); |
805 |
|
|
806 |
|
$log->logconfess("need file name") if (! $args->{'file'}); |
807 |
|
|
808 |
|
$log->debug("creating file ",$args->{'file'}); |
809 |
|
|
810 |
|
open(my $fh, ">", $args->{'file'}) || $log->logdie("can't open output file '$self->{'file'}': $!"); |
811 |
|
print $fh $self->output( |
812 |
|
template => $args->{'template'}, |
813 |
|
data => $args->{'data'}, |
814 |
|
) || $log->logdie("print: $!"); |
815 |
|
close($fh) || $log->logdie("close: $!"); |
816 |
|
} |
817 |
|
|
818 |
|
=head2 apply_format |
819 |
|
|
820 |
|
Apply format specified in tag with C<format_name="name"> and |
821 |
|
C<format_delimiter=";;">. |
822 |
|
|
823 |
|
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
824 |
|
|
825 |
|
Formats can contain C<lookup{...}> if you need them. |
826 |
|
|
827 |
|
=cut |
828 |
|
|
829 |
|
sub apply_format { |
830 |
|
my $self = shift; |
831 |
|
|
832 |
|
my ($name,$delimiter,$data) = @_; |
833 |
|
|
834 |
|
my $log = $self->_get_logger(); |
835 |
|
|
836 |
|
if (! $self->{'import_xml'}->{'format'}->{$name}) { |
837 |
|
$log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'}); |
838 |
|
return $data; |
839 |
|
} |
840 |
|
|
841 |
|
$log->warn("no delimiter for format $name") if (! $delimiter); |
842 |
|
|
843 |
|
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
844 |
|
|
845 |
|
my @data = split(/\Q$delimiter\E/, $data); |
846 |
|
|
847 |
|
my $out = sprintf($format, @data); |
848 |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
849 |
|
|
850 |
|
if ($out =~ m/$LOOKUP_REGEX/o) { |
851 |
|
return $self->lookup($out); |
852 |
|
} else { |
853 |
|
return $out; |
854 |
|
} |
855 |
|
|
856 |
|
} |
857 |
|
|
858 |
|
|
859 |
# |
# |
860 |
# |
# |
861 |
# |
# |
908 |
return $va <=> $vb; |
return $va <=> $vb; |
909 |
} |
} |
910 |
|
|
911 |
|
=head2 _get_logger |
912 |
|
|
913 |
|
Get C<Log::Log4perl> object with a twist: domains are defined for each |
914 |
|
method |
915 |
|
|
916 |
|
my $log = $webpac->_get_logger(); |
917 |
|
|
918 |
|
=cut |
919 |
|
|
920 |
sub _get_logger { |
sub _get_logger { |
921 |
my $self = shift; |
my $self = shift; |
922 |
|
|
924 |
return get_logger($name); |
return get_logger($name); |
925 |
} |
} |
926 |
|
|
927 |
|
=head2 _x |
928 |
|
|
929 |
|
Convert string from UTF-8 to code page defined in C<import_xml>. |
930 |
|
|
931 |
|
my $text = $webpac->_x('utf8 text'); |
932 |
|
|
933 |
|
=cut |
934 |
|
|
935 |
|
sub _x { |
936 |
|
my $self = shift; |
937 |
|
my $utf8 = shift || return; |
938 |
|
|
939 |
|
return $self->{'utf2cp'}->convert($utf8) || |
940 |
|
$self->_get_logger()->logwarn("can't convert '$utf8'"); |
941 |
|
} |
942 |
|
|
943 |
# |
# |
944 |
# |
# |
945 |
# |
# |