/[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 17 - (show annotations)
Wed Aug 16 22:26:45 2006 UTC (17 years, 8 months ago) by dpavlin
File size: 3391 byte(s)
return json object even without any search string (as on first load)
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 #warn "config = ", dump($config);
29
30 sub json {
31 return
32 '<textarea id="json" style="display:none">' .
33 $q->escapeHTML( JSON::Syck::Dump( $v ) ) .
34 '</textarea>';
35 }
36
37 sub get_results {
38 my ($search, $page) = @_;
39
40 warn "get_results( $search , $page )\n";
41
42 if (! $search || $search =~ m/^\s*$/) {
43 $v->{status} = 'Enter search query';
44 return('', json);
45 }
46
47 if (! $page) {
48 $v->{status} = 'Error: no page number?';
49 return('', json);
50 }
51
52 $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
53 $v->{search} = $search || '';
54
55 $v->{page} = $page;
56
57 my $node = new Search::Estraier::Node(%{ $config->{estraier} });
58
59 my $on_page = 30;
60 my $skip = ( $page - 1 ) * $on_page;
61
62 my $cond = new Search::Estraier::Condition;
63 $cond->set_phrase( $search );
64 $cond->set_max( $on_page );
65 $cond->set_skip( $skip );
66
67 my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );
68
69 my $out;
70
71 if (defined($nres)) {
72 $v->{hits} = $nres->hits;
73 $v->{time} = $nres->hint('TIME');
74 $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
75
76 $v->{status} = qq{
77 Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
78 in <em>$v->{time} s</em>
79 };
80
81 sub html_snippet {
82 my $text = shift || return;
83 my $out = '';
84 foreach my $s (split(/[\n\r]{2}/, $text)) {
85 $out .= ' ... ' if ($out);
86 my ($pre,$hit,$post) = split(/\n/,$s,3);
87 $hit =~ s/\t.*$//;
88 $out .=
89 $q->escapeHTML( $pre || '' ) . '<b>' .
90 $q->escapeHTML( $hit || '' ) . '</b>' .
91 $q->escapeHTML( $post || '');
92 }
93 return $out;
94 }
95
96 sub attr_regex {
97 my ($rdoc,$attr) = @_;
98 my $text = $rdoc->attr( $attr );
99 return unless defined($text);
100
101 if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {
102 my $do = '$text =~ ' . $r . ';';
103 eval $do;
104 if ($@) {
105 warn "eval $do failed: $@\n";
106 }
107 }
108 return $text;
109 }
110
111 # for each document in results
112 for my $i ( 0 ... $nres->doc_num - 1 ) {
113
114 my $rdoc = $nres->get_doc($i);
115
116 $out .= '<div class="item">' .
117 '<h1>' . $rdoc->attr('@title') . '</h1>' .
118 '<p>' . html_snippet( $rdoc->snippet ) . '</p>' .
119 '<h2>' . attr_regex( $rdoc, 'source' ) . '</h2>';
120 my $uri = attr_regex( $rdoc, '@uri' );
121 $out .=
122 qq{<a href="$uri"><tt>$uri</tt></a> } .
123 attr_regex( $rdoc, '@mdate' ) .
124 ' [' . ( $skip + $i + 1 ) . ']';
125 }
126
127 } else {
128 $out .= 'error: ' . $node->status;
129 }
130
131 return ($out,json);
132
133 }
134
135 if ($q->path_info() eq '/snippet') {
136
137 print join("\n<!-- json data -->",
138 get_results(
139 $q->param('search'),
140 $q->param('page'),
141 )
142 );
143
144
145 } else {
146
147 my ($get_results,$json);
148
149 if ($q->param('search')) {
150 ($get_results, $json) = get_results( $q->param('search'), 1 );
151 } else {
152 $v->{status} = 'Enter search query';
153 ($get_results, $json) = ('', json);
154 }
155
156 my $f = $q->path_info;
157 $f =~ s/\W+//g;
158 $f ||= 'search';
159 $f .= '.html';
160 open(my $s, $f) || die "$f: $!";
161 while(<$s>) {
162 s/<%(.+?)%>/eval "$1"/ge;
163 print;
164 }
165 close($f);
166
167 }

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26