3 |
use strict; |
use strict; |
4 |
|
|
5 |
use CGI::Simple; |
use CGI::Simple; |
6 |
use CGI::Carp qw(fatalsToBrowser); |
use CGI::Carp qw(fatalsToBrowser warningsToBrowser); |
7 |
use Search::Estraier; |
use Search::Estraier; |
8 |
|
use YAML::Syck; |
9 |
|
use Data::Dump qw/dump/; |
10 |
|
|
11 |
my $q = new CGI::Simple; |
my $q = new CGI::Simple; |
12 |
print qq{Content-type: text/html\n\r\n\r}; |
print qq{Content-type: text/html\n\r\n\r}; |
13 |
|
|
14 |
|
my $config = LoadFile('config.yml'); |
15 |
|
|
16 |
|
#warn "config = ", dump($config); |
17 |
|
|
18 |
if ($q->path_info() eq '/snippet') { |
if ($q->path_info() eq '/snippet') { |
19 |
|
|
20 |
print qq{ |
print qq{ |
30 |
<ul> |
<ul> |
31 |
}; |
}; |
32 |
|
|
33 |
my $node = new Search::Estraier::Node( |
my $node = new Search::Estraier::Node(%{ $config->{estraier} }); |
|
url => 'http://localhost:1978/node/cpan', |
|
|
user => 'admin', |
|
|
passwd => 'admin', |
|
|
croak_on_error => 1, |
|
|
); |
|
34 |
|
|
35 |
my $o = $q->param('index') || 0; |
my $o = $q->param('index') || 0; |
36 |
my $search = $q->param('q'); |
my $search = $q->param('q'); |
37 |
|
$search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/); |
38 |
|
|
39 |
my $on_page = 30; |
my $on_page = 30; |
40 |
my $skip = $o * $on_page; |
my $skip = $o * $on_page; |
44 |
$cond->set_max( $on_page ); |
$cond->set_max( $on_page ); |
45 |
$cond->set_skip( $skip ); |
$cond->set_skip( $skip ); |
46 |
|
|
47 |
my $nres = $node->search($cond, 0); |
my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) ); |
48 |
|
|
49 |
my $max = 0; |
my $max = 0; |
50 |
|
|
51 |
if (defined($nres)) { |
if (defined($nres)) { |
52 |
$max = $nres->hits; |
$max = $nres->hits; |
53 |
print "Got ", $nres->hits, " results for $search\n"; |
my $time = $nres->hint('TIME'); |
54 |
|
print qq{ |
55 |
|
<div id="status_update" style="display:none;"> |
56 |
|
Got <b>$max</b> results for <tt>$search</tt> in <em>$time s</em> |
57 |
|
</div> |
58 |
|
}; |
59 |
|
|
60 |
sub html_snippet { |
sub html_snippet { |
61 |
my $text = shift || return; |
my $text = shift || return; |
78 |
|
|
79 |
my $rdoc = $nres->get_doc($i); |
my $rdoc = $nres->get_doc($i); |
80 |
|
|
81 |
print "<ul>"; |
print "<li>"; |
82 |
|
|
83 |
print "<h1>", $rdoc->attr('@title'),"</h1>\n"; |
print "<h1>", $rdoc->attr('@title'),"</h1>\n"; |
84 |
print "<h2>", $rdoc->attr('source'),"</h2>\n"; |
print "<h2>", $rdoc->attr('source'),"</h2>\n"; |
85 |
print "", html_snippet( $rdoc->snippet ),"<br/>\n"; |
print "<h3>", $rdoc->attr('@mdate'),"</h3>\n"; |
86 |
|
print "<p>", html_snippet( $rdoc->snippet ),"</p>\n"; |
87 |
print "[", $skip + $i, "] "; |
print "[", $skip + $i, "] "; |
88 |
print "<tt>", $rdoc->attr('@uri'),"</tt>"; |
print "<tt>", $rdoc->attr('@uri'),"</tt>"; |
89 |
print "</ul>"; |
print "</li>"; |
90 |
} |
} |
91 |
} else { |
} else { |
92 |
die "error: ", $node->status,"\n"; |
die "error: ", $node->status,"\n"; |
116 |
$f .= '.html'; |
$f .= '.html'; |
117 |
open(my $s, $f) || die "$f: $!"; |
open(my $s, $f) || die "$f: $!"; |
118 |
while(<$s>) { |
while(<$s>) { |
119 |
s/<%(.*?)%>/eval "$1"/ge; |
no strict 'vars'; |
120 |
|
s/<%(.+?)%>/eval "$1"/ge; |
121 |
print; |
print; |
122 |
} |
} |
123 |
close($f); |
close($f); |