11 |
use WebPAC::Output::TT; |
use WebPAC::Output::TT; |
12 |
use WebPAC::Search::Estraier 0.02; |
use WebPAC::Search::Estraier 0.02; |
13 |
use File::Slurp; |
use File::Slurp; |
14 |
|
use Time::HiRes; |
15 |
|
|
16 |
=head1 NAME |
=head1 NAME |
17 |
|
|
99 |
"'" |
"'" |
100 |
); |
); |
101 |
|
|
102 |
|
# save config parametars in object |
103 |
|
foreach my $f (qw/hits_on_page/) { |
104 |
|
$self->{$f} = $c->config->{hyperestraier}->{$f}; |
105 |
|
$log->debug("self->{$f} = " . $self->{$f}); |
106 |
|
} |
107 |
|
|
108 |
return $self; |
return $self; |
109 |
|
|
110 |
} |
} |
133 |
|
|
134 |
my $m->search( |
my $m->search( |
135 |
phrase => 'query phrase', |
phrase => 'query phrase', |
|
template => 'result_template.tt', |
|
136 |
add_attr => \@add_attr |
add_attr => \@add_attr |
137 |
|
get_attr => [ '@uri' ], |
138 |
|
max => 42, |
139 |
|
template => 'result_template.tt', |
140 |
); |
); |
141 |
|
|
142 |
|
All fields are standard C<WebPAC::Search::Estraier> parametars except |
143 |
|
C<template> which will (if specified) return results in HTML using |
144 |
|
selected template. |
145 |
|
|
146 |
=cut |
=cut |
147 |
|
|
148 |
sub search { |
sub search { |
156 |
|
|
157 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
158 |
|
|
159 |
$log->debug("search model query: '$query', add_attr: '" . join("','", @{$args->{add_attr}}) . "'"); |
$log->debug("search model query: '$query'"); |
160 |
|
if ($args->{add_attr}) { |
161 |
|
$log->debug(" + add_attr: " . |
162 |
|
join("','", @{ $args->{add_attr} }) |
163 |
|
); |
164 |
|
} |
165 |
|
|
166 |
my $template_filename = $args->{template} || $self->{template}; |
my $template_filename = $args->{template} || $self->{template}; |
167 |
|
|
168 |
my @results = $self->{est}->search( |
$args->{max} ||= $self->{'hits_on_page'}; |
169 |
phrase => $query, |
if (! $args->{max}) { |
170 |
get_attr => [ '@uri' ], |
$args->{max} = 10; |
171 |
max => ( $self->{est}->{hits_on_page} || 30 ), |
$log->warn("max not set when calling model. Using default of 10"); |
172 |
add_attr => $args->{add_attr}, |
} |
173 |
); |
|
174 |
|
my $times; # store some times for benchmarking |
175 |
|
|
176 |
|
my $t = time(); |
177 |
|
|
178 |
|
my @results = $self->{est}->search( %{ $args } ); |
179 |
|
|
180 |
|
$times->{est} += time() - $t; |
181 |
|
|
182 |
my $hits = $#results + 1; |
my $hits = $#results + 1; |
183 |
|
|
184 |
$log->debug("processing $hits results"); |
$log->debug( sprintf("search took %.2fs and returned $hits hits.", $times->{est}) ); |
185 |
|
|
186 |
|
# just return results? |
187 |
|
return @results unless ($args->{'template'}); |
188 |
|
|
189 |
|
# |
190 |
|
# construct HTML results |
191 |
|
# |
192 |
|
|
193 |
my @html_results; |
my @html_results; |
194 |
|
|
198 |
|
|
199 |
#$log->debug("load_ds( $mfn )"); |
#$log->debug("load_ds( $mfn )"); |
200 |
|
|
201 |
|
$t = time(); |
202 |
|
|
203 |
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
204 |
|
|
205 |
|
$times->{db} += time() - $t; |
206 |
|
|
207 |
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
208 |
|
|
209 |
|
$t = time(); |
210 |
|
|
211 |
my $html = $self->{out}->apply( |
my $html = $self->{out}->apply( |
212 |
template => $template_filename, |
template => $template_filename, |
213 |
data => $ds, |
data => $ds, |
214 |
); |
); |
215 |
|
|
216 |
|
$times->{out} += time() - $t; |
217 |
|
|
218 |
|
$t = time(); |
219 |
|
|
220 |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
221 |
|
|
222 |
|
$times->{iconv} += time() - $t; |
223 |
|
|
224 |
push @html_results, $html; |
push @html_results, $html; |
225 |
|
|
226 |
} |
} |
227 |
|
|
228 |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
#$log->debug( '@html_results = ' . Dumper( \@html_results ) ); |
229 |
|
|
230 |
|
$log->debug( sprintf( |
231 |
|
"time spent: db = %.2f, out = %.2f, iconv = %.2f", |
232 |
|
$times->{db}, $times->{out}, $times->{iconv}, |
233 |
|
) ); |
234 |
|
|
235 |
return \@html_results; |
return \@html_results; |
236 |
} |
} |
237 |
|
|
238 |
|
=head2 record |
239 |
|
|
240 |
|
my $html = $m->record( |
241 |
|
mfn => 42, |
242 |
|
template => 'foo.tt', |
243 |
|
); |
244 |
|
|
245 |
|
This will load one record, convert it to html using C<template> and return |
246 |
|
it. |
247 |
|
|
248 |
|
=cut |
249 |
|
|
250 |
|
sub record { |
251 |
|
my $self = shift; |
252 |
|
|
253 |
|
my $args = {@_}; |
254 |
|
my $log = $self->{log}; |
255 |
|
$log->debug("args: " . Dumper( $args )); |
256 |
|
|
257 |
|
foreach my $f (qw/mfn template/) { |
258 |
|
$log->die("need $f") unless ($args->{$f}); |
259 |
|
} |
260 |
|
|
261 |
|
my $mfn = $args->{mfn}; |
262 |
|
|
263 |
|
my $ds = $self->{db}->load_ds( $mfn ) || $log->error("can't load_ds( $mfn )") && next; |
264 |
|
|
265 |
|
my $html = $self->{out}->apply( |
266 |
|
template => $args->{template}, |
267 |
|
data => $ds, |
268 |
|
); |
269 |
|
|
270 |
|
$html = $self->{iconv}->convert( $html ) || $log->error("can't convert: $html"); |
271 |
|
|
272 |
|
return $html; |
273 |
|
} |
274 |
|
|
275 |
=head2 save_html |
=head2 save_html |
276 |
|
|
277 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |