/[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

Diff of /no_pager/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.6  
changed lines
  Added in v.24

  ViewVC Help
Powered by ViewVC 1.1.26