--- trunk2/lib/WebPac.pm 2004/06/16 11:29:37 353 +++ trunk2/lib/WebPAC.pm 2004/06/20 15:49:09 373 @@ -1,24 +1,37 @@ -package WebPac; +package WebPAC; + +use warnings; +use strict; use Carp; use Text::Iconv; use Config::IniFiles; +use XML::Simple; +use Template; +use Log::Log4perl qw(get_logger :levels); + +use Data::Dumper; + +#my $LOOKUP_REGEX = '\[[^\[\]]+\]'; +#my $LOOKUP_REGEX_SAVE = '\[([^\[\]]+)\]'; +my $LOOKUP_REGEX = 'lookup{[^\{\}]+}'; +my $LOOKUP_REGEX_SAVE = 'lookup{([^\{\}]+)}'; =head1 NAME -WebPac - base class for WebPac +WebPAC - base class for WebPAC =head1 DESCRIPTION -This module implements methods used by WebPac. +This module implements methods used by WebPAC. =head1 METHODS =head2 new -This will create new instance of WebPac using configuration specified by C. +This will create new instance of WebPAC using configuration specified by C. - my $webpac = new WebPac( + my $webpac = new WebPAC( config_file => 'name.conf', [code_page => 'ISO-8859-2',] ); @@ -32,11 +45,25 @@ =cut +# mapping between data type and tag which specify +# format in XML file +my %type2tag = ( + 'isis' => 'isis', +# 'excel' => 'column', +# 'marc' => 'marc', +# 'feed' => 'feed' +); + sub new { my $class = shift; my $self = {@_}; bless($self, $class); + my $log_file = $self->{'log'} || "log.conf"; + Log::Log4perl->init($log_file); + + my $log = $self->_get_logger(); + # fill in default values # output codepage $self->{'code_page'} = 'ISO-8859-2' if (! $self->{'code_page'}); @@ -44,8 +71,9 @@ # # read global.conf # + $log->debug("read 'global.conf'"); - $self->{global_config_file} = new Config::IniFiles( -file => 'global.conf' ) || croak "can't open 'global.conf'"; + my $config = new Config::IniFiles( -file => 'global.conf' ) || $log->logcroak("can't open 'global.conf'"); # read global config parametars foreach my $var (qw( @@ -55,27 +83,28 @@ dbi_passwd show_progress my_unac_filter + output_template )) { - $self->{global_config}->{$var} = $self->{global_config_file}->val('global', $var); + $self->{'global_config'}->{$var} = $config->val('global', $var); } # # read indexer config file # - $self->{indexer_config_file} = new Config::IniFiles( -file => $self->{config_file} ) || croak "can't open '$self->{config_file}'"; + $self->{indexer_config_file} = new Config::IniFiles( -file => $self->{config_file} ) || $log->logcroak("can't open '",$self->{config_file},"'"); - # read global config parametars - foreach my $var (qw( - dbi_dbd - dbi_dsn - dbi_user - dbi_passwd - show_progress - my_unac_filter - )) { - $self->{global_config}->{$var} = $self->{global_config_file}->val('global', $var); - } + # create UTF-8 convertor for import_xml files + $self->{'utf2cp'} = Text::Iconv->new('UTF-8' ,$self->{'code_page'}); + + # create Template toolkit instance + $self->{'tt'} = Template->new( + INCLUDE_PATH => ($self->{'global_config_file'}->{'output_template'} || './output_template'), +# FILTERS => { +# 'foo' => \&foo_filter, +# }, + EVAL_PERL => 1, + ); return $self; } @@ -96,8 +125,6 @@ If optional parametar C is set, it will read just 500 records from database in example above. -Returns number of last record read into memory (size of database, really). - C argument is an array of lookups to create. Each lookup must have C and C. Optional parametar C is perl code to evaluate before storing value in index. @@ -109,13 +136,17 @@ 'val' => 'v900' }, ] +Returns number of last record read into memory (size of database, really). + =cut sub open_isis { my $self = shift; my $arg = {@_}; - croak "need filename" if (! $arg->{'filename'}); + my $log = $self->_get_logger(); + + $log->logcroak("need filename") if (! $arg->{'filename'}); my $code_page = $arg->{'code_page'} || '852'; use OpenIsis; @@ -125,10 +156,16 @@ # create Text::Iconv object my $cp = Text::Iconv->new($code_page,$self->{'code_page'}); + $log->info("reading ISIS database '",$arg->{'filename'},"'"); + my $isis_db = OpenIsis::open($arg->{'filename'}); my $maxmfn = OpenIsis::maxRowid( $isis_db ) || 1; + $maxmfn = $self->{limit_mfn} if ($self->{limit_mfn}); + + $log->info("processing $maxmfn records..."); + # read database for (my $mfn = 1; $mfn <= $maxmfn; $mfn++) { @@ -156,28 +193,162 @@ } # create lookup + my $rec = $self->{'data'}->{$mfn}; + $self->create_lookup($rec, @{$arg->{'lookup'}}); + + } + + $self->{'current_mfn'} = 1; + + # store max mfn and return it. + return $self->{'max_mfn'} = $maxmfn; +} + +=head2 fetch_rec + +Fetch next record from database. It will also display progress bar (once +it's implemented, that is). + + my $rec = $webpac->fetch_rec; + +=cut + +sub fetch_rec { + my $self = shift; + + my $log = $self->_get_logger(); + + my $mfn = $self->{'current_mfn'}++ || $log->logconfess("it seems that you didn't load database!"); + + if ($mfn > $self->{'max_mfn'}) { + $self->{'current_mfn'} = $self->{'max_mfn'}; + $log->debug("at EOF"); + return; + } + + return $self->{'data'}->{$mfn}; +} + +=head2 open_import_xml + +Read file from C directory and parse it. + + $webpac->open_import_xml(type => 'isis'); + +=cut + +sub open_import_xml { + my $self = shift; + + my $log = $self->_get_logger(); + + my $arg = {@_}; + $log->logconfess("need type to load file from import_xml/") if (! $arg->{'type'}); + + $self->{'type'} = $arg->{'type'}; + + my $type_base = $arg->{'type'}; + $type_base =~ s/_.*$//g; + + $self->{'tag'} = $type2tag{$type_base}; + + $log->debug("using type '",$self->{'type'},"' tag <",$self->{'tag'},">") if ($self->{'debug'}); + + my $f = "./import_xml/".$self->{'type'}.".xml"; + $log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f"); + + $log->debug("reading '$f'") if ($self->{'debug'}); + + $self->{'import_xml'} = XMLin($f, + ForceArray => [ $self->{'tag'}, 'config', 'format' ], + ); + +} + +=head2 create_lookup + +Create lookup from record using lookup definition. - foreach my $i (@{$arg->{lookup}}) { - my $rec = $self->{'data'}->{$mfn}; - if ($i->{'eval'}) { - my $eval = $self->fill_in($rec,$i->{'eval'}); - my $key = $self->fill_in($rec,$i->{'key'}); - my @val = $self->fill_in($rec,$i->{'val'}); - if ($key && @val && eval $eval) { - push @{$self->{'lookup'}->{$key}}, @val; + $self->create_lookup($rec, @lookups); + +Called internally by C methods. + +=cut + +sub create_lookup { + my $self = shift; + + my $log = $self->_get_logger(); + + my $rec = shift || $log->logconfess("need record to create lookup"); + $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); + + foreach my $i (@_) { + if ($i->{'eval'}) { + my $eval = $self->fill_in($rec,$i->{'eval'}); + my $key = $self->fill_in($rec,$i->{'key'}); + my @val = $self->fill_in($rec,$i->{'val'}); + if ($key && @val && eval $eval) { + $log->debug("stored $key = ",sub { join(" | ",@val) }); + push @{$self->{'lookup'}->{$key}}, @val; + } + } else { + my $key = $self->fill_in($rec,$i->{'key'}); + my @val = $self->fill_in($rec,$i->{'val'}); + if ($key && @val) { + $log->debug("stored $key = ",sub { join(" | ",@val) }); + push @{$self->{'lookup'}->{$key}}, @val; + } + } + } +} + +=head2 get_data + +Returns value from record. + + my $text = $self->get_data(\$rec,$f,$sf,$i,\$found); + +Arguments are: +record reference C<$rec>, +field C<$f>, +optional subfiled C<$sf>, +index for repeatable values C<$i>. + +Optinal variable C<$found> will be incremeted if there +is field. + +Returns value or empty string. + +=cut + +sub get_data { + my $self = shift; + + my ($rec,$f,$sf,$i,$found) = @_; + + if ($$rec->{$f}) { + return '' if (! $$rec->{$f}->[$i]); + if ($sf && $$rec->{$f}->[$i]->{$sf}) { + $$found++ if (defined($$found)); + return $$rec->{$f}->[$i]->{$sf}; + } elsif ($$rec->{$f}->[$i]) { + $$found++ if (defined($$found)); + # it still might have subfield, just + # not specified, so we'll dump all + if ($$rec->{$f}->[$i] =~ /HASH/o) { + my $out; + foreach my $k (keys %{$$rec->{$f}->[$i]}) { + $out .= $$rec->{$f}->[$i]->{$k}." "; } + return $out; } else { - my $key = $self->fill_in($rec,$i->{'key'}); - my @val = $self->fill_in($rec,$i->{'val'}); - if ($key && @val) { - push @{$self->{'lookup'}->{$key}}, @val; - } + return $$rec->{$f}->[$i]; } } + } else { + return ''; } - - # store max mfn and return it. - return $self->{'max_mfn'} = $maxmfn; } =head2 fill_in @@ -186,14 +357,14 @@ strings with placeholders and returns string or array of with substituted values from record. - $webpac->fill_in($rec,'v250^a'); + my $text = $webpac->fill_in($rec,'v250^a'); Optional argument is ordinal number for repeatable fields. By default, 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. - $webpac->fill_in($rec,'Title: v250^a',1); + my $text = $webpac->fill_in($rec,'Title: v250^a',1); This function B perform parsing of format to inteligenty skip delimiters before fields which aren't used. @@ -203,47 +374,34 @@ sub fill_in { my $self = shift; - my $rec = shift || confess "need data record"; - my $format = shift || confess "need format to parse"; + my $log = $self->_get_logger(); + + my $rec = shift || $log->logconfess("need data record"); + my $format = shift || $log->logconfess("need format to parse"); # iteration (for repeatable fields) my $i = shift || 0; # FIXME remove for speedup? - if ($rec !~ /HASH/o) { - confess("need HASH as first argument!"); - } + $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); my $found = 0; - # get field with subfield - sub get_sf { - my ($found,$rec,$f,$sf,$i) = @_; - if ($$rec->{$f} && $$rec->{$f}->[$i]->{$sf}) { - $$found++; - return $$rec->{$f}->[$i]->{$sf}; - } else { - return ''; - } - } - - # get field (without subfield) - sub get_nosf { - my ($found,$rec,$f,$i) = @_; - if ($$rec->{$f} && $$rec->{$f}->[$i]) { - $$found++; - return $$rec->{$f}->[$i]; - } else { - return ''; - } - } + my $eval_code; + # remove eval{...} from beginning + $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); # do actual replacement of placeholders - $format =~ s/v(\d+)\^(\w)/get_sf(\$found,\$rec,$1,$2,$i)/ges; - $format =~ s/v(\d+)/get_nosf(\$found,\$rec,$1,$i)/ges; + $format =~ s/v(\d+)(?:\^(\w))?/$self->get_data(\$rec,$1,$2,$i,\$found)/ges; if ($found) { + $log->debug("format: $format"); + if ($eval_code) { + my $eval = $self->fill_in($rec,$eval_code,$i); + return if (! $self->_eval($eval)); + } # do we have lookups? - if ($format =~ /\[[^\[\]]+\]/o) { + if ($format =~ /$LOOKUP_REGEX/o) { + $log->debug("format '$format' has lookup"); return $self->lookup($format); } else { return $format; @@ -255,44 +413,364 @@ =head2 lookup -This function will perform lookups on format supplied to it. +Perform lookups on format supplied to it. - my $txt = $self->lookup('[v900]'); + my $text = $self->lookup('[v900]'); + +Lookups can be nested (like C<[d:[a:[v900]]]>). =cut sub lookup { my $self = shift; - my $tmp = shift || confess "need format"; + my $log = $self->_get_logger(); + + my $tmp = shift || $log->logconfess("need format"); - if ($tmp =~ /\[[^\[\]]+\]/o) { + if ($tmp =~ /$LOOKUP_REGEX/o) { my @in = ( $tmp ); -#print "##lookup $tmp\n"; + + $log->debug("lookup for: ",$tmp); + my @out; while (my $f = shift @in) { - if ($f =~ /\[([^\[\]]+)\]/) { + if ($f =~ /$LOOKUP_REGEX_SAVE/o) { my $k = $1; if ($self->{'lookup'}->{$k}) { -#print "## lookup key = $k\n"; foreach my $nv (@{$self->{'lookup'}->{$k}}) { my $tmp2 = $f; - $tmp2 =~ s/\[$k\]/$nv/g; + $tmp2 =~ s/lookup{$k}/$nv/g; push @in, $tmp2; -#print "## lookup in => $tmp2\n"; } } else { undef $f; } } elsif ($f) { push @out, $f; -#print "## lookup out => $f\n"; } } + $log->logconfess("return is array and it's not expected!") unless wantarray; return @out; } else { return $tmp; } } +=head2 parse + +Perform smart parsing of string, skipping delimiters for fields which aren't +defined. It can also eval code in format starting with C and +return output or nothing depending on eval code. + + my $text = $webpac->parse($rec,'eval{"v901^a" eq "Deskriptor"}descriptor: v250^a', $i); + +=cut + +sub parse { + my $self = shift; + + my ($rec, $format_utf8, $i) = @_; + + return if (! $format_utf8); + + my $log = $self->_get_logger(); + + $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); + $log->logconfess("need utf2cp Text::Iconv object!") if (! $self->{'utf2cp'}); + + $i = 0 if (! $i); + + my $format = $self->{'utf2cp'}->convert($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); + + my @out; + + $log->debug("format: $format"); + + my $eval_code; + # remove eval{...} from beginning + $eval_code = $1 if ($format =~ s/^eval{([^}]+)}//s); + + my $prefix; + my $all_found=0; + + while ($format =~ s/^(.*?)v(\d+)(?:\^(\w))?//s) { + + my $del = $1 || ''; + $prefix ||= $del if ($all_found == 0); + + my $found = 0; + my $tmp = $self->get_data(\$rec,$2,$3,$i,\$found); + + if ($found) { + push @out, $del; + push @out, $tmp; + $all_found += $found; + } + } + + return if (! $all_found); + + my $out = join('',@out); + + if ($out) { + # add rest of format (suffix) + $out .= $format; + + # add prefix if not there + $out = $prefix . $out if ($out !~ m/^\Q$prefix\E/); + + $log->debug("result: $out"); + } + + if ($eval_code) { + my $eval = $self->fill_in($rec,$eval_code,$i); + $log->debug("about to eval{",$eval,"} format: $out"); + return if (! $self->_eval($eval)); + } + + return $out; +} + +=head2 parse_to_arr + +Similar to C, but returns array of all repeatable fields + + my @arr = $webpac->parse_to_arr($rec,'v250^a'); + +=cut + +sub parse_to_arr { + my $self = shift; + + my ($rec, $format_utf8) = @_; + + my $log = $self->_get_logger(); + + $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); + return if (! $format_utf8); + + my $i = 0; + my @arr; + + while (my $v = $self->parse($rec,$format_utf8,$i++)) { + push @arr, $v; + } + + $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); + + return @arr; +} + +=head2 fill_in_to_arr + +Similar to C, but returns array of all repeatable fields. Usable +for fields which have lookups, so they shouldn't be parsed but rather +Ced. + + my @arr = $webpac->fill_in_to_arr($rec,'[v900];;[v250^a]'); + +=cut + +sub fill_in_to_arr { + my $self = shift; + + my ($rec, $format_utf8) = @_; + + my $log = $self->_get_logger(); + + $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); + return if (! $format_utf8); + + my $i = 0; + my @arr; + + while (my @v = $self->fill_in($rec,$format_utf8,$i++)) { + push @arr, @v; + } + + $log->debug("format '$format_utf8' returned ",--$i," elements: ", sub { join(" | ",@arr) }) if (@arr); + + return @arr; +} + + +=head2 data_structure + +Create in-memory data structure which represents layout from C. +It is used later to produce output. + + my @ds = $webpac->data_structure($rec); + +=cut + +sub data_structure { + my $self = shift; + + my $log = $self->_get_logger(); + + my $rec = shift; + $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); + + my @sorted_tags; + if ($self->{tags_by_order}) { + @sorted_tags = @{$self->{tags_by_order}}; + } else { + @sorted_tags = sort { $self->_sort_by_order } keys %{$self->{'import_xml'}->{'indexer'}}; + $self->{tags_by_order} = \@sorted_tags; + } + + my @ds; + + $log->debug("tags: ",sub { join(", ",@sorted_tags) }); + + foreach my $field (@sorted_tags) { + + my $row; + +#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); + + foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { + my $format = $tag->{'value'} || $tag->{'content'}; + + $log->debug("format: $format"); + + my @v; + if ($format =~ /$LOOKUP_REGEX/o) { + @v = $self->fill_in_to_arr($rec,$format); + } else { + @v = $self->parse_to_arr($rec,$format); + } + next if (! @v); + + # does tag have type? + if ($tag->{'type'}) { + push @{$row->{$tag->{'type'}}}, @v; + } else { + push @{$row->{'display'}}, @v; + push @{$row->{'swish'}}, @v; + } + + } + + if ($row) { + $row->{'tag'} = $field; + push @ds, $row; + $log->debug("row $field: ",sub { Dumper($row) }); + } + + } + + return @ds; + +} + +=head2 output + +Create output from in-memory data structure using Template Toolkit template. + +my $text = $webpac->output( template => 'text.tt', data => @ds ); + +=cut + +sub output { + my $self = shift; + + my $args = {@_}; + + my $log = $self->_get_logger(); + + $log->logconfess("need template name") if (! $args->{'template'}); + $log->logconfess("need data array") if (! $args->{'data'}); + + my $out; + + $self->{'tt'}->process( + $args->{'template'}, + $args, + \$out + ) || confess $self->{'tt'}->error(); + + return $out; +} + +# +# +# + +=head1 INTERNAL METHODS + +Here is a quick list of internal methods, mostly useful to turn debugging +on them (see L below for explanation). + +=cut + +=head2 _eval + +Internal function to eval code without C. + +=cut + +sub _eval { + my $self = shift; + + my $code = shift || return; + + my $log = $self->_get_logger(); + + no strict 'subs'; + my $ret = eval $code; + if ($@) { + $log->error("problem with eval code [$code]: $@"); + } + + $log->debug("eval: ",$code," [",$ret,"]"); + + return $ret || 0; +} + +=head2 _sort_by_order + +Sort xml tags data structure accoding to C attribute. + +=cut + +sub _sort_by_order { + my $self = shift; + + 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; +} + +sub _get_logger { + my $self = shift; + + my @c = caller(1); + return get_logger($c[3]); +} + +# +# +# + +=head1 LOGGING + +Logging in WebPAC is performed by L with config file +C. + +Methods defined above have different levels of logging, so +it's descriptions will be useful to turn (mostry B logging) on +or off to see why WabPAC isn't perforing as you expect it (it might even +be a bug!). + +B. To repeat, you can +also use method names, and not only classes (which are just few) +to filter logging. + +=cut + 1;