2 |
|
|
3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
use blib; |
6 |
|
use WebPAC::Common; |
7 |
use base 'WebPAC::Common'; |
use base 'WebPAC::Common'; |
8 |
use Data::Dumper; |
use Data::Dumper; |
9 |
|
|
13 |
|
|
14 |
=head1 VERSION |
=head1 VERSION |
15 |
|
|
16 |
Version 0.07 |
Version 0.08 |
17 |
|
|
18 |
=cut |
=cut |
19 |
|
|
20 |
our $VERSION = '0.07'; |
our $VERSION = '0.08'; |
21 |
|
|
22 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
23 |
|
|
196 |
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
197 |
$format = $tag->{'value'} || $tag->{'content'}; |
$format = $tag->{'value'} || $tag->{'content'}; |
198 |
|
|
|
$log->debug("format: $format"); |
|
|
|
|
199 |
my @v; |
my @v; |
200 |
if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { |
if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { |
201 |
@v = $self->fill_in_to_arr($rec,$format); |
@v = $self->_rec_to_arr($rec,$format,'fill_in'); |
202 |
|
} else { |
203 |
|
@v = $self->_rec_to_arr($rec,$format,'parse'); |
204 |
|
} |
205 |
|
if (! @v) { |
206 |
|
$log->debug("$field <",$self->{tag},"> format: $format no values"); |
207 |
|
# next; |
208 |
} else { |
} else { |
209 |
@v = $self->parse_to_arr($rec,$format); |
$log->debug("$field <",$self->{tag},"> format: $format values: ", join(",", @v)); |
210 |
} |
} |
|
next if (! @v); |
|
211 |
|
|
212 |
if ($tag->{'sort'}) { |
if ($tag->{'sort'}) { |
213 |
@v = $self->sort_arr(@v); |
@v = $self->sort_arr(@v); |
230 |
|
|
231 |
foreach my $type (@types) { |
foreach my $type (@types) { |
232 |
# append to previous line? |
# append to previous line? |
233 |
$log->debug("type: $type ",sub { join(" ",@v) }, " ", $row->{'append'} || 'no append'); |
$log->debug("tag $field / $type [",sub { join(",",@v) }, "] ", $row->{'append'} || 'no append'); |
234 |
if ($tag->{'append'}) { |
if ($tag->{'append'}) { |
235 |
|
|
236 |
# I will delimit appended part with |
# I will delimit appended part with |
308 |
sub parse { |
sub parse { |
309 |
my $self = shift; |
my $self = shift; |
310 |
|
|
311 |
my ($rec, $format_utf8, $i) = @_; |
my ($rec, $format_utf8, $i, $rec_size) = @_; |
312 |
|
|
313 |
return if (! $format_utf8); |
return if (! $format_utf8); |
314 |
|
|
322 |
|
|
323 |
my @out; |
my @out; |
324 |
|
|
325 |
$log->debug("format: $format"); |
$log->debug("format: $format [$i]"); |
326 |
|
|
327 |
my $eval_code; |
my $eval_code; |
328 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
332 |
# remove filter{...} from beginning |
# remove filter{...} from beginning |
333 |
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
334 |
|
|
335 |
|
# did we found any (att all) field from format in row? |
336 |
|
my $found_any; |
337 |
|
# prefix before first field which we preserve it $found_any |
338 |
my $prefix; |
my $prefix; |
339 |
my $all_found=0; |
|
340 |
|
my $f_step = 1; |
341 |
|
|
342 |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
343 |
|
|
344 |
my $del = $1 || ''; |
my $del = $1 || ''; |
345 |
$prefix ||= $del if ($all_found == 0); |
$prefix = $del if ($f_step == 1); |
346 |
|
|
347 |
|
my $fld_type = lc($2); |
348 |
|
|
349 |
# repeatable index |
# repeatable index |
350 |
my $r = $i; |
my $r = $i; |
351 |
$r = 0 if (lc("$2") eq 's'); |
if ($fld_type eq 's') { |
352 |
|
if ($found_any->{'v'}) { |
353 |
|
$r = 0; |
354 |
|
} else { |
355 |
|
return; |
356 |
|
} |
357 |
|
} |
358 |
|
|
359 |
my $found = 0; |
my $found = 0; |
360 |
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found,$rec_size); |
361 |
|
|
362 |
if ($found) { |
if ($found) { |
363 |
push @out, $del; |
$found_any->{$fld_type} += $found; |
364 |
|
|
365 |
|
# we will skip delimiter before first occurence of field! |
366 |
|
push @out, $del unless($found_any->{$fld_type} == 1); |
367 |
push @out, $tmp; |
push @out, $tmp; |
|
$all_found += $found; |
|
368 |
} |
} |
369 |
|
$f_step++; |
370 |
} |
} |
371 |
|
|
372 |
return if (! $all_found); |
# test if any fields found? |
373 |
|
return if (! $found_any->{'v'} && ! $found_any->{'s'}); |
374 |
|
|
375 |
my $out = join('',@out); |
my $out = join('',@out); |
376 |
|
|
410 |
return $out; |
return $out; |
411 |
} |
} |
412 |
|
|
|
=head2 parse_to_arr |
|
|
|
|
|
Similar to C<parse>, but returns array of all repeatable fields |
|
|
|
|
|
my @arr = $webpac->parse_to_arr($rec,'v250^a'); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub parse_to_arr { |
|
|
my $self = shift; |
|
|
|
|
|
my ($rec, $format_utf8) = @_; |
|
|
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
|
return if (! $format_utf8); |
|
|
|
|
|
my $i = 0; |
|
|
my @arr; |
|
|
|
|
|
while (my $v = $self->parse($rec,$format_utf8,$i++)) { |
|
|
push @arr, $v; |
|
|
} |
|
|
|
|
|
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
|
|
|
|
|
return @arr; |
|
|
} |
|
|
|
|
|
|
|
413 |
=head2 fill_in |
=head2 fill_in |
414 |
|
|
415 |
Workhourse of all: takes record from in-memory structure of database and |
Workhourse of all: takes record from in-memory structure of database and |
431 |
This method will automatically decode UTF-8 string to local code page |
This method will automatically decode UTF-8 string to local code page |
432 |
if needed. |
if needed. |
433 |
|
|
434 |
|
There is optional parametar C<$record_size> which can be used to get sizes of |
435 |
|
all C<field^subfield> combinations in this format. |
436 |
|
|
437 |
|
my $text = $webpac->fill_in($rec,'got: v900^a v900^x',0,\$rec_size); |
438 |
|
|
439 |
=cut |
=cut |
440 |
|
|
441 |
sub fill_in { |
sub fill_in { |
443 |
|
|
444 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
445 |
|
|
446 |
my $rec = shift || $log->logconfess("need data record"); |
my ($rec,$format,$i,$rec_size) = @_; |
447 |
my $format = shift || $log->logconfess("need format to parse"); |
|
448 |
|
$log->logconfess("need data record") unless ($rec); |
449 |
|
$log->logconfess("need format to parse") unless($format); |
450 |
|
|
451 |
# iteration (for repeatable fields) |
# iteration (for repeatable fields) |
452 |
my $i = shift || 0; |
$i ||= 0; |
453 |
|
|
454 |
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
455 |
|
|
461 |
} |
} |
462 |
|
|
463 |
my $found = 0; |
my $found = 0; |
464 |
|
my $just_single = 1; |
465 |
|
|
466 |
my $eval_code; |
my $eval_code; |
467 |
# remove eval{...} from beginning |
# remove eval{...} from beginning |
473 |
|
|
474 |
# do actual replacement of placeholders |
# do actual replacement of placeholders |
475 |
# repeatable fields |
# repeatable fields |
476 |
$format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; |
if ($format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found,$rec_size)/ges) { |
477 |
|
$just_single = 0; |
478 |
|
} |
479 |
|
|
480 |
# non-repeatable fields |
# non-repeatable fields |
481 |
$format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; |
if ($format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found,$rec_size)/ges) { |
482 |
|
return if ($i > 0 && $just_single); |
483 |
|
} |
484 |
|
|
485 |
if ($found) { |
if ($found) { |
486 |
$log->debug("format: $format"); |
$log->debug("format: $format"); |
512 |
} |
} |
513 |
|
|
514 |
|
|
515 |
=head2 fill_in_to_arr |
=head2 _rec_to_arr |
516 |
|
|
517 |
Similar to C<fill_in>, but returns array of all repeatable fields. Usable |
Similar to C<parse> and C<fill_in>, but returns array of all repeatable fields. Usable |
518 |
for fields which have lookups, so they shouldn't be parsed but rather |
for fields which have lookups, so they shouldn't be parsed but rather |
519 |
C<fill_id>ed. |
C<paste>d or C<fill_id>ed. Last argument is name of operation: C<paste> or C<fill_in>. |
520 |
|
|
521 |
my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); |
my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]','paste'); |
522 |
|
|
523 |
=cut |
=cut |
524 |
|
|
525 |
sub fill_in_to_arr { |
sub _rec_to_arr { |
526 |
my $self = shift; |
my $self = shift; |
527 |
|
|
528 |
my ($rec, $format_utf8) = @_; |
my ($rec, $format_utf8, $code) = @_; |
529 |
|
|
530 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
531 |
|
|
533 |
return if (! $format_utf8); |
return if (! $format_utf8); |
534 |
|
|
535 |
my $i = 0; |
my $i = 0; |
536 |
|
my $max = 0; |
537 |
my @arr; |
my @arr; |
538 |
|
my $rec_size = {}; |
539 |
|
|
540 |
while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { |
while ($i <= $max) { |
541 |
push @arr, @v; |
my $v = $self->$code($rec,$format_utf8,$i++,\$rec_size) || next; |
542 |
|
push @arr, $v; |
543 |
|
if ($rec_size) { |
544 |
|
foreach my $f (keys %{ $rec_size }) { |
545 |
|
$max = $rec_size->{$f} if ($rec_size->{$f} > $max); |
546 |
|
} |
547 |
|
warn "max set to $max, rec_size = ", Dumper($rec_size); |
548 |
|
undef $rec_size; |
549 |
|
} |
550 |
} |
} |
551 |
|
|
552 |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
559 |
|
|
560 |
Returns value from record. |
Returns value from record. |
561 |
|
|
562 |
my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); |
my $text = $self->get_data(\$rec,$f,$sf,$i,\$found,\$rec_size); |
563 |
|
|
564 |
|
Required arguments are: |
565 |
|
|
566 |
|
=over 8 |
567 |
|
|
568 |
|
=item C<$rec> |
569 |
|
|
570 |
|
record reference |
571 |
|
|
572 |
|
=item C<$f> |
573 |
|
|
574 |
|
field |
575 |
|
|
576 |
Arguments are: |
=item C<$sf> |
|
record reference C<$rec>, |
|
|
field C<$f>, |
|
|
optional subfiled C<$sf>, |
|
|
index for repeatable values C<$i>. |
|
577 |
|
|
578 |
Optinal variable C<$found> will be incremeted if there |
optional subfield |
|
is field. |
|
579 |
|
|
580 |
Returns value or empty string. |
=item C<$i> |
581 |
|
|
582 |
|
index offset for repeatable values ( 0 ... $rec_size->{'400^a'} ) |
583 |
|
|
584 |
|
=item C<$found> |
585 |
|
|
586 |
|
optional variable that will be incremeted if preset |
587 |
|
|
588 |
|
=item C<$rec_size> |
589 |
|
|
590 |
|
hash to hold maximum occurances of C<field^subfield> combinations |
591 |
|
(which can be accessed using keys in same format) |
592 |
|
|
593 |
|
=back |
594 |
|
|
595 |
|
Returns value or empty string, updates C<$found> and C<rec_size> |
596 |
|
if present. |
597 |
|
|
598 |
=cut |
=cut |
599 |
|
|
600 |
sub get_data { |
sub get_data { |
601 |
my $self = shift; |
my $self = shift; |
602 |
|
|
603 |
my ($rec,$f,$sf,$i,$found) = @_; |
my ($rec,$f,$sf,$i,$found,$cache) = @_; |
604 |
|
|
605 |
if ($$rec->{$f}) { |
return '' unless ($$rec->{$f} && ref($$rec->{$f}) eq 'ARRAY'); |
606 |
return '' if (! $$rec->{$f}->[$i]); |
|
607 |
|
if (defined($$cache)) { |
608 |
|
$$cache->{ $f . ( $sf ? '^' . $sf : '' ) } ||= scalar @{ $$rec->{$f} }; |
609 |
|
} |
610 |
|
|
611 |
|
return '' unless ($$rec->{$f}->[$i]); |
612 |
|
|
613 |
|
{ |
614 |
no strict 'refs'; |
no strict 'refs'; |
615 |
if ($sf && $$rec->{$f}->[$i]->{$sf}) { |
if (defined($sf)) { |
616 |
$$found++ if (defined($$found)); |
$$found++ if (defined($$found) && $$rec->{$f}->[$i]->{$sf}); |
617 |
return $$rec->{$f}->[$i]->{$sf}; |
return $$rec->{$f}->[$i]->{$sf}; |
618 |
} elsif (! $sf && $$rec->{$f}->[$i]) { |
} else { |
619 |
$$found++ if (defined($$found)); |
$$found++ if (defined($$found)); |
620 |
# it still might have subfield, just |
# it still might have subfields, just |
621 |
# not specified, so we'll dump all |
# not specified, so we'll dump some debug info |
622 |
if ($$rec->{$f}->[$i] =~ /HASH/o) { |
if ($$rec->{$f}->[$i] =~ /HASH/o) { |
623 |
my $out; |
my $out; |
624 |
foreach my $k (keys %{$$rec->{$f}->[$i]}) { |
foreach my $k (keys %{$$rec->{$f}->[$i]}) { |
625 |
$out .= $$rec->{$f}->[$i]->{$k}." "; |
$out .= '$' . $k .':' . $$rec->{$f}->[$i]->{$k}." "; |
626 |
} |
} |
627 |
return $out; |
return $out; |
628 |
} else { |
} else { |
629 |
return $$rec->{$f}->[$i]; |
return $$rec->{$f}->[$i]; |
630 |
} |
} |
|
} else { |
|
|
return ''; |
|
631 |
} |
} |
|
} else { |
|
|
return ''; |
|
632 |
} |
} |
633 |
} |
} |
634 |
|
|