/[jquery]/no_pager/index.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /no_pager/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (show annotations)
Fri Aug 18 09:41:35 2006 UTC (17 years, 8 months ago) by dpavlin
File size: 4416 byte(s)
fix for broken set_max
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 JSON::Syck;
10 use Data::Dump qw/dump/;
11
12 my $q = new CGI::Simple;
13 print qq{Content-type: text/html\n\r\n\r};
14
15 my $config = LoadFile('config.yml');
16
17 my $v = {
18 search => '',
19 hits => 0,
20 page => 0,
21 max_page => 0,
22 time => '',
23 id => time() . rand(99),
24 };
25
26 my $json;
27
28 sub debug {
29 my ($text,$var) = @_;
30 print "<pre>$text = ", dump($var), "</pre>";
31 }
32
33 #debug('config', $config);
34
35 sub json {
36 return
37 '<textarea id="json" style="display:none">' .
38 $q->escapeHTML( JSON::Syck::Dump( $v ) ) .
39 '</textarea>';
40 }
41
42 sub sort_order {
43 my $out;
44
45 my $sort = $q->param('sort');
46
47 $out .= '<select name="sort" id="sort">';
48
49 foreach my $s (@{ $config->{estraier}->{order} }) {
50 my ($text,$value) = %{$s};
51 $out .= qq{<option value="$value"} .
52 ( $sort eq $value ? ' selected' : '' ) .
53 qq{>$text</option>};
54 }
55
56 $out .= '</select>';
57 }
58
59 sub get_results {
60 my $p = {@_};
61
62 my ($search,$page) = ( $p->{search} , $p->{page});
63
64 sub next_page {
65 return '<div id="next_page">' .
66 join("\n", @_) . json() . '</div>';
67 }
68
69 if (! $search || $search =~ m/^\s*$/) {
70 $v->{status} = 'Enter search query';
71 return next_page();
72 }
73
74 if (! $page) {
75 $v->{status} = 'Error: no page number?';
76 return next_page();
77 }
78
79 $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
80 $v->{search} = $search;
81
82 $v->{page} = $page;
83
84 my $node = new Search::Estraier::Node(%{ $config->{estraier} });
85
86 my $on_page = 30;
87 my $skip = ( $page - 1 ) * $on_page;
88
89 my $cond = new Search::Estraier::Condition;
90 $cond->set_phrase( $search );
91 $cond->set_max( $on_page * $page ); ## FIXME * $page is needed by hest 1.3.8
92 $cond->set_skip( $skip );
93 $cond->set_order( $p->{sort} ) if ($p->{sort});
94
95 my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );
96
97 my $out;
98
99 if (defined($nres)) {
100
101 $v->{hits} = $nres->hits;
102 $v->{time} = $nres->hint('TIME');
103
104 if ($v->{hits} == 0) {
105 $v->{status} = qq{<strong>No results for your search.</strong>};
106 return next_page();
107 } elsif ($nres->doc_num == 0) {
108 $v->{status} = qq{<strong>Error getting results for page $page.</strong>};
109 return next_page('<strong>No results found.</strong>');
110 }
111
112 $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
113
114 $v->{status} = qq{
115 Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
116 in <em>$v->{time} s</em>
117 };
118
119 sub html_snippet {
120 my $text = shift || return;
121 my $out = '';
122 foreach my $s (split(/[\n\r]{2}/, $text)) {
123 $out .= ' ... ' if ($out);
124 my ($pre,$hit,$post) = split(/\n/,$s,3);
125 $hit =~ s/\t.*$//;
126 $out .=
127 $q->escapeHTML( $pre || '' ) . '<b>' .
128 $q->escapeHTML( $hit || '' ) . '</b>' .
129 $q->escapeHTML( $post || '');
130 }
131 return $out;
132 }
133
134 sub attr_regex {
135 my ($rdoc,$attr) = @_;
136 my $text = $rdoc->attr( $attr );
137 return unless defined($text);
138
139 if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {
140 my $do = '$text =~ ' . $r . ';';
141 eval $do;
142 if ($@) {
143 warn "eval $do failed: $@\n";
144 }
145 }
146 return $text;
147 }
148
149 my @template;
150 open(my $t, 'result.html') || die "result.html: $!";
151 while(<$t>) {
152 push @template, $_;
153 }
154 close($t);
155
156 # for each document in results
157 for my $i ( 0 ... $nres->doc_num - 1 ) {
158
159 my $rdoc = $nres->get_doc($i);
160 my $uri = attr_regex( $rdoc, '@uri' );
161 my $nr = $skip + $i + 1;
162
163 map {
164 my $l = $_;
165 $l =~ s/<%(.+?)%>/eval "$1"/ge;
166 $out .= $l;
167 } @template;
168
169 }
170
171 } else {
172 $out .= 'error: ' . $node->status;
173 }
174
175 if ($v->{page} == $v->{max_page}) {
176 $out .= next_page('<br/><strong>All results shown</strong>');
177 } else {
178 $out .= next_page(
179 '<br/><strong>Loading results...</strong><br/>',
180 'If you are using the scroll bar, release the mouse to see more results.'
181 );
182 }
183
184 return $out;
185
186 }
187
188 if ($q->path_info() eq '/snippet') {
189
190 print get_results(
191 search => $q->param('search') || '',
192 page => $q->param('page') || 0,
193 sort => $q->param('sort') || undef,
194 );
195
196
197 } else {
198
199 my $get_results = get_results(
200 search => $q->param('search') || '',
201 page => 1,
202 sort => $q->param('sort') || undef,
203 );
204
205 my $f = $q->path_info;
206 $f =~ s/\W+//g;
207 $f ||= 'search';
208 $f .= '.html';
209 open(my $s, $f) || die "$f: $!";
210 while(<$s>) {
211 s/<%(.+?)%>/eval "$1"/ge;
212 print;
213 }
214 close($s);
215
216 }

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26