2 |
|
|
3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
use base 'WebPAC::Common'; |
6 |
use Data::Dumper; |
use Data::Dumper; |
7 |
|
|
8 |
=head1 NAME |
=head1 NAME |
9 |
|
|
10 |
WebPAC::Normalize - normalisation of source file |
WebPAC::Normalize - data mungling for normalisation |
11 |
|
|
12 |
=head1 VERSION |
=head1 VERSION |
13 |
|
|
14 |
Version 0.01 |
Version 0.06 |
15 |
|
|
16 |
=cut |
=cut |
17 |
|
|
18 |
our $VERSION = '0.01'; |
our $VERSION = '0.06'; |
19 |
|
|
20 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
21 |
|
|
22 |
This package contains code that could be helpful in implementing different |
This package contains code that mungle data to produce normalized format. |
23 |
normalisation front-ends. |
|
24 |
|
It contains several assumptions: |
25 |
|
|
26 |
|
=over |
27 |
|
|
28 |
|
=item * |
29 |
|
|
30 |
|
format of fields is defined using C<v123^a> notation for repeatable fields |
31 |
|
or C<s123^a> for single (or first) value, where C<123> is field number and |
32 |
|
C<a> is subfield. |
33 |
|
|
34 |
|
=item * |
35 |
|
|
36 |
|
source data records (C<$rec>) have unique identifiers in field C<000> |
37 |
|
|
38 |
|
=item * |
39 |
|
|
40 |
|
optional C<eval{length('v123^a') == 3}> tag at B<beginning of format> will be |
41 |
|
perl code that is evaluated before producing output (value of field will be |
42 |
|
interpolated before that) |
43 |
|
|
44 |
|
=item * |
45 |
|
|
46 |
|
optional C<filter{filter_name}> at B<begining of format> will apply perl |
47 |
|
code defined as code ref on format after field substitution to producing |
48 |
|
output |
49 |
|
|
50 |
|
There is one built-in filter called C<regex> which can be use like this: |
51 |
|
|
52 |
|
filter{regex(s/foo/bar/)} |
53 |
|
|
54 |
|
=item * |
55 |
|
|
56 |
|
optional C<lookup{...}> will be then performed. See C<WebPAC::Lookups>. |
57 |
|
|
58 |
|
=item * |
59 |
|
|
60 |
|
at end, optional C<format>s rules are resolved. Format rules are similar to |
61 |
|
C<sprintf> and can also contain C<lookup{...}> which is performed after |
62 |
|
values are inserted in format. |
63 |
|
|
64 |
|
=back |
65 |
|
|
66 |
|
This also describes order in which transformations are applied (eval, |
67 |
|
filter, lookup, format) which is important to undestand when deciding how to |
68 |
|
solve your data mungling and normalisation process. |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
74 |
|
|
77 |
Create new normalisation object |
Create new normalisation object |
78 |
|
|
79 |
my $n = new WebPAC::Normalize::Something( |
my $n = new WebPAC::Normalize::Something( |
80 |
cache_data_structure => './cache/ds/', |
filter => { |
81 |
|
'filter_name_1' => sub { |
82 |
|
# filter code |
83 |
|
return length($_); |
84 |
|
}, ... |
85 |
|
}, |
86 |
|
db => $db_obj, |
87 |
lookup_regex => $lookup->regex, |
lookup_regex => $lookup->regex, |
88 |
|
lookup => $lookup_obj, |
89 |
|
prefix => 'foobar', |
90 |
); |
); |
91 |
|
|
92 |
Optional parameter C<cache_data_structure> defines path to directory |
Parametar C<filter> defines user supplied snippets of perl code which can |
93 |
in which cache file for C<data_structure> call will be created. |
be use with C<filter{...}> notation. |
94 |
|
|
95 |
|
C<prefix> is used to form filename for database record (to support multiple |
96 |
|
source files which are joined in one database). |
97 |
|
|
98 |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
99 |
in structures. |
in structures. If you pass this parametar, you must also pass C<lookup> |
100 |
|
which is C<WebPAC::Lookup> object. |
101 |
|
|
102 |
=cut |
=cut |
103 |
|
|
106 |
my $self = {@_}; |
my $self = {@_}; |
107 |
bless($self, $class); |
bless($self, $class); |
108 |
|
|
109 |
$self->setup_cache_dir( $self->{'cache_data_structure'} ); |
my $r = $self->{'lookup_regex'} ? 1 : 0; |
110 |
|
my $l = $self->{'lookup'} ? 1 : 0; |
|
$self ? return $self : return undef; |
|
|
} |
|
|
|
|
|
=head2 setup_cache_dir |
|
|
|
|
|
Check if specified cache directory exist, and if not, disable caching. |
|
|
|
|
|
$setup_cache_dir('./cache/ds/'); |
|
|
|
|
|
If you pass false or zero value to this function, it will disable |
|
|
cacheing. |
|
111 |
|
|
112 |
=cut |
my $log = $self->_get_logger(); |
113 |
|
|
114 |
sub setup_cache_dir { |
# those two must be in pair |
115 |
my $self = shift; |
if ( ($r & $l) != ($r || $l) ) { |
116 |
|
my $log = $self->_get_logger(); |
117 |
|
$log->logdie("lookup_regex and lookup must be in pair"); |
118 |
|
} |
119 |
|
|
120 |
my $dir = shift; |
$log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup')); |
121 |
|
|
122 |
my $log = $self->_get_logger(); |
$log->warn("no prefix defined. please check that!") unless ($self->{'prefix'}); |
123 |
|
|
124 |
if ($dir) { |
$log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l); |
|
my $msg; |
|
|
if (! -e $dir) { |
|
|
$msg = "doesn't exist"; |
|
|
} elsif (! -d $dir) { |
|
|
$msg = "is not directory"; |
|
|
} elsif (! -w $dir) { |
|
|
$msg = "not writable"; |
|
|
} |
|
125 |
|
|
126 |
if ($msg) { |
if ($self->{filter} && ! $self->{filter}->{regex}) { |
127 |
undef $self->{'cache_data_structure'}; |
$log->debug("adding built-in filter regex"); |
128 |
$log->warn("cache_data_structure $dir $msg, disabling..."); |
$self->{filter}->{regex} = sub { |
129 |
} else { |
my ($val, $regex) = @_; |
130 |
$log->debug("using cache dir $dir"); |
eval "\$val =~ $regex"; |
131 |
} |
return $val; |
132 |
} else { |
}; |
|
$log->debug("disabling cache"); |
|
|
undef $self->{'cache_data_structure'}; |
|
133 |
} |
} |
134 |
|
|
135 |
|
$self ? return $self : return undef; |
136 |
} |
} |
137 |
|
|
138 |
|
|
143 |
|
|
144 |
This structures are used to produce output. |
This structures are used to produce output. |
145 |
|
|
146 |
my @ds = $webpac->data_structure($rec); |
my $ds = $webpac->data_structure($rec); |
|
|
|
|
B<Note: historical oddity follows> |
|
|
|
|
|
This method will also set C<< $webpac->{'currnet_filename'} >> if there is |
|
|
C<< <filename> >> tag and C<< $webpac->{'headline'} >> if there is |
|
|
C<< <headline> >> tag. |
|
147 |
|
|
148 |
=cut |
=cut |
149 |
|
|
155 |
my $rec = shift; |
my $rec = shift; |
156 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
157 |
|
|
158 |
|
$log->debug("data_structure rec = ", sub { Dumper($rec) }); |
159 |
|
|
160 |
|
$log->logdie("need unique ID (mfn) in field 000 of record ", sub { Dumper($rec) } ) unless (defined($rec->{'000'})); |
161 |
|
|
162 |
|
my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!"); |
163 |
|
|
164 |
my $cache_file; |
my $cache_file; |
165 |
|
|
166 |
if (my $cache_path = $self->{'cache_data_structure'}) { |
if ($self->{'db'}) { |
167 |
my $id = $rec->{'000'}; |
my $ds = $self->{'db'}->load_ds( id => $id, prefix => $self->{prefix} ); |
168 |
$id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o); |
$log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) }); |
169 |
unless (defined($id)) { |
return $ds if ($ds); |
170 |
$log->warn("Can't use cache_data_structure on records without unique identifier in field 000"); |
$log->debug("cache miss, creating"); |
|
undef $self->{'cache_data_structure'}; |
|
|
} else { |
|
|
$cache_file = "$cache_path/$id"; |
|
|
if (-r $cache_file) { |
|
|
my $ds_ref = retrieve($cache_file); |
|
|
if ($ds_ref) { |
|
|
$log->debug("cache hit: $cache_file"); |
|
|
my $ok = 1; |
|
|
foreach my $f (qw(current_filename headline)) { |
|
|
if ($ds_ref->{$f}) { |
|
|
$self->{$f} = $ds_ref->{$f}; |
|
|
} else { |
|
|
$ok = 0; |
|
|
} |
|
|
}; |
|
|
if ($ok && $ds_ref->{'ds'}) { |
|
|
return @{ $ds_ref->{'ds'} }; |
|
|
} else { |
|
|
$log->warn("cache_data_structure $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!"); |
|
|
undef $self->{'cache_data_structure'}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
171 |
} |
} |
172 |
|
|
173 |
undef $self->{'currnet_filename'}; |
undef $self->{'currnet_filename'}; |
181 |
$self->{tags_by_order} = \@sorted_tags; |
$self->{tags_by_order} = \@sorted_tags; |
182 |
} |
} |
183 |
|
|
184 |
my @ds; |
my $ds; |
185 |
|
|
186 |
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
187 |
|
|
192 |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
193 |
|
|
194 |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
195 |
my $format = $tag->{'value'} || $tag->{'content'}; |
my $format; |
196 |
|
|
197 |
|
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
198 |
|
$format = $tag->{'value'} || $tag->{'content'}; |
199 |
|
|
200 |
$log->debug("format: $format"); |
$log->debug("format: $format"); |
201 |
|
|
216 |
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
217 |
} |
} |
218 |
|
|
|
if ($field eq 'filename') { |
|
|
$self->{'current_filename'} = join('',@v); |
|
|
$log->debug("filename: ",$self->{'current_filename'}); |
|
|
} elsif ($field eq 'headline') { |
|
|
$self->{'headline'} .= join('',@v); |
|
|
$log->debug("headline: ",$self->{'headline'}); |
|
|
next; # don't return headline in data_structure! |
|
|
} |
|
|
|
|
219 |
# delimiter will join repeatable fields |
# delimiter will join repeatable fields |
220 |
if ($tag->{'delimiter'}) { |
if ($tag->{'delimiter'}) { |
221 |
@v = ( join($tag->{'delimiter'}, @v) ); |
@v = ( join($tag->{'delimiter'}, @v) ); |
222 |
} |
} |
223 |
|
|
224 |
# default types |
# default types |
225 |
my @types = qw(display swish); |
my @types = qw(display search); |
226 |
# override by type attribute |
# override by type attribute |
227 |
@types = ( $tag->{'type'} ) if ($tag->{'type'}); |
@types = ( $tag->{'type'} ) if ($tag->{'type'}); |
228 |
|
|
229 |
foreach my $type (@types) { |
foreach my $type (@types) { |
230 |
# append to previous line? |
# append to previous line? |
231 |
$log->debug("type: $type ",sub { join(" ",@v) }, $row->{'append'} || 'no append'); |
$log->debug("type: $type ",sub { join(" ",@v) }, " ", $row->{'append'} || 'no append'); |
232 |
if ($tag->{'append'}) { |
if ($tag->{'append'}) { |
233 |
|
|
234 |
# I will delimit appended part with |
# I will delimit appended part with |
255 |
|
|
256 |
# TODO: name_sigular, name_plural |
# TODO: name_sigular, name_plural |
257 |
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
258 |
$row->{'name'} = $name ? $self->_x($name) : $field; |
my $row_name = $name ? $self->_x($name) : $field; |
259 |
|
|
260 |
# post-sort all values in field |
# post-sort all values in field |
261 |
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
262 |
$log->warn("sort at field tag not implemented"); |
$log->warn("sort at field tag not implemented"); |
263 |
} |
} |
264 |
|
|
265 |
push @ds, $row; |
$ds->{$row_name} = $row; |
266 |
|
|
267 |
$log->debug("row $field: ",sub { Dumper($row) }); |
$log->debug("row $field: ",sub { Dumper($row) }); |
268 |
} |
} |
269 |
|
|
270 |
} |
} |
271 |
|
|
272 |
if ($cache_file) { |
$self->{'db'}->save_ds( |
273 |
store { |
id => $id, |
274 |
ds => \@ds, |
ds => $ds, |
275 |
current_filename => $self->{'current_filename'}, |
prefix => $self->{prefix}, |
276 |
headline => $self->{'headline'}, |
) if ($self->{'db'}); |
|
}, $cache_file; |
|
|
$log->debug("created storable cache file $cache_file"); |
|
|
} |
|
|
|
|
|
return @ds; |
|
|
|
|
|
} |
|
|
|
|
|
=head2 apply_format |
|
|
|
|
|
Apply format specified in tag with C<format_name="name"> and |
|
|
C<format_delimiter=";;">. |
|
|
|
|
|
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
|
|
|
|
|
Formats can contain C<lookup{...}> if you need them. |
|
|
|
|
|
=cut |
|
|
|
|
|
sub apply_format { |
|
|
my $self = shift; |
|
|
|
|
|
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); |
|
277 |
|
|
278 |
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
$log->debug("ds: ", sub { Dumper($ds) }); |
279 |
|
|
280 |
my @data = split(/\Q$delimiter\E/, $data); |
$log->logconfess("data structure returned is not array any more!") if wantarray; |
281 |
|
|
282 |
my $out = sprintf($format, @data); |
return $ds; |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
|
|
|
|
|
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
|
|
return $self->lookup($out); |
|
|
} else { |
|
|
return $out; |
|
|
} |
|
283 |
|
|
284 |
} |
} |
285 |
|
|
291 |
|
|
292 |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
293 |
|
|
294 |
|
Filters are implemented here. While simple form of filters looks like this: |
295 |
|
|
296 |
|
filter{name_of_filter} |
297 |
|
|
298 |
|
but, filters can also have variable number of parametars like this: |
299 |
|
|
300 |
|
filter{name_of_filter(param,param,param)} |
301 |
|
|
302 |
=cut |
=cut |
303 |
|
|
304 |
sub parse { |
sub parse { |
370 |
return if (! $self->_eval($eval)); |
return if (! $self->_eval($eval)); |
371 |
} |
} |
372 |
|
|
373 |
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
if ($filter_name) { |
374 |
$log->debug("about to filter{$filter_name} format: $out"); |
my @filter_args; |
375 |
$out = $self->{'filter'}->{$filter_name}->($out); |
if ($filter_name =~ s/(\w+)\((.*)\)/$1/) { |
376 |
return unless(defined($out)); |
@filter_args = split(/,/, $2); |
377 |
$log->debug("filter result: $out"); |
} |
378 |
|
if ($self->{'filter'}->{$filter_name}) { |
379 |
|
$log->debug("about to filter{$filter_name} format: $out with arguments: ", join(",", @filter_args)); |
380 |
|
unshift @filter_args, $out; |
381 |
|
$out = $self->{'filter'}->{$filter_name}->(@filter_args); |
382 |
|
return unless(defined($out)); |
383 |
|
$log->debug("filter result: $out"); |
384 |
|
} else { |
385 |
|
$log->warn("trying to use undefined filter $filter_name"); |
386 |
|
} |
387 |
} |
} |
388 |
|
|
389 |
return $out; |
return $out; |
419 |
return @arr; |
return @arr; |
420 |
} |
} |
421 |
|
|
422 |
|
|
423 |
|
=head2 fill_in |
424 |
|
|
425 |
|
Workhourse of all: takes record from in-memory structure of database and |
426 |
|
strings with placeholders and returns string or array of with substituted |
427 |
|
values from record. |
428 |
|
|
429 |
|
my $text = $webpac->fill_in($rec,'v250^a'); |
430 |
|
|
431 |
|
Optional argument is ordinal number for repeatable fields. By default, |
432 |
|
it's assume to be first repeatable field (fields are perl array, so first |
433 |
|
element is 0). |
434 |
|
Following example will read second value from repeatable field. |
435 |
|
|
436 |
|
my $text = $webpac->fill_in($rec,'Title: v250^a',1); |
437 |
|
|
438 |
|
This function B<does not> perform parsing of format to inteligenty skip |
439 |
|
delimiters before fields which aren't used. |
440 |
|
|
441 |
|
This method will automatically decode UTF-8 string to local code page |
442 |
|
if needed. |
443 |
|
|
444 |
|
=cut |
445 |
|
|
446 |
|
sub fill_in { |
447 |
|
my $self = shift; |
448 |
|
|
449 |
|
my $log = $self->_get_logger(); |
450 |
|
|
451 |
|
my $rec = shift || $log->logconfess("need data record"); |
452 |
|
my $format = shift || $log->logconfess("need format to parse"); |
453 |
|
# iteration (for repeatable fields) |
454 |
|
my $i = shift || 0; |
455 |
|
|
456 |
|
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
457 |
|
|
458 |
|
# FIXME remove for speedup? |
459 |
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
460 |
|
|
461 |
|
if (utf8::is_utf8($format)) { |
462 |
|
$format = $self->_x($format); |
463 |
|
} |
464 |
|
|
465 |
|
my $found = 0; |
466 |
|
|
467 |
|
my $eval_code; |
468 |
|
# remove eval{...} from beginning |
469 |
|
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
470 |
|
|
471 |
|
my $filter_name; |
472 |
|
# remove filter{...} from beginning |
473 |
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
474 |
|
|
475 |
|
# do actual replacement of placeholders |
476 |
|
# repeatable fields |
477 |
|
$format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; |
478 |
|
# non-repeatable fields |
479 |
|
$format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found)/ges; |
480 |
|
|
481 |
|
if ($found) { |
482 |
|
$log->debug("format: $format"); |
483 |
|
if ($eval_code) { |
484 |
|
my $eval = $self->fill_in($rec,$eval_code,$i); |
485 |
|
return if (! $self->_eval($eval)); |
486 |
|
} |
487 |
|
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
488 |
|
$log->debug("filter '$filter_name' for $format"); |
489 |
|
$format = $self->{'filter'}->{$filter_name}->($format); |
490 |
|
return unless(defined($format)); |
491 |
|
$log->debug("filter result: $format"); |
492 |
|
} |
493 |
|
# do we have lookups? |
494 |
|
if ($self->{'lookup'}) { |
495 |
|
if ($self->{'lookup'}->can('lookup')) { |
496 |
|
my @lookup = $self->{lookup}->lookup($format); |
497 |
|
$log->debug("lookup $format", join(", ", @lookup)); |
498 |
|
return @lookup; |
499 |
|
} else { |
500 |
|
$log->warn("Have lookup object but can't invoke lookup method"); |
501 |
|
} |
502 |
|
} else { |
503 |
|
return $format; |
504 |
|
} |
505 |
|
} else { |
506 |
|
return; |
507 |
|
} |
508 |
|
} |
509 |
|
|
510 |
|
|
511 |
=head2 fill_in_to_arr |
=head2 fill_in_to_arr |
512 |
|
|
513 |
Similar to C<fill_in>, but returns array of all repeatable fields. Usable |
Similar to C<fill_in>, but returns array of all repeatable fields. Usable |
540 |
return @arr; |
return @arr; |
541 |
} |
} |
542 |
|
|
543 |
|
|
544 |
|
=head2 get_data |
545 |
|
|
546 |
|
Returns value from record. |
547 |
|
|
548 |
|
my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); |
549 |
|
|
550 |
|
Arguments are: |
551 |
|
record reference C<$rec>, |
552 |
|
field C<$f>, |
553 |
|
optional subfiled C<$sf>, |
554 |
|
index for repeatable values C<$i>. |
555 |
|
|
556 |
|
Optinal variable C<$found> will be incremeted if there |
557 |
|
is field. |
558 |
|
|
559 |
|
Returns value or empty string. |
560 |
|
|
561 |
|
=cut |
562 |
|
|
563 |
|
sub get_data { |
564 |
|
my $self = shift; |
565 |
|
|
566 |
|
my ($rec,$f,$sf,$i,$found) = @_; |
567 |
|
|
568 |
|
if ($$rec->{$f}) { |
569 |
|
return '' if (! $$rec->{$f}->[$i]); |
570 |
|
no strict 'refs'; |
571 |
|
if ($sf && $$rec->{$f}->[$i]->{$sf}) { |
572 |
|
$$found++ if (defined($$found)); |
573 |
|
return $$rec->{$f}->[$i]->{$sf}; |
574 |
|
} elsif (! $sf && $$rec->{$f}->[$i]) { |
575 |
|
$$found++ if (defined($$found)); |
576 |
|
# it still might have subfield, just |
577 |
|
# not specified, so we'll dump all |
578 |
|
if ($$rec->{$f}->[$i] =~ /HASH/o) { |
579 |
|
my $out; |
580 |
|
foreach my $k (keys %{$$rec->{$f}->[$i]}) { |
581 |
|
$out .= $$rec->{$f}->[$i]->{$k}." "; |
582 |
|
} |
583 |
|
return $out; |
584 |
|
} else { |
585 |
|
return $$rec->{$f}->[$i]; |
586 |
|
} |
587 |
|
} else { |
588 |
|
return ''; |
589 |
|
} |
590 |
|
} else { |
591 |
|
return ''; |
592 |
|
} |
593 |
|
} |
594 |
|
|
595 |
|
|
596 |
|
=head2 apply_format |
597 |
|
|
598 |
|
Apply format specified in tag with C<format_name="name"> and |
599 |
|
C<format_delimiter=";;">. |
600 |
|
|
601 |
|
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
602 |
|
|
603 |
|
Formats can contain C<lookup{...}> if you need them. |
604 |
|
|
605 |
|
=cut |
606 |
|
|
607 |
|
sub apply_format { |
608 |
|
my $self = shift; |
609 |
|
|
610 |
|
my ($name,$delimiter,$data) = @_; |
611 |
|
|
612 |
|
my $log = $self->_get_logger(); |
613 |
|
|
614 |
|
if (! $self->{'import_xml'}->{'format'}->{$name}) { |
615 |
|
$log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'}); |
616 |
|
return $data; |
617 |
|
} |
618 |
|
|
619 |
|
$log->warn("no delimiter for format $name") if (! $delimiter); |
620 |
|
|
621 |
|
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
622 |
|
|
623 |
|
my @data = split(/\Q$delimiter\E/, $data); |
624 |
|
|
625 |
|
my $out = sprintf($format, @data); |
626 |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
627 |
|
|
628 |
|
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
629 |
|
return $self->{'lookup'}->lookup($out); |
630 |
|
} else { |
631 |
|
return $out; |
632 |
|
} |
633 |
|
|
634 |
|
} |
635 |
|
|
636 |
=head2 sort_arr |
=head2 sort_arr |
637 |
|
|
638 |
Sort array ignoring case and html in data |
Sort array ignoring case and html in data |
659 |
} |
} |
660 |
|
|
661 |
|
|
662 |
|
=head1 INTERNAL METHODS |
663 |
|
|
664 |
=head2 _sort_by_order |
=head2 _sort_by_order |
665 |
|
|
666 |
Sort xml tags data structure accoding to C<order=""> attribute. |
Sort xml tags data structure accoding to C<order=""> attribute. |
680 |
|
|
681 |
=head2 _x |
=head2 _x |
682 |
|
|
683 |
Convert strings from C<conf/normalize> encoding into application specific |
Convert strings from C<conf/normalize/*.xml> encoding into application |
684 |
(optinally specified using C<code_page> to C<new> constructor. |
specific encoding (optinally specified using C<code_page> to C<new> |
685 |
|
constructor). |
686 |
|
|
687 |
my $text = $n->_x('normalize text string'); |
my $text = $n->_x('normalize text string'); |
688 |
|
|
709 |
|
|
710 |
=cut |
=cut |
711 |
|
|
712 |
1; # End of WebPAC::DB |
1; # End of WebPAC::Normalize |