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 |
|
tag search display |
7 |
|
rec1 rec2 rec |
8 |
|
regex prefix suffix surround |
9 |
|
first lookup join_with |
10 |
|
/; |
11 |
|
|
12 |
use warnings; |
use warnings; |
13 |
use strict; |
use strict; |
14 |
use base 'WebPAC::Common'; |
|
15 |
|
#use base qw/WebPAC::Common/; |
16 |
use Data::Dumper; |
use Data::Dumper; |
17 |
|
|
18 |
=head1 NAME |
=head1 NAME |
19 |
|
|
20 |
WebPAC::Normalize - data mungling for normalisation |
WebPAC::Normalize - describe normalisaton rules using sets |
21 |
|
|
22 |
=head1 VERSION |
=head1 VERSION |
23 |
|
|
29 |
|
|
30 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
31 |
|
|
32 |
This package contains code that mungle data to produce normalized format. |
This module uses C<conf/normalize/*.pl> files to perform normalisation |
33 |
|
from input records using perl functions which are specialized for set |
34 |
It contains several assumptions: |
processing. |
35 |
|
|
36 |
=over |
Sets are implemented as arrays, and normalisation file is valid perl, which |
37 |
|
means that you check it's validity before running WebPAC using |
38 |
=item * |
C<perl -c normalize.pl>. |
|
|
|
|
format of fields is defined using C<v123^a> notation for repeatable fields |
|
|
or C<s123^a> for single (or first) value, where C<123> is field number and |
|
|
C<a> is subfield. |
|
|
|
|
|
=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 |
|
|
|
|
|
=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. |
|
|
|
|
|
|
|
39 |
|
|
40 |
|
Normalisation can generate multiple output normalized data. For now, supported output |
41 |
|
types (on the left side of definition) are: C<tag>, C<display> and C<search>. |
42 |
|
|
43 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
44 |
|
|
45 |
=head2 new |
=head2 data_structure |
46 |
|
|
47 |
Create new normalisation object |
Return data structure |
48 |
|
|
49 |
my $n = new WebPAC::Normalize::Something( |
my $ds = WebPAC::Normalize( |
50 |
filter => { |
lookup => $lookup->lookup_hash, |
51 |
'filter_name_1' => sub { |
row => $row, |
52 |
# filter code |
rules => $normalize_pl_config, |
|
return length($_); |
|
|
}, ... |
|
|
}, |
|
|
db => $db_obj, |
|
|
lookup_regex => $lookup->regex, |
|
|
lookup => $lookup_obj, |
|
|
prefix => 'foobar', |
|
53 |
); |
); |
54 |
|
|
55 |
Parametar C<filter> defines user supplied snippets of perl code which can |
This function will B<die> if normalizastion can't be evaled. |
|
be use with C<filter{...}> notation. |
|
|
|
|
|
C<prefix> is used to form filename for database record (to support multiple |
|
|
source files which are joined in one database). |
|
|
|
|
|
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
|
|
in structures. If you pass this parametar, you must also pass C<lookup> |
|
|
which is C<WebPAC::Lookup> object. |
|
56 |
|
|
57 |
=cut |
=cut |
58 |
|
|
59 |
sub new { |
sub data_structure { |
60 |
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')); |
|
61 |
|
|
62 |
$log->warn("no prefix defined. please check that!") unless ($self->{'prefix'}); |
die "need row argument" unless ($arg->{row}); |
63 |
|
die "need normalisation argument" unless ($arg->{rules}); |
64 |
|
|
65 |
$self ? return $self : return undef; |
no strict 'subs'; |
66 |
|
set_lookup( $arg->{lookup} ); |
67 |
|
set_rec( $arg->{row} ); |
68 |
|
clean_ds(); |
69 |
|
eval "$arg->{rules}"; |
70 |
|
die "error evaling $arg->{rules}: $@\n" if ($@); |
71 |
|
return get_ds(); |
72 |
} |
} |
73 |
|
|
74 |
|
=head2 set_rec |
75 |
|
|
76 |
=head2 data_structure |
Set current record hash |
77 |
|
|
78 |
Create in-memory data structure which represents normalized layout from |
set_rec( $rec ); |
|
C<conf/normalize/*.xml>. |
|
|
|
|
|
This structures are used to produce output. |
|
|
|
|
|
my $ds = $webpac->data_structure($rec); |
|
79 |
|
|
80 |
=cut |
=cut |
81 |
|
|
82 |
sub data_structure { |
my $rec; |
|
my $self = shift; |
|
|
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
|
my $rec = shift; |
|
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
|
|
|
|
$log->debug("data_structure rec = ", sub { Dumper($rec) }); |
|
|
|
|
|
$log->logdie("need unique ID (mfn) in field 000 of record ", sub { Dumper($rec) } ) unless (defined($rec->{'000'})); |
|
|
|
|
|
my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!"); |
|
|
|
|
|
my $cache_file; |
|
83 |
|
|
84 |
if ($self->{'db'}) { |
sub set_rec { |
85 |
my $ds = $self->{'db'}->load_ds( id => $id, prefix => $self->{prefix} ); |
$rec = shift or die "no record hash"; |
86 |
$log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) }); |
} |
|
return $ds if ($ds); |
|
|
$log->debug("cache miss, creating"); |
|
|
} |
|
|
|
|
|
undef $self->{'currnet_filename'}; |
|
|
undef $self->{'headline'}; |
|
|
|
|
|
my @sorted_tags; |
|
|
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; |
|
|
} |
|
|
|
|
|
my $ds; |
|
|
|
|
|
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
|
|
|
|
|
foreach my $field (@sorted_tags) { |
|
|
|
|
|
my $row; |
|
|
|
|
|
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
|
|
|
|
|
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
|
|
my $format; |
|
|
|
|
|
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
|
|
$format = $tag->{'value'} || $tag->{'content'}; |
|
|
|
|
|
$log->debug("format: $format"); |
|
|
|
|
|
my @v; |
|
|
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); |
|
|
|
|
|
if ($tag->{'sort'}) { |
|
|
@v = $self->sort_arr(@v); |
|
|
} |
|
|
|
|
|
# use format? |
|
|
if ($tag->{'format_name'}) { |
|
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
|
|
} |
|
|
|
|
|
# delimiter will join repeatable fields |
|
|
if ($tag->{'delimiter'}) { |
|
|
@v = ( join($tag->{'delimiter'}, @v) ); |
|
|
} |
|
|
|
|
|
# default types |
|
|
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; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
if ($row) { |
|
|
$row->{'tag'} = $field; |
|
|
|
|
|
# TODO: name_sigular, name_plural |
|
|
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
|
|
my $row_name = $name ? $self->_x($name) : $field; |
|
|
|
|
|
# post-sort all values in field |
|
|
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
|
|
$log->warn("sort at field tag not implemented"); |
|
|
} |
|
|
|
|
|
$ds->{$row_name} = $row; |
|
87 |
|
|
88 |
$log->debug("row $field: ",sub { Dumper($row) }); |
=head2 tag |
|
} |
|
89 |
|
|
90 |
} |
Define new tag for I<search> and I<display>. |
91 |
|
|
92 |
$self->{'db'}->save_ds( |
tag('Title', rec('200','a') ); |
|
id => $id, |
|
|
ds => $ds, |
|
|
prefix => $self->{prefix}, |
|
|
) if ($self->{'db'}); |
|
93 |
|
|
|
$log->debug("ds: ", sub { Dumper($ds) }); |
|
94 |
|
|
95 |
$log->logconfess("data structure returned is not array any more!") if wantarray; |
=cut |
96 |
|
|
97 |
return $ds; |
my $out; |
98 |
|
|
99 |
|
sub tag { |
100 |
|
my $name = shift or die "tag needs name as first argument"; |
101 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
102 |
|
return unless (@o); |
103 |
|
$out->{$name}->{tag} = $name; |
104 |
|
$out->{$name}->{search} = \@o; |
105 |
|
$out->{$name}->{display} = \@o; |
106 |
} |
} |
107 |
|
|
108 |
=head2 parse |
=head2 display |
109 |
|
|
110 |
Perform smart parsing of string, skipping delimiters for fields which aren't |
Define tag just for I<display> |
|
defined. It can also eval code in format starting with C<eval{...}> and |
|
|
return output or nothing depending on eval code. |
|
111 |
|
|
112 |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
@v = display('Title', rec('200','a') ); |
113 |
|
|
114 |
=cut |
=cut |
115 |
|
|
116 |
sub parse { |
sub display { |
117 |
my $self = shift; |
my $name = shift or die "display needs name as first argument"; |
118 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
119 |
my ($rec, $format_utf8, $i) = @_; |
return unless (@o); |
120 |
|
$out->{$name}->{tag} = $name; |
121 |
return if (! $format_utf8); |
$out->{$name}->{display} = \@o; |
122 |
|
} |
|
my $log = $self->_get_logger(); |
|
|
|
|
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
|
|
|
|
$i = 0 if (! $i); |
|
|
|
|
|
my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
|
|
|
|
|
my @out; |
|
|
|
|
|
$log->debug("format: $format"); |
|
|
|
|
|
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); |
|
|
|
|
|
my $prefix; |
|
|
my $all_found=0; |
|
|
|
|
|
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
|
123 |
|
|
124 |
my $del = $1 || ''; |
=head2 search |
|
$prefix ||= $del if ($all_found == 0); |
|
125 |
|
|
126 |
# repeatable index |
Prepare values just for I<search> |
|
my $r = $i; |
|
|
$r = 0 if (lc("$2") eq 's'); |
|
127 |
|
|
128 |
my $found = 0; |
@v = search('Title', rec('200','a') ); |
|
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
|
129 |
|
|
130 |
if ($found) { |
=cut |
|
push @out, $del; |
|
|
push @out, $tmp; |
|
|
$all_found += $found; |
|
|
} |
|
|
} |
|
131 |
|
|
132 |
return if (! $all_found); |
sub search { |
133 |
|
my $name = shift or die "search needs name as first argument"; |
134 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
135 |
|
return unless (@o); |
136 |
|
$out->{$name}->{tag} = $name; |
137 |
|
$out->{$name}->{search} = \@o; |
138 |
|
} |
139 |
|
|
140 |
my $out = join('',@out); |
=head2 get_ds |
141 |
|
|
142 |
if ($out) { |
Return hash formatted as data structure |
|
# add rest of format (suffix) |
|
|
$out .= $format; |
|
143 |
|
|
144 |
# add prefix if not there |
my $ds = get_ds(); |
|
$out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); |
|
145 |
|
|
146 |
$log->debug("result: $out"); |
=cut |
|
} |
|
|
|
|
|
if ($eval_code) { |
|
|
my $eval = $self->fill_in($rec,$eval_code,$i) || return; |
|
|
$log->debug("about to eval{$eval} format: $out"); |
|
|
return if (! $self->_eval($eval)); |
|
|
} |
|
|
|
|
|
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
|
|
$log->debug("about to filter{$filter_name} format: $out"); |
|
|
$out = $self->{'filter'}->{$filter_name}->($out); |
|
|
return unless(defined($out)); |
|
|
$log->debug("filter result: $out"); |
|
|
} |
|
147 |
|
|
148 |
|
sub get_ds { |
149 |
return $out; |
return $out; |
150 |
} |
} |
151 |
|
|
152 |
=head2 parse_to_arr |
=head2 clean_ds |
153 |
|
|
154 |
Similar to C<parse>, but returns array of all repeatable fields |
Clean data structure hash for next record |
155 |
|
|
156 |
my @arr = $webpac->parse_to_arr($rec,'v250^a'); |
clean_ds(); |
157 |
|
|
158 |
=cut |
=cut |
159 |
|
|
160 |
sub parse_to_arr { |
sub clean_ds { |
161 |
my $self = shift; |
$out = undef; |
|
|
|
|
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; |
|
162 |
} |
} |
163 |
|
|
164 |
|
=head2 set_lookup |
165 |
|
|
166 |
=head2 fill_in |
Set current lookup hash |
|
|
|
|
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. |
|
|
|
|
|
my $text = $webpac->fill_in($rec,'v250^a'); |
|
|
|
|
|
Optional argument is ordinal number for repeatable fields. By default, |
|
|
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. |
|
|
|
|
|
my $text = $webpac->fill_in($rec,'Title: v250^a',1); |
|
167 |
|
|
168 |
This function B<does not> perform parsing of format to inteligenty skip |
set_lookup( $lookup ); |
|
delimiters before fields which aren't used. |
|
|
|
|
|
This method will automatically decode UTF-8 string to local code page |
|
|
if needed. |
|
169 |
|
|
170 |
=cut |
=cut |
171 |
|
|
172 |
sub fill_in { |
my $lookup; |
|
my $self = shift; |
|
173 |
|
|
174 |
my $log = $self->_get_logger(); |
sub set_lookup { |
175 |
|
$lookup = shift; |
176 |
|
} |
177 |
|
|
178 |
my $rec = shift || $log->logconfess("need data record"); |
=head2 rec1 |
|
my $format = shift || $log->logconfess("need format to parse"); |
|
|
# iteration (for repeatable fields) |
|
|
my $i = shift || 0; |
|
179 |
|
|
180 |
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
Return all values in some field |
181 |
|
|
182 |
# FIXME remove for speedup? |
@v = rec1('200') |
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
183 |
|
|
184 |
if (utf8::is_utf8($format)) { |
TODO: order of values is probably same as in source data, need to investigate that |
|
$format = $self->_x($format); |
|
|
} |
|
185 |
|
|
186 |
my $found = 0; |
=cut |
187 |
|
|
188 |
my $eval_code; |
sub rec1 { |
189 |
# remove eval{...} from beginning |
my $f = shift; |
190 |
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
return unless (defined($rec) && defined($rec->{$f})); |
191 |
|
if (ref($rec->{$f}) eq 'ARRAY') { |
192 |
my $filter_name; |
return map { |
193 |
# remove filter{...} from beginning |
if (ref($_) eq 'HASH') { |
194 |
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
values %{$_}; |
|
|
|
|
# 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')) { |
|
|
return $self->{'lookup'}->lookup($format); |
|
195 |
} else { |
} else { |
196 |
$log->warn("Have lookup object but can't invoke lookup method"); |
$_; |
197 |
} |
} |
198 |
} else { |
} @{ $rec->{$f} }; |
199 |
return $format; |
} elsif( defined($rec->{$f}) ) { |
200 |
} |
return $rec->{$f}; |
|
} else { |
|
|
return; |
|
201 |
} |
} |
202 |
} |
} |
203 |
|
|
204 |
|
=head2 rec2 |
205 |
|
|
206 |
=head2 fill_in_to_arr |
Return all values in specific field and subfield |
207 |
|
|
208 |
Similar to C<fill_in>, but returns array of all repeatable fields. Usable |
@v = rec2('200','a') |
|
for fields which have lookups, so they shouldn't be parsed but rather |
|
|
C<fill_id>ed. |
|
|
|
|
|
my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); |
|
209 |
|
|
210 |
=cut |
=cut |
211 |
|
|
212 |
sub fill_in_to_arr { |
sub rec2 { |
213 |
my $self = shift; |
my $f = shift; |
214 |
|
return unless (defined($rec && $rec->{$f})); |
215 |
|
my $sf = shift; |
216 |
|
return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; |
217 |
|
} |
218 |
|
|
219 |
my ($rec, $format_utf8) = @_; |
=head2 rec |
220 |
|
|
221 |
my $log = $self->_get_logger(); |
syntaxtic sugar for |
222 |
|
|
223 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
@v = rec('200') |
224 |
return if (! $format_utf8); |
@v = rec('200','a') |
225 |
|
|
226 |
my $i = 0; |
=cut |
|
my @arr; |
|
227 |
|
|
228 |
while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { |
sub rec { |
229 |
push @arr, @v; |
if ($#_ == 0) { |
230 |
|
return rec1(@_); |
231 |
|
} elsif ($#_ == 1) { |
232 |
|
return rec2(@_); |
233 |
} |
} |
|
|
|
|
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
|
|
|
|
|
return @arr; |
|
234 |
} |
} |
235 |
|
|
236 |
|
=head2 regex |
237 |
|
|
238 |
=head2 get_data |
Apply regex to some or all values |
|
|
|
|
Returns value from record. |
|
|
|
|
|
my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); |
|
|
|
|
|
Arguments are: |
|
|
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. |
|
239 |
|
|
240 |
Returns value or empty string. |
@v = regex( 's/foo/bar/g', @v ); |
241 |
|
|
242 |
=cut |
=cut |
243 |
|
|
244 |
sub get_data { |
sub regex { |
245 |
my $self = shift; |
my $r = shift; |
246 |
|
my @out; |
247 |
my ($rec,$f,$sf,$i,$found) = @_; |
#warn "r: $r\n",Dumper(\@_); |
248 |
|
foreach my $t (@_) { |
249 |
if ($$rec->{$f}) { |
next unless ($t); |
250 |
return '' if (! $$rec->{$f}->[$i]); |
eval "\$t =~ $r"; |
251 |
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 ''; |
|
252 |
} |
} |
253 |
|
return @out; |
254 |
} |
} |
255 |
|
|
256 |
|
=head2 prefix |
257 |
|
|
258 |
=head2 apply_format |
Prefix all values with a string |
|
|
|
|
Apply format specified in tag with C<format_name="name"> and |
|
|
C<format_delimiter=";;">. |
|
|
|
|
|
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
|
259 |
|
|
260 |
Formats can contain C<lookup{...}> if you need them. |
@v = prefix( 'my_', @v ); |
261 |
|
|
262 |
=cut |
=cut |
263 |
|
|
264 |
sub apply_format { |
sub prefix { |
265 |
my $self = shift; |
my $p = shift or die "prefix needs string as first argument"; |
266 |
|
return map { $p . $_ } grep { defined($_) } @_; |
267 |
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); |
|
268 |
|
|
269 |
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
=head2 suffix |
270 |
|
|
271 |
my @data = split(/\Q$delimiter\E/, $data); |
suffix all values with a string |
272 |
|
|
273 |
my $out = sprintf($format, @data); |
@v = suffix( '_my', @v ); |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
|
274 |
|
|
275 |
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
=cut |
|
return $self->{'lookup'}->lookup($out); |
|
|
} else { |
|
|
return $out; |
|
|
} |
|
276 |
|
|
277 |
|
sub suffix { |
278 |
|
my $s = shift or die "suffix needs string as first argument"; |
279 |
|
return map { $_ . $s } grep { defined($_) } @_; |
280 |
} |
} |
281 |
|
|
282 |
=head2 sort_arr |
=head2 surround |
283 |
|
|
284 |
Sort array ignoring case and html in data |
surround all values with a two strings |
285 |
|
|
286 |
my @sorted = $webpac->sort_arr(@unsorted); |
@v = surround( 'prefix_', '_suffix', @v ); |
287 |
|
|
288 |
=cut |
=cut |
289 |
|
|
290 |
sub sort_arr { |
sub surround { |
291 |
my $self = shift; |
my $p = shift or die "surround need prefix as first argument"; |
292 |
|
my $s = shift or die "surround needs suffix as second argument"; |
293 |
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; |
|
294 |
} |
} |
295 |
|
|
296 |
|
=head2 first |
297 |
|
|
298 |
=head1 INTERNAL METHODS |
Return first element |
299 |
|
|
300 |
=head2 _sort_by_order |
$v = first( @v ); |
|
|
|
|
Sort xml tags data structure accoding to C<order=""> attribute. |
|
301 |
|
|
302 |
=cut |
=cut |
303 |
|
|
304 |
sub _sort_by_order { |
sub first { |
305 |
my $self = shift; |
my $r = shift; |
306 |
|
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; |
|
307 |
} |
} |
308 |
|
|
309 |
=head2 _x |
=head2 lookup |
|
|
|
|
Convert strings from C<conf/normalize/*.xml> encoding into application |
|
|
specific encoding (optinally specified using C<code_page> to C<new> |
|
|
constructor). |
|
310 |
|
|
311 |
my $text = $n->_x('normalize text string'); |
Consult lookup hashes for some value |
312 |
|
|
313 |
This is a stub so that other modules doesn't have to implement it. |
@v = lookup( $v ); |
314 |
|
@v = lookup( @v ); |
315 |
|
|
316 |
=cut |
=cut |
317 |
|
|
318 |
sub _x { |
sub lookup { |
319 |
my $self = shift; |
my $k = shift or return; |
320 |
return shift; |
return unless (defined($lookup->{$k})); |
321 |
|
if (ref($lookup->{$k}) eq 'ARRAY') { |
322 |
|
return @{ $lookup->{$k} }; |
323 |
|
} else { |
324 |
|
return $lookup->{$k}; |
325 |
|
} |
326 |
} |
} |
327 |
|
|
328 |
|
=head2 join_with |
329 |
|
|
330 |
=head1 AUTHOR |
Joins walues with some delimiter |
|
|
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
|
331 |
|
|
332 |
=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. |
|
333 |
|
|
334 |
=cut |
=cut |
335 |
|
|
336 |
1; # End of WebPAC::Normalize |
sub join_with { |
337 |
|
my $d = shift; |
338 |
|
return join($d, grep { defined($_) && $_ ne '' } @_); |
339 |
|
} |
340 |
|
|
341 |
|
# END |
342 |
|
1; |