--- Webpacus/lib/Webpacus/Model/WebPAC.pm 2005/11/23 21:52:35 119 +++ Webpacus/lib/Webpacus/Model/WebPAC.pm 2006/02/19 20:16:11 404 @@ -6,10 +6,12 @@ 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 Search::Estraier 0.04; +use File::Slurp; +use Time::HiRes qw/time/; +use Encode qw/encode decode from_to/; +use Template; =head1 NAME @@ -32,9 +34,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' @@ -42,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 @@ -59,96 +63,688 @@ my $est_cfg = $c->config->{hyperestraier}; $est_cfg->{'log'} = $log; - $log->debug("using config:" . Dumper($est_cfg) ); + $est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); - $self->{est} = new WebPAC::Search::Estraier( %{ $est_cfg } ); + $log->dumper($est_cfg, 'est_cfg'); - my $db_path = $c->config->{webpac}->{db_path}; - my $template_path = $c->config->{webpac}->{template_path}; + 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; + } + + 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 defaultdepth + masterurl defaultnode + /) { + $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->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 + + 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