2 |
use Exporter 'import'; |
use Exporter 'import'; |
3 |
@EXPORT = qw/ |
@EXPORT = qw/ |
4 |
_set_rec _set_lookup |
_set_rec _set_lookup |
5 |
|
_set_load_row |
6 |
_get_ds _clean_ds |
_get_ds _clean_ds |
7 |
_debug |
_debug |
8 |
_pack_subfields_hash |
_pack_subfields_hash |
9 |
|
|
10 |
tag search display |
search_display search display sorted |
11 |
|
|
12 |
marc marc_indicators marc_repeatable_subfield |
marc marc_indicators marc_repeatable_subfield |
13 |
marc_compose marc_leader |
marc_compose marc_leader marc_fixed |
14 |
marc_duplicate marc_remove |
marc_duplicate marc_remove marc_count |
15 |
marc_original_order |
marc_original_order |
16 |
|
|
17 |
rec1 rec2 rec |
rec1 rec2 rec |
20 |
save_into_lookup |
save_into_lookup |
21 |
|
|
22 |
split_rec_on |
split_rec_on |
23 |
|
|
24 |
|
get set |
25 |
|
count |
26 |
/; |
/; |
27 |
|
|
28 |
use warnings; |
use warnings; |
31 |
#use base qw/WebPAC::Common/; |
#use base qw/WebPAC::Common/; |
32 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
33 |
use Storable qw/dclone/; |
use Storable qw/dclone/; |
34 |
|
use Carp qw/confess/; |
35 |
|
|
36 |
# debugging warn(s) |
# debugging warn(s) |
37 |
my $debug = 0; |
my $debug = 0; |
41 |
|
|
42 |
WebPAC::Normalize - describe normalisaton rules using sets |
WebPAC::Normalize - describe normalisaton rules using sets |
43 |
|
|
|
=head1 VERSION |
|
|
|
|
|
Version 0.20 |
|
|
|
|
44 |
=cut |
=cut |
45 |
|
|
46 |
our $VERSION = '0.20'; |
our $VERSION = '0.31'; |
47 |
|
|
48 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
49 |
|
|
56 |
C<perl -c normalize.pl>. |
C<perl -c normalize.pl>. |
57 |
|
|
58 |
Normalisation can generate multiple output normalized data. For now, supported output |
Normalisation can generate multiple output normalized data. For now, supported output |
59 |
types (on the left side of definition) are: C<tag>, C<display>, C<search> and |
types (on the left side of definition) are: C<search_display>, C<display>, C<search> and |
60 |
C<marc>. |
C<marc>. |
61 |
|
|
62 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
69 |
Return data structure |
Return data structure |
70 |
|
|
71 |
my $ds = WebPAC::Normalize::data_structure( |
my $ds = WebPAC::Normalize::data_structure( |
72 |
lookup => $lookup_variable, |
lookup => $lookup_hash, |
73 |
row => $row, |
row => $row, |
74 |
rules => $normalize_pl_config, |
rules => $normalize_pl_config, |
75 |
marc_encoding => 'utf-8', |
marc_encoding => 'utf-8', |
76 |
config => $config, |
config => $config, |
77 |
|
load_row_coderef => sub { |
78 |
|
my ($database,$input,$mfn) = shift; |
79 |
|
$store->load_row( database => $database, input => $input, id => $mfn ); |
80 |
|
}, |
81 |
); |
); |
82 |
|
|
83 |
Options C<row>, C<rules> and C<log> are mandatory while all |
Options C<row>, C<rules> and C<log> are mandatory while all |
84 |
other are optional. |
other are optional. |
85 |
|
|
86 |
|
C<load_row_coderef> is closure only used when executing lookups, so they will |
87 |
|
die if it's not defined. |
88 |
|
|
89 |
This function will B<die> if normalizastion can't be evaled. |
This function will B<die> if normalizastion can't be evaled. |
90 |
|
|
91 |
Since this function isn't exported you have to call it with |
Since this function isn't exported you have to call it with |
93 |
|
|
94 |
=cut |
=cut |
95 |
|
|
96 |
|
my $load_row_coderef; |
97 |
|
|
98 |
sub data_structure { |
sub data_structure { |
99 |
my $arg = {@_}; |
my $arg = {@_}; |
100 |
|
|
102 |
die "need normalisation argument" unless ($arg->{rules}); |
die "need normalisation argument" unless ($arg->{rules}); |
103 |
|
|
104 |
no strict 'subs'; |
no strict 'subs'; |
105 |
_set_lookup( $arg->{lookup} ) if (defined( $arg->{lookup} )); |
_set_lookup( $arg->{lookup} ) if defined($arg->{lookup}); |
106 |
_set_rec( $arg->{row} ); |
_set_rec( $arg->{row} ); |
107 |
_set_config( $arg->{config} ) if (defined( $arg->{config} )); |
_set_config( $arg->{config} ) if defined($arg->{config}); |
108 |
_clean_ds( %{ $arg } ); |
_clean_ds( %{ $arg } ); |
109 |
|
$load_row_coderef = $arg->{load_row_coderef}; |
110 |
|
|
111 |
eval "$arg->{rules}"; |
eval "$arg->{rules}"; |
112 |
die "error evaling $arg->{rules}: $@\n" if ($@); |
die "error evaling $arg->{rules}: $@\n" if ($@); |
113 |
|
|
164 |
|
|
165 |
=cut |
=cut |
166 |
|
|
167 |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader); |
168 |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
169 |
|
|
170 |
sub _get_ds { |
sub _get_ds { |
181 |
|
|
182 |
sub _clean_ds { |
sub _clean_ds { |
183 |
my $a = {@_}; |
my $a = {@_}; |
184 |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader) = (); |
185 |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
186 |
$marc_encoding = $a->{marc_encoding}; |
$marc_encoding = $a->{marc_encoding}; |
187 |
} |
} |
212 |
return $lookup; |
return $lookup; |
213 |
} |
} |
214 |
|
|
215 |
|
=head2 _set_load_row |
216 |
|
|
217 |
|
Setup code reference which will return L<data_structure> from |
218 |
|
L<WebPAC::Store> |
219 |
|
|
220 |
|
_set_load_row(sub { |
221 |
|
my ($database,$input,$mfn) = @_; |
222 |
|
$store->load_row( database => $database, input => $input, id => $mfn ); |
223 |
|
}); |
224 |
|
|
225 |
|
=cut |
226 |
|
|
227 |
|
sub _set_load_row { |
228 |
|
my $coderef = shift; |
229 |
|
confess "argument isn't CODE" unless ref($coderef) eq 'CODE'; |
230 |
|
|
231 |
|
$load_row_coderef = $coderef; |
232 |
|
} |
233 |
|
|
234 |
=head2 _get_marc_fields |
=head2 _get_marc_fields |
235 |
|
|
236 |
Get all fields defined by calls to C<marc> |
Get all fields defined by calls to C<marc> |
286 |
|
|
287 |
=cut |
=cut |
288 |
|
|
289 |
|
my $fetch_pos; |
290 |
|
|
291 |
sub _get_marc_fields { |
sub _get_marc_fields { |
292 |
|
|
293 |
my $arg = {@_}; |
my $arg = {@_}; |
294 |
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
295 |
my $offset = $marc_fetch_offset; |
$fetch_pos = $marc_fetch_offset; |
296 |
if ($arg->{offset}) { |
if ($arg->{offset}) { |
297 |
$offset = $arg->{offset}; |
$fetch_pos = $arg->{offset}; |
298 |
} elsif($arg->{fetch_next}) { |
} elsif($arg->{fetch_next}) { |
299 |
$marc_fetch_offset++; |
$marc_fetch_offset++; |
300 |
} |
} |
303 |
|
|
304 |
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
305 |
|
|
306 |
my $marc_rec = $marc_record->[ $offset ]; |
my $marc_rec = $marc_record->[ $fetch_pos ]; |
307 |
|
|
308 |
warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
warn "## _get_marc_fields (at offset: $fetch_pos) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
309 |
|
|
310 |
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
311 |
|
|
326 |
|
|
327 |
if ($debug) { |
if ($debug) { |
328 |
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
329 |
warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; |
warn "## marc_record[$fetch_pos] = ", dump( $marc_rec ), $/; |
330 |
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
331 |
warn "## subfield count = ", dump( $u ), $/; |
warn "## subfield count = ", dump( $u ), $/; |
332 |
} |
} |
407 |
return \@m; |
return \@m; |
408 |
} |
} |
409 |
|
|
410 |
|
=head2 _get_marc_leader |
411 |
|
|
412 |
|
Return leader from currently fetched record by L</_get_marc_fields> |
413 |
|
|
414 |
|
print WebPAC::Normalize::_get_marc_leader(); |
415 |
|
|
416 |
|
=cut |
417 |
|
|
418 |
|
sub _get_marc_leader { |
419 |
|
die "no fetch_pos, did you called _get_marc_fields first?" unless ( defined( $fetch_pos ) ); |
420 |
|
return $marc_leader->[ $fetch_pos ]; |
421 |
|
} |
422 |
|
|
423 |
=head2 _debug |
=head2 _debug |
424 |
|
|
425 |
Change level of debug warnings |
Change level of debug warnings |
439 |
|
|
440 |
Those functions generally have to first in your normalization file. |
Those functions generally have to first in your normalization file. |
441 |
|
|
442 |
=head2 tag |
=head2 search_display |
443 |
|
|
444 |
Define new tag for I<search> and I<display>. |
Define output for L<search> and L<display> at the same time |
445 |
|
|
446 |
tag('Title', rec('200','a') ); |
search_display('Title', rec('200','a') ); |
447 |
|
|
448 |
|
|
449 |
=cut |
=cut |
450 |
|
|
451 |
sub tag { |
sub search_display { |
452 |
my $name = shift or die "tag needs name as first argument"; |
my $name = shift or die "search_display needs name as first argument"; |
453 |
my @o = grep { defined($_) && $_ ne '' } @_; |
my @o = grep { defined($_) && $_ ne '' } @_; |
454 |
return unless (@o); |
return unless (@o); |
|
$out->{$name}->{tag} = $name; |
|
455 |
$out->{$name}->{search} = \@o; |
$out->{$name}->{search} = \@o; |
456 |
$out->{$name}->{display} = \@o; |
$out->{$name}->{display} = \@o; |
457 |
} |
} |
458 |
|
|
459 |
|
=head2 tag |
460 |
|
|
461 |
|
Old name for L<search_display>, but supported |
462 |
|
|
463 |
|
=cut |
464 |
|
|
465 |
|
sub tag { |
466 |
|
search_display( @_ ); |
467 |
|
} |
468 |
|
|
469 |
=head2 display |
=head2 display |
470 |
|
|
471 |
Define tag just for I<display> |
Define output just for I<display> |
472 |
|
|
473 |
@v = display('Title', rec('200','a') ); |
@v = display('Title', rec('200','a') ); |
474 |
|
|
475 |
=cut |
=cut |
476 |
|
|
477 |
sub display { |
sub _field { |
478 |
my $name = shift or die "display needs name as first argument"; |
my $type = shift or confess "need type -- BUG?"; |
479 |
|
my $name = shift or confess "needs name as first argument"; |
480 |
my @o = grep { defined($_) && $_ ne '' } @_; |
my @o = grep { defined($_) && $_ ne '' } @_; |
481 |
return unless (@o); |
return unless (@o); |
482 |
$out->{$name}->{tag} = $name; |
$out->{$name}->{$type} = \@o; |
|
$out->{$name}->{display} = \@o; |
|
483 |
} |
} |
484 |
|
|
485 |
|
sub display { _field( 'display', @_ ) } |
486 |
|
|
487 |
=head2 search |
=head2 search |
488 |
|
|
489 |
Prepare values just for I<search> |
Prepare values just for I<search> |
492 |
|
|
493 |
=cut |
=cut |
494 |
|
|
495 |
sub search { |
sub search { _field( 'search', @_ ) } |
496 |
my $name = shift or die "search needs name as first argument"; |
|
497 |
my @o = grep { defined($_) && $_ ne '' } @_; |
=head2 sorted |
498 |
return unless (@o); |
|
499 |
$out->{$name}->{tag} = $name; |
Insert into lists which will be automatically sorted |
500 |
$out->{$name}->{search} = \@o; |
|
501 |
} |
sorted('Title', rec('200','a') ); |
502 |
|
|
503 |
|
=cut |
504 |
|
|
505 |
|
sub sorted { _field( 'sorted', @_ ) } |
506 |
|
|
507 |
|
|
508 |
=head2 marc_leader |
=head2 marc_leader |
509 |
|
|
518 |
my ($offset,$value) = @_; |
my ($offset,$value) = @_; |
519 |
|
|
520 |
if ($offset) { |
if ($offset) { |
521 |
$out->{' leader'}->{ $offset } = $value; |
$marc_leader->[ $marc_record_offset ]->{ $offset } = $value; |
522 |
} else { |
} else { |
523 |
return $out->{' leader'}; |
|
524 |
|
if (defined($marc_leader)) { |
525 |
|
die "marc_leader not array = ", dump( $marc_leader ) unless (ref($marc_leader) eq 'ARRAY'); |
526 |
|
return $marc_leader->[ $marc_record_offset ]; |
527 |
|
} else { |
528 |
|
return; |
529 |
|
} |
530 |
|
} |
531 |
|
} |
532 |
|
|
533 |
|
=head2 marc_fixed |
534 |
|
|
535 |
|
Create control/indentifier fields with values in fixed positions |
536 |
|
|
537 |
|
marc_fixed('008', 00, '070402'); |
538 |
|
marc_fixed('008', 39, '|'); |
539 |
|
|
540 |
|
Positions not specified will be filled with spaces (C<0x20>). |
541 |
|
|
542 |
|
There will be no effort to extend last specified value to full length of |
543 |
|
field in standard. |
544 |
|
|
545 |
|
=cut |
546 |
|
|
547 |
|
sub marc_fixed { |
548 |
|
my ($f, $pos, $val) = @_; |
549 |
|
die "need marc(field, position, value)" unless defined($f) && defined($pos); |
550 |
|
|
551 |
|
confess "need val" unless defined $val; |
552 |
|
|
553 |
|
my $update = 0; |
554 |
|
|
555 |
|
map { |
556 |
|
if ($_->[0] eq $f) { |
557 |
|
my $old = $_->[1]; |
558 |
|
if (length($old) <= $pos) { |
559 |
|
$_->[1] .= ' ' x ( $pos - length($old) ) . $val; |
560 |
|
warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1); |
561 |
|
} else { |
562 |
|
$_->[1] = substr($old, 0, $pos) . $val . substr($old, $pos + length($val)); |
563 |
|
warn "## marc_fixed($f,$pos,'$val') update '$old' -> '$_->[1]'\n" if ($debug > 1); |
564 |
|
} |
565 |
|
$update++; |
566 |
|
} |
567 |
|
} @{ $marc_record->[ $marc_record_offset ] }; |
568 |
|
|
569 |
|
if (! $update) { |
570 |
|
my $v = ' ' x $pos . $val; |
571 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ]; |
572 |
|
warn "## marc_fixed($f,$pos,'val') created '$v'\n" if ($debug > 1); |
573 |
} |
} |
574 |
} |
} |
575 |
|
|
701 |
my $m = $marc_record->[ -1 ]; |
my $m = $marc_record->[ -1 ]; |
702 |
die "can't duplicate record which isn't defined" unless ($m); |
die "can't duplicate record which isn't defined" unless ($m); |
703 |
push @{ $marc_record }, dclone( $m ); |
push @{ $marc_record }, dclone( $m ); |
704 |
warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); |
push @{ $marc_leader }, dclone( marc_leader() ); |
705 |
|
warn "## marc_duplicate = ", dump(@$marc_leader, @$marc_record), $/ if ($debug > 1); |
706 |
$marc_record_offset = $#{ $marc_record }; |
$marc_record_offset = $#{ $marc_record }; |
707 |
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
708 |
|
|
709 |
} |
} |
710 |
|
|
711 |
=head2 marc_remove |
=head2 marc_remove |
717 |
|
|
718 |
This will erase field C<200> or C<200^a> from current MARC record. |
This will erase field C<200> or C<200^a> from current MARC record. |
719 |
|
|
720 |
|
marc_remove('*'); |
721 |
|
|
722 |
|
Will remove all fields in current MARC record. |
723 |
|
|
724 |
This is useful after calling C<marc_duplicate> or on it's own (but, you |
This is useful after calling C<marc_duplicate> or on it's own (but, you |
725 |
should probably just remove that subfield definition if you are not |
should probably just remove that subfield definition if you are not |
726 |
using C<marc_duplicate>). |
using C<marc_duplicate>). |
738 |
|
|
739 |
warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); |
warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); |
740 |
|
|
741 |
my $i = 0; |
if ($f eq '*') { |
742 |
foreach ( 0 .. $#{ $marc } ) { |
|
743 |
last unless (defined $marc->[$i]); |
delete( $marc_record->[ $marc_record_offset ] ); |
744 |
warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); |
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
745 |
if ($marc->[$i]->[0] eq $f) { |
|
746 |
if (! defined $sf) { |
} else { |
747 |
# remove whole field |
|
748 |
splice @$marc, $i, 1; |
my $i = 0; |
749 |
warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); |
foreach ( 0 .. $#{ $marc } ) { |
750 |
$i--; |
last unless (defined $marc->[$i]); |
751 |
} else { |
warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); |
752 |
foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { |
if ($marc->[$i]->[0] eq $f) { |
753 |
my $o = ($j * 2) + 3; |
if (! defined $sf) { |
754 |
if ($marc->[$i]->[$o] eq $sf) { |
# remove whole field |
755 |
# remove subfield |
splice @$marc, $i, 1; |
756 |
splice @{$marc->[$i]}, $o, 2; |
warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); |
757 |
warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); |
$i--; |
758 |
# is record now empty? |
} else { |
759 |
if ($#{ $marc->[$i] } == 2) { |
foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { |
760 |
splice @$marc, $i, 1; |
my $o = ($j * 2) + 3; |
761 |
warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); |
if ($marc->[$i]->[$o] eq $sf) { |
762 |
$i--; |
# remove subfield |
763 |
}; |
splice @{$marc->[$i]}, $o, 2; |
764 |
|
warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); |
765 |
|
# is record now empty? |
766 |
|
if ($#{ $marc->[$i] } == 2) { |
767 |
|
splice @$marc, $i, 1; |
768 |
|
warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); |
769 |
|
$i--; |
770 |
|
}; |
771 |
|
} |
772 |
} |
} |
773 |
} |
} |
774 |
} |
} |
775 |
|
$i++; |
776 |
} |
} |
|
$i++; |
|
|
} |
|
777 |
|
|
778 |
warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); |
warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); |
779 |
|
|
780 |
$marc_record->[ $marc_record_offset ] = $marc; |
$marc_record->[ $marc_record_offset ] = $marc; |
781 |
|
} |
782 |
|
|
783 |
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
784 |
} |
} |
853 |
warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); |
warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); |
854 |
} |
} |
855 |
|
|
856 |
|
=head2 marc_count |
857 |
|
|
858 |
|
Return number of MARC records created using L</marc_duplicate>. |
859 |
|
|
860 |
|
print "created ", marc_count(), " records"; |
861 |
|
|
862 |
|
=cut |
863 |
|
|
864 |
|
sub marc_count { |
865 |
|
return $#{ $marc_record }; |
866 |
|
} |
867 |
|
|
868 |
|
|
869 |
=head1 Functions to extract data from input |
=head1 Functions to extract data from input |
870 |
|
|
887 |
|
|
888 |
my ($h,$include_subfields) = @_; |
my ($h,$include_subfields) = @_; |
889 |
|
|
890 |
|
# sanity and ease of use |
891 |
|
return $h if (ref($h) ne 'HASH'); |
892 |
|
|
893 |
if ( defined($h->{subfields}) ) { |
if ( defined($h->{subfields}) ) { |
894 |
my $sfs = delete $h->{subfields} || die "no subfields?"; |
my $sfs = delete $h->{subfields} || die "no subfields?"; |
895 |
my @out; |
my @out; |
989 |
@v = rec('200') |
@v = rec('200') |
990 |
@v = rec('200','a') |
@v = rec('200','a') |
991 |
|
|
992 |
|
If rec() returns just single value, it will |
993 |
|
return scalar, not array. |
994 |
|
|
995 |
=cut |
=cut |
996 |
|
|
997 |
sub rec { |
sub rec { |
1001 |
} elsif ($#_ == 1) { |
} elsif ($#_ == 1) { |
1002 |
@out = rec2(@_); |
@out = rec2(@_); |
1003 |
} |
} |
1004 |
if (@out) { |
if ($#out == 0 && ! wantarray) { |
1005 |
|
return $out[0]; |
1006 |
|
} elsif (@out) { |
1007 |
return @out; |
return @out; |
1008 |
} else { |
} else { |
1009 |
return ''; |
return ''; |
1039 |
=cut |
=cut |
1040 |
|
|
1041 |
sub prefix { |
sub prefix { |
1042 |
my $p = shift or return; |
my $p = shift; |
1043 |
|
return @_ unless defined( $p ); |
1044 |
return map { $p . $_ } grep { defined($_) } @_; |
return map { $p . $_ } grep { defined($_) } @_; |
1045 |
} |
} |
1046 |
|
|
1053 |
=cut |
=cut |
1054 |
|
|
1055 |
sub suffix { |
sub suffix { |
1056 |
my $s = shift or die "suffix needs string as first argument"; |
my $s = shift; |
1057 |
|
return @_ unless defined( $s ); |
1058 |
return map { $_ . $s } grep { defined($_) } @_; |
return map { $_ . $s } grep { defined($_) } @_; |
1059 |
} |
} |
1060 |
|
|
1067 |
=cut |
=cut |
1068 |
|
|
1069 |
sub surround { |
sub surround { |
1070 |
my $p = shift or die "surround need prefix as first argument"; |
my $p = shift; |
1071 |
my $s = shift or die "surround needs suffix as second argument"; |
my $s = shift; |
1072 |
|
$p = '' unless defined( $p ); |
1073 |
|
$s = '' unless defined( $s ); |
1074 |
return map { $p . $_ . $s } grep { defined($_) } @_; |
return map { $p . $_ . $s } grep { defined($_) } @_; |
1075 |
} |
} |
1076 |
|
|
1091 |
|
|
1092 |
Consult lookup hashes for some value |
Consult lookup hashes for some value |
1093 |
|
|
1094 |
@v = lookup( $v ); |
@v = lookup( |
1095 |
@v = lookup( @v ); |
sub { |
1096 |
|
'ffkk/peri/mfn'.rec('000') |
1097 |
|
}, |
1098 |
|
'ffkk','peri','200-a-200-e', |
1099 |
|
sub { |
1100 |
|
first(rec(200,'a')).' '.first(rec('200','e')) |
1101 |
|
} |
1102 |
|
); |
1103 |
|
|
1104 |
|
Code like above will be B<automatically generated> using L<WebPAC::Parse> from |
1105 |
|
normal lookup definition in C<conf/lookup/something.pl> which looks like: |
1106 |
|
|
1107 |
|
lookup( |
1108 |
|
# which results to return from record recorded in lookup |
1109 |
|
sub { 'ffkk/peri/mfn' . rec('000') }, |
1110 |
|
# from which database and input |
1111 |
|
'ffkk','peri', |
1112 |
|
# such that following values match |
1113 |
|
sub { first(rec(200,'a')) . ' ' . first(rec('200','e')) }, |
1114 |
|
# if this part is missing, we will try to match same fields |
1115 |
|
# from lookup record and current one, or you can override |
1116 |
|
# which records to use from current record using |
1117 |
|
sub { rec('900','x') . ' ' . rec('900','y') }, |
1118 |
|
) |
1119 |
|
|
1120 |
|
You can think about this lookup as SQL (if that helps): |
1121 |
|
|
1122 |
|
select |
1123 |
|
sub { what } |
1124 |
|
from |
1125 |
|
database, input |
1126 |
|
where |
1127 |
|
sub { filter from lookuped record } |
1128 |
|
having |
1129 |
|
sub { optional filter on current record } |
1130 |
|
|
1131 |
FIXME B<currently this one is broken!> |
Easy as pie, right? |
1132 |
|
|
1133 |
=cut |
=cut |
1134 |
|
|
1135 |
sub lookup { |
sub lookup { |
1136 |
my $k = shift or return; |
my ($what, $database, $input, $key, $having) = @_; |
1137 |
return unless (defined($lookup->{$k})); |
|
1138 |
if (ref($lookup->{$k}) eq 'ARRAY') { |
confess "lookup needs 5 arguments: what, database, input, key, having\n" unless ($#_ == 4); |
1139 |
return @{ $lookup->{$k} }; |
|
1140 |
|
warn "## lookup ($database, $input, $key)", $/ if ($debug > 1); |
1141 |
|
return unless (defined($lookup->{$database}->{$input}->{$key})); |
1142 |
|
|
1143 |
|
confess "lookup really need load_row_coderef added to data_structure\n" unless ($load_row_coderef); |
1144 |
|
|
1145 |
|
my $mfns; |
1146 |
|
my @having = $having->(); |
1147 |
|
|
1148 |
|
warn "## having = ", dump( @having ) if ($debug > 2); |
1149 |
|
|
1150 |
|
foreach my $h ( @having ) { |
1151 |
|
if (defined($lookup->{$database}->{$input}->{$key}->{$h})) { |
1152 |
|
warn "lookup for $database/$input/$key/$h return ",dump($lookup->{$database}->{$input}->{$key}->{$h}),"\n" if ($debug); |
1153 |
|
$mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} }; |
1154 |
|
} |
1155 |
|
} |
1156 |
|
|
1157 |
|
return unless ($mfns); |
1158 |
|
|
1159 |
|
my @mfns = sort keys %$mfns; |
1160 |
|
|
1161 |
|
warn "# lookup loading $database/$input/$key mfn ", join(",",@mfns)," having ",dump(@having),"\n" if ($debug); |
1162 |
|
|
1163 |
|
my $old_rec = $rec; |
1164 |
|
my @out; |
1165 |
|
|
1166 |
|
foreach my $mfn (@mfns) { |
1167 |
|
$rec = $load_row_coderef->( $database, $input, $mfn ); |
1168 |
|
|
1169 |
|
warn "got $database/$input/$mfn = ", dump($rec), $/ if ($debug); |
1170 |
|
|
1171 |
|
my @vals = $what->(); |
1172 |
|
|
1173 |
|
push @out, ( @vals ); |
1174 |
|
|
1175 |
|
warn "lookup for mfn $mfn returned ", dump(@vals), $/ if ($debug); |
1176 |
|
} |
1177 |
|
|
1178 |
|
# if (ref($lookup->{$k}) eq 'ARRAY') { |
1179 |
|
# return @{ $lookup->{$k} }; |
1180 |
|
# } else { |
1181 |
|
# return $lookup->{$k}; |
1182 |
|
# } |
1183 |
|
|
1184 |
|
$rec = $old_rec; |
1185 |
|
|
1186 |
|
warn "## lookup returns = ", dump(@out), $/ if ($debug); |
1187 |
|
|
1188 |
|
if ($#out == 0) { |
1189 |
|
return $out[0]; |
1190 |
} else { |
} else { |
1191 |
return $lookup->{$k}; |
return @out; |
1192 |
} |
} |
1193 |
} |
} |
1194 |
|
|
1195 |
=head2 save_into_lookup |
=head2 save_into_lookup |
1196 |
|
|
1197 |
Save value into lookup. |
Save value into lookup. It associates current database, input |
1198 |
|
and specific keys with one or more values which will be |
1199 |
|
associated over MFN. |
1200 |
|
|
1201 |
save_into_lookup($database,$input,$key,sub { |
MFN will be extracted from first occurence current of field 000 |
1202 |
|
in current record, or if it doesn't exist from L<_set_config> C<_mfn>. |
1203 |
|
|
1204 |
|
my $nr = save_into_lookup($database,$input,$key,sub { |
1205 |
# code which produce one or more values |
# code which produce one or more values |
1206 |
}); |
}); |
1207 |
|
|
1208 |
This function shouldn't be called directly, it's called from code created by L<WebPAC::Parser>. |
It returns number of items saved. |
1209 |
|
|
1210 |
|
This function shouldn't be called directly, it's called from code created by |
1211 |
|
L<WebPAC::Parser>. |
1212 |
|
|
1213 |
=cut |
=cut |
1214 |
|
|
1218 |
die "save_into_lookup needs input" unless defined($input); |
die "save_into_lookup needs input" unless defined($input); |
1219 |
die "save_into_lookup needs key" unless defined($key); |
die "save_into_lookup needs key" unless defined($key); |
1220 |
die "save_into_lookup needs CODE" unless ( defined($coderef) && ref($coderef) eq 'CODE' ); |
die "save_into_lookup needs CODE" unless ( defined($coderef) && ref($coderef) eq 'CODE' ); |
1221 |
my $mfn = $rec->{'000'}->[0] || die "mfn not defined or zero"; |
|
1222 |
|
warn "## save_into_lookup rec = ", dump($rec), " config = ", dump($config), $/ if ($debug > 2); |
1223 |
|
|
1224 |
|
my $mfn = |
1225 |
|
defined($rec->{'000'}->[0]) ? $rec->{'000'}->[0] : |
1226 |
|
defined($config->{_mfn}) ? $config->{_mfn} : |
1227 |
|
die "mfn not defined or zero"; |
1228 |
|
|
1229 |
|
my $nr = 0; |
1230 |
|
|
1231 |
foreach my $v ( $coderef->() ) { |
foreach my $v ( $coderef->() ) { |
1232 |
$lookup->{$database}->{$input}->{$key}->{$v}->{$mfn}++; |
$lookup->{$database}->{$input}->{$key}->{$v}->{$mfn}++; |
1233 |
warn "# saved lookup $database/$input/$key [$v] $mfn\n"; #if ($debug > 1); |
warn "# saved lookup $database/$input/$key [$v] $mfn\n" if ($debug > 1); |
1234 |
|
$nr++; |
1235 |
} |
} |
1236 |
|
|
1237 |
|
return $nr; |
1238 |
} |
} |
1239 |
|
|
1240 |
=head2 config |
=head2 config |
1245 |
$database_code = config(); # use _ from hash |
$database_code = config(); # use _ from hash |
1246 |
$database_name = config('name'); |
$database_name = config('name'); |
1247 |
$database_input_name = config('input name'); |
$database_input_name = config('input name'); |
|
$tag = config('input normalize tag'); |
|
1248 |
|
|
1249 |
Up to three levels are supported. |
Up to three levels are supported. |
1250 |
|
|
1360 |
} |
} |
1361 |
} |
} |
1362 |
|
|
1363 |
|
my $hash; |
1364 |
|
|
1365 |
|
=head2 set |
1366 |
|
|
1367 |
|
set( key => 'value' ); |
1368 |
|
|
1369 |
|
=cut |
1370 |
|
|
1371 |
|
sub set { |
1372 |
|
my ($k,$v) = @_; |
1373 |
|
warn "## set ( $k => ", dump($v), " )", $/ if ( $debug ); |
1374 |
|
$hash->{$k} = $v; |
1375 |
|
}; |
1376 |
|
|
1377 |
|
=head2 get |
1378 |
|
|
1379 |
|
get( 'key' ); |
1380 |
|
|
1381 |
|
=cut |
1382 |
|
|
1383 |
|
sub get { |
1384 |
|
my $k = shift || return; |
1385 |
|
my $v = $hash->{$k}; |
1386 |
|
warn "## get $k = ", dump( $v ), $/ if ( $debug ); |
1387 |
|
return $v; |
1388 |
|
} |
1389 |
|
|
1390 |
|
=head2 count |
1391 |
|
|
1392 |
|
if ( count( @result ) == 1 ) { |
1393 |
|
# do something if only 1 result is there |
1394 |
|
} |
1395 |
|
|
1396 |
|
=cut |
1397 |
|
|
1398 |
|
sub count { |
1399 |
|
warn "## count ",dump(@_),$/ if ( $debug ); |
1400 |
|
return @_ . ''; |
1401 |
|
} |
1402 |
|
|
1403 |
# END |
# END |
1404 |
1; |
1; |