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 Data::Dumper; |
|
23 |
use Storable; |
#use base qw/WebPAC::Common/; |
24 |
|
use Data::Dump qw/dump/; |
25 |
|
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 - normalisation of source file |
WebPAC::Normalize - describe normalisaton rules using sets |
35 |
|
|
36 |
=head1 VERSION |
=head1 VERSION |
37 |
|
|
38 |
Version 0.01 |
Version 0.14 |
39 |
|
|
40 |
=cut |
=cut |
41 |
|
|
42 |
our $VERSION = '0.01'; |
our $VERSION = '0.14'; |
43 |
|
|
44 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
45 |
|
|
46 |
This package contains code that could be helpful in implementing different |
This module uses C<conf/normalize/*.pl> files to perform normalisation |
47 |
normalisation front-ends. |
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 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
59 |
|
|
60 |
=head2 new |
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 |
Create new normalisation object |
=head2 data_structure |
64 |
|
|
65 |
|
Return data structure |
66 |
|
|
67 |
my $n = new WebPAC::Normalize::Something( |
my $ds = WebPAC::Normalize::data_structure( |
68 |
cache_data_structure => './cache/ds/', |
lookup => $lookup->lookup_hash, |
69 |
lookup_regex => $lookup->regex, |
row => $row, |
70 |
|
rules => $normalize_pl_config, |
71 |
|
marc_encoding => 'utf-8', |
72 |
|
config => $config, |
73 |
); |
); |
74 |
|
|
75 |
Optional parameter C<cache_data_structure> defines path to directory |
Options C<lookup>, C<row>, C<rules> and C<log> are mandatory while all |
76 |
in which cache file for C<data_structure> call will be created. |
other are optional. |
77 |
|
|
78 |
|
This function will B<die> if normalizastion can't be evaled. |
79 |
|
|
80 |
Recommended parametar C<lookup_regex> is used to enable parsing of lookups |
Since this function isn't exported you have to call it with |
81 |
in structures. |
C<WebPAC::Normalize::data_structure>. |
82 |
|
|
83 |
=cut |
=cut |
84 |
|
|
85 |
sub new { |
sub data_structure { |
86 |
my $class = shift; |
my $arg = {@_}; |
87 |
my $self = {@_}; |
|
88 |
bless($self, $class); |
die "need row argument" unless ($arg->{row}); |
89 |
|
die "need normalisation argument" unless ($arg->{rules}); |
90 |
|
|
91 |
|
no strict 'subs'; |
92 |
|
_set_lookup( $arg->{lookup} ); |
93 |
|
_set_rec( $arg->{row} ); |
94 |
|
_set_config( $arg->{config} ); |
95 |
|
_clean_ds( %{ $arg } ); |
96 |
|
eval "$arg->{rules}"; |
97 |
|
die "error evaling $arg->{rules}: $@\n" if ($@); |
98 |
|
|
99 |
|
return _get_ds(); |
100 |
|
} |
101 |
|
|
102 |
|
=head2 _set_rec |
103 |
|
|
104 |
|
Set current record hash |
105 |
|
|
106 |
$self->setup_cache_dir( $self->{'cache_data_structure'} ); |
_set_rec( $rec ); |
107 |
|
|
108 |
$self ? return $self : return undef; |
=cut |
109 |
|
|
110 |
|
my $rec; |
111 |
|
|
112 |
|
sub _set_rec { |
113 |
|
$rec = shift or die "no record hash"; |
114 |
} |
} |
115 |
|
|
116 |
=head2 setup_cache_dir |
=head2 _set_config |
117 |
|
|
118 |
|
Set current config hash |
119 |
|
|
120 |
|
_set_config( $config ); |
121 |
|
|
122 |
|
Magic keys are: |
123 |
|
|
124 |
|
=over 4 |
125 |
|
|
126 |
Check if specified cache directory exist, and if not, disable caching. |
=item _ |
127 |
|
|
128 |
$setup_cache_dir('./cache/ds/'); |
Code of current database |
129 |
|
|
130 |
If you pass false or zero value to this function, it will disable |
=item _mfn |
131 |
cacheing. |
|
132 |
|
Current MFN |
133 |
|
|
134 |
|
=back |
135 |
|
|
136 |
=cut |
=cut |
137 |
|
|
138 |
sub setup_cache_dir { |
my $config; |
|
my $self = shift; |
|
139 |
|
|
140 |
my $dir = shift; |
sub _set_config { |
141 |
|
$config = shift; |
142 |
|
} |
143 |
|
|
144 |
my $log = $self->_get_logger(); |
=head2 _get_ds |
145 |
|
|
146 |
if ($dir) { |
Return hash formatted as data structure |
|
my $msg; |
|
|
if (! -e $dir) { |
|
|
$msg = "doesn't exist"; |
|
|
} elsif (! -d $dir) { |
|
|
$msg = "is not directory"; |
|
|
} elsif (! -w $dir) { |
|
|
$msg = "not writable"; |
|
|
} |
|
147 |
|
|
148 |
if ($msg) { |
my $ds = _get_ds(); |
149 |
undef $self->{'cache_data_structure'}; |
|
150 |
$log->warn("cache_data_structure $dir $msg, disabling..."); |
=cut |
151 |
} else { |
|
152 |
$log->debug("using cache dir $dir"); |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); |
153 |
} |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
154 |
} else { |
|
155 |
$log->debug("disabling cache"); |
sub _get_ds { |
156 |
undef $self->{'cache_data_structure'}; |
return $out; |
|
} |
|
157 |
} |
} |
158 |
|
|
159 |
|
=head2 _clean_ds |
160 |
|
|
161 |
=head2 data_structure |
Clean data structure hash for next record |
162 |
|
|
163 |
Create in-memory data structure which represents normalized layout from |
_clean_ds(); |
|
C<conf/normalize/*.xml>. |
|
164 |
|
|
165 |
This structures are used to produce output. |
=cut |
166 |
|
|
167 |
|
sub _clean_ds { |
168 |
|
my $a = {@_}; |
169 |
|
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); |
170 |
|
($marc_record_offset, $marc_fetch_offset) = (0,0); |
171 |
|
$marc_encoding = $a->{marc_encoding}; |
172 |
|
} |
173 |
|
|
174 |
my @ds = $webpac->data_structure($rec); |
=head2 _set_lookup |
175 |
|
|
176 |
B<Note: historical oddity follows> |
Set current lookup hash |
177 |
|
|
178 |
This method will also set C<< $webpac->{'currnet_filename'} >> if there is |
_set_lookup( $lookup ); |
|
C<< <filename> >> tag and C<< $webpac->{'headline'} >> if there is |
|
|
C<< <headline> >> tag. |
|
179 |
|
|
180 |
=cut |
=cut |
181 |
|
|
182 |
sub data_structure { |
my $lookup; |
|
my $self = shift; |
|
183 |
|
|
184 |
my $log = $self->_get_logger(); |
sub _set_lookup { |
185 |
|
$lookup = shift; |
186 |
|
} |
187 |
|
|
188 |
my $rec = shift; |
=head2 _get_marc_fields |
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
189 |
|
|
190 |
my $cache_file; |
Get all fields defined by calls to C<marc> |
191 |
|
|
192 |
if (my $cache_path = $self->{'cache_data_structure'}) { |
$marc->add_fields( WebPAC::Normalize:_get_marc_fields() ); |
|
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'}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
193 |
|
|
194 |
undef $self->{'currnet_filename'}; |
We are using I<magic> which detect repeatable fields only from |
195 |
undef $self->{'headline'}; |
sequence of field/subfield data generated by normalization. |
196 |
|
|
197 |
my @sorted_tags; |
Repeatable field is created when there is second occurence of same subfield or |
198 |
if ($self->{tags_by_order}) { |
if any of indicators are different. |
|
@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; |
|
|
} |
|
199 |
|
|
200 |
my @ds; |
This is sane for most cases. Something like: |
201 |
|
|
202 |
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
900a-1 900b-1 900c-1 |
203 |
|
900a-2 900b-2 |
204 |
|
900a-3 |
205 |
|
|
206 |
foreach my $field (@sorted_tags) { |
will be created from any combination of: |
207 |
|
|
208 |
my $row; |
900a-1 900a-2 900a-3 900b-1 900b-2 900c-1 |
209 |
|
|
210 |
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
and following rules: |
211 |
|
|
212 |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
marc('900','a', rec('200','a') ); |
213 |
my $format = $tag->{'value'} || $tag->{'content'}; |
marc('900','b', rec('200','b') ); |
214 |
|
marc('900','c', rec('200','c') ); |
215 |
|
|
216 |
$log->debug("format: $format"); |
which might not be what you have in mind. If you need repeatable subfield, |
217 |
|
define it using C<marc_repeatable_subfield> like this: |
218 |
|
|
219 |
my @v; |
marc_repeatable_subfield('900','a'); |
220 |
if ($self->{'lookup_regex'} && $format =~ $self->{'lookup_regex'}) { |
marc('900','a', rec('200','a') ); |
221 |
@v = $self->fill_in_to_arr($rec,$format); |
marc('900','b', rec('200','b') ); |
222 |
} else { |
marc('900','c', rec('200','c') ); |
|
@v = $self->parse_to_arr($rec,$format); |
|
|
} |
|
|
next if (! @v); |
|
223 |
|
|
224 |
if ($tag->{'sort'}) { |
will create: |
|
@v = $self->sort_arr(@v); |
|
|
} |
|
225 |
|
|
226 |
# use format? |
900a-1 900a-2 900a-3 900b-1 900c-1 |
227 |
if ($tag->{'format_name'}) { |
900b-2 |
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
|
|
} |
|
228 |
|
|
229 |
if ($field eq 'filename') { |
There is also support for returning next or specific using: |
|
$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! |
|
|
} |
|
230 |
|
|
231 |
# delimiter will join repeatable fields |
while (my $mf = WebPAC::Normalize:_get_marc_fields( fetch_next => 1 ) ) { |
232 |
if ($tag->{'delimiter'}) { |
# do something with $mf |
233 |
@v = ( join($tag->{'delimiter'}, @v) ); |
} |
|
} |
|
234 |
|
|
235 |
# default types |
will always return fields from next MARC record or |
|
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; |
|
236 |
|
|
237 |
} else { |
my $mf = WebPAC::Normalize::_get_marc_fields( offset => 42 ); |
|
push @{$row->{$type}}, @v; |
|
|
} |
|
|
} |
|
238 |
|
|
239 |
|
will return 42th copy record (if it exists). |
240 |
|
|
241 |
|
=cut |
242 |
|
|
243 |
|
sub _get_marc_fields { |
244 |
|
|
245 |
|
my $arg = {@_}; |
246 |
|
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
247 |
|
my $offset = $marc_fetch_offset; |
248 |
|
if ($arg->{offset}) { |
249 |
|
$offset = $arg->{offset}; |
250 |
|
} elsif($arg->{fetch_next}) { |
251 |
|
$marc_fetch_offset++; |
252 |
|
} |
253 |
|
|
254 |
|
return if (! $marc_record || ref($marc_record) ne 'ARRAY'); |
255 |
|
|
256 |
|
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
257 |
|
|
258 |
|
my $marc_rec = $marc_record->[ $offset ]; |
259 |
|
|
260 |
|
warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
261 |
|
|
262 |
|
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
263 |
|
|
264 |
|
# first, sort all existing fields |
265 |
|
# XXX might not be needed, but modern perl might randomize elements in hash |
266 |
|
my @sorted_marc_record = sort { |
267 |
|
$a->[0] . ( $a->[3] || '' ) cmp $b->[0] . ( $b->[3] || '') |
268 |
|
} @{ $marc_rec }; |
269 |
|
|
270 |
|
@sorted_marc_record = @{ $marc_rec }; ### FIXME disable sorting |
271 |
|
|
272 |
|
# output marc fields |
273 |
|
my @m; |
274 |
|
|
275 |
|
# count unique field-subfields (used for offset when walking to next subfield) |
276 |
|
my $u; |
277 |
|
map { $u->{ $_->[0] . ( $_->[3] || '') }++ } @sorted_marc_record; |
278 |
|
|
279 |
|
if ($debug) { |
280 |
|
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
281 |
|
warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; |
282 |
|
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
283 |
|
warn "## subfield count = ", dump( $u ), $/; |
284 |
|
} |
285 |
|
|
286 |
|
my $len = $#sorted_marc_record; |
287 |
|
my $visited; |
288 |
|
my $i = 0; |
289 |
|
my $field; |
290 |
|
|
291 |
|
foreach ( 0 .. $len ) { |
292 |
|
|
293 |
|
# find next element which isn't visited |
294 |
|
while ($visited->{$i}) { |
295 |
|
$i = ($i + 1) % ($len + 1); |
296 |
} |
} |
297 |
|
|
298 |
if ($row) { |
# mark it visited |
299 |
$row->{'tag'} = $field; |
$visited->{$i}++; |
300 |
|
|
301 |
# TODO: name_sigular, name_plural |
my $row = dclone( $sorted_marc_record[$i] ); |
302 |
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
|
303 |
$row->{'name'} = $name ? $self->_x($name) : $field; |
# field and subfield which is key for |
304 |
|
# marc_repeatable_subfield and u |
305 |
# post-sort all values in field |
my $fsf = $row->[0] . ( $row->[3] || '' ); |
306 |
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
|
307 |
$log->warn("sort at field tag not implemented"); |
if ($debug > 1) { |
308 |
} |
|
309 |
|
print "### field so far [", $#$field, "] : ", dump( $field ), " ", $field ? 'T' : 'F', $/; |
310 |
|
print "### this [$i]: ", dump( $row ),$/; |
311 |
|
print "### sf: ", $row->[3], " vs ", $field->[3], |
312 |
|
$marc_repeatable_subfield->{ $row->[0] . $row->[3] } ? ' (repeatable)' : '', $/, |
313 |
|
if ($#$field >= 0); |
314 |
|
|
315 |
|
} |
316 |
|
|
317 |
push @ds, $row; |
# if field exists |
318 |
|
if ( $#$field >= 0 ) { |
319 |
|
if ( |
320 |
|
$row->[0] ne $field->[0] || # field |
321 |
|
$row->[1] ne $field->[1] || # i1 |
322 |
|
$row->[2] ne $field->[2] # i2 |
323 |
|
) { |
324 |
|
push @m, $field; |
325 |
|
warn "## saved/1 ", dump( $field ),$/ if ($debug); |
326 |
|
$field = $row; |
327 |
|
|
328 |
|
} elsif ( |
329 |
|
( $row->[3] lt $field->[-2] ) # subfield which is not next (e.g. a after c) |
330 |
|
|| |
331 |
|
( $row->[3] eq $field->[-2] && # same subfield, but not repeatable |
332 |
|
! $marc_repeatable_subfield->{ $fsf } |
333 |
|
) |
334 |
|
) { |
335 |
|
push @m, $field; |
336 |
|
warn "## saved/2 ", dump( $field ),$/ if ($debug); |
337 |
|
$field = $row; |
338 |
|
|
339 |
$log->debug("row $field: ",sub { Dumper($row) }); |
} else { |
340 |
|
# append new subfields to existing field |
341 |
|
push @$field, ( $row->[3], $row->[4] ); |
342 |
|
} |
343 |
|
} else { |
344 |
|
# insert first field |
345 |
|
$field = $row; |
346 |
} |
} |
347 |
|
|
348 |
|
if (! $marc_repeatable_subfield->{ $fsf }) { |
349 |
|
# make step to next subfield |
350 |
|
$i = ($i + $u->{ $fsf } ) % ($len + 1); |
351 |
|
} |
352 |
} |
} |
353 |
|
|
354 |
if ($cache_file) { |
if ($#$field >= 0) { |
355 |
store { |
push @m, $field; |
356 |
ds => \@ds, |
warn "## saved/3 ", dump( $field ),$/ if ($debug); |
|
current_filename => $self->{'current_filename'}, |
|
|
headline => $self->{'headline'}, |
|
|
}, $cache_file; |
|
|
$log->debug("created storable cache file $cache_file"); |
|
357 |
} |
} |
358 |
|
|
359 |
return @ds; |
return \@m; |
360 |
|
} |
361 |
|
|
362 |
|
=head2 _debug |
363 |
|
|
364 |
|
Change level of debug warnings |
365 |
|
|
366 |
|
_debug( 2 ); |
367 |
|
|
368 |
|
=cut |
369 |
|
|
370 |
|
sub _debug { |
371 |
|
my $l = shift; |
372 |
|
return $debug unless defined($l); |
373 |
|
warn "debug level $l",$/ if ($l > 0); |
374 |
|
$debug = $l; |
375 |
} |
} |
376 |
|
|
377 |
=head2 apply_format |
=head1 Functions to create C<data_structure> |
378 |
|
|
379 |
Apply format specified in tag with C<format_name="name"> and |
Those functions generally have to first in your normalization file. |
|
C<format_delimiter=";;">. |
|
380 |
|
|
381 |
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
=head2 tag |
382 |
|
|
383 |
|
Define new tag for I<search> and I<display>. |
384 |
|
|
385 |
|
tag('Title', rec('200','a') ); |
386 |
|
|
|
Formats can contain C<lookup{...}> if you need them. |
|
387 |
|
|
388 |
=cut |
=cut |
389 |
|
|
390 |
sub apply_format { |
sub tag { |
391 |
my $self = shift; |
my $name = shift or die "tag needs name as first argument"; |
392 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
393 |
|
return unless (@o); |
394 |
|
$out->{$name}->{tag} = $name; |
395 |
|
$out->{$name}->{search} = \@o; |
396 |
|
$out->{$name}->{display} = \@o; |
397 |
|
} |
398 |
|
|
399 |
|
=head2 display |
400 |
|
|
401 |
my ($name,$delimiter,$data) = @_; |
Define tag just for I<display> |
402 |
|
|
403 |
my $log = $self->_get_logger(); |
@v = display('Title', rec('200','a') ); |
404 |
|
|
405 |
if (! $self->{'import_xml'}->{'format'}->{$name}) { |
=cut |
406 |
$log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'}); |
|
407 |
return $data; |
sub display { |
408 |
} |
my $name = shift or die "display needs name as first argument"; |
409 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
410 |
|
return unless (@o); |
411 |
|
$out->{$name}->{tag} = $name; |
412 |
|
$out->{$name}->{display} = \@o; |
413 |
|
} |
414 |
|
|
415 |
|
=head2 search |
416 |
|
|
417 |
|
Prepare values just for I<search> |
418 |
|
|
419 |
$log->warn("no delimiter for format $name") if (! $delimiter); |
@v = search('Title', rec('200','a') ); |
420 |
|
|
421 |
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
=cut |
422 |
|
|
423 |
|
sub search { |
424 |
|
my $name = shift or die "search needs name as first argument"; |
425 |
|
my @o = grep { defined($_) && $_ ne '' } @_; |
426 |
|
return unless (@o); |
427 |
|
$out->{$name}->{tag} = $name; |
428 |
|
$out->{$name}->{search} = \@o; |
429 |
|
} |
430 |
|
|
431 |
|
=head2 marc_leader |
432 |
|
|
433 |
my @data = split(/\Q$delimiter\E/, $data); |
Setup fields within MARC leader or get leader |
434 |
|
|
435 |
|
marc_leader('05','c'); |
436 |
|
my $leader = marc_leader(); |
437 |
|
|
438 |
|
=cut |
439 |
|
|
440 |
my $out = sprintf($format, @data); |
sub marc_leader { |
441 |
$log->debug("using format $name [$format] on $data to produce: $out"); |
my ($offset,$value) = @_; |
442 |
|
|
443 |
if ($self->{'lookup_regex'} && $out =~ $self->{'lookup_regex'}) { |
if ($offset) { |
444 |
return $self->lookup($out); |
$out->{' leader'}->{ $offset } = $value; |
445 |
} else { |
} else { |
446 |
return $out; |
return $out->{' leader'}; |
447 |
|
} |
448 |
|
} |
449 |
|
|
450 |
|
=head2 marc |
451 |
|
|
452 |
|
Save value for MARC field |
453 |
|
|
454 |
|
marc('900','a', rec('200','a') ); |
455 |
|
marc('001', rec('000') ); |
456 |
|
|
457 |
|
=cut |
458 |
|
|
459 |
|
sub marc { |
460 |
|
my $f = shift or die "marc needs field"; |
461 |
|
die "marc field must be numer" unless ($f =~ /^\d+$/); |
462 |
|
|
463 |
|
my $sf; |
464 |
|
if ($f >= 10) { |
465 |
|
$sf = shift or die "marc needs subfield"; |
466 |
} |
} |
467 |
|
|
468 |
|
foreach (@_) { |
469 |
|
my $v = $_; # make var read-write for Encode |
470 |
|
next unless (defined($v) && $v !~ /^\s*$/); |
471 |
|
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
472 |
|
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
473 |
|
if (defined $sf) { |
474 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $i1, $i2, $sf => $v ]; |
475 |
|
} else { |
476 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ]; |
477 |
|
} |
478 |
|
} |
479 |
} |
} |
480 |
|
|
481 |
=head2 parse |
=head2 marc_repeatable_subfield |
482 |
|
|
483 |
Perform smart parsing of string, skipping delimiters for fields which aren't |
Save values for MARC repetable subfield |
|
defined. It can also eval code in format starting with C<eval{...}> and |
|
|
return output or nothing depending on eval code. |
|
484 |
|
|
485 |
my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); |
marc_repeatable_subfield('910', 'z', rec('909') ); |
486 |
|
|
487 |
=cut |
=cut |
488 |
|
|
489 |
sub parse { |
sub marc_repeatable_subfield { |
490 |
my $self = shift; |
my ($f,$sf) = @_; |
491 |
|
die "marc_repeatable_subfield need field and subfield!\n" unless ($f && $sf); |
492 |
|
$marc_repeatable_subfield->{ $f . $sf }++; |
493 |
|
marc(@_); |
494 |
|
} |
495 |
|
|
496 |
my ($rec, $format_utf8, $i) = @_; |
=head2 marc_indicators |
497 |
|
|
498 |
return if (! $format_utf8); |
Set both indicators for MARC field |
499 |
|
|
500 |
my $log = $self->_get_logger(); |
marc_indicators('900', ' ', 1); |
501 |
|
|
502 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
Any indicator value other than C<0-9> will be treated as undefined. |
503 |
|
|
504 |
$i = 0 if (! $i); |
=cut |
505 |
|
|
506 |
my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
sub marc_indicators { |
507 |
|
my $f = shift || die "marc_indicators need field!\n"; |
508 |
|
my ($i1,$i2) = @_; |
509 |
|
die "marc_indicators($f, ...) need i1!\n" unless(defined($i1)); |
510 |
|
die "marc_indicators($f, $i1, ...) need i2!\n" unless(defined($i2)); |
511 |
|
|
512 |
my @out; |
$i1 = ' ' if ($i1 !~ /^\d$/); |
513 |
|
$i2 = ' ' if ($i2 !~ /^\d$/); |
514 |
|
@{ $marc_indicators->{$f} } = ($i1,$i2); |
515 |
|
} |
516 |
|
|
517 |
$log->debug("format: $format"); |
=head2 marc_compose |
518 |
|
|
519 |
my $eval_code; |
Save values for each MARC subfield explicitly |
|
# remove eval{...} from beginning |
|
|
$eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); |
|
520 |
|
|
521 |
my $filter_name; |
marc_compose('900', |
522 |
# remove filter{...} from beginning |
'a', rec('200','a') |
523 |
$filter_name = $1 if ($format =~ s/^filter{([^}]+)}//s); |
'b', rec('201','a') |
524 |
|
'a', rec('200','b') |
525 |
|
'c', rec('200','c') |
526 |
|
); |
527 |
|
|
528 |
my $prefix; |
=cut |
|
my $all_found=0; |
|
529 |
|
|
530 |
while ($format =~ s/^(.*?)(v|s)(\d+)(?:\^(\w))?//s) { |
sub marc_compose { |
531 |
|
my $f = shift or die "marc_compose needs field"; |
532 |
|
die "marc_compose field must be numer" unless ($f =~ /^\d+$/); |
533 |
|
|
534 |
my $del = $1 || ''; |
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
535 |
$prefix ||= $del if ($all_found == 0); |
my $m = [ $f, $i1, $i2 ]; |
536 |
|
|
537 |
# repeatable index |
warn "### marc_compose input subfields = ", dump(@_),$/ if ($debug > 2); |
|
my $r = $i; |
|
|
$r = 0 if (lc("$2") eq 's'); |
|
538 |
|
|
539 |
my $found = 0; |
while (@_) { |
540 |
my $tmp = $self->get_data(\$rec,$3,$4,$r,\$found); |
my $sf = shift or die "marc_compose $f needs subfield"; |
541 |
|
my $v = shift; |
542 |
|
|
543 |
if ($found) { |
next unless (defined($v) && $v !~ /^\s*$/); |
544 |
push @out, $del; |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
545 |
push @out, $tmp; |
push @$m, ( $sf, $v ); |
546 |
$all_found += $found; |
warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1); |
|
} |
|
547 |
} |
} |
548 |
|
|
549 |
return if (! $all_found); |
warn "## marc_compose current marc = ", dump( $m ),$/ if ($debug > 1); |
550 |
|
|
551 |
my $out = join('',@out); |
push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2); |
552 |
|
} |
553 |
|
|
554 |
if ($out) { |
=head2 marc_duplicate |
|
# add rest of format (suffix) |
|
|
$out .= $format; |
|
555 |
|
|
556 |
# add prefix if not there |
Generate copy of current MARC record and continue working on copy |
|
$out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); |
|
557 |
|
|
558 |
$log->debug("result: $out"); |
marc_duplicate(); |
|
} |
|
559 |
|
|
560 |
if ($eval_code) { |
Copies can be accessed using C<< _get_marc_fields( fetch_next => 1 ) >> or |
561 |
my $eval = $self->fill_in($rec,$eval_code,$i) || return; |
C<< _get_marc_fields( offset => 42 ) >>. |
|
$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"); |
|
|
} |
|
562 |
|
|
563 |
return $out; |
=cut |
564 |
|
|
565 |
|
sub marc_duplicate { |
566 |
|
my $m = $marc_record->[ -1 ]; |
567 |
|
die "can't duplicate record which isn't defined" unless ($m); |
568 |
|
push @{ $marc_record }, dclone( $m ); |
569 |
|
warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); |
570 |
|
$marc_record_offset = $#{ $marc_record }; |
571 |
|
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
572 |
} |
} |
573 |
|
|
574 |
=head2 parse_to_arr |
=head2 marc_remove |
575 |
|
|
576 |
|
Remove some field or subfield from MARC record. |
577 |
|
|
578 |
Similar to C<parse>, but returns array of all repeatable fields |
marc_remove('200'); |
579 |
|
marc_remove('200','a'); |
580 |
|
|
581 |
my @arr = $webpac->parse_to_arr($rec,'v250^a'); |
This will erase field C<200> or C<200^a> from current MARC record. |
582 |
|
|
583 |
|
This is useful after calling C<marc_duplicate> or on it's own (but, you |
584 |
|
should probably just remove that subfield definition if you are not |
585 |
|
using C<marc_duplicate>). |
586 |
|
|
587 |
|
FIXME: support fields < 10. |
588 |
|
|
589 |
=cut |
=cut |
590 |
|
|
591 |
sub parse_to_arr { |
sub marc_remove { |
592 |
my $self = shift; |
my ($f, $sf) = @_; |
593 |
|
|
594 |
my ($rec, $format_utf8) = @_; |
die "marc_remove needs record number" unless defined($f); |
595 |
|
|
596 |
my $log = $self->_get_logger(); |
my $marc = $marc_record->[ $marc_record_offset ]; |
597 |
|
|
598 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); |
|
return if (! $format_utf8); |
|
599 |
|
|
600 |
my $i = 0; |
my $i = 0; |
601 |
my @arr; |
foreach ( 0 .. $#{ $marc } ) { |
602 |
|
last unless (defined $marc->[$i]); |
603 |
|
warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); |
604 |
|
if ($marc->[$i]->[0] eq $f) { |
605 |
|
if (! defined $sf) { |
606 |
|
# remove whole field |
607 |
|
splice @$marc, $i, 1; |
608 |
|
warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); |
609 |
|
$i--; |
610 |
|
} else { |
611 |
|
foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { |
612 |
|
my $o = ($j * 2) + 3; |
613 |
|
if ($marc->[$i]->[$o] eq $sf) { |
614 |
|
# remove subfield |
615 |
|
splice @{$marc->[$i]}, $o, 2; |
616 |
|
warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); |
617 |
|
# is record now empty? |
618 |
|
if ($#{ $marc->[$i] } == 2) { |
619 |
|
splice @$marc, $i, 1; |
620 |
|
warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); |
621 |
|
$i--; |
622 |
|
}; |
623 |
|
} |
624 |
|
} |
625 |
|
} |
626 |
|
} |
627 |
|
$i++; |
628 |
|
} |
629 |
|
|
630 |
|
warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); |
631 |
|
|
632 |
|
$marc_record->[ $marc_record_offset ] = $marc; |
633 |
|
|
634 |
|
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
635 |
|
} |
636 |
|
|
637 |
|
=head1 Functions to extract data from input |
638 |
|
|
639 |
|
This function should be used inside functions to create C<data_structure> described |
640 |
|
above. |
641 |
|
|
642 |
while (my $v = $self->parse($rec,$format_utf8,$i++)) { |
=head2 rec1 |
643 |
push @arr, $v; |
|
644 |
|
Return all values in some field |
645 |
|
|
646 |
|
@v = rec1('200') |
647 |
|
|
648 |
|
TODO: order of values is probably same as in source data, need to investigate that |
649 |
|
|
650 |
|
=cut |
651 |
|
|
652 |
|
sub rec1 { |
653 |
|
my $f = shift; |
654 |
|
warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
655 |
|
return unless (defined($rec) && defined($rec->{$f})); |
656 |
|
warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
657 |
|
if (ref($rec->{$f}) eq 'ARRAY') { |
658 |
|
return map { |
659 |
|
if (ref($_) eq 'HASH') { |
660 |
|
values %{$_}; |
661 |
|
} else { |
662 |
|
$_; |
663 |
|
} |
664 |
|
} @{ $rec->{$f} }; |
665 |
|
} elsif( defined($rec->{$f}) ) { |
666 |
|
return $rec->{$f}; |
667 |
} |
} |
668 |
|
} |
669 |
|
|
670 |
|
=head2 rec2 |
671 |
|
|
672 |
|
Return all values in specific field and subfield |
673 |
|
|
674 |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
@v = rec2('200','a') |
675 |
|
|
676 |
return @arr; |
=cut |
677 |
|
|
678 |
|
sub rec2 { |
679 |
|
my $f = shift; |
680 |
|
return unless (defined($rec && $rec->{$f})); |
681 |
|
my $sf = shift; |
682 |
|
warn "rec2($f,$sf) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
683 |
|
return map { |
684 |
|
if (ref($_->{$sf}) eq 'ARRAY') { |
685 |
|
@{ $_->{$sf} }; |
686 |
|
} else { |
687 |
|
$_->{$sf}; |
688 |
|
} |
689 |
|
} grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} }; |
690 |
} |
} |
691 |
|
|
692 |
=head2 fill_in_to_arr |
=head2 rec |
693 |
|
|
694 |
Similar to C<fill_in>, but returns array of all repeatable fields. Usable |
syntaxtic sugar for |
|
for fields which have lookups, so they shouldn't be parsed but rather |
|
|
C<fill_id>ed. |
|
695 |
|
|
696 |
my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); |
@v = rec('200') |
697 |
|
@v = rec('200','a') |
698 |
|
|
699 |
=cut |
=cut |
700 |
|
|
701 |
sub fill_in_to_arr { |
sub rec { |
702 |
my $self = shift; |
my @out; |
703 |
|
if ($#_ == 0) { |
704 |
|
@out = rec1(@_); |
705 |
|
} elsif ($#_ == 1) { |
706 |
|
@out = rec2(@_); |
707 |
|
} |
708 |
|
if (@out) { |
709 |
|
return @out; |
710 |
|
} else { |
711 |
|
return ''; |
712 |
|
} |
713 |
|
} |
714 |
|
|
715 |
my ($rec, $format_utf8) = @_; |
=head2 regex |
716 |
|
|
717 |
my $log = $self->_get_logger(); |
Apply regex to some or all values |
718 |
|
|
719 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
@v = regex( 's/foo/bar/g', @v ); |
|
return if (! $format_utf8); |
|
720 |
|
|
721 |
my $i = 0; |
=cut |
|
my @arr; |
|
722 |
|
|
723 |
while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { |
sub regex { |
724 |
push @arr, @v; |
my $r = shift; |
725 |
|
my @out; |
726 |
|
#warn "r: $r\n", dump(\@_); |
727 |
|
foreach my $t (@_) { |
728 |
|
next unless ($t); |
729 |
|
eval "\$t =~ $r"; |
730 |
|
push @out, $t if ($t && $t ne ''); |
731 |
} |
} |
732 |
|
return @out; |
733 |
|
} |
734 |
|
|
735 |
|
=head2 prefix |
736 |
|
|
737 |
|
Prefix all values with a string |
738 |
|
|
739 |
|
@v = prefix( 'my_', @v ); |
740 |
|
|
741 |
|
=cut |
742 |
|
|
743 |
|
sub prefix { |
744 |
|
my $p = shift or return; |
745 |
|
return map { $p . $_ } grep { defined($_) } @_; |
746 |
|
} |
747 |
|
|
748 |
|
=head2 suffix |
749 |
|
|
750 |
|
suffix all values with a string |
751 |
|
|
752 |
|
@v = suffix( '_my', @v ); |
753 |
|
|
754 |
|
=cut |
755 |
|
|
756 |
|
sub suffix { |
757 |
|
my $s = shift or die "suffix needs string as first argument"; |
758 |
|
return map { $_ . $s } grep { defined($_) } @_; |
759 |
|
} |
760 |
|
|
761 |
|
=head2 surround |
762 |
|
|
763 |
$log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); |
surround all values with a two strings |
764 |
|
|
765 |
|
@v = surround( 'prefix_', '_suffix', @v ); |
766 |
|
|
767 |
|
=cut |
768 |
|
|
769 |
return @arr; |
sub surround { |
770 |
|
my $p = shift or die "surround need prefix as first argument"; |
771 |
|
my $s = shift or die "surround needs suffix as second argument"; |
772 |
|
return map { $p . $_ . $s } grep { defined($_) } @_; |
773 |
} |
} |
774 |
|
|
775 |
=head2 sort_arr |
=head2 first |
776 |
|
|
777 |
Sort array ignoring case and html in data |
Return first element |
778 |
|
|
779 |
my @sorted = $webpac->sort_arr(@unsorted); |
$v = first( @v ); |
780 |
|
|
781 |
=cut |
=cut |
782 |
|
|
783 |
sub sort_arr { |
sub first { |
784 |
my $self = shift; |
my $r = shift; |
785 |
|
return $r; |
786 |
|
} |
787 |
|
|
788 |
|
=head2 lookup |
789 |
|
|
790 |
my $log = $self->_get_logger(); |
Consult lookup hashes for some value |
791 |
|
|
792 |
# FIXME add Schwartzian Transformation? |
@v = lookup( $v ); |
793 |
|
@v = lookup( @v ); |
794 |
|
|
795 |
my @sorted = sort { |
=cut |
|
$a =~ s#<[^>]+/*>##; |
|
|
$b =~ s#<[^>]+/*>##; |
|
|
lc($b) cmp lc($a) |
|
|
} @_; |
|
|
$log->debug("sorted values: ",sub { join(", ",@sorted) }); |
|
796 |
|
|
797 |
return @sorted; |
sub lookup { |
798 |
|
my $k = shift or return; |
799 |
|
return unless (defined($lookup->{$k})); |
800 |
|
if (ref($lookup->{$k}) eq 'ARRAY') { |
801 |
|
return @{ $lookup->{$k} }; |
802 |
|
} else { |
803 |
|
return $lookup->{$k}; |
804 |
|
} |
805 |
} |
} |
806 |
|
|
807 |
|
=head2 config |
808 |
|
|
809 |
|
Consult config values stored in C<config.yml> |
810 |
|
|
811 |
=head2 _sort_by_order |
# return database code (key under databases in yaml) |
812 |
|
$database_code = config(); # use _ from hash |
813 |
|
$database_name = config('name'); |
814 |
|
$database_input_name = config('input name'); |
815 |
|
$tag = config('input normalize tag'); |
816 |
|
|
817 |
Sort xml tags data structure accoding to C<order=""> attribute. |
Up to three levels are supported. |
818 |
|
|
819 |
=cut |
=cut |
820 |
|
|
821 |
sub _sort_by_order { |
sub config { |
822 |
my $self = shift; |
return unless ($config); |
823 |
|
|
824 |
|
my $p = shift; |
825 |
|
|
826 |
my $va = $self->{'import_xml'}->{'indexer'}->{$a}->{'order'} || |
$p ||= ''; |
|
$self->{'import_xml'}->{'indexer'}->{$a}; |
|
|
my $vb = $self->{'import_xml'}->{'indexer'}->{$b}->{'order'} || |
|
|
$self->{'import_xml'}->{'indexer'}->{$b}; |
|
827 |
|
|
828 |
return $va <=> $vb; |
my $v; |
829 |
|
|
830 |
|
warn "### getting config($p)\n" if ($debug > 1); |
831 |
|
|
832 |
|
my @p = split(/\s+/,$p); |
833 |
|
if ($#p < 0) { |
834 |
|
$v = $config->{ '_' }; # special, database code |
835 |
|
} else { |
836 |
|
|
837 |
|
my $c = dclone( $config ); |
838 |
|
|
839 |
|
foreach my $k (@p) { |
840 |
|
warn "### k: $k c = ",dump($c),$/ if ($debug > 1); |
841 |
|
if (ref($c) eq 'ARRAY') { |
842 |
|
$c = shift @$c; |
843 |
|
warn "config($p) taking first occurence of '$k', probably not what you wanted!\n"; |
844 |
|
last; |
845 |
|
} |
846 |
|
|
847 |
|
if (! defined($c->{$k}) ) { |
848 |
|
$c = undef; |
849 |
|
last; |
850 |
|
} else { |
851 |
|
$c = $c->{$k}; |
852 |
|
} |
853 |
|
} |
854 |
|
$v = $c if ($c); |
855 |
|
|
856 |
|
} |
857 |
|
|
858 |
|
warn "## config( '$p' ) = ",dump( $v ),$/ if ($v && $debug); |
859 |
|
warn "config( '$p' ) is empty\n" if (! $v); |
860 |
|
|
861 |
|
return $v; |
862 |
} |
} |
863 |
|
|
864 |
=head2 _x |
=head2 id |
865 |
|
|
866 |
Convert strings from C<conf/normalize> encoding into application specific |
Returns unique id of this record |
|
(optinally specified using C<code_page> to C<new> constructor. |
|
867 |
|
|
868 |
my $text = $n->_x('normalize text string'); |
$id = id(); |
869 |
|
|
870 |
This is a stub so that other modules doesn't have to implement it. |
Returns C<42/2> for 2nd occurence of MFN 42. |
871 |
|
|
872 |
=cut |
=cut |
873 |
|
|
874 |
sub _x { |
sub id { |
875 |
my $self = shift; |
my $mfn = $config->{_mfn} || die "no _mfn in config data"; |
876 |
return shift; |
return $mfn . $#{$marc_record} ? $#{$marc_record} + 1 : ''; |
877 |
} |
} |
878 |
|
|
879 |
|
=head2 join_with |
880 |
|
|
881 |
=head1 AUTHOR |
Joins walues with some delimiter |
882 |
|
|
883 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
$v = join_with(", ", @v); |
884 |
|
|
885 |
=head1 COPYRIGHT & LICENSE |
=cut |
886 |
|
|
887 |
|
sub join_with { |
888 |
|
my $d = shift; |
889 |
|
warn "### join_with('$d',",dump(@_),")\n" if ($debug > 2); |
890 |
|
my $v = join($d, grep { defined($_) && $_ ne '' } @_); |
891 |
|
return '' unless defined($v); |
892 |
|
return $v; |
893 |
|
} |
894 |
|
|
895 |
|
=head2 split_rec_on |
896 |
|
|
897 |
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
Split record subfield on some regex and take one of parts out |
898 |
|
|
899 |
This program is free software; you can redistribute it and/or modify it |
$a_before_semi_column = |
900 |
under the same terms as Perl itself. |
split_rec_on('200','a', /\s*;\s*/, $part); |
901 |
|
|
902 |
|
C<$part> is optional number of element. First element is |
903 |
|
B<1>, not 0! |
904 |
|
|
905 |
|
If there is no C<$part> parameter or C<$part> is 0, this function will |
906 |
|
return all values produced by splitting. |
907 |
|
|
908 |
=cut |
=cut |
909 |
|
|
910 |
1; # End of WebPAC::DB |
sub split_rec_on { |
911 |
|
die "split_rec_on need (fld,sf,regex[,part]" if ($#_ < 2); |
912 |
|
|
913 |
|
my ($fld, $sf, $regex, $part) = @_; |
914 |
|
warn "### regex ", ref($regex), $regex, $/ if ($debug > 2); |
915 |
|
|
916 |
|
my @r = rec( $fld, $sf ); |
917 |
|
my $v = shift @r; |
918 |
|
warn "### first rec($fld,$sf) = ",dump($v),$/ if ($debug > 2); |
919 |
|
|
920 |
|
return '' if( ! defined($v) || $v =~ /^\s*$/); |
921 |
|
|
922 |
|
my @s = split( $regex, $v ); |
923 |
|
warn "## split_rec_on($fld,$sf,$regex,$part) = ",dump(@s),$/ if ($debug > 1); |
924 |
|
if ($part && $part > 0) { |
925 |
|
return $s[ $part - 1 ]; |
926 |
|
} else { |
927 |
|
return @s; |
928 |
|
} |
929 |
|
} |
930 |
|
|
931 |
|
# END |
932 |
|
1; |