/[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 15 - (show annotations)
Wed Aug 16 21:34:37 2006 UTC (17 years, 8 months ago) by dpavlin
File size: 3152 byte(s)
another major refactor:

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

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26