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 |
|
marc21 |
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 |
|
|
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 - normalisation of source file |
WebPAC::Normalize - describe normalisaton rules using sets |
25 |
|
|
26 |
=head1 VERSION |
=head1 VERSION |
27 |
|
|
28 |
Version 0.01 |
Version 0.06 |
29 |
|
|
30 |
=cut |
=cut |
31 |
|
|
32 |
our $VERSION = '0.01'; |
our $VERSION = '0.06'; |
33 |
|
|
34 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
35 |
|
|
36 |
This package contains code that could be helpful in implementing different |
This module uses C<conf/normalize/*.pl> files to perform normalisation |
37 |
normalisation front-ends. |
from input records using perl functions which are specialized for set |
38 |
|
processing. |
39 |
|
|
40 |
|
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 |
|
C<perl -c normalize.pl>. |
43 |
|
|
44 |
|
Normalisation can generate multiple output normalized data. For now, supported output |
45 |
|
types (on the left side of definition) are: C<tag>, C<display>, C<search> and |
46 |
|
C<marc21>. |
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 |
|
=head2 data_structure |
54 |
|
|
55 |
Create new normalisation object |
Return data structure |
56 |
|
|
57 |
my $n = new WebPAC::Normalize::Something( |
my $ds = WebPAC::Normalize::data_structure( |
58 |
cache_data_structure => './cache/ds/', |
lookup => $lookup->lookup_hash, |
59 |
lookup_regex => $lookup->regex, |
row => $row, |
60 |
|
rules => $normalize_pl_config, |
61 |
|
marc_encoding => 'utf-8', |
62 |
); |
); |
63 |
|
|
64 |
Optional parameter C<cache_data_structure> defines path to directory |
Options C<lookup>, C<row>, C<rules> and C<log> are mandatory while all |
65 |
in which cache file for C<data_structure> call will be created. |
other are optional. |
66 |
|
|
67 |
|
This function will B<die> if normalizastion can't be evaled. |
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. |
C<WebPAC::Normalize::data_structure>. |
71 |
|
|
72 |
=cut |
=cut |
73 |
|
|
74 |
sub new { |
sub data_structure { |
75 |
my $class = shift; |
my $arg = {@_}; |
|
my $self = {@_}; |
|
|
bless($self, $class); |
|
76 |
|
|
77 |
$self->setup_cache_dir( $self->{'cache_data_structure'} ); |
die "need row argument" unless ($arg->{row}); |
78 |
|
die "need normalisation argument" unless ($arg->{rules}); |
79 |
|
|
80 |
$self ? return $self : return undef; |
no strict 'subs'; |
81 |
} |
_set_lookup( $arg->{lookup} ); |
82 |
|
_set_rec( $arg->{row} ); |
83 |
|
_clean_ds( %{ $arg } ); |
84 |
|
eval "$arg->{rules}"; |
85 |
|
die "error evaling $arg->{rules}: $@\n" if ($@); |
86 |
|
|
87 |
=head2 setup_cache_dir |
return _get_ds(); |
88 |
|
} |
89 |
|
|
90 |
Check if specified cache directory exist, and if not, disable caching. |
=head2 _set_rec |
91 |
|
|
92 |
$setup_cache_dir('./cache/ds/'); |
Set current record hash |
93 |
|
|
94 |
If you pass false or zero value to this function, it will disable |
_set_rec( $rec ); |
|
cacheing. |
|
95 |
|
|
96 |
=cut |
=cut |
97 |
|
|
98 |
sub setup_cache_dir { |
my $rec; |
|
my $self = shift; |
|
99 |
|
|
100 |
my $dir = shift; |
sub _set_rec { |
101 |
|
$rec = shift or die "no record hash"; |
|
my $log = $self->_get_logger(); |
|
|
|
|
|
if ($dir) { |
|
|
my $msg; |
|
|
if (! -e $dir) { |
|
|
$msg = "doesn't exist"; |
|
|
} elsif (! -d $dir) { |
|
|
$msg = "is not directory"; |
|
|
} elsif (! -w $dir) { |
|
|
$msg = "not writable"; |
|
|
} |
|
|
|
|
|
if ($msg) { |
|
|
undef $self->{'cache_data_structure'}; |
|
|
$log->warn("cache_data_structure $dir $msg, disabling..."); |
|
|
} else { |
|
|
$log->debug("using cache dir $dir"); |
|
|
} |
|
|
} else { |
|
|
$log->debug("disabling cache"); |
|
|
undef $self->{'cache_data_structure'}; |
|
|
} |
|
102 |
} |
} |
103 |
|
|
104 |
|
=head2 _get_ds |
105 |
|
|
106 |
=head2 data_structure |
Return hash formatted as data structure |
|
|
|
|
Create in-memory data structure which represents normalized layout from |
|
|
C<conf/normalize/*.xml>. |
|
|
|
|
|
This structures are used to produce output. |
|
|
|
|
|
my @ds = $webpac->data_structure($rec); |
|
|
|
|
|
B<Note: historical oddity follows> |
|
107 |
|
|
108 |
This method will also set C<< $webpac->{'currnet_filename'} >> if there is |
my $ds = _get_ds(); |
|
C<< <filename> >> tag and C<< $webpac->{'headline'} >> if there is |
|
|
C<< <headline> >> tag. |
|
109 |
|
|
110 |
=cut |
=cut |
111 |
|
|
112 |
sub data_structure { |
my $out; |
113 |
my $self = shift; |
my $marc21; |
114 |
|
my $marc_encoding; |
|
my $log = $self->_get_logger(); |
|
|
|
|
|
my $rec = shift; |
|
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
115 |
|
|
116 |
my $cache_file; |
sub _get_ds { |
117 |
|
return $out; |
118 |
if (my $cache_path = $self->{'cache_data_structure'}) { |
} |
|
my $id = $rec->{'000'}; |
|
|
$id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o); |
|
|
unless (defined($id)) { |
|
|
$log->warn("Can't use cache_data_structure on records without unique identifier in field 000"); |
|
|
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'}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
119 |
|
|
120 |
undef $self->{'currnet_filename'}; |
=head2 _clean_ds |
|
undef $self->{'headline'}; |
|
121 |
|
|
122 |
my @sorted_tags; |
Clean data structure hash for next record |
|
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; |
|
|
} |
|
123 |
|
|
124 |
my @ds; |
_clean_ds(); |
125 |
|
|
126 |
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
=cut |
127 |
|
|
128 |
foreach my $field (@sorted_tags) { |
sub _clean_ds { |
129 |
|
my $a = {@_}; |
130 |
|
$out = undef; |
131 |
|
$marc21 = undef; |
132 |
|
$marc_encoding = $a->{marc_encoding}; |
133 |
|
} |
134 |
|
|
135 |
my $row; |
=head2 _set_lookup |
136 |
|
|
137 |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
Set current lookup hash |
138 |
|
|
139 |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
_set_lookup( $lookup ); |
|
my $format = $tag->{'value'} || $tag->{'content'}; |
|
140 |
|
|
141 |
$log->debug("format: $format"); |
=cut |
142 |
|
|
143 |
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); |
|
144 |
|
|
145 |
if ($tag->{'sort'}) { |
sub _set_lookup { |
146 |
@v = $self->sort_arr(@v); |
$lookup = shift; |
147 |
} |
} |
148 |
|
|
149 |
# use format? |
=head2 _get_marc21_fields |
|
if ($tag->{'format_name'}) { |
|
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
|
|
} |
|
150 |
|
|
151 |
if ($field eq 'filename') { |
Get all fields defined by calls to C<marc21> |
|
$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! |
|
|
} |
|
152 |
|
|
153 |
# delimiter will join repeatable fields |
$marc->add_fields( WebPAC::Normalize:_get_marc21_fields() ); |
|
if ($tag->{'delimiter'}) { |
|
|
@v = ( join($tag->{'delimiter'}, @v) ); |
|
|
} |
|
154 |
|
|
|
# default types |
|
|
my @types = qw(display swish); |
|
|
# 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; |
|
|
} |
|
|
} |
|
155 |
|
|
156 |
|
|
157 |
} |
We are using I<magic> which detect repeatable fields only from |
158 |
|
sequence of field/subfield data generated by normalization. |
159 |
|
|
160 |
if ($row) { |
Repeatable field is created if there is second occurence of same subfield or |
161 |
$row->{'tag'} = $field; |
if any of indicators are different. This is sane for most cases except for |
162 |
|
non-repeatable fields with repeatable subfields. |
163 |
|
|
164 |
# TODO: name_sigular, name_plural |
B<TODO>: implement exceptions to magic |
|
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
|
|
$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"); |
|
|
} |
|
165 |
|
|
166 |
push @ds, $row; |
=cut |
167 |
|
|
168 |
$log->debug("row $field: ",sub { Dumper($row) }); |
sub _get_marc21_fields { |
169 |
|
my @m; |
170 |
|
my $last; |
171 |
|
foreach my $row (@{ $marc21 }) { |
172 |
|
if ($last && |
173 |
|
$last->[0] eq $row->[0] && # check if field is same |
174 |
|
$last->[1] eq $row->[1] && # check for i1 |
175 |
|
$last->[2] eq $row->[2] && # and for i2 |
176 |
|
$last->[3] ne $row->[3] # and subfield is different |
177 |
|
) { |
178 |
|
push @$last, ( $row->[3] , $row->[4] ); |
179 |
|
warn "## ++ added $row->[0] ^$row->[3] to $last->[0]\n"; |
180 |
|
next; |
181 |
|
} elsif ($last) { |
182 |
|
push @m, $last; |
183 |
} |
} |
184 |
|
|
185 |
|
$last = $row; |
186 |
} |
} |
187 |
|
|
188 |
if ($cache_file) { |
push @m, $last if ($last); |
|
store { |
|
|
ds => \@ds, |
|
|
current_filename => $self->{'current_filename'}, |
|
|
headline => $self->{'headline'}, |
|
|
}, $cache_file; |
|
|
$log->debug("created storable cache file $cache_file"); |
|
|
} |
|
|
|
|
|
return @ds; |
|
189 |
|
|
190 |
|
return @m; |
191 |
} |
} |
192 |
|
|
193 |
=head2 apply_format |
=head1 Functions to create C<data_structure> |
194 |
|
|
195 |
Apply format specified in tag with C<format_name="name"> and |
Those functions generally have to first in your normalization file. |
|
C<format_delimiter=";;">. |
|
196 |
|
|
197 |
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
=head2 tag |
|
|
|
|
Formats can contain C<lookup{...}> if you need them. |
|
|
|
|
|
=cut |
|
198 |
|
|
199 |
sub apply_format { |
Define new tag for I<search> and I<display>. |
|
my $self = shift; |
|
200 |
|
|
201 |
my ($name,$delimiter,$data) = @_; |
tag('Title', rec('200','a') ); |
202 |
|
|
|
my $log = $self->_get_logger(); |
|
203 |
|
|
204 |
if (! $self->{'import_xml'}->{'format'}->{$name}) { |
=cut |
|
$log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'}); |
|
|
return $data; |
|
|
} |
|
205 |
|
|
206 |
$log->warn("no delimiter for format $name") if (! $delimiter); |
sub tag { |
207 |
|
my $name = shift or die "tag needs name as first argument"; |
208 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
209 |
|
return unless (@o); |
210 |
|
$out->{$name}->{tag} = $name; |
211 |
|
$out->{$name}->{search} = \@o; |
212 |
|
$out->{$name}->{display} = \@o; |
213 |
|
} |
214 |
|
|
215 |
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
=head2 display |
216 |
|
|
217 |
my @data = split(/\Q$delimiter\E/, $data); |
Define tag just for I<display> |
218 |
|
|
219 |
my $out = sprintf($format, @data); |
@v = display('Title', rec('200','a') ); |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
|
220 |
|
|
221 |
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
=cut |
|
return $self->lookup($out); |
|
|
} else { |
|
|
return $out; |
|
|
} |
|
222 |
|
|
223 |
|
sub display { |
224 |
|
my $name = shift or die "display needs name as first argument"; |
225 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
226 |
|
return unless (@o); |
227 |
|
$out->{$name}->{tag} = $name; |
228 |
|
$out->{$name}->{display} = \@o; |
229 |
} |
} |
230 |
|
|
231 |
=head2 parse |
=head2 search |
232 |
|
|
233 |
Perform smart parsing of string, skipping delimiters for fields which aren't |
Prepare values just for I<search> |
|
defined. It can also eval code in format starting with C<eval{...}> and |
|
|
return output or nothing depending on eval code. |
|
234 |
|
|
235 |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
@v = search('Title', rec('200','a') ); |
236 |
|
|
237 |
=cut |
=cut |
238 |
|
|
239 |
sub parse { |
sub search { |
240 |
my $self = shift; |
my $name = shift or die "search needs name as first argument"; |
241 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
242 |
my ($rec, $format_utf8, $i) = @_; |
return unless (@o); |
243 |
|
$out->{$name}->{tag} = $name; |
244 |
|
$out->{$name}->{search} = \@o; |
245 |
|
} |
246 |
|
|
247 |
return if (! $format_utf8); |
=head2 marc21 |
248 |
|
|
249 |
my $log = $self->_get_logger(); |
Save value for MARC field |
250 |
|
|
251 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
marc21('900','a', rec('200','a') ); |
252 |
|
|
253 |
$i = 0 if (! $i); |
=cut |
254 |
|
|
255 |
my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
sub marc21 { |
256 |
|
my $f = shift or die "marc21 needs field"; |
257 |
|
die "marc21 field must be numer" unless ($f =~ /^\d+$/); |
258 |
|
|
259 |
my @out; |
my $sf = shift or die "marc21 needs subfield"; |
260 |
|
|
261 |
$log->debug("format: $format"); |
foreach (@_) { |
262 |
|
my $v = $_; # make var read-write for Encode |
263 |
|
next unless (defined($v) && $v !~ /^\s*$/); |
264 |
|
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
265 |
|
push @{ $marc21 }, [ $f, ' ', ' ', $sf => $v ]; |
266 |
|
} |
267 |
|
} |
268 |
|
|
269 |
my $eval_code; |
=head1 Functions to extract data from input |
|
# remove eval{...} from beginning |
|
|
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
|
270 |
|
|
271 |
my $filter_name; |
This function should be used inside functions to create C<data_structure> described |
272 |
# remove filter{...} from beginning |
above. |
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
|
273 |
|
|
274 |
my $prefix; |
=head2 rec1 |
|
my $all_found=0; |
|
275 |
|
|
276 |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
Return all values in some field |
277 |
|
|
278 |
my $del = $1 || ''; |
@v = rec1('200') |
|
$prefix ||= $del if ($all_found == 0); |
|
279 |
|
|
280 |
# repeatable index |
TODO: order of values is probably same as in source data, need to investigate that |
|
my $r = $i; |
|
|
$r = 0 if (lc("$2") eq 's'); |
|
281 |
|
|
282 |
my $found = 0; |
=cut |
|
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
|
283 |
|
|
284 |
if ($found) { |
sub rec1 { |
285 |
push @out, $del; |
my $f = shift; |
286 |
push @out, $tmp; |
return unless (defined($rec) && defined($rec->{$f})); |
287 |
$all_found += $found; |
if (ref($rec->{$f}) eq 'ARRAY') { |
288 |
} |
return map { |
289 |
|
if (ref($_) eq 'HASH') { |
290 |
|
values %{$_}; |
291 |
|
} else { |
292 |
|
$_; |
293 |
|
} |
294 |
|
} @{ $rec->{$f} }; |
295 |
|
} elsif( defined($rec->{$f}) ) { |
296 |
|
return $rec->{$f}; |
297 |
} |
} |
298 |
|
} |
299 |
|
|
300 |
return if (! $all_found); |
=head2 rec2 |
|
|
|
|
my $out = join('',@out); |
|
|
|
|
|
if ($out) { |
|
|
# add rest of format (suffix) |
|
|
$out .= $format; |
|
301 |
|
|
302 |
# add prefix if not there |
Return all values in specific field and subfield |
|
$out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); |
|
303 |
|
|
304 |
$log->debug("result: $out"); |
@v = rec2('200','a') |
|
} |
|
305 |
|
|
306 |
if ($eval_code) { |
=cut |
|
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"); |
|
|
} |
|
307 |
|
|
308 |
return $out; |
sub rec2 { |
309 |
|
my $f = shift; |
310 |
|
return unless (defined($rec && $rec->{$f})); |
311 |
|
my $sf = shift; |
312 |
|
return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; |
313 |
} |
} |
314 |
|
|
315 |
=head2 parse_to_arr |
=head2 rec |
316 |
|
|
317 |
Similar to C<parse>, but returns array of all repeatable fields |
syntaxtic sugar for |
318 |
|
|
319 |
my @arr = $webpac->parse_to_arr($rec,'v250^a'); |
@v = rec('200') |
320 |
|
@v = rec('200','a') |
321 |
|
|
322 |
=cut |
=cut |
323 |
|
|
324 |
sub parse_to_arr { |
sub rec { |
325 |
my $self = shift; |
if ($#_ == 0) { |
326 |
|
return rec1(@_); |
327 |
|
} elsif ($#_ == 1) { |
328 |
|
return rec2(@_); |
329 |
|
} |
330 |
|
} |
331 |
|
|
332 |
my ($rec, $format_utf8) = @_; |
=head2 regex |
333 |
|
|
334 |
my $log = $self->_get_logger(); |
Apply regex to some or all values |
335 |
|
|
336 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
@v = regex( 's/foo/bar/g', @v ); |
|
return if (! $format_utf8); |
|
337 |
|
|
338 |
my $i = 0; |
=cut |
|
my @arr; |
|
339 |
|
|
340 |
while (my $v = $self->parse($rec,$format_utf8,$i++)) { |
sub regex { |
341 |
push @arr, $v; |
my $r = shift; |
342 |
|
my @out; |
343 |
|
#warn "r: $r\n",Dumper(\@_); |
344 |
|
foreach my $t (@_) { |
345 |
|
next unless ($t); |
346 |
|
eval "\$t =~ $r"; |
347 |
|
push @out, $t if ($t && $t ne ''); |
348 |
} |
} |
349 |
|
return @out; |
|
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
|
|
|
|
|
return @arr; |
|
350 |
} |
} |
351 |
|
|
352 |
=head2 fill_in_to_arr |
=head2 prefix |
353 |
|
|
354 |
Similar to C<fill_in>, but returns array of all repeatable fields. Usable |
Prefix all values with a string |
|
for fields which have lookups, so they shouldn't be parsed but rather |
|
|
C<fill_id>ed. |
|
355 |
|
|
356 |
my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); |
@v = prefix( 'my_', @v ); |
357 |
|
|
358 |
=cut |
=cut |
359 |
|
|
360 |
sub fill_in_to_arr { |
sub prefix { |
361 |
my $self = shift; |
my $p = shift or die "prefix needs string as first argument"; |
362 |
|
return map { $p . $_ } grep { defined($_) } @_; |
363 |
my ($rec, $format_utf8) = @_; |
} |
|
|
|
|
my $log = $self->_get_logger(); |
|
364 |
|
|
365 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
=head2 suffix |
|
return if (! $format_utf8); |
|
366 |
|
|
367 |
my $i = 0; |
suffix all values with a string |
|
my @arr; |
|
368 |
|
|
369 |
while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { |
@v = suffix( '_my', @v ); |
|
push @arr, @v; |
|
|
} |
|
370 |
|
|
371 |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
=cut |
372 |
|
|
373 |
return @arr; |
sub suffix { |
374 |
|
my $s = shift or die "suffix needs string as first argument"; |
375 |
|
return map { $_ . $s } grep { defined($_) } @_; |
376 |
} |
} |
377 |
|
|
378 |
=head2 sort_arr |
=head2 surround |
379 |
|
|
380 |
Sort array ignoring case and html in data |
surround all values with a two strings |
381 |
|
|
382 |
my @sorted = $webpac->sort_arr(@unsorted); |
@v = surround( 'prefix_', '_suffix', @v ); |
383 |
|
|
384 |
=cut |
=cut |
385 |
|
|
386 |
sub sort_arr { |
sub surround { |
387 |
my $self = shift; |
my $p = shift or die "surround need prefix as first argument"; |
388 |
|
my $s = shift or die "surround needs suffix as second argument"; |
389 |
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; |
|
390 |
} |
} |
391 |
|
|
392 |
|
=head2 first |
393 |
|
|
394 |
=head2 _sort_by_order |
Return first element |
395 |
|
|
396 |
Sort xml tags data structure accoding to C<order=""> attribute. |
$v = first( @v ); |
397 |
|
|
398 |
=cut |
=cut |
399 |
|
|
400 |
sub _sort_by_order { |
sub first { |
401 |
my $self = shift; |
my $r = shift; |
402 |
|
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; |
|
403 |
} |
} |
404 |
|
|
405 |
=head2 _x |
=head2 lookup |
|
|
|
|
Convert strings from C<conf/normalize> encoding into application specific |
|
|
(optinally specified using C<code_page> to C<new> constructor. |
|
406 |
|
|
407 |
my $text = $n->_x('normalize text string'); |
Consult lookup hashes for some value |
408 |
|
|
409 |
This is a stub so that other modules doesn't have to implement it. |
@v = lookup( $v ); |
410 |
|
@v = lookup( @v ); |
411 |
|
|
412 |
=cut |
=cut |
413 |
|
|
414 |
sub _x { |
sub lookup { |
415 |
my $self = shift; |
my $k = shift or return; |
416 |
return shift; |
return unless (defined($lookup->{$k})); |
417 |
|
if (ref($lookup->{$k}) eq 'ARRAY') { |
418 |
|
return @{ $lookup->{$k} }; |
419 |
|
} else { |
420 |
|
return $lookup->{$k}; |
421 |
|
} |
422 |
} |
} |
423 |
|
|
424 |
|
=head2 join_with |
425 |
|
|
426 |
=head1 AUTHOR |
Joins walues with some delimiter |
|
|
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
427 |
|
|
428 |
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
$v = join_with(", ", @v); |
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
|
under the same terms as Perl itself. |
|
429 |
|
|
430 |
=cut |
=cut |
431 |
|
|
432 |
1; # End of WebPAC::DB |
sub join_with { |
433 |
|
my $d = shift; |
434 |
|
return join($d, grep { defined($_) && $_ ne '' } @_); |
435 |
|
} |
436 |
|
|
437 |
|
# END |
438 |
|
1; |