use WebPAC::Common;
use base qw/WebPAC::Common/;
-use Text::Iconv;
use Data::Dumper;
+use Encode qw/from_to/;
=head1 NAME
=head1 VERSION
-Version 0.09
+Version 0.12
=cut
-our $VERSION = '0.09';
+our $VERSION = '0.12';
=head1 SYNOPSIS
#eval $self->{module} .'->import';
# check if required subclasses are implemented
- foreach my $subclass (qw/open_db fetch_rec init/) {
+ foreach my $subclass (qw/open_db fetch_rec init dump_rec/) {
my $n = $self->{module} . '::' . $subclass;
if (! defined &{ $n }) {
my $missing = "missing $subclass in $self->{module}";
$input->open(
path => '/path/to/database/file',
- code_page => '852',
+ code_page => 'cp852',
limit => 500,
offset => 6000,
lookup => $lookup_obj,
900 => { '^a' => { ' : ' => '^b' } },
901 => { '*' => { '^b' => ' ; ' } },
},
+ modify_file => 'conf/modify/mapping.map',
);
-By default, C<code_page> is assumed to be C<852>.
+By default, C<code_page> is assumed to be C<cp852>.
C<offset> is optional parametar to position at some offset before reading from database.
delimiters to subfields, as well as oprations on fields (if subfield is
defined as C<*>.
+C<modify_file> is alternative for C<modify_records> above which preserves order and offers
+(hopefully) simplier sintax than YAML or perl (see L</modify_file_regex>). This option
+overrides C<modify_records> if both exists for same input.
+
Returns size of database, regardless of C<offset> and C<limit>
parametars, see also C<size>.
if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE');
$log->logcroak("need path") if (! $arg->{'path'});
- my $code_page = $arg->{'code_page'} || '852';
+ my $code_page = $arg->{'code_page'} || 'cp852';
# store data in object
$self->{'input_code_page'} = $code_page;
$self->{$v} = $arg->{$v} if ($arg->{$v});
}
- # create Text::Iconv object
- $self->{iconv} = Text::Iconv->new($code_page,$self->{'encoding'}); ## FIXME remove!
-
my $filter_ref;
my $recode_regex;
my $recode_map;
}
- my $rec_regex = $self->modify_record_regexps(%{ $arg->{modify_records} });
- $log->debug("rec_regex: ", Dumper($rec_regex));
+ my $rec_regex;
+ if (my $p = $arg->{modify_file}) {
+ $log->debug("using modify_file $p");
+ $rec_regex = $self->modify_file_regexps( $p );
+ } elsif (my $h = $arg->{modify_records}) {
+ $log->debug("using modify_records ", Dumper( $h ));
+ $rec_regex = $self->modify_record_regexps(%{ $h });
+ }
+ $log->debug("rec_regex: ", Dumper($rec_regex)) if ($rec_regex);
my ($db, $size) = $self->{open_db}->( $self,
path => $arg->{path},
- filter => sub {
- my ($l,$f_nr) = @_;
- return unless defined($l);
-
- ## FIXME remove iconv!
- $l = $self->{iconv}->convert($l) if ($self->{iconv});
-
- $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);
-
- # 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} }) {
- while ( eval '$l =~ ' . $r ) { $c++ };
- }
- warn "## field $f_nr triggered $c regexpes\n" if ($c && $self->{debug});
- }
-
- return $l;
- },
+# filter => sub {
+# my ($l,$f_nr) = @_;
+# return unless defined($l);
+# from_to($l, $code_page, $self->{'encoding'});
+# $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map);
+# return $l;
+# },
%{ $arg },
);
$log->debug("position: $pos\n");
- my $rec = $self->{fetch_rec}->($self, $db, $pos );
+ my $rec = $self->{fetch_rec}->($self, $pos, sub {
+ my ($l,$f_nr) = @_;
+# return unless defined($l);
+# return $l unless ($rec_regex && $f_nr);
+
+ $log->debug("-=> $f_nr ## $l");
+
+ # codepage conversion and recode_regex
+ from_to($l, $code_page, $self->{'encoding'});
+ $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map);
+
+ # 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} }) {
+ my $old_l = $l;
+ eval '$l =~ ' . $r;
+ if ($old_l ne $l) {
+ $log->debug("REGEX on $f_nr eval \$l =~ $r\n## old l: [$old_l]\n## new l: [$l]");
+ }
+ $log->error("error applying regex: $r") if ($@);
+ }
+ }
+
+ $log->debug("<=- $f_nr ## $l");
+ return $l;
+ });
$log->debug(sub { Dumper($rec) });
# update counters for statistics
if ($self->{stats}) {
+ # fetch clean record with regexpes applied for statistics
+ my $rec = $self->{fetch_rec}->($self, $pos);
+
foreach my $fld (keys %{ $rec }) {
$self->{_stats}->{fld}->{ $fld }++;
if (ref($row) eq 'HASH') {
foreach my $sf (keys %{ $row }) {
+ next if ($sf eq 'subfields');
$self->{_stats}->{sf}->{ $fld }->{ $sf }->{count}++;
$self->{_stats}->{sf}->{ $fld }->{ $sf }->{repeatable}++
if (ref($row->{$sf}) eq 'ARRAY');
return $out;
}
+=head2 dump
+
+Display humanly readable dump of record
+
+=cut
+
+sub dump {
+ my $self = shift;
+
+ return $self->{dump_rec}->($self, $self->{pos});
+
+}
+
=head2 modify_record_regexps
-Generate hash with regexpes to be applied using L<filter>.
+Generate hash with regexpes to be applied using l<filter>.
my $regexpes = $input->modify_record_regexps(
900 => { '^a' => { ' : ' => '^b' } },
=cut
+sub _get_regex {
+ my ($sf,$from,$to) = @_;
+ if ($sf =~ /^\^/) {
+ return
+ 's/\Q'. $sf .'\E([^\^]*?)\Q'. $from .'\E([^\^]*?)/'. $sf .'$1'. $to .'$2/';
+ } else {
+ return
+ 's/\Q'. $from .'\E/'. $to .'/g';
+ }
+}
+
sub modify_record_regexps {
my $self = shift;
my $modify_record = {@_};
my $regexpes;
+ my $log = $self->_get_logger();
+
foreach my $f (keys %$modify_record) {
-warn "--- f: $f\n";
+ $log->debug("field: $f");
+
foreach my $sf (keys %{ $modify_record->{$f} }) {
-warn "---- sf: $sf\n";
+ $log->debug("subfield: $sf");
+
foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) {
my $to = $modify_record->{$f}->{$sf}->{$from};
#die "no field?" unless defined($to);
-warn "----- transform: |$from| -> |$to|\n";
-
- if ($sf =~ /^\^/) {
- my $regex =
- 's/\Q'. $sf .'\E([^\^]+)\Q'. $from .'\E([^\^]+)/'. $sf .'$1'. $to .'$2/g';
- push @{ $regexpes->{$f} }, $regex;
-warn ">>>>> $regex [sf]\n";
- } else {
- my $regex =
- 's/\Q'. $from .'\E/'. $to .'/g';
- push @{ $regexpes->{$f} }, $regex;
-warn ">>>>> $regex [global]\n";
- }
+ $log->debug("transform: |$from| -> |$to|");
+ my $regex = _get_regex($sf,$from,$to);
+ push @{ $regexpes->{$f} }, $regex;
+ $log->debug("regex: $regex");
}
}
}
return $regexpes;
}
+=head2 modify_file_regexps
+
+Generate hash with regexpes to be applied using l<filter> from
+pseudo hash/yaml format for regex mappings.
+
+It should be obvious:
+
+ 200
+ '^a'
+ ' : ' => '^e'
+ ' = ' => '^d'
+
+In field I<200> find C<'^a'> and then C<' : '>, and replace it with C<'^e'>.
+In field I<200> find C<'^a'> and then C<' = '>, and replace it with C<'^d'>.
+
+ my $regexpes = $input->modify_file_regexps( 'conf/modify/common.pl' );
+
+On undef path it will just return.
+
+=cut
+
+sub modify_file_regexps {
+ my $self = shift;
+
+ my $modify_path = shift || return;
+
+ my $log = $self->_get_logger();
+
+ my $regexpes;
+
+ CORE::open(my $fh, $modify_path) || $log->logdie("can't open modify file $modify_path: $!");
+
+ my ($f,$sf);
+
+ while(<$fh>) {
+ chomp;
+ next if (/^#/ || /^\s*$/);
+
+ if (/^\s*(\d+)\s*$/) {
+ $f = $1;
+ $log->debug("field: $f");
+ next;
+ } elsif (/^\s*'([^']*)'\s*$/) {
+ $sf = $1;
+ $log->die("can't define subfiled before field in: $_") unless ($f);
+ $log->debug("subfield: $sf");
+ } elsif (/^\s*'([^']*)'\s*=>\s*'([^']*)'\s*$/) {
+ my ($from,$to) = ($1, $2);
+
+ $log->debug("transform: |$from| -> |$to|");
+
+ my $regex = _get_regex($sf,$from,$to);
+ push @{ $regexpes->{$f} }, $regex;
+ $log->debug("regex: $regex");
+ }
+ }
+
+ return $regexpes;
+}
+
=head1 MEMORY USAGE
C<low_mem> options is double-edged sword. If enabled, WebPAC