7 |
|
|
8 |
use WebPAC::Common; |
use WebPAC::Common; |
9 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common/; |
|
use Text::Iconv; |
|
10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
|
use Encode qw/from_to/; |
12 |
|
|
13 |
=head1 NAME |
=head1 NAME |
14 |
|
|
16 |
|
|
17 |
=head1 VERSION |
=head1 VERSION |
18 |
|
|
19 |
Version 0.10 |
Version 0.11 |
20 |
|
|
21 |
=cut |
=cut |
22 |
|
|
23 |
our $VERSION = '0.10'; |
our $VERSION = '0.11'; |
24 |
|
|
25 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
26 |
|
|
159 |
|
|
160 |
$input->open( |
$input->open( |
161 |
path => '/path/to/database/file', |
path => '/path/to/database/file', |
162 |
code_page => '852', |
code_page => 'cp852', |
163 |
limit => 500, |
limit => 500, |
164 |
offset => 6000, |
offset => 6000, |
165 |
lookup => $lookup_obj, |
lookup => $lookup_obj, |
174 |
}, |
}, |
175 |
); |
); |
176 |
|
|
177 |
By default, C<code_page> is assumed to be C<852>. |
By default, C<code_page> is assumed to be C<cp852>. |
178 |
|
|
179 |
C<offset> is optional parametar to position at some offset before reading from database. |
C<offset> is optional parametar to position at some offset before reading from database. |
180 |
|
|
205 |
if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE'); |
if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE'); |
206 |
|
|
207 |
$log->logcroak("need path") if (! $arg->{'path'}); |
$log->logcroak("need path") if (! $arg->{'path'}); |
208 |
my $code_page = $arg->{'code_page'} || '852'; |
my $code_page = $arg->{'code_page'} || 'cp852'; |
209 |
|
|
210 |
# store data in object |
# store data in object |
211 |
$self->{'input_code_page'} = $code_page; |
$self->{'input_code_page'} = $code_page; |
213 |
$self->{$v} = $arg->{$v} if ($arg->{$v}); |
$self->{$v} = $arg->{$v} if ($arg->{$v}); |
214 |
} |
} |
215 |
|
|
|
# create Text::Iconv object |
|
|
$self->{iconv} = Text::Iconv->new($code_page,$self->{'encoding'}); ## FIXME remove! |
|
|
|
|
216 |
my $filter_ref; |
my $filter_ref; |
217 |
my $recode_regex; |
my $recode_regex; |
218 |
my $recode_map; |
my $recode_map; |
240 |
|
|
241 |
my ($db, $size) = $self->{open_db}->( $self, |
my ($db, $size) = $self->{open_db}->( $self, |
242 |
path => $arg->{path}, |
path => $arg->{path}, |
243 |
filter => sub { |
# filter => sub { |
244 |
my ($l,$f_nr) = @_; |
# my ($l,$f_nr) = @_; |
245 |
return unless defined($l); |
# return unless defined($l); |
246 |
|
# from_to($l, $code_page, $self->{'encoding'}); |
247 |
## FIXME remove iconv! |
# $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map); |
248 |
$l = $self->{iconv}->convert($l) if ($self->{iconv}); |
# return $l; |
249 |
|
# }, |
|
$l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map); |
|
|
|
|
|
## FIXME remove this warning when we are sure that none of API is calling |
|
|
## this wrongly |
|
|
#warn "filter called without field number" unless ($f_nr); |
|
|
|
|
|
return $l unless ($rec_regex && $f_nr); |
|
|
|
|
|
# my $max_regex = 100; |
|
|
|
|
|
# apply regexps |
|
|
if ($rec_regex && defined($rec_regex->{$f_nr})) { |
|
|
$log->logconfess("regexps->{$f_nr} must be ARRAY") if (ref($rec_regex->{$f_nr}) ne 'ARRAY'); |
|
|
my $c = 0; |
|
|
foreach my $r (@{ $rec_regex->{$f_nr} }) { |
|
|
#$log->debug("\$l = $l\neval \$l =~ $r"); |
|
|
eval '$l =~ ' . $r; |
|
|
$log->error("error applying regex: $r") if ($@); |
|
|
|
|
|
# while ( $c < $max_regex && eval '$l =~ ' . $r ) { $c++ }; |
|
|
# $log->error("field $f_nr has more than $max_regex regex iterations\n\$l = $l\neval \$l =~ $r") if ($c == $max_regex); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
return $l; |
|
|
}, |
|
250 |
%{ $arg }, |
%{ $arg }, |
251 |
); |
); |
252 |
|
|
286 |
|
|
287 |
$log->debug("position: $pos\n"); |
$log->debug("position: $pos\n"); |
288 |
|
|
289 |
my $rec = $self->{fetch_rec}->($self, $db, $pos ); |
my $rec = $self->{fetch_rec}->($self, $db, $pos, sub { |
290 |
|
my ($l,$f_nr) = @_; |
291 |
|
# return unless defined($l); |
292 |
|
# return $l unless ($rec_regex && $f_nr); |
293 |
|
|
294 |
|
$log->debug("-=> $f_nr ## $l"); |
295 |
|
|
296 |
|
# codepage conversion and recode_regex |
297 |
|
# from_to($l, $code_page, $self->{'encoding'}); |
298 |
|
from_to($l, $code_page, 'utf-8'); |
299 |
|
$l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map); |
300 |
|
|
301 |
|
# apply regexps |
302 |
|
if ($rec_regex && defined($rec_regex->{$f_nr})) { |
303 |
|
$log->logconfess("regexps->{$f_nr} must be ARRAY") if (ref($rec_regex->{$f_nr}) ne 'ARRAY'); |
304 |
|
my $c = 0; |
305 |
|
foreach my $r (@{ $rec_regex->{$f_nr} }) { |
306 |
|
#$log->debug("\$l = $l\neval \$l =~ $r"); |
307 |
|
eval '$l =~ ' . $r; |
308 |
|
$log->error("error applying regex: $r") if ($@); |
309 |
|
} |
310 |
|
} |
311 |
|
|
312 |
|
$log->debug("<=- $f_nr ## $l"); |
313 |
|
return $l; |
314 |
|
}); |
315 |
|
|
316 |
$log->debug(sub { Dumper($rec) }); |
$log->debug(sub { Dumper($rec) }); |
317 |
|
|
333 |
# update counters for statistics |
# update counters for statistics |
334 |
if ($self->{stats}) { |
if ($self->{stats}) { |
335 |
|
|
336 |
|
# fetch clean record with regexpes applied for statistics |
337 |
|
my $rec = $self->{fetch_rec}->($self, $db, $pos); |
338 |
|
|
339 |
foreach my $fld (keys %{ $rec }) { |
foreach my $fld (keys %{ $rec }) { |
340 |
$self->{_stats}->{fld}->{ $fld }++; |
$self->{_stats}->{fld}->{ $fld }++; |
341 |
|
|
347 |
if (ref($row) eq 'HASH') { |
if (ref($row) eq 'HASH') { |
348 |
|
|
349 |
foreach my $sf (keys %{ $row }) { |
foreach my $sf (keys %{ $row }) { |
350 |
|
next if ($sf eq 'subfields'); |
351 |
$self->{_stats}->{sf}->{ $fld }->{ $sf }->{count}++; |
$self->{_stats}->{sf}->{ $fld }->{ $sf }->{count}++; |
352 |
$self->{_stats}->{sf}->{ $fld }->{ $sf }->{repeatable}++ |
$self->{_stats}->{sf}->{ $fld }->{ $sf }->{repeatable}++ |
353 |
if (ref($row->{$sf}) eq 'ARRAY'); |
if (ref($row->{$sf}) eq 'ARRAY'); |