12 |
|
|
13 |
use Data::Dumper; |
use Data::Dumper; |
14 |
|
|
15 |
|
#my $LOOKUP_REGEX = '\[[^\[\]]+\]'; |
16 |
|
#my $LOOKUP_REGEX_SAVE = '\[([^\[\]]+)\]'; |
17 |
|
my $LOOKUP_REGEX = 'lookup{[^\{\}]+}'; |
18 |
|
my $LOOKUP_REGEX_SAVE = 'lookup{([^\{\}]+)}'; |
19 |
|
|
20 |
=head1 NAME |
=head1 NAME |
21 |
|
|
22 |
WebPAC - base class for WebPAC |
WebPAC - base class for WebPAC |
188 |
|
|
189 |
push @{$self->{'data'}->{$mfn}->{$k}}, $val; |
push @{$self->{'data'}->{$mfn}->{$k}}, $val; |
190 |
} |
} |
191 |
|
} else { |
192 |
|
push @{$self->{'data'}->{$mfn}->{'000'}}, $mfn; |
193 |
} |
} |
194 |
|
|
195 |
} |
} |
224 |
|
|
225 |
if ($mfn > $self->{'max_mfn'}) { |
if ($mfn > $self->{'max_mfn'}) { |
226 |
$self->{'current_mfn'} = $self->{'max_mfn'}; |
$self->{'current_mfn'} = $self->{'max_mfn'}; |
227 |
|
$log->debug("at EOF"); |
228 |
return; |
return; |
229 |
} |
} |
230 |
|
|
254 |
|
|
255 |
$self->{'tag'} = $type2tag{$type_base}; |
$self->{'tag'} = $type2tag{$type_base}; |
256 |
|
|
257 |
$log->debug("using type '",$self->{'type'},"' tag <",$self->{'tag'},">") if ($self->{'debug'}); |
$log->info("using type '",$self->{'type'},"' tag <",$self->{'tag'},">"); |
258 |
|
|
259 |
my $f = "./import_xml/".$self->{'type'}.".xml"; |
my $f = "./import_xml/".$self->{'type'}.".xml"; |
260 |
$log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f"); |
$log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f"); |
261 |
|
|
262 |
$log->debug("reading '$f'") if ($self->{'debug'}); |
$log->info("reading '$f'"); |
263 |
|
|
264 |
|
$self->{'import_xml_file'} = $f; |
265 |
|
|
266 |
$self->{'import_xml'} = XMLin($f, |
$self->{'import_xml'} = XMLin($f, |
267 |
ForceArray => [ $self->{'tag'}, 'config', 'format' ], |
ForceArray => [ $self->{'tag'}, 'config', 'format' ], |
|
ForceContent => 1 |
|
268 |
); |
); |
269 |
|
|
270 |
|
$log->debug("import xml is ",sub { Dumper($self->{'import_xml'}) }); |
271 |
|
|
272 |
} |
} |
273 |
|
|
274 |
=head2 create_lookup |
=head2 create_lookup |
295 |
my $key = $self->fill_in($rec,$i->{'key'}); |
my $key = $self->fill_in($rec,$i->{'key'}); |
296 |
my @val = $self->fill_in($rec,$i->{'val'}); |
my @val = $self->fill_in($rec,$i->{'val'}); |
297 |
if ($key && @val && eval $eval) { |
if ($key && @val && eval $eval) { |
298 |
|
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
299 |
push @{$self->{'lookup'}->{$key}}, @val; |
push @{$self->{'lookup'}->{$key}}, @val; |
300 |
} |
} |
301 |
} else { |
} else { |
302 |
my $key = $self->fill_in($rec,$i->{'key'}); |
my $key = $self->fill_in($rec,$i->{'key'}); |
303 |
my @val = $self->fill_in($rec,$i->{'val'}); |
my @val = $self->fill_in($rec,$i->{'val'}); |
304 |
if ($key && @val) { |
if ($key && @val) { |
305 |
|
$log->debug("stored $key = ",sub { join(" | ",@val) }); |
306 |
push @{$self->{'lookup'}->{$key}}, @val; |
push @{$self->{'lookup'}->{$key}}, @val; |
307 |
} |
} |
308 |
} |
} |
397 |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
398 |
|
|
399 |
# do actual replacement of placeholders |
# do actual replacement of placeholders |
400 |
$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; |
401 |
|
|
402 |
if ($found) { |
if ($found) { |
403 |
|
$log->debug("format: $format"); |
404 |
if ($eval_code) { |
if ($eval_code) { |
405 |
my $eval = $self->fill_in($rec,$eval_code,$i); |
my $eval = $self->fill_in($rec,$eval_code,$i); |
406 |
return if (! $self->_eval($eval)); |
return if (! $self->_eval($eval)); |
407 |
} |
} |
408 |
# do we have lookups? |
# do we have lookups? |
409 |
$log->debug("test format '$format' for lookups"); |
if ($format =~ /$LOOKUP_REGEX/o) { |
410 |
if ($format =~ /\[[^\[\]]+\]/o) { |
$log->debug("format '$format' has lookup"); |
411 |
return $self->lookup($format); |
return $self->lookup($format); |
412 |
} else { |
} else { |
413 |
return $format; |
return $format; |
434 |
|
|
435 |
my $tmp = shift || $log->logconfess("need format"); |
my $tmp = shift || $log->logconfess("need format"); |
436 |
|
|
437 |
if ($tmp =~ /\[[^\[\]]+\]/o) { |
if ($tmp =~ /$LOOKUP_REGEX/o) { |
438 |
my @in = ( $tmp ); |
my @in = ( $tmp ); |
439 |
|
|
440 |
$log->debug("lookup for: ",$tmp); |
$log->debug("lookup for: ",$tmp); |
441 |
|
|
442 |
my @out; |
my @out; |
443 |
while (my $f = shift @in) { |
while (my $f = shift @in) { |
444 |
if ($f =~ /\[([^\[\]]+)\]/) { |
if ($f =~ /$LOOKUP_REGEX_SAVE/o) { |
445 |
my $k = $1; |
my $k = $1; |
446 |
if ($self->{'lookup'}->{$k}) { |
if ($self->{'lookup'}->{$k}) { |
447 |
foreach my $nv (@{$self->{'lookup'}->{$k}}) { |
foreach my $nv (@{$self->{'lookup'}->{$k}}) { |
448 |
my $tmp2 = $f; |
my $tmp2 = $f; |
449 |
$tmp2 =~ s/\[$k\]/$nv/g; |
$tmp2 =~ s/lookup{$k}/$nv/g; |
450 |
push @in, $tmp2; |
push @in, $tmp2; |
451 |
} |
} |
452 |
} else { |
} else { |
456 |
push @out, $f; |
push @out, $f; |
457 |
} |
} |
458 |
} |
} |
459 |
|
$log->logconfess("return is array and it's not expected!") unless wantarray; |
460 |
return @out; |
return @out; |
461 |
} else { |
} else { |
462 |
return $tmp; |
return $tmp; |
487 |
|
|
488 |
$i = 0 if (! $i); |
$i = 0 if (! $i); |
489 |
|
|
490 |
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'}); |
491 |
|
|
492 |
my @out; |
my @out; |
493 |
|
|
494 |
|
$log->debug("format: $format"); |
495 |
|
|
496 |
my $eval_code; |
my $eval_code; |
497 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
498 |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
500 |
my $prefix; |
my $prefix; |
501 |
my $all_found=0; |
my $all_found=0; |
502 |
|
|
503 |
while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))*//s) { |
while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))?//s) { |
504 |
|
|
505 |
my $del = $1 || ''; |
my $del = $1 || ''; |
506 |
$prefix ||= $del if ($all_found == 0); |
$prefix ||= $del if ($all_found == 0); |
517 |
|
|
518 |
return if (! $all_found); |
return if (! $all_found); |
519 |
|
|
520 |
my $out = join('',@out) . $format; |
my $out = join('',@out); |
521 |
|
|
522 |
|
if ($out) { |
523 |
|
# add rest of format (suffix) |
524 |
|
$out .= $format; |
525 |
|
|
526 |
# add prefix if not there |
# add prefix if not there |
527 |
$out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); |
$out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); |
528 |
|
|
529 |
|
$log->debug("result: $out"); |
530 |
|
} |
531 |
|
|
532 |
if ($eval_code) { |
if ($eval_code) { |
533 |
my $eval = $self->fill_in($rec,$eval_code,$i); |
my $eval = $self->fill_in($rec,$eval_code,$i); |
534 |
$log->debug("about to eval ",$eval," [$out]"); |
$log->debug("about to eval{",$eval,"} format: $out"); |
535 |
return if (! $self->_eval($eval)); |
return if (! $self->_eval($eval)); |
536 |
} |
} |
537 |
|
|
563 |
push @arr, $v; |
push @arr, $v; |
564 |
} |
} |
565 |
|
|
566 |
|
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
567 |
|
|
568 |
|
return @arr; |
569 |
|
} |
570 |
|
|
571 |
|
=head2 fill_in_to_arr |
572 |
|
|
573 |
|
Similar to C<fill_in>, but returns array of all repeatable fields. Usable |
574 |
|
for fields which have lookups, so they shouldn't be parsed but rather |
575 |
|
C<fill_id>ed. |
576 |
|
|
577 |
|
my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); |
578 |
|
|
579 |
|
=cut |
580 |
|
|
581 |
|
sub fill_in_to_arr { |
582 |
|
my $self = shift; |
583 |
|
|
584 |
|
my ($rec, $format_utf8) = @_; |
585 |
|
|
586 |
|
my $log = $self->_get_logger(); |
587 |
|
|
588 |
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
589 |
|
return if (! $format_utf8); |
590 |
|
|
591 |
|
my $i = 0; |
592 |
|
my @arr; |
593 |
|
|
594 |
|
while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { |
595 |
|
push @arr, @v; |
596 |
|
} |
597 |
|
|
598 |
|
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
599 |
|
|
600 |
return @arr; |
return @arr; |
601 |
} |
} |
602 |
|
|
603 |
|
|
604 |
=head2 data_structure |
=head2 data_structure |
605 |
|
|
606 |
Create in-memory data structure which represents layout from C<import_xml>. |
Create in-memory data structure which represents layout from C<import_xml>. |
608 |
|
|
609 |
my @ds = $webpac->data_structure($rec); |
my @ds = $webpac->data_structure($rec); |
610 |
|
|
611 |
|
This method will also set C<$webpac->{'currnet_filename'}> if there is |
612 |
|
<filename> tag in C<import_xml>. |
613 |
|
|
614 |
=cut |
=cut |
615 |
|
|
616 |
sub data_structure { |
sub data_structure { |
621 |
my $rec = shift; |
my $rec = shift; |
622 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
623 |
|
|
624 |
|
undef $self->{'currnet_filename'}; |
625 |
|
|
626 |
my @sorted_tags; |
my @sorted_tags; |
627 |
if ($self->{tags_by_order}) { |
if ($self->{tags_by_order}) { |
628 |
@sorted_tags = @{$self->{tags_by_order}}; |
@sorted_tags = @{$self->{tags_by_order}}; |
633 |
|
|
634 |
my @ds; |
my @ds; |
635 |
|
|
636 |
|
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
637 |
|
|
638 |
foreach my $field (@sorted_tags) { |
foreach my $field (@sorted_tags) { |
639 |
|
|
640 |
my $row; |
my $row; |
642 |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
643 |
|
|
644 |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
645 |
my @v = $self->parse_to_arr($rec,$tag->{'content'}); |
my $format = $tag->{'value'} || $tag->{'content'}; |
646 |
|
|
647 |
|
$log->debug("format: $format"); |
648 |
|
|
649 |
|
my @v; |
650 |
|
if ($format =~ /$LOOKUP_REGEX/o) { |
651 |
|
@v = $self->fill_in_to_arr($rec,$format); |
652 |
|
} else { |
653 |
|
@v = $self->parse_to_arr($rec,$format); |
654 |
|
} |
655 |
next if (! @v); |
next if (! @v); |
656 |
|
|
657 |
|
# use format? |
658 |
|
if ($tag->{'format_name'}) { |
659 |
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
660 |
|
} |
661 |
|
|
662 |
# does tag have type? |
# does tag have type? |
663 |
if ($tag->{'type'}) { |
if ($tag->{'type'}) { |
664 |
push @{$row->{$tag->{'type'}}}, @v; |
push @{$row->{$tag->{'type'}}}, @v; |
666 |
push @{$row->{'display'}}, @v; |
push @{$row->{'display'}}, @v; |
667 |
push @{$row->{'swish'}}, @v; |
push @{$row->{'swish'}}, @v; |
668 |
} |
} |
669 |
|
|
670 |
|
if ($field eq 'filename') { |
671 |
|
$self->{'current_filename'} = join('',@v); |
672 |
|
$log->debug("filename: ",$self->{'current_filename'}); |
673 |
|
} |
674 |
|
|
675 |
} |
} |
676 |
|
|
677 |
if ($row) { |
if ($row) { |
678 |
$row->{'tag'} = $field; |
$row->{'tag'} = $field; |
679 |
|
|
680 |
|
# TODO: name_sigular, name_plural |
681 |
|
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
682 |
|
$row->{'name'} = $name ? $self->_x($name) : $field; |
683 |
|
|
684 |
push @ds, $row; |
push @ds, $row; |
685 |
|
|
686 |
|
$log->debug("row $field: ",sub { Dumper($row) }); |
687 |
} |
} |
688 |
|
|
689 |
} |
} |
721 |
return $out; |
return $out; |
722 |
} |
} |
723 |
|
|
724 |
|
=head2 apply_format |
725 |
|
|
726 |
|
Apply format specified in tag with C<format_name="name"> and |
727 |
|
C<format_delimiter=";;">. |
728 |
|
|
729 |
|
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
730 |
|
|
731 |
|
Formats can contain C<lookup{...}> if you need them. |
732 |
|
|
733 |
|
=cut |
734 |
|
|
735 |
|
sub apply_format { |
736 |
|
my $self = shift; |
737 |
|
|
738 |
|
my ($name,$delimiter,$data) = @_; |
739 |
|
|
740 |
|
my $log = $self->_get_logger(); |
741 |
|
|
742 |
|
if (! $self->{'import_xml'}->{'format'}->{$name}) { |
743 |
|
$log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'}); |
744 |
|
return $data; |
745 |
|
} |
746 |
|
|
747 |
|
$log->warn("no delimiter for format $name") if (! $delimiter); |
748 |
|
|
749 |
|
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
750 |
|
|
751 |
|
my @data = split(/\Q$delimiter\E/, $data); |
752 |
|
|
753 |
|
my $out = sprintf($format, @data); |
754 |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
755 |
|
|
756 |
|
if ($out =~ m/$LOOKUP_REGEX/o) { |
757 |
|
return $self->lookup($out); |
758 |
|
} else { |
759 |
|
return $out; |
760 |
|
} |
761 |
|
|
762 |
|
} |
763 |
|
|
764 |
|
|
765 |
# |
# |
766 |
# |
# |
767 |
# |
# |
814 |
return $va <=> $vb; |
return $va <=> $vb; |
815 |
} |
} |
816 |
|
|
817 |
|
=head2 _get_logger |
818 |
|
|
819 |
|
Get C<Log::Log4perl> object with a twist: domains are defined for each |
820 |
|
method |
821 |
|
|
822 |
|
my $log = $webpac->_get_logger(); |
823 |
|
|
824 |
|
=cut |
825 |
|
|
826 |
sub _get_logger { |
sub _get_logger { |
827 |
my $self = shift; |
my $self = shift; |
828 |
|
|
829 |
my @c = caller(1); |
my $name = (caller(1))[3] || caller; |
830 |
return get_logger($c[3]); |
return get_logger($name); |
831 |
|
} |
832 |
|
|
833 |
|
=head2 _x |
834 |
|
|
835 |
|
Convert string from UTF-8 to code page defined in C<import_xml>. |
836 |
|
|
837 |
|
my $text = $webpac->_x('utf8 text'); |
838 |
|
|
839 |
|
=cut |
840 |
|
|
841 |
|
sub _x { |
842 |
|
my $self = shift; |
843 |
|
my $utf8 = shift || return; |
844 |
|
|
845 |
|
return $self->{'utf2cp'}->convert($utf8) || |
846 |
|
$self->_get_logger()->logwarn("can't convert '$utf8'"); |
847 |
} |
} |
848 |
|
|
849 |
# |
# |