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 |
27 |
#use base qw/WebPAC::Common/; |
#use base qw/WebPAC::Common/; |
28 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
29 |
use Storable qw/dclone/; |
use Storable qw/dclone/; |
30 |
|
use Carp qw/confess/; |
31 |
|
|
32 |
# debugging warn(s) |
# debugging warn(s) |
33 |
my $debug = 0; |
my $debug = 0; |
39 |
|
|
40 |
=head1 VERSION |
=head1 VERSION |
41 |
|
|
42 |
Version 0.20 |
Version 0.23 |
43 |
|
|
44 |
=cut |
=cut |
45 |
|
|
46 |
our $VERSION = '0.20'; |
our $VERSION = '0.23'; |
47 |
|
|
48 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
49 |
|
|
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, $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, $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> |
487 |
my ($offset,$value) = @_; |
my ($offset,$value) = @_; |
488 |
|
|
489 |
if ($offset) { |
if ($offset) { |
490 |
$out->{' leader'}->{ $offset } = $value; |
$leader->{ $offset } = $value; |
491 |
} else { |
} else { |
492 |
return $out->{' leader'}; |
return $leader; |
493 |
} |
} |
494 |
} |
} |
495 |
|
|
880 |
@v = rec('200') |
@v = rec('200') |
881 |
@v = rec('200','a') |
@v = rec('200','a') |
882 |
|
|
883 |
|
If rec() returns just single value, it will |
884 |
|
return scalar, not array. |
885 |
|
|
886 |
=cut |
=cut |
887 |
|
|
888 |
sub rec { |
sub rec { |
892 |
} elsif ($#_ == 1) { |
} elsif ($#_ == 1) { |
893 |
@out = rec2(@_); |
@out = rec2(@_); |
894 |
} |
} |
895 |
if (@out) { |
if ($#out == 0 && ! wantarray) { |
896 |
|
return $out[0]; |
897 |
|
} elsif (@out) { |
898 |
return @out; |
return @out; |
899 |
} else { |
} else { |
900 |
return ''; |
return ''; |
978 |
|
|
979 |
Consult lookup hashes for some value |
Consult lookup hashes for some value |
980 |
|
|
981 |
@v = lookup( $v ); |
@v = lookup( |
982 |
@v = lookup( @v ); |
sub { |
983 |
|
'ffkk/peri/mfn'.rec('000') |
984 |
|
}, |
985 |
|
'ffkk','peri','200-a-200-e', |
986 |
|
sub { |
987 |
|
first(rec(200,'a')).' '.first(rec('200','e')) |
988 |
|
} |
989 |
|
); |
990 |
|
|
991 |
|
Code like above will be B<automatically generated> using L<WebPAC::Parse> from |
992 |
|
normal lookup definition in C<conf/lookup/something.pl> which looks like: |
993 |
|
|
994 |
|
lookup( |
995 |
|
# which results to return from record recorded in lookup |
996 |
|
sub { 'ffkk/peri/mfn' . rec('000') }, |
997 |
|
# from which database and input |
998 |
|
'ffkk','peri', |
999 |
|
# such that following values match |
1000 |
|
sub { first(rec(200,'a')) . ' ' . first(rec('200','e')) }, |
1001 |
|
# if this part is missing, we will try to match same fields |
1002 |
|
# from lookup record and current one, or you can override |
1003 |
|
# which records to use from current record using |
1004 |
|
sub { rec('900','x') . ' ' . rec('900','y') }, |
1005 |
|
) |
1006 |
|
|
1007 |
|
You can think about this lookup as SQL (if that helps): |
1008 |
|
|
1009 |
|
select |
1010 |
|
sub { what } |
1011 |
|
from |
1012 |
|
database, input |
1013 |
|
where |
1014 |
|
sub { filter from lookuped record } |
1015 |
|
having |
1016 |
|
sub { optional filter on current record } |
1017 |
|
|
1018 |
FIXME B<currently this one is broken!> |
Easy as pie, right? |
1019 |
|
|
1020 |
=cut |
=cut |
1021 |
|
|
1022 |
sub lookup { |
sub lookup { |
1023 |
my $k = shift or return; |
my ($what, $database, $input, $key, $having) = @_; |
1024 |
return unless (defined($lookup->{$k})); |
|
1025 |
if (ref($lookup->{$k}) eq 'ARRAY') { |
confess "lookup needs 5 arguments: what, database, input, key, having" unless ($#_ == 4); |
1026 |
return @{ $lookup->{$k} }; |
|
1027 |
|
warn "## lookup ($database, $input, $key)", $/ if ($debug > 1); |
1028 |
|
return unless (defined($lookup->{$database}->{$input}->{$key})); |
1029 |
|
|
1030 |
|
confess "lookup really need load_row_coderef added to data_structure\n" unless ($load_row_coderef); |
1031 |
|
|
1032 |
|
my $mfns; |
1033 |
|
my @having = $having->(); |
1034 |
|
|
1035 |
|
warn "## having = ", dump( @having ) if ($debug > 2); |
1036 |
|
|
1037 |
|
foreach my $h ( @having ) { |
1038 |
|
if (defined($lookup->{$database}->{$input}->{$key}->{$h})) { |
1039 |
|
warn "lookup for $database/$input/$key/$h return ",dump($lookup->{$database}->{$input}->{$key}->{$h}),"\n"; |
1040 |
|
$mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} }; |
1041 |
|
} |
1042 |
|
} |
1043 |
|
|
1044 |
|
return unless ($mfns); |
1045 |
|
|
1046 |
|
my @mfns = sort keys %$mfns; |
1047 |
|
|
1048 |
|
warn "# lookup loading $database/$input/$key mfn ", join(",",@mfns)," having ",dump(@having),"\n" if ($debug); |
1049 |
|
|
1050 |
|
my $old_rec = $rec; |
1051 |
|
my @out; |
1052 |
|
|
1053 |
|
foreach my $mfn (@mfns) { |
1054 |
|
$rec = $load_row_coderef->( $database, $input, $mfn ); |
1055 |
|
|
1056 |
|
warn "got $database/$input/$mfn = ", dump($rec), $/; |
1057 |
|
|
1058 |
|
my @vals = $what->(); |
1059 |
|
|
1060 |
|
push @out, ( @vals ); |
1061 |
|
|
1062 |
|
warn "lookup for mfn $mfn returned ", dump(@vals), $/; |
1063 |
|
} |
1064 |
|
|
1065 |
|
# if (ref($lookup->{$k}) eq 'ARRAY') { |
1066 |
|
# return @{ $lookup->{$k} }; |
1067 |
|
# } else { |
1068 |
|
# return $lookup->{$k}; |
1069 |
|
# } |
1070 |
|
|
1071 |
|
$rec = $old_rec; |
1072 |
|
|
1073 |
|
warn "## lookup returns = ", dump(@out), $/ if ($debug); |
1074 |
|
|
1075 |
|
if ($#out == 0) { |
1076 |
|
return $out[0]; |
1077 |
} else { |
} else { |
1078 |
return $lookup->{$k}; |
return @out; |
1079 |
} |
} |
1080 |
} |
} |
1081 |
|
|
1082 |
=head2 save_into_lookup |
=head2 save_into_lookup |
1083 |
|
|
1084 |
Save value into lookup. |
Save value into lookup. It associates current database, input |
1085 |
|
and specific keys with one or more values which will be |
1086 |
|
associated over MFN. |
1087 |
|
|
1088 |
|
MFN will be extracted from first occurence current of field 000 |
1089 |
|
in current record, or if it doesn't exist from L<_set_config> C<_mfn>. |
1090 |
|
|
1091 |
save_into_lookup($key,sub { |
my $nr = save_into_lookup($database,$input,$key,sub { |
1092 |
# code which produce one or more values |
# code which produce one or more values |
1093 |
}); |
}); |
1094 |
|
|
1095 |
This function shouldn't be called directly, it's called from code created by L<WebPAC::Parser>. |
It returns number of items saved. |
1096 |
|
|
1097 |
|
This function shouldn't be called directly, it's called from code created by |
1098 |
|
L<WebPAC::Parser>. |
1099 |
|
|
1100 |
=cut |
=cut |
1101 |
|
|
1102 |
sub save_into_lookup { |
sub save_into_lookup { |
1103 |
my ($k,$coderef) = @_; |
my ($database,$input,$key,$coderef) = @_; |
1104 |
die "save_into_lookup needs key" unless defined($k); |
die "save_into_lookup needs database" unless defined($database); |
1105 |
|
die "save_into_lookup needs input" unless defined($input); |
1106 |
|
die "save_into_lookup needs key" unless defined($key); |
1107 |
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' ); |
1108 |
my $mfn = $rec->{'000'}->[0] || die "mfn not defined or zero"; |
|
1109 |
|
warn "## save_into_lookup rec = ", dump($rec), " config = ", dump($config), $/ if ($debug > 2); |
1110 |
|
|
1111 |
|
my $mfn = |
1112 |
|
defined($rec->{'000'}->[0]) ? $rec->{'000'}->[0] : |
1113 |
|
defined($config->{_mfn}) ? $config->{_mfn} : |
1114 |
|
die "mfn not defined or zero"; |
1115 |
|
|
1116 |
|
my $nr = 0; |
1117 |
|
|
1118 |
foreach my $v ( $coderef->() ) { |
foreach my $v ( $coderef->() ) { |
1119 |
$lookup->{$k}->{$v}->{$mfn}++; |
$lookup->{$database}->{$input}->{$key}->{$v}->{$mfn}++; |
1120 |
warn "# lookup $k $v $mfn saved\n"; # if ($debug > 1); |
warn "# saved lookup $database/$input/$key [$v] $mfn\n" if ($debug > 1); |
1121 |
|
$nr++; |
1122 |
} |
} |
1123 |
|
|
1124 |
|
return $nr; |
1125 |
} |
} |
1126 |
|
|
1127 |
=head2 config |
=head2 config |