/[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 2 by dpavlin, Tue Aug 15 15:43:31 2006 UTC revision 17 by dpavlin, Wed Aug 16 22:26:45 2006 UTC
# Line 3  Line 3 
3  use strict;  use strict;
4    
5  use CGI::Simple;  use CGI::Simple;
6  use CGI::Carp qw(fatalsToBrowser);  use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
7  use Search::Estraier;  use Search::Estraier;
8    use YAML::Syck;
9    use JSON::Syck;
10    use Data::Dump qw/dump/;
11    
12  my $q = new CGI::Simple;  my $q = new CGI::Simple;
13  print qq{Content-type: text/html\n\r\n\r};  print qq{Content-type: text/html\n\r\n\r};
14    
15  if ($q->path_info() eq '/snippet') {  my $config = LoadFile('config.yml');
16    
17  print qq{  my $v = {
18  <HTML>          search => '',
19  <HEAD>          hits => 0,
20  <META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type">          page => 0,
21  <META CONTENT="no-cache" HTTP-EQUIV="Pragma">          max_page => 0,
22  <META CONTENT="-1" HTTP-EQUIV="Expires">          time => '',
23  </HEAD>          id => time() . rand(99),
   
 <div>  
         <div class="post">  
         <ul>  
24  };  };
25    
26  my $node = new Search::Estraier::Node(  my $json;
27          url => 'http://localhost:1978/node/cpan',  
28          user => 'admin',  #warn "config = ", dump($config);
29          passwd => 'admin',  
30          croak_on_error => 1,  sub json {
31  );          return
32            '<textarea id="json" style="display:none">' .
33  my $o = $q->param('index') || 0;          $q->escapeHTML( JSON::Syck::Dump( $v ) ) .
34  my $search = $q->param('q');          '</textarea>';
35    }
36  my $on_page = 30;  
37  my $skip = $o * $on_page;  sub get_results {
38            my ($search, $page) = @_;
39  my $cond = new Search::Estraier::Condition;  
40  $cond->set_phrase( $search );          warn "get_results( $search , $page )\n";
41  $cond->set_max( $on_page );  
42  $cond->set_skip( $skip );          if (! $search || $search =~ m/^\s*$/) {
43                    $v->{status} = 'Enter search query';
44  my $nres = $node->search($cond, 0);                  return('', json);
45            }
46  my $max = 0;  
47            if (! $page) {
48  if (defined($nres)) {                  $v->{status} = 'Error: no page number?';
49          $max = $nres->hits;                  return('', json);
         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;  
50          }          }
51    
52            $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
53            $v->{search} = $search || '';
54    
55          # for each document in results          $v->{page} = $page;
         for my $i ( 0 ... $nres->doc_num - 1 ) {  
56    
57                  my $rdoc = $nres->get_doc($i);          my $node = new Search::Estraier::Node(%{ $config->{estraier} });
58    
59                  print "<ul>";          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                  print "<h1>", $rdoc->attr('@title'),"</h1>\n";                  # for each document in results
112                  print "<h2>", $rdoc->attr('source'),"</h2>\n";                  for my $i ( 0 ... $nres->doc_num - 1 ) {
113                  print "", html_snippet( $rdoc->snippet ),"<br/>\n";  
114                  print "[", $skip + $i, "] ";                          my $rdoc = $nres->get_doc($i);
115                  print "<tt>", $rdoc->attr('@uri'),"</tt>";  
116                  print "</ul>";                          $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          }          }
 } else {  
         die "error: ", $node->status,"\n";  
 }  
130    
131            return ($out,json);
132    
133  print qq{  }
         </ul>  
         </div>  
134    
135  </div>  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    
 </html>  
 };  
144    
145  } else {  } else {
146    
147          sub page_id {          my ($get_results,$json);
148                  my $page_id = time() . rand(99);  
149                  warn "page_id = $page_id\n";          if ($q->param('search')) {
150                  return $page_id;                  ($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;          my $f = $q->path_info;
157          $f =~ s/\W+//g;          $f =~ s/\W+//g;
# Line 108  print qq{ Line 159  print qq{
159          $f .= '.html';          $f .= '.html';
160          open(my $s, $f)  || die "$f: $!";          open(my $s, $f)  || die "$f: $!";
161          while(<$s>) {          while(<$s>) {
162                  s/<%(.*?)%>/eval "$1"/ge;                  s/<%(.+?)%>/eval "$1"/ge;
163                  print;                  print;
164          }          }
165          close($f);          close($f);

Legend:
Removed from v.2  
changed lines
  Added in v.17

  ViewVC Help
Powered by ViewVC 1.1.26