--- Webpacus/lib/Webpacus/Model/WebPAC.pm 2005/11/25 00:23:33 142
+++ Webpacus/lib/Webpacus/Model/WebPAC.pm 2006/01/22 00:44:37 380
@@ -6,11 +6,13 @@
use base qw/
Catalyst::Model
/;
-use Data::Dumper;
-use WebPAC::DB;
-use WebPAC::Output::TT;
-use WebPAC::Search::Estraier 0.02;
+use WebPAC::Store 0.08;
+use WebPAC::Search::Estraier 0.05;
use File::Slurp;
+use Time::HiRes qw/time/;
+use Encode qw/encode decode from_to/;
+use Template;
+use Data::Dumper;
=head1 NAME
@@ -33,9 +35,13 @@
# configuration for hyper estraier full text search engine
hyperestraier:
- url: 'http://localhost:1978/node/webpac2'
+ masterurl: 'http://localhost:1978/node/webpac2'
+ defaultnode: 'webpac2'
+ defaultdepth: 1
user: 'admin'
passwd: 'admin'
+ hits_on_page: 100
+ hits_for_pager: 1000
webpac:
db_path: '/data/webpac2/db'
@@ -43,8 +49,6 @@
template: 'html_ffzg_results_short.tt'
# encoding comming from webpac
webpac_encoding: 'iso-8859-2'
- # encoding expected by Catalyst
- out_encoding: 'UTF-8'
=cut
@@ -60,109 +64,182 @@
my $est_cfg = $c->config->{hyperestraier};
$est_cfg->{'log'} = $log;
- $est_cfg->{encoding} = $est_cfg->{catalyst_encoding};
+ $est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding");
$log->debug("using config:" . Dumper($est_cfg) );
+ if (! $est_cfg->{database}) {
+ my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration");
+ $log->info("using default node $defaultnode");
+ $est_cfg->{database} = $defaultnode;
+ }
+
$self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
- my $db_path = $c->config->{webpac}->{db_path};
- my $template_path = $c->config->{webpac}->{template_path};
- $self->{template_path} = $template_path;
+ # save config parametars in object
+ foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) {
+ $self->{$f} = $c->config->{hyperestraier}->{$f} ||
+ $c->config->{webpac}->{$f};
+ $log->debug("self->{$f} = " . $self->{$f});
+ }
+ my $db_path = $self->{db_path};
+ my $template_path = $self->{template_path};
$log->debug("using db path '$db_path', template path '$template_path'");
- $self->{db} = new WebPAC::DB(
+ $self->{db} = new WebPAC::Store(
path => $db_path,
read_only => 1,
- );
-
- $self->{out} = new WebPAC::Output::TT(
- include_path => $template_path,
- filters => { foo => sub { shift } },
+ database => $est_cfg->{database},
);
# default template from config.yaml
$self->{template} ||= $c->config->{webpac}->{template};
- $self->{iconv} = new Text::Iconv(
- $c->config->{webpac}->{webpac_encoding},
- $c->config->{webpac}->{out_encoding}
- );
-
$log->debug("converting encoding from webpac_encoding '" .
$c->config->{webpac}->{webpac_encoding} .
- "' to '" .
- $c->config->{webpac}->{out_encoding} .
"'"
);
+ $self->{databases} = $c->config->{databases} || $log->error("can't find databases in config");
+
+ # create Template toolkit instance
+ $self->{'tt'} = Template->new(
+ INCLUDE_PATH => $template_path,
+ FILTERS => {
+ dump_html => sub {
+ return unless (@_);
+ my $out;
+ my $i = 1;
+ foreach my $v (@_) {
+ $out .= qq{
} .
+ Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) .
+ qq{
};
+ $i++;
+ }
+ $out =~ s!/]*>!!gis if ($out);
+ return $out;
+ }
+ },
+ EVAL_PERL => 1,
+ );
+
return $self;
}
-=head2 iconv_on_save
- my $out = $m->iconv_on_save( $content );
+=head2 search
-Convert data saved to disk in Webpac encoding.
+ my $m->search(
+ phrase => 'query phrase',
+ add_attr => \@add_attr
+ get_attr => [ '@uri' ],
+ max => 42,
+ template => 'result_template.tt',
+ depth => 1,
+ );
+
+All fields are standard C parametars except
+C which will (if specified) return results in HTML using
+selected template.
=cut
-sub iconv_on_save {
+sub search {
my $self = shift;
- $self->{iconv_save} ||= new Text::Iconv(
- $self->config->{webpac}->{out_encoding},
- $self->config->{webpac}->{webpac_encoding},
- );
+ my $search_start_t = time();
- $self->{iconv_save}->convert( @_ );
-}
+ my $args = {@_};
+ my $log = $self->{log};
-=head2 search
+ $log->debug("search args: " . Dumper( $args ));
- my $m->search( 'query phrase', 'result_template.tt', \@add_attr );
+ my $query = $args->{phrase} || $log->warn("no query phrase") && return;
-=cut
+ $log->debug("search model query: '$query'");
+ if ($args->{add_attr}) {
+ $log->debug(" + add_attr: " .
+ join("','", @{ $args->{add_attr} })
+ );
+ }
-sub search {
- my ( $self, $query, $template, $add_attr ) = @_;
+ my $template_filename = $args->{template} || $self->{template};
- my $log = $self->{log};
+ $args->{max} ||= $self->{'hits_for_pager'};
+ if (! $args->{max}) {
+ $args->{max} = 100;
+ $log->warn("max not set when calling model. Using default of $args->{max}");
+ }
- $log->debug("search model query: '$query', add_attr: '" . join("','", @{$add_attr}) . "'");
+ my $times; # store some times for benchmarking
- my $template_filename = $template || $self->{template};
+ my $t = time();
- my @results = $self->{est}->search(
- phrase => $query,
- get_attr => [ '@uri' ],
- max => 100,
- add_attr => $add_attr,
- );
+ # transfer depth of search
+ if (! $args->{depth}) {
+ my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration");
+ $args->{depth} = $default;
+ $log->warn("using default search depth $default");
+ }
+
+ my @results = $self->{est}->search( %{ $args } );
+
+ $times->{est} += time() - $t;
+
+ my $hits = $#results + 1;
+
+ $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );
- $log->debug("loading " . ($#results + 1) . " results");
+ # just return results?
+ return @results unless ($args->{'template'});
+
+ #
+ # construct HTML results
+ #
my @html_results;
for my $i ( 0 .. $#results ) {
- my $mfn = $1 if ( $results[$i]->{'@uri'} =~ m#/(\d+)$#);
+ my ($database, $prefix, $id);
+ if ( $results[$i]->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) {
+ ($database, $prefix,$id) = ($1,$2,$3);
+ } else {
+ $log->warn("can't decode database/prefix/id from " . $results[$i]->{'@uri'});
+ next;
+ }
+
+ #$log->debug("load_ds( id => $id, prefix => '$prefix' )");
+
+ $t = time();
+
+ my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id );
+ if (! $ds) {
+ $log->error("can't load_ds( ${database}/${prefix}/${id} )");
+ next;
+ }
- #$log->debug("load_ds( $mfn )");
+ $times->{db} += time() - $t;
- my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next;
-
#$log->debug( "ds = " . Dumper( \@html_results ) );
- my $html = $self->{out}->apply(
+ $t = time();
+
+ my $html = $self->apply(
template => $template_filename,
data => $ds,
+ record_uri => "${database}/${prefix}/${id}",
+ config => $self->{databases}->{$database},
);
- $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
+ $times->{out} += time() - $t;
+
+ $t = time();
+
+ $html = decode($self->{webpac_encoding}, $html);
push @html_results, $html;
@@ -170,14 +247,70 @@
#$log->debug( '@html_results = ' . Dumper( \@html_results ) );
+ $log->debug( sprintf(
+ "duration breakdown: store %.6fs, apply %.6fs, total: %.6fs",
+ $times->{db}, $times->{out}, time() - $search_start_t,
+ ) );
+
return \@html_results;
}
+=head2 record
+
+ my $html = $m->record(
+ mfn => 42,
+ template => 'foo.tt',
+ );
+
+This will load one record, convert it to html using C and return
+it.
+
+=cut
+
+sub record {
+ my $self = shift;
+
+ my $args = {@_};
+ my $log = $self->{log};
+ $log->debug("record args: " . Dumper( $args ));
+
+ foreach my $f (qw/record_uri template/) {
+ $log->fatal("need $f") unless ($args->{$f});
+ }
+
+ my ($database, $prefix, $id);
+
+ if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) {
+ ($database, $prefix, $id) = ($1,$2,$3);
+ } else {
+ $log->error("can't parse $args->{record_uri} into prefix, database and uri");
+ return;
+ }
+
+ my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database );
+ if (! $ds) {
+ $log->error("can't load_ds( $database/$prefix/$id )");
+ return;
+ }
+
+ my $html = $self->apply(
+ template => $args->{template},
+ data => $ds,
+ record_uri => $args->{record_uri},
+ config => $self->{databases}->{$database},
+ );
+
+ $html = decode($self->{webpac_encoding}, $html);
+
+ return $html;
+}
+
+
=head2 save_html
$m->save_html( '/full/path/to/file', $content );
-It will use C to convert content encoding back to
+It will use C to convert content encoding back to
Webpac codepage, recode JavaScript Unicode entities (%u1234),
strip extra newlines at beginning and end, and save to
C and if that succeeds, just rename
@@ -188,17 +321,24 @@
sub save_html {
my ($self, $path, $content) = @_;
- $content = $self->iconv_on_save( $content ) || die "no content?";
+ # FIXME Should this be UTF-8 or someting?
+ my $js_encoding = $self->{webpac_encoding};
+ $js_encoding = 'UTF-16';
sub _conv_js {
- my $t = shift || return;
- return $self->{iconv}->convert(chr(hex($t)));
+ return '0x' . $_[1];
+ return encode($_[0], chr(hex($_[1])));
}
- $content =~ s/%u([a-fA-F0-9]{4})/_conv_js($1)/gex;
+ #$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($js_encoding,$1)/gex;
$content =~ s/^[\n\r]+//s;
$content =~ s/[\n\r]+$/\n/s;
+ $content =~ s/\n\r/\n/gs;
- write_file($path . '.new', $content) || die "can't save ${path}.new $!";
+ my $disk_encoding = $self->{webpac_encoding} || 'utf-8';
+ $self->{log}->debug("convert encoding to $disk_encoding");
+ from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content");
+
+ write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!";
rename $path . '.new', $path || die "can't rename to $path: $!";
}
@@ -217,16 +357,257 @@
die "no path?" unless ($path);
- my $content = read_file($path) || die "can't read $path: $!";
- #$content = $q->escapeHTML($iconv_utf8->convert($content));
- $content = $self->{iconv}->convert($content);
+ my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!";
+
+ return decode($self->{webpac_encoding}, $content);
+}
+
+
+=head2 apply
+
+Create output from in-memory data structure using Template Toolkit template.
+
+ my $text = $tt->apply(
+ template => 'text.tt',
+ data => $ds,
+ record_uri => 'database/prefix/mfn',
+ );
+
+It also has follwing template toolikit filter routies defined:
+
+=cut
+
+sub apply {
+ my $self = shift;
+
+ my $args = {@_};
+
+ my $log = $self->{log} || die "no log?";
+
+ foreach my $a (qw/template data/) {
+ $log->logconfess("need $a") unless ($args->{$a});
+ }
+
+=head3 tt_filter_type
+
+filter to return values of specified from $ds, usage from TT template is in form
+C, where C is optional, like this:
+
+ [% d('Title') %]
+ [% d('Author',', ' %]
+
+=cut
+
+ sub tt_filter_type {
+ my ($data,$type) = @_;
+
+ die "no data?" unless ($data);
+ $type ||= 'display';
+
+ my $default_delimiter = {
+ 'display' => '¶
',
+ 'index' => '\n',
+ };
+
+ return sub {
+
+ my ($name,$join) = @_;
+
+ die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
+ # Hm? Should we die here?
+ return unless ($name);
+
+ my $item = $data->{'data'}->{$name} || return;
+
+ my $v = $item->{$type} || return;
+
+ if (ref($v) eq 'ARRAY') {
+ if ($#{$v} == 0) {
+ $v = $v->[0];
+ } else {
+ $join = $default_delimiter->{$type} unless defined($join);
+ $v = join($join, @{$v});
+ }
+ } else {
+ warn("TT filter $type(): field $name values aren't ARRAY, ignoring");
+ }
+
+ return $v;
+ }
+ }
+
+ $args->{'d'} = tt_filter_type($args, 'display');
+ $args->{'display'} = tt_filter_type($args, 'display');
+
+=head3 tt_filter_search
+
+filter to return links to search, usage in TT:
+
+ [% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %]
+
+=cut
+
+ sub tt_filter_search {
+
+ my ($data) = @_;
+
+ die "no data?" unless ($data);
+
+ return sub {
+
+ my ($display,$search,$delimiter,$template) = @_;
+
+ # default delimiter
+ $delimiter ||= '¶
',
+
+ die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
+ # Hm? Should we die here?
+ return unless ($display);
+
+ my $item = $data->{'data'}->{$display} || return;
+
+ return unless($item->{'display'});
+ if (! $item->{'search'}) {
+ warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)";
+ return;
+ }
+
+ my @warn;
+ foreach my $type (qw/display search/) {
+ push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY');
+ }
+
+ if (@warn) {
+ warn("TT filter search(): " . join(",", @warn) . ", skipping");
+ return;
+ }
+ my @html;
+
+ my $d_el = $#{ $item->{'display'} };
+ my $s_el = $#{ $item->{'search'} };
+
+ # easy, both fields have same number of elements or there is just
+ # one search and multiple display
+ if ( $d_el == $s_el || $s_el == 0 ) {
+
+ foreach my $i ( 0 .. $d_el ) {
+
+ my $s;
+ if ($s_el > 0) {
+ $s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search";
+ } else {
+ $s = $item->{'search'}->[0];
+ }
+ #$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;
+ $s = __quotemeta( $s );
+
+ my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display";
+
+ my $template_arg = '';
+ $template_arg = qq{,'$template'} if ($template);
+
+ push @html, qq{$d};
+ }
+
+ return join($delimiter, @html);
+ } else {
+ my $html = qq{WARNING: we should really support if there is $d_el display elements and $s_el search elements, but currently there is no nice way to do so, so we will just display values
};
+ my $v = $item->{'display'};
+
+ if ($#{$v} == 0) {
+ $html .= $v->[0];
+ } else {
+ $html .= join($delimiter, @{$v});
+ }
+ return $html;
+ }
+ }
+ }
+
+ $args->{'search'} = tt_filter_search($args);
+
+=head3 load_rec
+
+Used mostly for onClick events like this:
+
+ bar
- return $content;
+=cut
+
+ $args->{'load_template'} = sub {
+ my $template = shift or return "Logger.error('load_template missing template name!'); return false;";
+ return "load_template($template); return false;";
+ };
+
+ my $out;
+
+ $self->{'tt'}->process(
+ $args->{'template'},
+ $args,
+ \$out
+ ) || $log->error( "apply can't process template: ", $self->{'tt'}->error() );
+
+ return $out;
}
+
+=head2 __quotemeta
+
+Helper to quote JavaScript-friendly characters
+
+=cut
+
+sub __quotemeta {
+ local $_ = shift;
+ $_ = decode('iso-8859-2', $_);
+
+ s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) );
+ {
+ use bytes;
+ s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge;
+ }
+
+ s/\\x09/\\t/g;
+ s/\\x0A/\\n/g;
+ s/\\x0D/\\r/g;
+ s/"/\\"/g;
+ s/\\x5C/\\\\/g;
+
+ return $_;
+}
+
+
+
=head1 AUTHOR
-Dobrica Pavlinusic
+Dobrica Pavlinusic C<< >>
=head1 LICENSE