1 |
package WebPAC::Normalize; |
package WebPAC::Normalize; |
2 |
|
use Exporter 'import'; |
3 |
|
@EXPORT = qw/ |
4 |
|
_set_rec _set_lookup |
5 |
|
_get_ds _clean_ds |
6 |
|
|
7 |
|
tag search display |
8 |
|
marc marc_indicators marc_repeatable_subfield |
9 |
|
|
10 |
|
rec1 rec2 rec |
11 |
|
regex prefix suffix surround |
12 |
|
first lookup join_with |
13 |
|
/; |
14 |
|
|
15 |
use warnings; |
use warnings; |
16 |
use strict; |
use strict; |
17 |
use base 'WebPAC::Common'; |
|
18 |
|
#use base qw/WebPAC::Common/; |
19 |
use Data::Dumper; |
use Data::Dumper; |
20 |
|
use Encode qw/from_to/; |
21 |
|
|
22 |
=head1 NAME |
=head1 NAME |
23 |
|
|
24 |
WebPAC::Normalize - data mungling for normalisation |
WebPAC::Normalize - describe normalisaton rules using sets |
25 |
|
|
26 |
=head1 VERSION |
=head1 VERSION |
27 |
|
|
28 |
Version 0.07 |
Version 0.06 |
29 |
|
|
30 |
=cut |
=cut |
31 |
|
|
32 |
our $VERSION = '0.07'; |
our $VERSION = '0.06'; |
33 |
|
|
34 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
35 |
|
|
36 |
This package contains code that mungle data to produce normalized format. |
This module uses C<conf/normalize/*.pl> files to perform normalisation |
37 |
|
from input records using perl functions which are specialized for set |
38 |
It contains several assumptions: |
processing. |
39 |
|
|
40 |
=over |
Sets are implemented as arrays, and normalisation file is valid perl, which |
41 |
|
means that you check it's validity before running WebPAC using |
42 |
=item * |
C<perl -c normalize.pl>. |
43 |
|
|
44 |
format of fields is defined using C<v123^a> notation for repeatable fields |
Normalisation can generate multiple output normalized data. For now, supported output |
45 |
or C<s123^a> for single (or first) value, where C<123> is field number and |
types (on the left side of definition) are: C<tag>, C<display>, C<search> and |
46 |
C<a> is subfield. |
C<marc>. |
|
|
|
|
=item * |
|
|
|
|
|
source data records (C<$rec>) have unique identifiers in field C<000> |
|
|
|
|
|
=item * |
|
|
|
|
|
optional C<eval{length('v123^a') == 3}> tag at B<beginning of format> will be |
|
|
perl code that is evaluated before producing output (value of field will be |
|
|
interpolated before that) |
|
|
|
|
|
=item * |
|
|
|
|
|
optional C<filter{filter_name}> at B<begining of format> will apply perl |
|
|
code defined as code ref on format after field substitution to producing |
|
|
output |
|
|
|
|
|
There is one built-in filter called C<regex> which can be use like this: |
|
|
|
|
|
filter{regex(s/foo/bar/)} |
|
|
|
|
|
=item * |
|
|
|
|
|
optional C<lookup{...}> will be then performed. See C<WebPAC::Lookups>. |
|
|
|
|
|
=item * |
|
|
|
|
|
at end, optional C<format>s rules are resolved. Format rules are similar to |
|
|
C<sprintf> and can also contain C<lookup{...}> which is performed after |
|
|
values are inserted in format. |
|
|
|
|
|
=back |
|
|
|
|
|
This also describes order in which transformations are applied (eval, |
|
|
filter, lookup, format) which is important to undestand when deciding how to |
|
|
solve your data mungling and normalisation process. |
|
|
|
|
|
|
|
|
|
|
47 |
|
|
48 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
49 |
|
|
50 |
=head2 new |
Functions which start with C<_> are private and used by WebPAC internally. |
51 |
|
All other functions are available for use within normalisation rules. |
52 |
|
|
53 |
Create new normalisation object |
=head2 data_structure |
54 |
|
|
55 |
my $n = new WebPAC::Normalize::Something( |
Return data structure |
56 |
filter => { |
|
57 |
'filter_name_1' => sub { |
my $ds = WebPAC::Normalize::data_structure( |
58 |
# filter code |
lookup => $lookup->lookup_hash, |
59 |
return length($_); |
row => $row, |
60 |
}, ... |
rules => $normalize_pl_config, |
61 |
}, |
marc_encoding => 'utf-8', |
|
db => $db_obj, |
|
|
lookup_regex => $lookup->regex, |
|
|
lookup => $lookup_obj, |
|
|
prefix => 'foobar', |
|
62 |
); |
); |
63 |
|
|
64 |
Parametar C<filter> defines user supplied snippets of perl code which can |
Options C<lookup>, C<row>, C<rules> and C<log> are mandatory while all |
65 |
be use with C<filter{...}> notation. |
other are optional. |
66 |
|
|
67 |
C<prefix> is used to form filename for database record (to support multiple |
This function will B<die> if normalizastion can't be evaled. |
|
source files which are joined in one database). |
|
68 |
|
|
69 |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
Since this function isn't exported you have to call it with |
70 |
in structures. If you pass this parametar, you must also pass C<lookup> |
C<WebPAC::Normalize::data_structure>. |
|
which is C<WebPAC::Lookup> object. |
|
71 |
|
|
72 |
=cut |
=cut |
73 |
|
|
74 |
sub new { |
sub data_structure { |
75 |
my $class = shift; |
my $arg = {@_}; |
|
my $self = {@_}; |
|
|
bless($self, $class); |
|
|
|
|
|
my $r = $self->{'lookup_regex'} ? 1 : 0; |
|
|
my $l = $self->{'lookup'} ? 1 : 0; |
|
|
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
# those two must be in pair |
|
|
if ( ($r & $l) != ($r || $l) ) { |
|
|
my $log = $self->_get_logger(); |
|
|
$log->logdie("lookup_regex and lookup must be in pair"); |
|
|
} |
|
|
|
|
|
$log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup')); |
|
|
|
|
|
$log->warn("no prefix defined. please check that!") unless ($self->{'prefix'}); |
|
76 |
|
|
77 |
$log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l); |
die "need row argument" unless ($arg->{row}); |
78 |
|
die "need normalisation argument" unless ($arg->{rules}); |
79 |
|
|
80 |
if (! $self->{filter} || ! $self->{filter}->{regex}) { |
no strict 'subs'; |
81 |
$log->debug("adding built-in filter regex"); |
_set_lookup( $arg->{lookup} ); |
82 |
$self->{filter}->{regex} = sub { |
_set_rec( $arg->{row} ); |
83 |
my ($val, $regex) = @_; |
_clean_ds( %{ $arg } ); |
84 |
eval "\$val =~ $regex"; |
eval "$arg->{rules}"; |
85 |
return $val; |
die "error evaling $arg->{rules}: $@\n" if ($@); |
|
}; |
|
|
} |
|
86 |
|
|
87 |
$self ? return $self : return undef; |
return _get_ds(); |
88 |
} |
} |
89 |
|
|
90 |
|
=head2 _set_rec |
91 |
|
|
92 |
=head2 data_structure |
Set current record hash |
|
|
|
|
Create in-memory data structure which represents normalized layout from |
|
|
C<conf/normalize/*.xml>. |
|
|
|
|
|
This structures are used to produce output. |
|
93 |
|
|
94 |
my $ds = $webpac->data_structure($rec); |
_set_rec( $rec ); |
95 |
|
|
96 |
=cut |
=cut |
97 |
|
|
98 |
sub data_structure { |
my $rec; |
|
my $self = shift; |
|
99 |
|
|
100 |
my $log = $self->_get_logger(); |
sub _set_rec { |
101 |
|
$rec = shift or die "no record hash"; |
102 |
|
} |
103 |
|
|
104 |
my $rec = shift; |
=head2 _get_ds |
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
105 |
|
|
106 |
$log->debug("data_structure rec = ", sub { Dumper($rec) }); |
Return hash formatted as data structure |
107 |
|
|
108 |
$log->logdie("need unique ID (mfn) in field 000 of record ", sub { Dumper($rec) } ) unless (defined($rec->{'000'})); |
my $ds = _get_ds(); |
109 |
|
|
110 |
my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!"); |
=cut |
111 |
|
|
112 |
my $cache_file; |
my ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); |
113 |
|
|
114 |
if ($self->{'db'}) { |
sub _get_ds { |
115 |
my $ds = $self->{'db'}->load_ds( id => $id, prefix => $self->{prefix} ); |
return $out; |
116 |
$log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) }); |
} |
|
return $ds if ($ds); |
|
|
$log->debug("cache miss, creating"); |
|
|
} |
|
117 |
|
|
118 |
my @sorted_tags; |
=head2 _clean_ds |
|
if ($self->{tags_by_order}) { |
|
|
@sorted_tags = @{$self->{tags_by_order}}; |
|
|
} else { |
|
|
@sorted_tags = sort { $self->_sort_by_order } keys %{$self->{'import_xml'}->{'indexer'}}; |
|
|
$self->{tags_by_order} = \@sorted_tags; |
|
|
} |
|
119 |
|
|
120 |
my $ds; |
Clean data structure hash for next record |
121 |
|
|
122 |
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
_clean_ds(); |
123 |
|
|
124 |
foreach my $field (@sorted_tags) { |
=cut |
125 |
|
|
126 |
my $row; |
sub _clean_ds { |
127 |
|
my $a = {@_}; |
128 |
|
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (undef); |
129 |
|
$marc_encoding = $a->{marc_encoding}; |
130 |
|
} |
131 |
|
|
132 |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
=head2 _set_lookup |
133 |
|
|
134 |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
Set current lookup hash |
|
my $format; |
|
135 |
|
|
136 |
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
_set_lookup( $lookup ); |
|
$format = $tag->{'value'} || $tag->{'content'}; |
|
137 |
|
|
138 |
$log->debug("format: $format"); |
=cut |
139 |
|
|
140 |
my @v; |
my $lookup; |
|
if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { |
|
|
@v = $self->fill_in_to_arr($rec,$format); |
|
|
} else { |
|
|
@v = $self->parse_to_arr($rec,$format); |
|
|
} |
|
|
next if (! @v); |
|
141 |
|
|
142 |
if ($tag->{'sort'}) { |
sub _set_lookup { |
143 |
@v = $self->sort_arr(@v); |
$lookup = shift; |
144 |
} |
} |
145 |
|
|
146 |
# use format? |
=head2 _get_marc_fields |
|
if ($tag->{'format_name'}) { |
|
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
|
|
} |
|
147 |
|
|
148 |
# delimiter will join repeatable fields |
Get all fields defined by calls to C<marc> |
|
if ($tag->{'delimiter'}) { |
|
|
@v = ( join($tag->{'delimiter'}, @v) ); |
|
|
} |
|
149 |
|
|
150 |
# default types |
$marc->add_fields( WebPAC::Normalize:_get_marc_fields() ); |
|
my @types = qw(display search); |
|
|
# override by type attribute |
|
|
@types = ( $tag->{'type'} ) if ($tag->{'type'}); |
|
|
|
|
|
foreach my $type (@types) { |
|
|
# append to previous line? |
|
|
$log->debug("type: $type ",sub { join(" ",@v) }, " ", $row->{'append'} || 'no append'); |
|
|
if ($tag->{'append'}) { |
|
|
|
|
|
# I will delimit appended part with |
|
|
# delimiter (or ,) |
|
|
my $d = $tag->{'delimiter'}; |
|
|
# default delimiter |
|
|
$d ||= " "; |
|
|
|
|
|
my $last = pop @{$row->{$type}}; |
|
|
$d = "" if (! $last); |
|
|
$last .= $d . join($d, @v); |
|
|
push @{$row->{$type}}, $last; |
|
|
|
|
|
} else { |
|
|
push @{$row->{$type}}, @v; |
|
|
} |
|
|
} |
|
151 |
|
|
152 |
|
|
|
} |
|
153 |
|
|
154 |
if ($row) { |
We are using I<magic> which detect repeatable fields only from |
155 |
$row->{'tag'} = $field; |
sequence of field/subfield data generated by normalization. |
156 |
|
|
157 |
# TODO: name_sigular, name_plural |
Repeatable field is created if there is second occurence of same subfield or |
158 |
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
if any of indicators are different. This is sane for most cases except for |
159 |
my $row_name = $name ? $self->_x($name) : $field; |
non-repeatable fields with repeatable subfields. |
|
|
|
|
# post-sort all values in field |
|
|
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
|
|
$log->warn("sort at field tag not implemented"); |
|
|
} |
|
160 |
|
|
161 |
$ds->{$row_name} = $row; |
You can change behaviour of that using C<marc_repeatable_subfield>. |
162 |
|
|
163 |
$log->debug("row $field: ",sub { Dumper($row) }); |
=cut |
164 |
|
|
165 |
|
sub _get_marc_fields { |
166 |
|
my @m; |
167 |
|
my $last; |
168 |
|
foreach my $row (@{ $marc_record }) { |
169 |
|
if ($last && |
170 |
|
$last->[0] eq $row->[0] && # check if field is same |
171 |
|
$last->[1] eq $row->[1] && # check for i1 |
172 |
|
$last->[2] eq $row->[2] && # and for i2 |
173 |
|
( $last->[3] ne $row->[3] || # and subfield is different |
174 |
|
$last->[3] eq $row->[3] && # or subfield is same, |
175 |
|
$marc_repeatable_subfield->{ $row->[3] } # but is repeatable |
176 |
|
) |
177 |
|
) { |
178 |
|
push @$last, ( $row->[3] , $row->[4] ); |
179 |
|
next; |
180 |
|
} elsif ($last) { |
181 |
|
push @m, $last; |
182 |
} |
} |
183 |
|
|
184 |
|
$last = $row; |
185 |
} |
} |
186 |
|
|
187 |
$self->{'db'}->save_ds( |
push @m, $last if ($last); |
|
id => $id, |
|
|
ds => $ds, |
|
|
prefix => $self->{prefix}, |
|
|
) if ($self->{'db'}); |
|
|
|
|
|
$log->debug("ds: ", sub { Dumper($ds) }); |
|
|
|
|
|
$log->logconfess("data structure returned is not array any more!") if wantarray; |
|
|
|
|
|
return $ds; |
|
188 |
|
|
189 |
|
return @m; |
190 |
} |
} |
191 |
|
|
192 |
=head2 parse |
=head1 Functions to create C<data_structure> |
193 |
|
|
194 |
Perform smart parsing of string, skipping delimiters for fields which aren't |
Those functions generally have to first in your normalization file. |
|
defined. It can also eval code in format starting with C<eval{...}> and |
|
|
return output or nothing depending on eval code. |
|
195 |
|
|
196 |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
=head2 tag |
197 |
|
|
198 |
Filters are implemented here. While simple form of filters looks like this: |
Define new tag for I<search> and I<display>. |
199 |
|
|
200 |
filter{name_of_filter} |
tag('Title', rec('200','a') ); |
201 |
|
|
|
but, filters can also have variable number of parametars like this: |
|
|
|
|
|
filter{name_of_filter(param,param,param)} |
|
202 |
|
|
203 |
=cut |
=cut |
204 |
|
|
205 |
my $warn_once; |
sub tag { |
206 |
|
my $name = shift or die "tag needs name as first argument"; |
207 |
sub parse { |
my @o = grep { defined($_) && $_ ne '' } @_; |
208 |
my $self = shift; |
return unless (@o); |
209 |
|
$out->{$name}->{tag} = $name; |
210 |
my ($rec, $format_utf8, $i) = @_; |
$out->{$name}->{search} = \@o; |
211 |
|
$out->{$name}->{display} = \@o; |
212 |
return if (! $format_utf8); |
} |
|
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
|
|
|
|
$i = 0 if (! $i); |
|
213 |
|
|
214 |
my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
=head2 display |
215 |
|
|
216 |
my @out; |
Define tag just for I<display> |
217 |
|
|
218 |
$log->debug("format: $format"); |
@v = display('Title', rec('200','a') ); |
219 |
|
|
220 |
my $eval_code; |
=cut |
|
# remove eval{...} from beginning |
|
|
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
|
221 |
|
|
222 |
my $filter_name; |
sub display { |
223 |
# remove filter{...} from beginning |
my $name = shift or die "display needs name as first argument"; |
224 |
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
my @o = grep { defined($_) && $_ ne '' } @_; |
225 |
|
return unless (@o); |
226 |
|
$out->{$name}->{tag} = $name; |
227 |
|
$out->{$name}->{display} = \@o; |
228 |
|
} |
229 |
|
|
230 |
my $prefix; |
=head2 search |
|
my $all_found=0; |
|
231 |
|
|
232 |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
Prepare values just for I<search> |
233 |
|
|
234 |
my $del = $1 || ''; |
@v = search('Title', rec('200','a') ); |
|
$prefix ||= $del if ($all_found == 0); |
|
235 |
|
|
236 |
# repeatable index |
=cut |
|
my $r = $i; |
|
|
$r = 0 if (lc("$2") eq 's'); |
|
237 |
|
|
238 |
my $found = 0; |
sub search { |
239 |
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
my $name = shift or die "search needs name as first argument"; |
240 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
241 |
|
return unless (@o); |
242 |
|
$out->{$name}->{tag} = $name; |
243 |
|
$out->{$name}->{search} = \@o; |
244 |
|
} |
245 |
|
|
246 |
if ($found) { |
=head2 marc |
|
push @out, $del; |
|
|
push @out, $tmp; |
|
|
$all_found += $found; |
|
|
} |
|
|
} |
|
247 |
|
|
248 |
return if (! $all_found); |
Save value for MARC field |
249 |
|
|
250 |
my $out = join('',@out); |
marc('900','a', rec('200','a') ); |
251 |
|
|
252 |
if ($out) { |
=cut |
|
# add rest of format (suffix) |
|
|
$out .= $format; |
|
253 |
|
|
254 |
# add prefix if not there |
sub marc { |
255 |
$out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); |
my $f = shift or die "marc needs field"; |
256 |
|
die "marc field must be numer" unless ($f =~ /^\d+$/); |
257 |
|
|
258 |
$log->debug("result: $out"); |
my $sf = shift or die "marc needs subfield"; |
|
} |
|
259 |
|
|
260 |
if ($eval_code) { |
foreach (@_) { |
261 |
my $eval = $self->fill_in($rec,$eval_code,$i) || return; |
my $v = $_; # make var read-write for Encode |
262 |
$log->debug("about to eval{$eval} format: $out"); |
next unless (defined($v) && $v !~ /^\s*$/); |
263 |
return if (! $self->_eval($eval)); |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
264 |
} |
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
265 |
|
push @{ $marc_record }, [ $f, $i1, $i2, $sf => $v ]; |
|
if ($filter_name) { |
|
|
my @filter_args; |
|
|
if ($filter_name =~ s/(\w+)\((.*)\)/$1/) { |
|
|
@filter_args = split(/,/, $2); |
|
|
} |
|
|
if ($self->{'filter'}->{$filter_name}) { |
|
|
$log->debug("about to filter{$filter_name} format: $out with arguments: ", join(",", @filter_args)); |
|
|
unshift @filter_args, $out; |
|
|
$out = $self->{'filter'}->{$filter_name}->(@filter_args); |
|
|
return unless(defined($out)); |
|
|
$log->debug("filter result: $out"); |
|
|
} elsif (! $warn_once->{$filter_name}) { |
|
|
$log->warn("trying to use undefined filter $filter_name"); |
|
|
$warn_once->{$filter_name}++; |
|
|
} |
|
266 |
} |
} |
|
|
|
|
return $out; |
|
267 |
} |
} |
268 |
|
|
269 |
=head2 parse_to_arr |
=head2 marc_repeatable_subfield |
270 |
|
|
271 |
Similar to C<parse>, but returns array of all repeatable fields |
Save values for MARC repetable subfield |
272 |
|
|
273 |
my @arr = $webpac->parse_to_arr($rec,'v250^a'); |
marc_repeatable_subfield('910', 'z', rec('909') ); |
274 |
|
|
275 |
=cut |
=cut |
276 |
|
|
277 |
sub parse_to_arr { |
sub marc_repeatable_subfield { |
278 |
my $self = shift; |
die "marc_repeatable_subfield need subfield!\n" unless (defined($_[1])); |
279 |
|
$marc_repeatable_subfield->{ $_[1] }++; |
280 |
|
marc(@_); |
281 |
|
} |
282 |
|
|
283 |
my ($rec, $format_utf8) = @_; |
=head2 marc_indicators |
284 |
|
|
285 |
my $log = $self->_get_logger(); |
Set both indicators for MARC field |
286 |
|
|
287 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
marc_indicators('900', ' ', 1); |
|
return if (! $format_utf8); |
|
288 |
|
|
289 |
my $i = 0; |
Any indicator value other than C<0-9> will be treated as undefined. |
|
my @arr; |
|
290 |
|
|
291 |
while (my $v = $self->parse($rec,$format_utf8,$i++)) { |
=cut |
|
push @arr, $v; |
|
|
} |
|
292 |
|
|
293 |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
sub marc_indicators { |
294 |
|
my $f = shift || die "marc_indicators need field!\n"; |
295 |
|
my ($i1,$i2) = @_; |
296 |
|
die "marc_indicators($f, ...) need i1!\n" unless(defined($i1)); |
297 |
|
die "marc_indicators($f, $i1, ...) need i2!\n" unless(defined($i2)); |
298 |
|
|
299 |
return @arr; |
$i1 = ' ' if ($i1 !~ /^\d$/); |
300 |
|
$i2 = ' ' if ($i2 !~ /^\d$/); |
301 |
|
@{ $marc_indicators->{$f} } = ($i1,$i2); |
302 |
} |
} |
303 |
|
|
304 |
|
|
305 |
=head2 fill_in |
=head1 Functions to extract data from input |
|
|
|
|
Workhourse of all: takes record from in-memory structure of database and |
|
|
strings with placeholders and returns string or array of with substituted |
|
|
values from record. |
|
306 |
|
|
307 |
my $text = $webpac->fill_in($rec,'v250^a'); |
This function should be used inside functions to create C<data_structure> described |
308 |
|
above. |
309 |
|
|
310 |
Optional argument is ordinal number for repeatable fields. By default, |
=head2 rec1 |
|
it's assume to be first repeatable field (fields are perl array, so first |
|
|
element is 0). |
|
|
Following example will read second value from repeatable field. |
|
311 |
|
|
312 |
my $text = $webpac->fill_in($rec,'Title: v250^a',1); |
Return all values in some field |
313 |
|
|
314 |
This function B<does not> perform parsing of format to inteligenty skip |
@v = rec1('200') |
|
delimiters before fields which aren't used. |
|
315 |
|
|
316 |
This method will automatically decode UTF-8 string to local code page |
TODO: order of values is probably same as in source data, need to investigate that |
|
if needed. |
|
317 |
|
|
318 |
=cut |
=cut |
319 |
|
|
320 |
sub fill_in { |
sub rec1 { |
321 |
my $self = shift; |
my $f = shift; |
322 |
|
return unless (defined($rec) && defined($rec->{$f})); |
323 |
my $log = $self->_get_logger(); |
if (ref($rec->{$f}) eq 'ARRAY') { |
324 |
|
return map { |
325 |
my $rec = shift || $log->logconfess("need data record"); |
if (ref($_) eq 'HASH') { |
326 |
my $format = shift || $log->logconfess("need format to parse"); |
values %{$_}; |
|
# iteration (for repeatable fields) |
|
|
my $i = shift || 0; |
|
|
|
|
|
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
|
|
|
|
|
# FIXME remove for speedup? |
|
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
|
|
|
|
if (utf8::is_utf8($format)) { |
|
|
$format = $self->_x($format); |
|
|
} |
|
|
|
|
|
my $found = 0; |
|
|
|
|
|
my $eval_code; |
|
|
# remove eval{...} from beginning |
|
|
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
|
|
|
|
|
my $filter_name; |
|
|
# remove filter{...} from beginning |
|
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
|
|
|
|
|
# do actual replacement of placeholders |
|
|
# repeatable fields |
|
|
$format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; |
|
|
# non-repeatable fields |
|
|
$format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; |
|
|
|
|
|
if ($found) { |
|
|
$log->debug("format: $format"); |
|
|
if ($eval_code) { |
|
|
my $eval = $self->fill_in($rec,$eval_code,$i); |
|
|
return if (! $self->_eval($eval)); |
|
|
} |
|
|
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
|
|
$log->debug("filter '$filter_name' for $format"); |
|
|
$format = $self->{'filter'}->{$filter_name}->($format); |
|
|
return unless(defined($format)); |
|
|
$log->debug("filter result: $format"); |
|
|
} |
|
|
# do we have lookups? |
|
|
if ($self->{'lookup'}) { |
|
|
if ($self->{'lookup'}->can('lookup')) { |
|
|
my @lookup = $self->{lookup}->lookup($format); |
|
|
$log->debug("lookup $format", join(", ", @lookup)); |
|
|
return @lookup; |
|
327 |
} else { |
} else { |
328 |
$log->warn("Have lookup object but can't invoke lookup method"); |
$_; |
329 |
} |
} |
330 |
} else { |
} @{ $rec->{$f} }; |
331 |
return $format; |
} elsif( defined($rec->{$f}) ) { |
332 |
} |
return $rec->{$f}; |
|
} else { |
|
|
return; |
|
333 |
} |
} |
334 |
} |
} |
335 |
|
|
336 |
|
=head2 rec2 |
337 |
|
|
338 |
=head2 fill_in_to_arr |
Return all values in specific field and subfield |
|
|
|
|
Similar to C<fill_in>, but returns array of all repeatable fields. Usable |
|
|
for fields which have lookups, so they shouldn't be parsed but rather |
|
|
C<fill_id>ed. |
|
339 |
|
|
340 |
my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); |
@v = rec2('200','a') |
341 |
|
|
342 |
=cut |
=cut |
343 |
|
|
344 |
sub fill_in_to_arr { |
sub rec2 { |
345 |
my $self = shift; |
my $f = shift; |
346 |
|
return unless (defined($rec && $rec->{$f})); |
347 |
|
my $sf = shift; |
348 |
|
return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; |
349 |
|
} |
350 |
|
|
351 |
my ($rec, $format_utf8) = @_; |
=head2 rec |
352 |
|
|
353 |
my $log = $self->_get_logger(); |
syntaxtic sugar for |
354 |
|
|
355 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
@v = rec('200') |
356 |
return if (! $format_utf8); |
@v = rec('200','a') |
357 |
|
|
358 |
my $i = 0; |
=cut |
|
my @arr; |
|
359 |
|
|
360 |
while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { |
sub rec { |
361 |
push @arr, @v; |
if ($#_ == 0) { |
362 |
|
return rec1(@_); |
363 |
|
} elsif ($#_ == 1) { |
364 |
|
return rec2(@_); |
365 |
} |
} |
|
|
|
|
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
|
|
|
|
|
return @arr; |
|
366 |
} |
} |
367 |
|
|
368 |
|
=head2 regex |
369 |
|
|
370 |
=head2 get_data |
Apply regex to some or all values |
|
|
|
|
Returns value from record. |
|
|
|
|
|
my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); |
|
371 |
|
|
372 |
Arguments are: |
@v = regex( 's/foo/bar/g', @v ); |
|
record reference C<$rec>, |
|
|
field C<$f>, |
|
|
optional subfiled C<$sf>, |
|
|
index for repeatable values C<$i>. |
|
|
|
|
|
Optinal variable C<$found> will be incremeted if there |
|
|
is field. |
|
|
|
|
|
Returns value or empty string. |
|
373 |
|
|
374 |
=cut |
=cut |
375 |
|
|
376 |
sub get_data { |
sub regex { |
377 |
my $self = shift; |
my $r = shift; |
378 |
|
my @out; |
379 |
my ($rec,$f,$sf,$i,$found) = @_; |
#warn "r: $r\n",Dumper(\@_); |
380 |
|
foreach my $t (@_) { |
381 |
if ($$rec->{$f}) { |
next unless ($t); |
382 |
return '' if (! $$rec->{$f}->[$i]); |
eval "\$t =~ $r"; |
383 |
no strict 'refs'; |
push @out, $t if ($t && $t ne ''); |
|
if ($sf && $$rec->{$f}->[$i]->{$sf}) { |
|
|
$$found++ if (defined($$found)); |
|
|
return $$rec->{$f}->[$i]->{$sf}; |
|
|
} elsif (! $sf && $$rec->{$f}->[$i]) { |
|
|
$$found++ if (defined($$found)); |
|
|
# it still might have subfield, just |
|
|
# not specified, so we'll dump all |
|
|
if ($$rec->{$f}->[$i] =~ /HASH/o) { |
|
|
my $out; |
|
|
foreach my $k (keys %{$$rec->{$f}->[$i]}) { |
|
|
$out .= $$rec->{$f}->[$i]->{$k}." "; |
|
|
} |
|
|
return $out; |
|
|
} else { |
|
|
return $$rec->{$f}->[$i]; |
|
|
} |
|
|
} else { |
|
|
return ''; |
|
|
} |
|
|
} else { |
|
|
return ''; |
|
384 |
} |
} |
385 |
|
return @out; |
386 |
} |
} |
387 |
|
|
388 |
|
=head2 prefix |
389 |
|
|
390 |
=head2 apply_format |
Prefix all values with a string |
391 |
|
|
392 |
Apply format specified in tag with C<format_name="name"> and |
@v = prefix( 'my_', @v ); |
|
C<format_delimiter=";;">. |
|
|
|
|
|
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
|
|
|
|
|
Formats can contain C<lookup{...}> if you need them. |
|
393 |
|
|
394 |
=cut |
=cut |
395 |
|
|
396 |
sub apply_format { |
sub prefix { |
397 |
my $self = shift; |
my $p = shift or die "prefix needs string as first argument"; |
398 |
|
return map { $p . $_ } grep { defined($_) } @_; |
399 |
my ($name,$delimiter,$data) = @_; |
} |
|
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
if (! $self->{'import_xml'}->{'format'}->{$name}) { |
|
|
$log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'}); |
|
|
return $data; |
|
|
} |
|
|
|
|
|
$log->warn("no delimiter for format $name") if (! $delimiter); |
|
400 |
|
|
401 |
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
=head2 suffix |
402 |
|
|
403 |
my @data = split(/\Q$delimiter\E/, $data); |
suffix all values with a string |
404 |
|
|
405 |
my $out = sprintf($format, @data); |
@v = suffix( '_my', @v ); |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
|
406 |
|
|
407 |
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
=cut |
|
return $self->{'lookup'}->lookup($out); |
|
|
} else { |
|
|
return $out; |
|
|
} |
|
408 |
|
|
409 |
|
sub suffix { |
410 |
|
my $s = shift or die "suffix needs string as first argument"; |
411 |
|
return map { $_ . $s } grep { defined($_) } @_; |
412 |
} |
} |
413 |
|
|
414 |
=head2 sort_arr |
=head2 surround |
415 |
|
|
416 |
Sort array ignoring case and html in data |
surround all values with a two strings |
417 |
|
|
418 |
my @sorted = $webpac->sort_arr(@unsorted); |
@v = surround( 'prefix_', '_suffix', @v ); |
419 |
|
|
420 |
=cut |
=cut |
421 |
|
|
422 |
sub sort_arr { |
sub surround { |
423 |
my $self = shift; |
my $p = shift or die "surround need prefix as first argument"; |
424 |
|
my $s = shift or die "surround needs suffix as second argument"; |
425 |
my $log = $self->_get_logger(); |
return map { $p . $_ . $s } grep { defined($_) } @_; |
|
|
|
|
# FIXME add Schwartzian Transformation? |
|
|
|
|
|
my @sorted = sort { |
|
|
$a =~ s#<[^>]+/*>##; |
|
|
$b =~ s#<[^>]+/*>##; |
|
|
lc($b) cmp lc($a) |
|
|
} @_; |
|
|
$log->debug("sorted values: ",sub { join(", ",@sorted) }); |
|
|
|
|
|
return @sorted; |
|
426 |
} |
} |
427 |
|
|
428 |
|
=head2 first |
429 |
|
|
430 |
=head1 INTERNAL METHODS |
Return first element |
|
|
|
|
=head2 _sort_by_order |
|
431 |
|
|
432 |
Sort xml tags data structure accoding to C<order=""> attribute. |
$v = first( @v ); |
433 |
|
|
434 |
=cut |
=cut |
435 |
|
|
436 |
sub _sort_by_order { |
sub first { |
437 |
my $self = shift; |
my $r = shift; |
438 |
|
return $r; |
|
my $va = $self->{'import_xml'}->{'indexer'}->{$a}->{'order'} || |
|
|
$self->{'import_xml'}->{'indexer'}->{$a}; |
|
|
my $vb = $self->{'import_xml'}->{'indexer'}->{$b}->{'order'} || |
|
|
$self->{'import_xml'}->{'indexer'}->{$b}; |
|
|
|
|
|
return $va <=> $vb; |
|
439 |
} |
} |
440 |
|
|
441 |
=head2 _x |
=head2 lookup |
442 |
|
|
443 |
Convert strings from C<conf/normalize/*.xml> encoding into application |
Consult lookup hashes for some value |
|
specific encoding (optinally specified using C<code_page> to C<new> |
|
|
constructor). |
|
444 |
|
|
445 |
my $text = $n->_x('normalize text string'); |
@v = lookup( $v ); |
446 |
|
@v = lookup( @v ); |
|
This is a stub so that other modules doesn't have to implement it. |
|
447 |
|
|
448 |
=cut |
=cut |
449 |
|
|
450 |
sub _x { |
sub lookup { |
451 |
my $self = shift; |
my $k = shift or return; |
452 |
return shift; |
return unless (defined($lookup->{$k})); |
453 |
|
if (ref($lookup->{$k}) eq 'ARRAY') { |
454 |
|
return @{ $lookup->{$k} }; |
455 |
|
} else { |
456 |
|
return $lookup->{$k}; |
457 |
|
} |
458 |
} |
} |
459 |
|
|
460 |
|
=head2 join_with |
461 |
|
|
462 |
=head1 AUTHOR |
Joins walues with some delimiter |
|
|
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
|
463 |
|
|
464 |
=head1 COPYRIGHT & LICENSE |
$v = join_with(", ", @v); |
|
|
|
|
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
|
under the same terms as Perl itself. |
|
465 |
|
|
466 |
=cut |
=cut |
467 |
|
|
468 |
1; # End of WebPAC::Normalize |
sub join_with { |
469 |
|
my $d = shift; |
470 |
|
return join($d, grep { defined($_) && $_ ne '' } @_); |
471 |
|
} |
472 |
|
|
473 |
|
# END |
474 |
|
1; |