1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
|
5 |
use CGI::Simple; |
6 |
use CGI::Carp qw(fatalsToBrowser warningsToBrowser); |
7 |
use Search::Estraier; |
8 |
use YAML::Syck; |
9 |
use Data::Dump qw/dump/; |
10 |
|
11 |
my $q = new CGI::Simple; |
12 |
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') { |
19 |
|
20 |
print qq{ |
21 |
<HTML> |
22 |
<HEAD> |
23 |
<META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type"> |
24 |
<META CONTENT="no-cache" HTTP-EQUIV="Pragma"> |
25 |
<META CONTENT="-1" HTTP-EQUIV="Expires"> |
26 |
</HEAD> |
27 |
|
28 |
<div> |
29 |
<div class="post"> |
30 |
<ul> |
31 |
}; |
32 |
|
33 |
my $node = new Search::Estraier::Node(%{ $config->{estraier} }); |
34 |
|
35 |
my $o = $q->param('index') || 0; |
36 |
my $search = $q->param('q'); |
37 |
|
38 |
my $on_page = 30; |
39 |
my $skip = $o * $on_page; |
40 |
|
41 |
my $cond = new Search::Estraier::Condition; |
42 |
$cond->set_phrase( $search ); |
43 |
$cond->set_max( $on_page ); |
44 |
$cond->set_skip( $skip ); |
45 |
|
46 |
my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) ); |
47 |
|
48 |
my $max = 0; |
49 |
|
50 |
if (defined($nres)) { |
51 |
$max = $nres->hits; |
52 |
print "Got ", $nres->hits, " results for $search\n"; |
53 |
|
54 |
sub html_snippet { |
55 |
my $text = shift || return; |
56 |
my $out = ''; |
57 |
foreach my $s (split(/[\n\r]{2}/, $text)) { |
58 |
$out .= ' ... ' if ($out); |
59 |
my ($pre,$hit,$post) = split(/\n/,$s,3); |
60 |
$hit =~ s/\t.*$//; |
61 |
$out .= |
62 |
$q->escapeHTML( $pre || '' ) . '<b>' . |
63 |
$q->escapeHTML( $hit || '' ) . '</b>' . |
64 |
$q->escapeHTML( $post || ''); |
65 |
} |
66 |
return $out; |
67 |
} |
68 |
|
69 |
|
70 |
# for each document in results |
71 |
for my $i ( 0 ... $nres->doc_num - 1 ) { |
72 |
|
73 |
my $rdoc = $nres->get_doc($i); |
74 |
|
75 |
print "<li>"; |
76 |
|
77 |
print "<h1>", $rdoc->attr('@title'),"</h1>\n"; |
78 |
print "<h2>", $rdoc->attr('source'),"</h2>\n"; |
79 |
print "", html_snippet( $rdoc->snippet ),"<br/>\n"; |
80 |
print "[", $skip + $i, "] "; |
81 |
print "<tt>", $rdoc->attr('@uri'),"</tt>"; |
82 |
print "</li>"; |
83 |
} |
84 |
} else { |
85 |
die "error: ", $node->status,"\n"; |
86 |
} |
87 |
|
88 |
|
89 |
print qq{ |
90 |
</ul> |
91 |
</div> |
92 |
|
93 |
</div> |
94 |
|
95 |
</html> |
96 |
}; |
97 |
|
98 |
} else { |
99 |
|
100 |
sub page_id { |
101 |
my $page_id = time() . rand(99); |
102 |
warn "page_id = $page_id\n"; |
103 |
return $page_id; |
104 |
}; |
105 |
|
106 |
my $f = $q->path_info; |
107 |
$f =~ s/\W+//g; |
108 |
$f ||= 'search'; |
109 |
$f .= '.html'; |
110 |
open(my $s, $f) || die "$f: $!"; |
111 |
while(<$s>) { |
112 |
no strict 'vars'; |
113 |
s/<%(.+?)%>/eval "$1"/ge; |
114 |
print; |
115 |
} |
116 |
close($f); |
117 |
|
118 |
} |