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 |
|
_debug |
7 |
|
|
8 |
|
tag search display |
9 |
|
marc marc_indicators marc_repeatable_subfield |
10 |
|
marc_compose marc_leader |
11 |
|
marc_duplicate marc_remove |
12 |
|
|
13 |
|
rec1 rec2 rec |
14 |
|
regex prefix suffix surround |
15 |
|
first lookup join_with |
16 |
|
|
17 |
|
split_rec_on |
18 |
|
/; |
19 |
|
|
20 |
use warnings; |
use warnings; |
21 |
use strict; |
use strict; |
22 |
use blib; |
|
23 |
use WebPAC::Common; |
#use base qw/WebPAC::Common/; |
24 |
use base 'WebPAC::Common'; |
use Data::Dump qw/dump/; |
25 |
use Data::Dumper; |
use Encode qw/from_to/; |
26 |
|
use Storable qw/dclone/; |
27 |
|
|
28 |
|
# debugging warn(s) |
29 |
|
my $debug = 0; |
30 |
|
|
31 |
|
|
32 |
=head1 NAME |
=head1 NAME |
33 |
|
|
34 |
WebPAC::Normalize - data mungling for normalisation |
WebPAC::Normalize - describe normalisaton rules using sets |
35 |
|
|
36 |
=head1 VERSION |
=head1 VERSION |
37 |
|
|
38 |
Version 0.08 |
Version 0.11 |
39 |
|
|
40 |
=cut |
=cut |
41 |
|
|
42 |
our $VERSION = '0.08'; |
our $VERSION = '0.11'; |
43 |
|
|
44 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
45 |
|
|
46 |
This package contains code that mungle data to produce normalized format. |
This module uses C<conf/normalize/*.pl> files to perform normalisation |
47 |
|
from input records using perl functions which are specialized for set |
48 |
|
processing. |
49 |
|
|
50 |
|
Sets are implemented as arrays, and normalisation file is valid perl, which |
51 |
|
means that you check it's validity before running WebPAC using |
52 |
|
C<perl -c normalize.pl>. |
53 |
|
|
54 |
|
Normalisation can generate multiple output normalized data. For now, supported output |
55 |
|
types (on the left side of definition) are: C<tag>, C<display>, C<search> and |
56 |
|
C<marc>. |
57 |
|
|
58 |
It contains several assumptions: |
=head1 FUNCTIONS |
59 |
|
|
60 |
=over |
Functions which start with C<_> are private and used by WebPAC internally. |
61 |
|
All other functions are available for use within normalisation rules. |
62 |
|
|
63 |
=item * |
=head2 data_structure |
64 |
|
|
65 |
format of fields is defined using C<v123^a> notation for repeatable fields |
Return data structure |
|
or C<s123^a> for single (or first) value, where C<123> is field number and |
|
|
C<a> is subfield. |
|
66 |
|
|
67 |
=item * |
my $ds = WebPAC::Normalize::data_structure( |
68 |
|
lookup => $lookup->lookup_hash, |
69 |
|
row => $row, |
70 |
|
rules => $normalize_pl_config, |
71 |
|
marc_encoding => 'utf-8', |
72 |
|
); |
73 |
|
|
74 |
source data records (C<$rec>) have unique identifiers in field C<000> |
Options C<lookup>, C<row>, C<rules> and C<log> are mandatory while all |
75 |
|
other are optional. |
76 |
|
|
77 |
=item * |
This function will B<die> if normalizastion can't be evaled. |
78 |
|
|
79 |
optional C<eval{length('v123^a') == 3}> tag at B<beginning of format> will be |
Since this function isn't exported you have to call it with |
80 |
perl code that is evaluated before producing output (value of field will be |
C<WebPAC::Normalize::data_structure>. |
|
interpolated before that) |
|
81 |
|
|
82 |
=item * |
=cut |
83 |
|
|
84 |
optional C<filter{filter_name}> at B<begining of format> will apply perl |
sub data_structure { |
85 |
code defined as code ref on format after field substitution to producing |
my $arg = {@_}; |
|
output |
|
86 |
|
|
87 |
There is one built-in filter called C<regex> which can be use like this: |
die "need row argument" unless ($arg->{row}); |
88 |
|
die "need normalisation argument" unless ($arg->{rules}); |
89 |
|
|
90 |
filter{regex(s/foo/bar/)} |
no strict 'subs'; |
91 |
|
_set_lookup( $arg->{lookup} ); |
92 |
|
_set_rec( $arg->{row} ); |
93 |
|
_clean_ds( %{ $arg } ); |
94 |
|
eval "$arg->{rules}"; |
95 |
|
die "error evaling $arg->{rules}: $@\n" if ($@); |
96 |
|
|
97 |
=item * |
return _get_ds(); |
98 |
|
} |
99 |
|
|
100 |
optional C<lookup{...}> will be then performed. See C<WebPAC::Lookups>. |
=head2 _set_rec |
101 |
|
|
102 |
=item * |
Set current record hash |
103 |
|
|
104 |
at end, optional C<format>s rules are resolved. Format rules are similar to |
_set_rec( $rec ); |
|
C<sprintf> and can also contain C<lookup{...}> which is performed after |
|
|
values are inserted in format. |
|
105 |
|
|
106 |
=back |
=cut |
107 |
|
|
108 |
This also describes order in which transformations are applied (eval, |
my $rec; |
|
filter, lookup, format) which is important to undestand when deciding how to |
|
|
solve your data mungling and normalisation process. |
|
109 |
|
|
110 |
|
sub _set_rec { |
111 |
|
$rec = shift or die "no record hash"; |
112 |
|
} |
113 |
|
|
114 |
|
=head2 _get_ds |
115 |
|
|
116 |
|
Return hash formatted as data structure |
117 |
|
|
118 |
=head1 FUNCTIONS |
my $ds = _get_ds(); |
119 |
|
|
120 |
=head2 new |
=cut |
121 |
|
|
122 |
Create new normalisation object |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); |
123 |
|
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
124 |
|
|
125 |
my $n = new WebPAC::Normalize::Something( |
sub _get_ds { |
126 |
filter => { |
return $out; |
127 |
'filter_name_1' => sub { |
} |
|
# filter code |
|
|
return length($_); |
|
|
}, ... |
|
|
}, |
|
|
db => $db_obj, |
|
|
lookup_regex => $lookup->regex, |
|
|
lookup => $lookup_obj, |
|
|
prefix => 'foobar', |
|
|
); |
|
128 |
|
|
129 |
Parametar C<filter> defines user supplied snippets of perl code which can |
=head2 _clean_ds |
|
be use with C<filter{...}> notation. |
|
130 |
|
|
131 |
C<prefix> is used to form filename for database record (to support multiple |
Clean data structure hash for next record |
|
source files which are joined in one database). |
|
132 |
|
|
133 |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
_clean_ds(); |
|
in structures. If you pass this parametar, you must also pass C<lookup> |
|
|
which is C<WebPAC::Lookup> object. |
|
134 |
|
|
135 |
=cut |
=cut |
136 |
|
|
137 |
sub new { |
sub _clean_ds { |
138 |
my $class = shift; |
my $a = {@_}; |
139 |
my $self = {@_}; |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); |
140 |
bless($self, $class); |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
141 |
|
$marc_encoding = $a->{marc_encoding}; |
142 |
my $r = $self->{'lookup_regex'} ? 1 : 0; |
} |
|
my $l = $self->{'lookup'} ? 1 : 0; |
|
|
|
|
|
my $log = $self->_get_logger(); |
|
143 |
|
|
144 |
# those two must be in pair |
=head2 _set_lookup |
|
if ( ($r & $l) != ($r || $l) ) { |
|
|
my $log = $self->_get_logger(); |
|
|
$log->logdie("lookup_regex and lookup must be in pair"); |
|
|
} |
|
145 |
|
|
146 |
$log->logdie("lookup must be WebPAC::Lookup object") if ($self->{'lookup'} && ! $self->{'lookup'}->isa('WebPAC::Lookup')); |
Set current lookup hash |
147 |
|
|
148 |
$log->warn("no prefix defined. please check that!") unless ($self->{'prefix'}); |
_set_lookup( $lookup ); |
149 |
|
|
150 |
$log->debug("using lookup regex: ", $self->{lookup_regex}) if ($r && $l); |
=cut |
151 |
|
|
152 |
if (! $self->{filter} || ! $self->{filter}->{regex}) { |
my $lookup; |
|
$log->debug("adding built-in filter regex"); |
|
|
$self->{filter}->{regex} = sub { |
|
|
my ($val, $regex) = @_; |
|
|
eval "\$val =~ $regex"; |
|
|
return $val; |
|
|
}; |
|
|
} |
|
153 |
|
|
154 |
$self ? return $self : return undef; |
sub _set_lookup { |
155 |
|
$lookup = shift; |
156 |
} |
} |
157 |
|
|
158 |
|
=head2 _get_marc_fields |
159 |
|
|
160 |
=head2 data_structure |
Get all fields defined by calls to C<marc> |
161 |
|
|
162 |
Create in-memory data structure which represents normalized layout from |
$marc->add_fields( WebPAC::Normalize:_get_marc_fields() ); |
|
C<conf/normalize/*.xml>. |
|
163 |
|
|
164 |
This structures are used to produce output. |
We are using I<magic> which detect repeatable fields only from |
165 |
|
sequence of field/subfield data generated by normalization. |
166 |
|
|
167 |
my $ds = $webpac->data_structure($rec); |
Repeatable field is created when there is second occurence of same subfield or |
168 |
|
if any of indicators are different. |
169 |
|
|
170 |
=cut |
This is sane for most cases. Something like: |
171 |
|
|
172 |
sub data_structure { |
900a-1 900b-1 900c-1 |
173 |
my $self = shift; |
900a-2 900b-2 |
174 |
|
900a-3 |
175 |
|
|
176 |
my $log = $self->_get_logger(); |
will be created from any combination of: |
177 |
|
|
178 |
my $rec = shift; |
900a-1 900a-2 900a-3 900b-1 900b-2 900c-1 |
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
179 |
|
|
180 |
$log->debug("data_structure rec = ", sub { Dumper($rec) }); |
and following rules: |
181 |
|
|
182 |
$log->logdie("need unique ID (mfn) in field 000 of record " . Dumper($rec) ) unless (defined($rec->{'000'})); |
marc('900','a', rec('200','a') ); |
183 |
|
marc('900','b', rec('200','b') ); |
184 |
|
marc('900','c', rec('200','c') ); |
185 |
|
|
186 |
my $id = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!"); |
which might not be what you have in mind. If you need repeatable subfield, |
187 |
|
define it using C<marc_repeatable_subfield> like this: |
188 |
|
|
189 |
my $cache_file; |
marc_repeatable_subfield('900','a'); |
190 |
|
marc('900','a', rec('200','a') ); |
191 |
|
marc('900','b', rec('200','b') ); |
192 |
|
marc('900','c', rec('200','c') ); |
193 |
|
|
194 |
if ($self->{'db'}) { |
will create: |
|
my $ds = $self->{'db'}->load_ds( id => $id, prefix => $self->{prefix} ); |
|
|
$log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) }); |
|
|
return $ds if ($ds); |
|
|
$log->debug("cache miss, creating"); |
|
|
} |
|
195 |
|
|
196 |
my @sorted_tags; |
900a-1 900a-2 900a-3 900b-1 900c-1 |
197 |
if ($self->{tags_by_order}) { |
900b-2 |
|
@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; |
|
|
} |
|
198 |
|
|
199 |
my $ds; |
There is also support for returning next or specific using: |
200 |
|
|
201 |
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
while (my $mf = WebPAC::Normalize:_get_marc_fields( fetch_next => 1 ) ) { |
202 |
|
# do something with $mf |
203 |
|
} |
204 |
|
|
205 |
foreach my $field (@sorted_tags) { |
will always return fields from next MARC record or |
206 |
|
|
207 |
my $row; |
my $mf = WebPAC::Normalize::_get_marc_fields( offset => 42 ); |
208 |
|
|
209 |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
will return 42th copy record (if it exists). |
210 |
|
|
211 |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
=cut |
|
my $format; |
|
212 |
|
|
213 |
$log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH'); |
sub _get_marc_fields { |
|
$format = $tag->{'value'} || $tag->{'content'}; |
|
214 |
|
|
215 |
my @v; |
my $arg = {@_}; |
216 |
if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { |
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
217 |
@v = $self->_rec_to_arr($rec,$format,'fill_in'); |
my $offset = $marc_fetch_offset; |
218 |
} else { |
if ($arg->{offset}) { |
219 |
@v = $self->_rec_to_arr($rec,$format,'parse'); |
$offset = $arg->{offset}; |
220 |
} |
} elsif($arg->{fetch_next}) { |
221 |
if (! @v) { |
$marc_fetch_offset++; |
222 |
$log->debug("$field <",$self->{tag},"> format: $format no values"); |
} |
|
next; |
|
|
} else { |
|
|
$log->debug("$field <",$self->{tag},"> format: $format values: ", join(",", @v)); |
|
|
} |
|
223 |
|
|
224 |
if ($tag->{'sort'}) { |
return if (! $marc_record || ref($marc_record) ne 'ARRAY'); |
|
@v = $self->sort_arr(@v); |
|
|
} |
|
225 |
|
|
226 |
# use format? |
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
|
if ($tag->{'format_name'}) { |
|
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
|
|
} |
|
227 |
|
|
228 |
# delimiter will join repeatable fields |
my $marc_rec = $marc_record->[ $offset ]; |
|
if ($tag->{'delimiter'}) { |
|
|
@v = ( join($tag->{'delimiter'}, @v) ); |
|
|
} |
|
229 |
|
|
230 |
# default types |
warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
|
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("tag $field / $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; |
|
231 |
|
|
232 |
} else { |
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
|
push @{$row->{$type}}, @v; |
|
|
} |
|
|
} |
|
233 |
|
|
234 |
|
# first, sort all existing fields |
235 |
|
# XXX might not be needed, but modern perl might randomize elements in hash |
236 |
|
my @sorted_marc_record = sort { |
237 |
|
$a->[0] . ( $a->[3] || '' ) cmp $b->[0] . ( $b->[3] || '') |
238 |
|
} @{ $marc_rec }; |
239 |
|
|
240 |
} |
@sorted_marc_record = @{ $marc_rec }; ### FIXME disable sorting |
241 |
|
|
242 |
|
# output marc fields |
243 |
|
my @m; |
244 |
|
|
245 |
if ($row) { |
# count unique field-subfields (used for offset when walking to next subfield) |
246 |
$row->{'tag'} = $field; |
my $u; |
247 |
|
map { $u->{ $_->[0] . ( $_->[3] || '') }++ } @sorted_marc_record; |
248 |
|
|
249 |
|
if ($debug) { |
250 |
|
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
251 |
|
warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; |
252 |
|
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
253 |
|
warn "## subfield count = ", dump( $u ), $/; |
254 |
|
} |
255 |
|
|
256 |
# TODO: name_sigular, name_plural |
my $len = $#sorted_marc_record; |
257 |
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
my $visited; |
258 |
my $row_name = $name ? $self->_x($name) : $field; |
my $i = 0; |
259 |
|
my $field; |
|
# post-sort all values in field |
|
|
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
|
|
$log->warn("sort at field tag not implemented"); |
|
|
} |
|
260 |
|
|
261 |
$ds->{$row_name} = $row; |
foreach ( 0 .. $len ) { |
262 |
|
|
263 |
$log->debug("row $field: ",sub { Dumper($row) }); |
# find next element which isn't visited |
264 |
} |
while ($visited->{$i}) { |
265 |
|
$i = ($i + 1) % ($len + 1); |
266 |
|
} |
267 |
|
|
268 |
|
# mark it visited |
269 |
|
$visited->{$i}++; |
270 |
|
|
271 |
|
my $row = dclone( $sorted_marc_record[$i] ); |
272 |
|
|
273 |
|
# field and subfield which is key for |
274 |
|
# marc_repeatable_subfield and u |
275 |
|
my $fsf = $row->[0] . ( $row->[3] || '' ); |
276 |
|
|
277 |
|
if ($debug > 1) { |
278 |
|
|
279 |
|
print "### field so far [", $#$field, "] : ", dump( $field ), " ", $field ? 'T' : 'F', $/; |
280 |
|
print "### this [$i]: ", dump( $row ),$/; |
281 |
|
print "### sf: ", $row->[3], " vs ", $field->[3], |
282 |
|
$marc_repeatable_subfield->{ $row->[0] . $row->[3] } ? ' (repeatable)' : '', $/, |
283 |
|
if ($#$field >= 0); |
284 |
|
|
285 |
} |
} |
286 |
|
|
287 |
$self->{'db'}->save_ds( |
# if field exists |
288 |
id => $id, |
if ( $#$field >= 0 ) { |
289 |
ds => $ds, |
if ( |
290 |
prefix => $self->{prefix}, |
$row->[0] ne $field->[0] || # field |
291 |
) if ($self->{'db'}); |
$row->[1] ne $field->[1] || # i1 |
292 |
|
$row->[2] ne $field->[2] # i2 |
293 |
|
) { |
294 |
|
push @m, $field; |
295 |
|
warn "## saved/1 ", dump( $field ),$/ if ($debug); |
296 |
|
$field = $row; |
297 |
|
|
298 |
|
} elsif ( |
299 |
|
( $row->[3] lt $field->[-2] ) # subfield which is not next (e.g. a after c) |
300 |
|
|| |
301 |
|
( $row->[3] eq $field->[-2] && # same subfield, but not repeatable |
302 |
|
! $marc_repeatable_subfield->{ $fsf } |
303 |
|
) |
304 |
|
) { |
305 |
|
push @m, $field; |
306 |
|
warn "## saved/2 ", dump( $field ),$/ if ($debug); |
307 |
|
$field = $row; |
308 |
|
|
309 |
$log->debug("ds: ", sub { Dumper($ds) }); |
} else { |
310 |
|
# append new subfields to existing field |
311 |
|
push @$field, ( $row->[3], $row->[4] ); |
312 |
|
} |
313 |
|
} else { |
314 |
|
# insert first field |
315 |
|
$field = $row; |
316 |
|
} |
317 |
|
|
318 |
$log->logconfess("data structure returned is not array any more!") if wantarray; |
if (! $marc_repeatable_subfield->{ $fsf }) { |
319 |
|
# make step to next subfield |
320 |
|
$i = ($i + $u->{ $fsf } ) % ($len + 1); |
321 |
|
} |
322 |
|
} |
323 |
|
|
324 |
return $ds; |
if ($#$field >= 0) { |
325 |
|
push @m, $field; |
326 |
|
warn "## saved/3 ", dump( $field ),$/ if ($debug); |
327 |
|
} |
328 |
|
|
329 |
|
return \@m; |
330 |
} |
} |
331 |
|
|
332 |
=head2 parse |
=head2 _debug |
333 |
|
|
334 |
Perform smart parsing of string, skipping delimiters for fields which aren't |
Change level of debug warnings |
|
defined. It can also eval code in format starting with C<eval{...}> and |
|
|
return output or nothing depending on eval code. |
|
335 |
|
|
336 |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
_debug( 2 ); |
337 |
|
|
338 |
Filters are implemented here. While simple form of filters looks like this: |
=cut |
339 |
|
|
340 |
filter{name_of_filter} |
sub _debug { |
341 |
|
my $l = shift; |
342 |
|
return $debug unless defined($l); |
343 |
|
warn "debug level $l",$/ if ($l > 0); |
344 |
|
$debug = $l; |
345 |
|
} |
346 |
|
|
347 |
but, filters can also have variable number of parametars like this: |
=head1 Functions to create C<data_structure> |
348 |
|
|
349 |
filter{name_of_filter(param,param,param)} |
Those functions generally have to first in your normalization file. |
350 |
|
|
351 |
=cut |
=head2 tag |
352 |
|
|
353 |
my $warn_once; |
Define new tag for I<search> and I<display>. |
354 |
|
|
355 |
sub parse { |
tag('Title', rec('200','a') ); |
|
my $self = shift; |
|
356 |
|
|
|
my ($rec, $format_utf8, $i, $rec_size) = @_; |
|
357 |
|
|
358 |
return if (! $format_utf8); |
=cut |
359 |
|
|
360 |
my $log = $self->_get_logger(); |
sub tag { |
361 |
|
my $name = shift or die "tag needs name as first argument"; |
362 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
363 |
|
return unless (@o); |
364 |
|
$out->{$name}->{tag} = $name; |
365 |
|
$out->{$name}->{search} = \@o; |
366 |
|
$out->{$name}->{display} = \@o; |
367 |
|
} |
368 |
|
|
369 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
=head2 display |
370 |
|
|
371 |
$i = 0 if (! $i); |
Define tag just for I<display> |
372 |
|
|
373 |
my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
@v = display('Title', rec('200','a') ); |
374 |
|
|
375 |
my @out; |
=cut |
376 |
|
|
377 |
$log->debug("format: $format [$i]"); |
sub display { |
378 |
|
my $name = shift or die "display needs name as first argument"; |
379 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
380 |
|
return unless (@o); |
381 |
|
$out->{$name}->{tag} = $name; |
382 |
|
$out->{$name}->{display} = \@o; |
383 |
|
} |
384 |
|
|
385 |
my $eval_code; |
=head2 search |
|
# remove eval{...} from beginning |
|
|
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
|
386 |
|
|
387 |
my $filter_name; |
Prepare values just for I<search> |
|
# remove filter{...} from beginning |
|
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
|
388 |
|
|
389 |
# did we found any (att all) field from format in row? |
@v = search('Title', rec('200','a') ); |
|
my $found_any; |
|
|
# prefix before first field which we preserve it $found_any |
|
|
my $prefix; |
|
390 |
|
|
391 |
my $f_step = 1; |
=cut |
392 |
|
|
393 |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
sub search { |
394 |
|
my $name = shift or die "search needs name as first argument"; |
395 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
396 |
|
return unless (@o); |
397 |
|
$out->{$name}->{tag} = $name; |
398 |
|
$out->{$name}->{search} = \@o; |
399 |
|
} |
400 |
|
|
401 |
my $del = $1 || ''; |
=head2 marc_leader |
|
$prefix = $del if ($f_step == 1); |
|
402 |
|
|
403 |
my $fld_type = lc($2); |
Setup fields within MARC leader or get leader |
404 |
|
|
405 |
# repeatable index |
marc_leader('05','c'); |
406 |
my $r = $i; |
my $leader = marc_leader(); |
|
if ($fld_type eq 's') { |
|
|
if ($found_any->{'v'}) { |
|
|
$r = 0; |
|
|
} else { |
|
|
return; |
|
|
} |
|
|
} |
|
407 |
|
|
408 |
my $found = 0; |
=cut |
|
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found,$rec_size); |
|
409 |
|
|
410 |
if ($found) { |
sub marc_leader { |
411 |
$found_any->{$fld_type} += $found; |
my ($offset,$value) = @_; |
412 |
|
|
413 |
# we will skip delimiter before first occurence of field! |
if ($offset) { |
414 |
push @out, $del unless($found_any->{$fld_type} == 1); |
$out->{' leader'}->{ $offset } = $value; |
415 |
push @out, $tmp; |
} else { |
416 |
} |
return $out->{' leader'}; |
|
$f_step++; |
|
417 |
} |
} |
418 |
|
} |
419 |
|
|
420 |
# test if any fields found? |
=head2 marc |
|
return if (! $found_any->{'v'} && ! $found_any->{'s'}); |
|
421 |
|
|
422 |
my $out = join('',@out); |
Save value for MARC field |
423 |
|
|
424 |
if ($out) { |
marc('900','a', rec('200','a') ); |
425 |
# add rest of format (suffix) |
marc('001', rec('000') ); |
|
$out .= $format; |
|
426 |
|
|
427 |
# add prefix if not there |
=cut |
|
$out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); |
|
428 |
|
|
429 |
$log->debug("result: $out"); |
sub marc { |
430 |
} |
my $f = shift or die "marc needs field"; |
431 |
|
die "marc field must be numer" unless ($f =~ /^\d+$/); |
432 |
|
|
433 |
if ($eval_code) { |
my $sf; |
434 |
my $eval = $self->fill_in($rec,$eval_code,$i) || return; |
if ($f >= 10) { |
435 |
$log->debug("about to eval{$eval} format: $out"); |
$sf = shift or die "marc needs subfield"; |
|
return if (! $self->_eval($eval)); |
|
436 |
} |
} |
437 |
|
|
438 |
if ($filter_name) { |
foreach (@_) { |
439 |
my @filter_args; |
my $v = $_; # make var read-write for Encode |
440 |
if ($filter_name =~ s/(\w+)\((.*)\)/$1/) { |
next unless (defined($v) && $v !~ /^\s*$/); |
441 |
@filter_args = split(/,/, $2); |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
442 |
} |
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
443 |
if ($self->{'filter'}->{$filter_name}) { |
if (defined $sf) { |
444 |
$log->debug("about to filter{$filter_name} format: $out with arguments: ", join(",", @filter_args)); |
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $i1, $i2, $sf => $v ]; |
445 |
unshift @filter_args, $out; |
} else { |
446 |
$out = $self->{'filter'}->{$filter_name}->(@filter_args); |
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ]; |
|
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}++; |
|
447 |
} |
} |
448 |
} |
} |
|
|
|
|
return $out; |
|
449 |
} |
} |
450 |
|
|
451 |
=head2 fill_in |
=head2 marc_repeatable_subfield |
452 |
|
|
453 |
Workhourse of all: takes record from in-memory structure of database and |
Save values for MARC repetable subfield |
|
strings with placeholders and returns string or array of with substituted |
|
|
values from record. |
|
454 |
|
|
455 |
my $text = $webpac->fill_in($rec,'v250^a'); |
marc_repeatable_subfield('910', 'z', rec('909') ); |
456 |
|
|
457 |
Optional argument is ordinal number for repeatable fields. By default, |
=cut |
|
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. |
|
458 |
|
|
459 |
my $text = $webpac->fill_in($rec,'Title: v250^a',1); |
sub marc_repeatable_subfield { |
460 |
|
my ($f,$sf) = @_; |
461 |
|
die "marc_repeatable_subfield need field and subfield!\n" unless ($f && $sf); |
462 |
|
$marc_repeatable_subfield->{ $f . $sf }++; |
463 |
|
marc(@_); |
464 |
|
} |
465 |
|
|
466 |
This function B<does not> perform parsing of format to inteligenty skip |
=head2 marc_indicators |
|
delimiters before fields which aren't used. |
|
467 |
|
|
468 |
This method will automatically decode UTF-8 string to local code page |
Set both indicators for MARC field |
|
if needed. |
|
469 |
|
|
470 |
There is optional parametar C<$record_size> which can be used to get sizes of |
marc_indicators('900', ' ', 1); |
|
all C<field^subfield> combinations in this format. |
|
471 |
|
|
472 |
my $text = $webpac->fill_in($rec,'got: v900^a v900^x',0,\$rec_size); |
Any indicator value other than C<0-9> will be treated as undefined. |
473 |
|
|
474 |
=cut |
=cut |
475 |
|
|
476 |
sub fill_in { |
sub marc_indicators { |
477 |
my $self = shift; |
my $f = shift || die "marc_indicators need field!\n"; |
478 |
|
my ($i1,$i2) = @_; |
479 |
|
die "marc_indicators($f, ...) need i1!\n" unless(defined($i1)); |
480 |
|
die "marc_indicators($f, $i1, ...) need i2!\n" unless(defined($i2)); |
481 |
|
|
482 |
|
$i1 = ' ' if ($i1 !~ /^\d$/); |
483 |
|
$i2 = ' ' if ($i2 !~ /^\d$/); |
484 |
|
@{ $marc_indicators->{$f} } = ($i1,$i2); |
485 |
|
} |
486 |
|
|
487 |
|
=head2 marc_compose |
488 |
|
|
489 |
my $log = $self->_get_logger(); |
Save values for each MARC subfield explicitly |
490 |
|
|
491 |
my ($rec,$format,$i,$rec_size) = @_; |
marc_compose('900', |
492 |
|
'a', rec('200','a') |
493 |
|
'b', rec('201','a') |
494 |
|
'a', rec('200','b') |
495 |
|
'c', rec('200','c') |
496 |
|
); |
497 |
|
|
498 |
$log->logconfess("need data record") unless ($rec); |
=cut |
|
$log->logconfess("need format to parse") unless($format); |
|
499 |
|
|
500 |
# iteration (for repeatable fields) |
sub marc_compose { |
501 |
$i ||= 0; |
my $f = shift or die "marc_compose needs field"; |
502 |
|
die "marc_compose field must be numer" unless ($f =~ /^\d+$/); |
503 |
|
|
504 |
$log->logdie("infitite loop in format $format") if ($i > ($self->{'max_mfn'} || 9999)); |
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
505 |
|
my $m = [ $f, $i1, $i2 ]; |
506 |
|
|
507 |
# FIXME remove for speedup? |
while (@_) { |
508 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
my $sf = shift or die "marc_compose $f needs subfield"; |
509 |
|
my $v = shift; |
510 |
|
|
511 |
if (utf8::is_utf8($format)) { |
next unless (defined($v) && $v !~ /^\s*$/); |
512 |
$format = $self->_x($format); |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
513 |
|
push @$m, ( $sf, $v ); |
514 |
|
warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1); |
515 |
} |
} |
516 |
|
|
517 |
my $found = 0; |
warn "## marc_compose(d) ", dump( $m ),$/ if ($debug > 1); |
|
my $just_single = 1; |
|
518 |
|
|
519 |
my $eval_code; |
push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2); |
520 |
# remove eval{...} from beginning |
} |
|
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
|
521 |
|
|
522 |
my $filter_name; |
=head2 marc_duplicate |
|
# remove filter{...} from beginning |
|
|
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
|
523 |
|
|
524 |
{ |
Generate copy of current MARC record and continue working on copy |
|
# fix warnings |
|
|
no warnings 'uninitialized'; |
|
525 |
|
|
526 |
# do actual replacement of placeholders |
marc_duplicate(); |
|
# repeatable fields |
|
|
if ($format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found,$rec_size)/ges) { |
|
|
$just_single = 0; |
|
|
} |
|
527 |
|
|
528 |
# non-repeatable fields |
Copies can be accessed using C<< _get_marc_fields( fetch_next => 1 ) >> or |
529 |
if ($format =~ s/s(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,0,\$found,$rec_size)/ges) { |
C<< _get_marc_fields( offset => 42 ) >>. |
|
return if ($i > 0 && $just_single); |
|
|
} |
|
|
} |
|
530 |
|
|
531 |
if ($found) { |
=cut |
532 |
$log->debug("format: $format"); |
|
533 |
if ($eval_code) { |
sub marc_duplicate { |
534 |
my $eval = $self->fill_in($rec,$eval_code,$i); |
my $m = $marc_record->[ -1 ]; |
535 |
return if (! $self->_eval($eval)); |
die "can't duplicate record which isn't defined" unless ($m); |
536 |
} |
push @{ $marc_record }, dclone( $m ); |
537 |
if ($filter_name && $self->{'filter'}->{$filter_name}) { |
warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); |
538 |
$log->debug("filter '$filter_name' for $format"); |
$marc_record_offset = $#{ $marc_record }; |
539 |
$format = $self->{'filter'}->{$filter_name}->($format); |
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
|
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; |
|
|
} else { |
|
|
$log->warn("Have lookup object but can't invoke lookup method"); |
|
|
} |
|
|
} else { |
|
|
return $format; |
|
|
} |
|
|
} else { |
|
|
return; |
|
|
} |
|
540 |
} |
} |
541 |
|
|
542 |
|
=head2 marc_remove |
543 |
|
|
544 |
=head2 _rec_to_arr |
Remove some field or subfield from MARC record. |
545 |
|
|
546 |
Similar to C<parse> and C<fill_in>, but returns array of all repeatable fields. Usable |
marc_remove('200'); |
547 |
for fields which have lookups, so they shouldn't be parsed but rather |
marc_remove('200','a'); |
|
C<paste>d or C<fill_id>ed. Last argument is name of operation: C<paste> or C<fill_in>. |
|
548 |
|
|
549 |
my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]','paste'); |
This will erase field C<200> or C<200^a> from current MARC record. |
550 |
|
|
551 |
=cut |
This is useful after calling C<marc_duplicate> or on it's own (but, you |
552 |
|
should probably just remove that subfield definition if you are not |
553 |
|
using C<marc_duplicate>). |
554 |
|
|
555 |
sub _rec_to_arr { |
FIXME: support fields < 10. |
556 |
my $self = shift; |
|
557 |
|
=cut |
558 |
|
|
559 |
my ($rec, $format_utf8, $code) = @_; |
sub marc_remove { |
560 |
|
my ($f, $sf) = @_; |
561 |
|
|
562 |
my $log = $self->_get_logger(); |
die "marc_remove needs record number" unless defined($f); |
563 |
|
|
564 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
my $marc = $marc_record->[ $marc_record_offset ]; |
|
return if (! $format_utf8); |
|
565 |
|
|
566 |
$log->debug("using $code on $format_utf8"); |
warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); |
567 |
|
|
568 |
my $i = 0; |
my $i = 0; |
569 |
my $max = 0; |
foreach ( 0 .. $#{ $marc } ) { |
570 |
my @arr; |
last unless (defined $marc->[$i]); |
571 |
my $rec_size = {}; |
warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); |
572 |
|
if ($marc->[$i]->[0] eq $f) { |
573 |
while ($i <= $max) { |
if (! defined $sf) { |
574 |
my @v = $self->$code($rec,$format_utf8,$i++,\$rec_size); |
# remove whole field |
575 |
if ($rec_size) { |
splice @$marc, $i, 1; |
576 |
foreach my $f (keys %{ $rec_size }) { |
warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); |
577 |
$max = $rec_size->{$f} if ($rec_size->{$f} > $max); |
$i--; |
578 |
|
} else { |
579 |
|
foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { |
580 |
|
my $o = ($j * 2) + 3; |
581 |
|
if ($marc->[$i]->[$o] eq $sf) { |
582 |
|
# remove subfield |
583 |
|
splice @{$marc->[$i]}, $o, 2; |
584 |
|
warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); |
585 |
|
# is record now empty? |
586 |
|
if ($#{ $marc->[$i] } == 2) { |
587 |
|
splice @$marc, $i, 1; |
588 |
|
warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); |
589 |
|
$i--; |
590 |
|
}; |
591 |
|
} |
592 |
|
} |
593 |
} |
} |
|
$log->debug("max set to $max"); |
|
|
undef $rec_size; |
|
|
} |
|
|
if (@v) { |
|
|
push @arr, @v; |
|
|
} else { |
|
|
push @arr, '' if ($max > $i); |
|
594 |
} |
} |
595 |
|
$i++; |
596 |
} |
} |
597 |
|
|
598 |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); |
599 |
|
|
600 |
return @arr; |
$marc_record->[ $marc_record_offset ] = $marc; |
|
} |
|
601 |
|
|
602 |
|
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
603 |
|
} |
604 |
|
|
605 |
=head2 get_data |
=head1 Functions to extract data from input |
|
|
|
|
Returns value from record. |
|
|
|
|
|
my $text = $self->get_data(\$rec,$f,$sf,$i,\$found,\$rec_size); |
|
|
|
|
|
Required arguments are: |
|
606 |
|
|
607 |
=over 8 |
This function should be used inside functions to create C<data_structure> described |
608 |
|
above. |
609 |
|
|
610 |
=item C<$rec> |
=head2 rec1 |
611 |
|
|
612 |
record reference |
Return all values in some field |
613 |
|
|
614 |
=item C<$f> |
@v = rec1('200') |
615 |
|
|
616 |
field |
TODO: order of values is probably same as in source data, need to investigate that |
617 |
|
|
618 |
=item C<$sf> |
=cut |
619 |
|
|
620 |
optional subfield |
sub rec1 { |
621 |
|
my $f = shift; |
622 |
|
warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
623 |
|
return unless (defined($rec) && defined($rec->{$f})); |
624 |
|
warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
625 |
|
if (ref($rec->{$f}) eq 'ARRAY') { |
626 |
|
return map { |
627 |
|
if (ref($_) eq 'HASH') { |
628 |
|
values %{$_}; |
629 |
|
} else { |
630 |
|
$_; |
631 |
|
} |
632 |
|
} @{ $rec->{$f} }; |
633 |
|
} elsif( defined($rec->{$f}) ) { |
634 |
|
return $rec->{$f}; |
635 |
|
} |
636 |
|
} |
637 |
|
|
638 |
=item C<$i> |
=head2 rec2 |
639 |
|
|
640 |
index offset for repeatable values ( 0 ... $rec_size->{'400^a'} ) |
Return all values in specific field and subfield |
641 |
|
|
642 |
=item C<$found> |
@v = rec2('200','a') |
643 |
|
|
644 |
optional variable that will be incremeted if preset |
=cut |
645 |
|
|
646 |
=item C<$rec_size> |
sub rec2 { |
647 |
|
my $f = shift; |
648 |
|
return unless (defined($rec && $rec->{$f})); |
649 |
|
my $sf = shift; |
650 |
|
return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; |
651 |
|
} |
652 |
|
|
653 |
hash to hold maximum occurances of C<field^subfield> combinations |
=head2 rec |
|
(which can be accessed using keys in same format) |
|
654 |
|
|
655 |
=back |
syntaxtic sugar for |
656 |
|
|
657 |
Returns value or empty string, updates C<$found> and C<rec_size> |
@v = rec('200') |
658 |
if present. |
@v = rec('200','a') |
659 |
|
|
660 |
=cut |
=cut |
661 |
|
|
662 |
sub get_data { |
sub rec { |
663 |
my $self = shift; |
if ($#_ == 0) { |
664 |
|
return rec1(@_); |
665 |
|
} elsif ($#_ == 1) { |
666 |
|
return rec2(@_); |
667 |
|
} |
668 |
|
} |
669 |
|
|
670 |
my ($rec,$f,$sf,$i,$found,$cache) = @_; |
=head2 regex |
671 |
|
|
672 |
return '' unless ($$rec->{$f} && ref($$rec->{$f}) eq 'ARRAY'); |
Apply regex to some or all values |
673 |
|
|
674 |
if (defined($$cache)) { |
@v = regex( 's/foo/bar/g', @v ); |
|
$$cache->{ $f . ( $sf ? '^' . $sf : '' ) } ||= scalar @{ $$rec->{$f} }; |
|
|
} |
|
675 |
|
|
676 |
return '' unless ($$rec->{$f}->[$i]); |
=cut |
677 |
|
|
678 |
{ |
sub regex { |
679 |
no strict 'refs'; |
my $r = shift; |
680 |
if (defined($sf)) { |
my @out; |
681 |
$$found++ if (defined($$found) && $$rec->{$f}->[$i]->{$sf}); |
#warn "r: $r\n", dump(\@_); |
682 |
return $$rec->{$f}->[$i]->{$sf}; |
foreach my $t (@_) { |
683 |
} else { |
next unless ($t); |
684 |
$$found++ if (defined($$found)); |
eval "\$t =~ $r"; |
685 |
# it still might have subfields, just |
push @out, $t if ($t && $t ne ''); |
|
# not specified, so we'll dump some debug info |
|
|
if ($$rec->{$f}->[$i] =~ /HASH/o) { |
|
|
my $out; |
|
|
foreach my $k (keys %{$$rec->{$f}->[$i]}) { |
|
|
$out .= '$' . $k .':' . $$rec->{$f}->[$i]->{$k}." "; |
|
|
} |
|
|
return $out; |
|
|
} else { |
|
|
return $$rec->{$f}->[$i]; |
|
|
} |
|
|
} |
|
686 |
} |
} |
687 |
|
return @out; |
688 |
} |
} |
689 |
|
|
690 |
|
=head2 prefix |
691 |
|
|
692 |
=head2 apply_format |
Prefix all values with a string |
|
|
|
|
Apply format specified in tag with C<format_name="name"> and |
|
|
C<format_delimiter=";;">. |
|
693 |
|
|
694 |
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
@v = prefix( 'my_', @v ); |
|
|
|
|
Formats can contain C<lookup{...}> if you need them. |
|
695 |
|
|
696 |
=cut |
=cut |
697 |
|
|
698 |
sub apply_format { |
sub prefix { |
699 |
my $self = shift; |
my $p = shift or die "prefix needs string as first argument"; |
700 |
|
return map { $p . $_ } grep { defined($_) } @_; |
701 |
|
} |
702 |
|
|
703 |
my ($name,$delimiter,$data) = @_; |
=head2 suffix |
704 |
|
|
705 |
my $log = $self->_get_logger(); |
suffix all values with a string |
706 |
|
|
707 |
if (! $self->{'import_xml'}->{'format'}->{$name}) { |
@v = suffix( '_my', @v ); |
|
$log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'}); |
|
|
return $data; |
|
|
} |
|
708 |
|
|
709 |
$log->warn("no delimiter for format $name") if (! $delimiter); |
=cut |
710 |
|
|
711 |
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
sub suffix { |
712 |
|
my $s = shift or die "suffix needs string as first argument"; |
713 |
|
return map { $_ . $s } grep { defined($_) } @_; |
714 |
|
} |
715 |
|
|
716 |
my @data = split(/\Q$delimiter\E/, $data); |
=head2 surround |
717 |
|
|
718 |
my $out = sprintf($format, @data); |
surround all values with a two strings |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
|
719 |
|
|
720 |
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
@v = surround( 'prefix_', '_suffix', @v ); |
|
return $self->{'lookup'}->lookup($out); |
|
|
} else { |
|
|
return $out; |
|
|
} |
|
721 |
|
|
722 |
|
=cut |
723 |
|
|
724 |
|
sub surround { |
725 |
|
my $p = shift or die "surround need prefix as first argument"; |
726 |
|
my $s = shift or die "surround needs suffix as second argument"; |
727 |
|
return map { $p . $_ . $s } grep { defined($_) } @_; |
728 |
} |
} |
729 |
|
|
730 |
=head2 sort_arr |
=head2 first |
731 |
|
|
732 |
Sort array ignoring case and html in data |
Return first element |
733 |
|
|
734 |
my @sorted = $webpac->sort_arr(@unsorted); |
$v = first( @v ); |
735 |
|
|
736 |
=cut |
=cut |
737 |
|
|
738 |
sub sort_arr { |
sub first { |
739 |
my $self = shift; |
my $r = shift; |
740 |
|
return $r; |
741 |
|
} |
742 |
|
|
743 |
my $log = $self->_get_logger(); |
=head2 lookup |
744 |
|
|
745 |
# FIXME add Schwartzian Transformation? |
Consult lookup hashes for some value |
746 |
|
|
747 |
my @sorted = sort { |
@v = lookup( $v ); |
748 |
$a =~ s#<[^>]+/*>##; |
@v = lookup( @v ); |
|
$b =~ s#<[^>]+/*>##; |
|
|
lc($b) cmp lc($a) |
|
|
} @_; |
|
|
$log->debug("sorted values: ",sub { join(", ",@sorted) }); |
|
749 |
|
|
750 |
return @sorted; |
=cut |
|
} |
|
751 |
|
|
752 |
|
sub lookup { |
753 |
|
my $k = shift or return; |
754 |
|
return unless (defined($lookup->{$k})); |
755 |
|
if (ref($lookup->{$k}) eq 'ARRAY') { |
756 |
|
return @{ $lookup->{$k} }; |
757 |
|
} else { |
758 |
|
return $lookup->{$k}; |
759 |
|
} |
760 |
|
} |
761 |
|
|
762 |
=head1 INTERNAL METHODS |
=head2 join_with |
763 |
|
|
764 |
=head2 _sort_by_order |
Joins walues with some delimiter |
765 |
|
|
766 |
Sort xml tags data structure accoding to C<order=""> attribute. |
$v = join_with(", ", @v); |
767 |
|
|
768 |
=cut |
=cut |
769 |
|
|
770 |
sub _sort_by_order { |
sub join_with { |
771 |
my $self = shift; |
my $d = shift; |
772 |
|
return join($d, grep { defined($_) && $_ ne '' } @_); |
|
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; |
|
773 |
} |
} |
774 |
|
|
775 |
=head2 _x |
=head2 split_rec_on |
776 |
|
|
777 |
Convert strings from C<conf/normalize/*.xml> encoding into application |
Split record subfield on some regex and take one of parts out |
|
specific encoding (optinally specified using C<code_page> to C<new> |
|
|
constructor). |
|
778 |
|
|
779 |
my $text = $n->_x('normalize text string'); |
$a_before_semi_column = |
780 |
|
split_rec_on('200','a', /\s*;\s*/, $part); |
781 |
|
|
782 |
This is a stub so that other modules doesn't have to implement it. |
C<$part> is optional number of element. First element is |
783 |
|
B<1>, not 0! |
784 |
|
|
785 |
=cut |
If there is no C<$part> parameter or C<$part> is 0, this function will |
786 |
|
return all values produced by splitting. |
|
sub _x { |
|
|
my $self = shift; |
|
|
return shift; |
|
|
} |
|
787 |
|
|
788 |
|
=cut |
789 |
|
|
790 |
=head1 AUTHOR |
sub split_rec_on { |
791 |
|
die "split_rec_on need (fld,sf,regex[,part]" if ($#_ < 2); |
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
|
792 |
|
|
793 |
=head1 COPYRIGHT & LICENSE |
my ($fld, $sf, $regex, $part) = @_; |
794 |
|
warn "### regex ", ref($regex), $regex, $/ if ($debug > 2); |
795 |
|
|
796 |
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
my @r = rec( $fld, $sf ); |
797 |
|
my $v = shift @r; |
798 |
|
warn "### first rec($fld,$sf) = ",dump($v),$/ if ($debug > 2); |
799 |
|
|
800 |
This program is free software; you can redistribute it and/or modify it |
return '' if( ! defined($v) || $v =~ /^\s*$/); |
|
under the same terms as Perl itself. |
|
801 |
|
|
802 |
=cut |
my @s = split( $regex, $v ); |
803 |
|
warn "## split_rec_on($fld,$sf,$regex,$part) = ",dump(@s),$/ if ($debug > 1); |
804 |
|
if ($part && $part > 0) { |
805 |
|
return $s[ $part - 1 ]; |
806 |
|
} else { |
807 |
|
return @s; |
808 |
|
} |
809 |
|
} |
810 |
|
|
811 |
1; # End of WebPAC::Normalize |
# END |
812 |
|
1; |