/[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 21 - (show annotations)
Thu Aug 17 01:08:23 2006 UTC (17 years, 8 months ago) by dpavlin
File size: 4204 byte(s)
add attr_regex for @title
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 );
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 $v->{hits} = $nres->hits;
101 $v->{time} = $nres->hint('TIME');
102 $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
103
104 $v->{status} = qq{
105 Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
106 in <em>$v->{time} s</em>
107 };
108
109 sub html_snippet {
110 my $text = shift || return;
111 my $out = '';
112 foreach my $s (split(/[\n\r]{2}/, $text)) {
113 $out .= ' ... ' if ($out);
114 my ($pre,$hit,$post) = split(/\n/,$s,3);
115 $hit =~ s/\t.*$//;
116 $out .=
117 $q->escapeHTML( $pre || '' ) . '<b>' .
118 $q->escapeHTML( $hit || '' ) . '</b>' .
119 $q->escapeHTML( $post || '');
120 }
121 return $out;
122 }
123
124 sub attr_regex {
125 my ($rdoc,$attr) = @_;
126 my $text = $rdoc->attr( $attr );
127 return unless defined($text);
128
129 if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {
130 my $do = '$text =~ ' . $r . ';';
131 eval $do;
132 if ($@) {
133 warn "eval $do failed: $@\n";
134 }
135 }
136 return $text;
137 }
138
139 # for each document in results
140 for my $i ( 0 ... $nres->doc_num - 1 ) {
141
142 my $rdoc = $nres->get_doc($i);
143
144 $out .= '<div class="item">' .
145 '<h1>' . attr_regex( $rdoc, '@title' ) . '</h1>' .
146 '<p>' . html_snippet( $rdoc->snippet ) . '</p>' .
147 '<strong>' . attr_regex( $rdoc, 'source' ) . '</strong>' .
148 ' [' . attr_regex( $rdoc, '@size' ) . ' bytes]<br/>';
149 my $uri = attr_regex( $rdoc, '@uri' );
150 $out .=
151 qq{<a href="$uri"><tt>$uri</tt></a> } .
152 attr_regex( $rdoc, '@mdate' ) .
153 ' [' . ( $skip + $i + 1 ) . ']';
154 }
155
156 } else {
157 $out .= 'error: ' . $node->status;
158 }
159
160 if ($v->{page} == $v->{max_page}) {
161 $out .= next_page('<strong>All results shown</strong>');
162 } else {
163 $out .= next_page(
164 '<strong>Loading results...</strong><br/>',
165 'If you are using the scroll bar, release the mouse to see more results.'
166 );
167 }
168
169 return $out;
170
171 }
172
173 if ($q->path_info() eq '/snippet') {
174
175 print get_results(
176 search => $q->param('search') || '',
177 page => $q->param('page') || 0,
178 sort => $q->param('sort') || undef,
179 );
180
181
182 } else {
183
184 my $get_results = get_results(
185 search => $q->param('search') || '',
186 page => 1,
187 sort => $q->param('sort') || undef,
188 );
189
190 my $f = $q->path_info;
191 $f =~ s/\W+//g;
192 $f ||= 'search';
193 $f .= '.html';
194 open(my $s, $f) || die "$f: $!";
195 while(<$s>) {
196 s/<%(.+?)%>/eval "$1"/ge;
197 print;
198 }
199 close($f);
200
201 }

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26