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

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26