--- Webpacus/lib/Webpacus/Model/WebPAC.pm 2005/12/05 19:15:11 224
+++ Webpacus/lib/Webpacus/Model/WebPAC.pm 2006/03/12 21:42:34 419
@@ -6,12 +6,12 @@
use base qw/
Catalyst::Model
/;
-use Data::Dumper;
-use WebPAC::Store 0.03;
-use WebPAC::Output::TT 0.02;
-use WebPAC::Search::Estraier 0.05;
+use WebPAC::Store 0.08;
+use Search::Estraier 0.04;
use File::Slurp;
-use Time::HiRes;
+use Time::HiRes qw/time/;
+use Encode qw/encode decode from_to/;
+use Template;
=head1 NAME
@@ -40,6 +40,7 @@
user: 'admin'
passwd: 'admin'
hits_on_page: 100
+ hits_for_pager: 1000
webpac:
db_path: '/data/webpac2/db'
@@ -47,8 +48,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
@@ -64,9 +63,9 @@
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) );
+ $log->dumper($est_cfg, 'est_cfg');
if (! $est_cfg->{database}) {
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration");
@@ -74,10 +73,23 @@
$est_cfg->{database} = $defaultnode;
}
- $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } );
+ my $url = $est_cfg->{masterurl} . '/node/' . $est_cfg->{database};
+
+ $log->info("opening Hyper Estraier index $url as $est_cfg->{'user'}");
+
+ $self->{est_node} = Search::Estraier::Node->new(
+ url => $url,
+ user => $est_cfg->{user},
+ passwd => $est_cfg->{passwd},
+ );
+
+ $log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node});
# save config parametars in object
- foreach my $f (qw/db_path template_path hits_on_page webpac_encoding out_encoding defaultdepth/) {
+ foreach my $f (qw/
+ db_path template_path hits_on_page webpac_encoding defaultdepth
+ masterurl defaultnode
+ /) {
$self->{$f} = $c->config->{hyperestraier}->{$f} ||
$c->config->{webpac}->{$f};
$log->debug("self->{$f} = " . $self->{$f});
@@ -93,31 +105,61 @@
database => $est_cfg->{database},
);
- $self->{out} = new WebPAC::Output::TT(
- include_path => $template_path,
- filters => { foo => sub { shift } },
- );
-
# 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->fatal("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 setup_site
+
+ $self->setup_site('site_name');
+
+Change node URL and database name according to site name (if available) or fallback
+to C from configuration.
+
+=cut
+
+sub setup_site {
+ my $self = shift;
+
+ my $site = shift || $self->{defaultnode};
+
+ $self->{log}->fatal("setup_site can't find site or defaultnode") unless ($site);
+
+ my $url = $self->{masterurl} . '/node/' . $site;
+ $self->{est_node}->set_url( $url );
+ $self->{log}->debug("setup_site $site using $url");
+}
=head2 search
@@ -139,27 +181,22 @@
sub search {
my $self = shift;
+ my $search_start_t = time();
+
my $args = {@_};
my $log = $self->{log};
- $log->debug("args: " . Dumper( $args ));
+ $log->dumper($args, 'args');
my $query = $args->{phrase} || $log->warn("no query phrase") && return;
- $log->debug("search model query: '$query'");
- if ($args->{add_attr}) {
- $log->debug(" + add_attr: " .
- join("','", @{ $args->{add_attr} })
- );
- }
-
my $template_filename = $args->{template} || $self->{template};
- $args->{max} ||= $self->{'hits_on_page'};
+ $args->{max} ||= $self->{'hits_for_pager'};
if (! $args->{max}) {
- $args->{max} = 10;
- $log->warn("max not set when calling model. Using default of 10");
+ $args->{max} = 100;
+ $log->warn("max not set when calling model. Using default of $args->{max}");
}
my $times; # store some times for benchmarking
@@ -172,73 +209,171 @@
$args->{depth} = $default;
$log->warn("using default search depth $default");
}
+ $args->{depth} ||= 0;
- my @results = $self->{est}->search( %{ $args } );
+ $log->debug("searching for maximum $args->{max} results using depth $args->{depth} phrase: ", $query || '[none]');
- $times->{est} += time() - $t;
+ #
+ # construct condition for Hyper Estraier
+ #
+ my $cond = Search::Estraier::Condition->new();
+ if ( ref($args->{add_attr}) eq 'ARRAY' ) {
+ $log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) );
+ map {
+ $cond->add_attr( $_ );
+ $log->debug(" + $_");
+ } @{ $args->{add_attr} };
+ };
+
+ $cond->set_phrase( $query ) if ($query);
+ $cond->set_options( $args->{options} ) if ($args->{options});
+ $cond->set_order( $args->{order} ) if ($args->{order});
+
+ my $max = $args->{max} || 7;
+ my $page = $args->{page} || 1;
+ if ($page < 1) {
+ $log->warn("page number $page < 1");
+ $page = 1;
+ }
- my $hits = $#results + 1;
+ $cond->set_max( $page * $max );
- $log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) );
+ my $result = $self->{est_node}->search($cond, $args->{depth});
+ if (! $result) {
+ $self->{log}->fatal("search didn't return result");
+ return;
+ }
+ my $hits = $result->doc_num;
- # just return results?
- return @results unless ($args->{'template'});
+ $times->{est} += time() - $t;
+
+ $log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) );
+
+ $self->{hints} = $result->{hints};
+ $log->dumper($self->{hints}, 'original hints' );
#
- # construct HTML results
+ # fetch results
#
- my @html_results;
+ my @results;
- for my $i ( 0 .. $#results ) {
+ for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) {
- my ($id, $prefix);
- if ( $results[$i]->{'@uri'} =~ m!/([^/]+)#(\d+)$!) {
- ($prefix,$id) = ($1,$2);
- } else {
- $log->warn("can't decode prefix#id from " . $results[$i]->{'@uri'});
+ $t = time();
+
+ #$log->debug("get_doc($i)");
+ my $doc = $result->get_doc( $i );
+ if (! $doc) {
+ $log->warn("can't find result $i");
next;
}
- #$log->debug("load_ds( id => $id, prefix => '$prefix' )");
+ my $hash;
- $t = time();
+ foreach my $attr (@{ $args->{get_attr} }) {
+ my $val = $doc->attr( $attr );
+ #$log->debug("attr $attr = ", $val || 'undef');
+ $hash->{$attr} = $val if (defined($val));
+ }
- my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix ) || $log->error("can't load_ds( id => $id, prefix => '$prefix' )") && next;
+ $times->{hash} += time() - $t;
- $times->{db} += time() - $t;
+ next unless ($hash);
- #$log->debug( "ds = " . Dumper( \@html_results ) );
+ if (! $args->{'template'}) {
+ push @results, $hash;
+ } else {
+ my ($database, $prefix, $id);
- $t = time();
+ if ( $hash->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) {
+ ($database, $prefix,$id) = ($1,$2,$3);
+ } else {
+ $log->warn("can't decode database/prefix/id from " . $hash->{'@uri'});
+ next;
+ }
- my $html = $self->{out}->apply(
- template => $template_filename,
- data => $ds,
- );
+ #$log->debug("load_ds( id => $id, prefix => '$prefix' )");
- $times->{out} += time() - $t;
+ $t = time();
- $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;
+ }
- $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
+ $times->{db} += time() - $t;
- $times->{iconv} += time() - $t;
+ $t = time();
- push @html_results, $html;
+ my $html = $self->apply(
+ template => $template_filename,
+ data => $ds,
+ record_uri => "${database}/${prefix}/${id}",
+ config => $self->{databases}->{$database},
+ );
- }
+ $times->{apply} += time() - $t;
+
+ $t = time();
- #$log->debug( '@html_results = ' . Dumper( \@html_results ) );
+ $html = decode($self->{webpac_encoding}, $html);
+
+ $times->{decode} += time() - $t;
+
+ push @results, $html;
+ }
+
+ }
$log->debug( sprintf(
- "time spent: db = %.2f, out = %.2f, iconv = %.2f",
- $times->{db}, $times->{out}, $times->{iconv},
+ "duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs",
+ $times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t,
) );
- return \@html_results;
+ return \@results;
}
+=head2 hints
+
+ my $hints = $m->hints;
+
+Return various useful hints about result
+
+=cut
+
+sub hints {
+ my $self = shift;
+
+ unless ($self->{hints}) {
+ $self->{log}->fatal("no hints found!");
+ return;
+ }
+
+ my $hints;
+
+ while (my ($key,$val) = each %{ $self->{hints} }) {
+
+ if ($key =~ m/^(?:HITS*|TIME|DOCNUM|WORDNUM)$/) {
+ $hints->{ lc($key) } = $val;
+ } elsif ($key =~ m/^HINT#/) {
+ my ($word,$count) = split(/\t/,$val,2);
+ $hints->{words}->{$word} = $count;
+ } elsif ($key =~ m/^LINK#/) {
+ my ($url,undef,undef,undef,undef,undef,$results) = split(/\t/,$val,7);
+ if ($url =~ m#/node/(.+)$#) {
+ $hints->{node}->{$1} = $results;
+ }
+ }
+ }
+
+ $self->{log}->dumper($hints, 'model hints' );
+
+ return $hints;
+}
+
+
=head2 record
my $html = $m->record(
@@ -256,31 +391,108 @@
my $args = {@_};
my $log = $self->{log};
- $log->debug("args: " . Dumper( $args ));
+ $log->dumper( $args, 'args' );
- foreach my $f (qw/mfn template prefix/) {
- $log->die("need $f") unless ($args->{$f});
+ foreach my $f (qw/record_uri template/) {
+ $log->fatal("need $f") unless ($args->{$f});
}
- my $mfn = $args->{mfn};
+ my ($database, $prefix, $id);
- my $ds = $self->{db}->load_ds( id => $mfn, prefix => 'FIXME' ) || $log->error("can't load_ds( $mfn )") && next;
+ 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->{out}->apply(
+ my $html = $self->apply(
template => $args->{template},
data => $ds,
+ record_uri => $args->{record_uri},
+ config => $self->{databases}->{$database},
);
- $html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html");
+ $html = decode($self->{webpac_encoding}, $html);
return $html;
}
+
+=head2 list_nodes
+
+ my @nodes = $m->list_nodes( 'site' );
+
+Return all databases which have records for selected site. Returned array of
+hashes has elements C and C