12 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
13 |
use Encode qw/encode decode from_to/; |
use Encode qw/encode decode from_to/; |
14 |
use Template; |
use Template; |
|
use Data::Dumper; |
|
15 |
|
|
16 |
=head1 NAME |
=head1 NAME |
17 |
|
|
65 |
|
|
66 |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); |
67 |
|
|
68 |
$log->debug("using config:" . Dumper($est_cfg) ); |
$log->dumper($est_cfg, 'est_cfg'); |
69 |
|
|
70 |
if (! $est_cfg->{database}) { |
if (! $est_cfg->{database}) { |
71 |
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration"); |
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration"); |
86 |
$log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node}); |
$log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node}); |
87 |
|
|
88 |
# save config parametars in object |
# save config parametars in object |
89 |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) { |
foreach my $f (qw/ |
90 |
|
db_path template_path hits_on_page webpac_encoding defaultdepth |
91 |
|
masterurl defaultnode |
92 |
|
/) { |
93 |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
94 |
$c->config->{webpac}->{$f}; |
$c->config->{webpac}->{$f}; |
95 |
$log->debug("self->{$f} = " . $self->{$f}); |
$log->debug("self->{$f} = " . $self->{$f}); |
140 |
|
|
141 |
} |
} |
142 |
|
|
143 |
|
=head2 setup_site |
144 |
|
|
145 |
|
$self->setup_site('site_name'); |
146 |
|
|
147 |
|
Change node URL and database name according to site name (if available) or fallback |
148 |
|
to C<defaultnode> from configuration. |
149 |
|
|
150 |
|
=cut |
151 |
|
|
152 |
|
sub setup_site { |
153 |
|
my $self = shift; |
154 |
|
|
155 |
|
my $site = shift || $self->{defaultnode}; |
156 |
|
|
157 |
|
$self->{log}->fatal("setup_site can't find site or defaultnode") unless ($site); |
158 |
|
|
159 |
|
my $url = $self->{masterurl} . '/node/' . $site; |
160 |
|
$self->{est_node}->set_url( $url ); |
161 |
|
$self->{log}->debug("setup_site $site using $url"); |
162 |
|
} |
163 |
|
|
164 |
=head2 search |
=head2 search |
165 |
|
|
187 |
|
|
188 |
my $log = $self->{log}; |
my $log = $self->{log}; |
189 |
|
|
190 |
$log->debug("search args: " . Dumper( $args )); |
$log->dumper($args, 'args'); |
191 |
|
|
192 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
193 |
|
|
|
$log->debug("search model query: '$query'"); |
|
|
if ($args->{add_attr}) { |
|
|
$log->debug(" + add_attr: " . |
|
|
join("','", @{ $args->{add_attr} }) |
|
|
); |
|
|
} |
|
|
|
|
194 |
my $template_filename = $args->{template} || $self->{template}; |
my $template_filename = $args->{template} || $self->{template}; |
195 |
|
|
196 |
$args->{max} ||= $self->{'hits_for_pager'}; |
$args->{max} ||= $self->{'hits_for_pager'}; |
209 |
$args->{depth} = $default; |
$args->{depth} = $default; |
210 |
$log->warn("using default search depth $default"); |
$log->warn("using default search depth $default"); |
211 |
} |
} |
212 |
|
$args->{depth} ||= 0; |
213 |
|
|
214 |
$log->debug("searching for maximum $args->{max} results using depth $args->{depth}"); |
$log->debug("searching for maximum $args->{max} results using depth $args->{depth} phrase: ", $query || '[none]'); |
215 |
|
|
216 |
# |
# |
217 |
# construct condition for Hyper Estraier |
# construct condition for Hyper Estraier |
220 |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
221 |
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
222 |
map { |
map { |
223 |
$cond->add_attr( _convert( $_ ) ); |
$cond->add_attr( $_ ); |
224 |
$log->debug(" + $_"); |
$log->debug(" + $_"); |
225 |
} @{ $args->{add_attr} }; |
} @{ $args->{add_attr} }; |
226 |
}; |
}; |
236 |
$page = 1; |
$page = 1; |
237 |
} |
} |
238 |
|
|
|
$times->{est} += time() - $t; |
|
|
|
|
239 |
$cond->set_max( $page * $max ); |
$cond->set_max( $page * $max ); |
240 |
|
|
241 |
my $result = $self->{est_node}->search($cond, ( $args->{depth} || 0 )); |
my $result = $self->{est_node}->search($cond, $args->{depth}); |
242 |
my $hits = $result->doc_num; |
my $hits = $result->doc_num; |
243 |
|
|
244 |
|
$times->{est} += time() - $t; |
245 |
|
|
246 |
$log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); |
$log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); |
247 |
|
|
248 |
|
$self->{hints} = $result->{hints}; |
249 |
|
$log->dumper($self->{hints}, 'hints' ); |
250 |
|
|
251 |
# |
# |
252 |
# fetch results |
# fetch results |
253 |
# |
# |
301 |
|
|
302 |
$times->{db} += time() - $t; |
$times->{db} += time() - $t; |
303 |
|
|
|
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
|
|
|
|
304 |
$t = time(); |
$t = time(); |
305 |
|
|
306 |
my $html = $self->apply( |
my $html = $self->apply( |
323 |
|
|
324 |
} |
} |
325 |
|
|
|
$log->debug( '@results = ' . Dumper( \@results ) ); |
|
|
|
|
326 |
$log->debug( sprintf( |
$log->debug( sprintf( |
327 |
"duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs", |
"duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs", |
328 |
$times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t, |
$times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t, |
331 |
return \@results; |
return \@results; |
332 |
} |
} |
333 |
|
|
334 |
|
=head2 hints |
335 |
|
|
336 |
|
my $hints = $m->hints; |
337 |
|
|
338 |
|
Return various useful hints about result |
339 |
|
|
340 |
|
=cut |
341 |
|
|
342 |
|
sub hints { |
343 |
|
my $self = shift; |
344 |
|
|
345 |
|
unless ($self->{hints}) { |
346 |
|
$self->{log}->fatal("no hints found!"); |
347 |
|
return; |
348 |
|
} |
349 |
|
|
350 |
|
my $hints; |
351 |
|
|
352 |
|
while (my ($key,$val) = each %{ $self->{hints} }) { |
353 |
|
|
354 |
|
if ($key =~ m/^(?:HITS*|TIME|DOCNUM|WORDNUM)$/) { |
355 |
|
$hints->{ lc($key) } = $val; |
356 |
|
} elsif ($key =~ m/^HINT#/) { |
357 |
|
my ($word,$count) = split(/\t/,$val,2); |
358 |
|
$hints->{words}->{$word} = $count; |
359 |
|
} elsif ($key =~ m/^LINK#/) { |
360 |
|
my ($url,undef,undef,undef,undef,undef,$results) = split(/\t/,$val,7); |
361 |
|
if ($url =~ m#/node/(.+)$#) { |
362 |
|
$hints->{node}->{$1} = $results; |
363 |
|
} |
364 |
|
} |
365 |
|
} |
366 |
|
|
367 |
|
return $hints; |
368 |
|
} |
369 |
|
|
370 |
|
|
371 |
=head2 record |
=head2 record |
372 |
|
|
373 |
my $html = $m->record( |
my $html = $m->record( |
385 |
|
|
386 |
my $args = {@_}; |
my $args = {@_}; |
387 |
my $log = $self->{log}; |
my $log = $self->{log}; |
388 |
$log->debug("record args: " . Dumper( $args )); |
$log->dumper( $args, 'args' ); |
389 |
|
|
390 |
foreach my $f (qw/record_uri template/) { |
foreach my $f (qw/record_uri template/) { |
391 |
$log->fatal("need $f") unless ($args->{$f}); |
$log->fatal("need $f") unless ($args->{$f}); |
419 |
} |
} |
420 |
|
|
421 |
|
|
422 |
|
=head2 list_nodes |
423 |
|
|
424 |
|
my @nodes = $m->list_nodes( 'site' ); |
425 |
|
|
426 |
|
Return all databases which have records for selected site. Returned array of |
427 |
|
hashes has elements C<name> and C<label>. |
428 |
|
|
429 |
|
=cut |
430 |
|
|
431 |
|
sub list_nodes { |
432 |
|
my $self = shift; |
433 |
|
|
434 |
|
my $site = shift; |
435 |
|
|
436 |
|
$self->{log}->debug("list_nodes use site $site"); |
437 |
|
|
438 |
|
$self->setup_site( $site ); |
439 |
|
|
440 |
|
my @nodes; |
441 |
|
|
442 |
|
if ($self->{est_node}->doc_num > 0) { |
443 |
|
push @nodes, { |
444 |
|
name => $self->{est_node}->name, |
445 |
|
label => $self->{est_node}->label, |
446 |
|
doc_num => $self->{est_node}->doc_num, |
447 |
|
} |
448 |
|
} |
449 |
|
|
450 |
|
# refresh set info |
451 |
|
$self->{est_node}->_set_info; |
452 |
|
|
453 |
|
my $links = $self->{est_node}->links || return @nodes; |
454 |
|
|
455 |
|
$self->{log}->dumper( $links, 'links' ); |
456 |
|
|
457 |
|
foreach my $link (@{ $links }) { |
458 |
|
my ($url, $label, $credit) = split(/\t/, $link, 3); |
459 |
|
if ($url =~ m#/node/(.+)$#) { |
460 |
|
my $node = $1; |
461 |
|
$self->setup_site( $node ); |
462 |
|
$self->{est_node}->_set_info; |
463 |
|
push @nodes, { |
464 |
|
name => $node, |
465 |
|
label => $label, |
466 |
|
doc_num => $self->{est_node}->doc_num, |
467 |
|
} |
468 |
|
} else { |
469 |
|
$self->{log}->warn("can't find node name in link $link"); |
470 |
|
} |
471 |
|
} |
472 |
|
|
473 |
|
$self->setup_site( $site ); |
474 |
|
$self->{est_node}->_set_info; |
475 |
|
|
476 |
|
$self->{log}->dumper( \@nodes, 'nodes' ); |
477 |
|
|
478 |
|
return @nodes; |
479 |
|
} |
480 |
|
|
481 |
|
=cut |
482 |
|
|
483 |
|
|
484 |
=head2 save_html |
=head2 save_html |
485 |
|
|
486 |
$m->save_html( '/full/path/to/file', $content ); |
$m->save_html( '/full/path/to/file', $content ); |
552 |
|
|
553 |
=cut |
=cut |
554 |
|
|
555 |
|
# Escape <, >, & and ", and to produce valid XML |
556 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
557 |
|
my $escape_re = join '|' => keys %escape; |
558 |
|
|
559 |
sub apply { |
sub apply { |
560 |
my $self = shift; |
my $self = shift; |
561 |
|
|
603 |
if (ref($v) eq 'ARRAY') { |
if (ref($v) eq 'ARRAY') { |
604 |
if ($#{$v} == 0) { |
if ($#{$v} == 0) { |
605 |
$v = $v->[0]; |
$v = $v->[0]; |
606 |
|
$v =~ s/($escape_re)/$escape{$1}/g; |
607 |
} else { |
} else { |
608 |
$join = $default_delimiter->{$type} unless defined($join); |
$join = $default_delimiter->{$type} unless defined($join); |
609 |
$v = join($join, @{$v}); |
$v = join($join, map { |
610 |
|
s/($escape_re)/$escape{$1}/g; |
611 |
|
} @{$v}); |
612 |
} |
} |
613 |
} else { |
} else { |
614 |
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
676 |
|
|
677 |
my $s; |
my $s; |
678 |
if ($s_el > 0) { |
if ($s_el > 0) { |
679 |
$s = $item->{'search'}->[$i] || die "can't find value $i for type search in field $search"; |
$s = $item->{'search'}->[$i] or warn "can't find value $i for type search in field $search"; |
680 |
} else { |
} else { |
681 |
$s = $item->{'search'}->[0]; |
$s = $item->{'search'}->[0]; |
682 |
} |
} |
683 |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
684 |
$s = __quotemeta( $s ); |
$s = __quotemeta( $s ); |
685 |
|
|
686 |
my $d = $item->{'display'}->[$i] || die "can't find value $i for type display in field $display"; |
my $d = $item->{'display'}->[$i] or warn "can't find value $i for type display in field $display"; |
687 |
|
|
688 |
my $template_arg = ''; |
my $template_arg = ''; |
689 |
$template_arg = qq{,'$template'} if ($template); |
$template_arg = qq{,'$template'} if ($template); |