/[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 4 by dpavlin, Tue Aug 15 16:03:16 2006 UTC revision 15 by dpavlin, Wed Aug 16 21:34:37 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/books',  
28          user => 'admin',  #warn "config = ", dump($config);
29          passwd => 'admin',  
30          croak_on_error => 1,  sub get_results {
31  );          my ($search, $page) = @_;
32    
33  my $o = $q->param('index') || 0;          if (! $search) {
34  my $search = $q->param('q');                  $v->{status} = 'Enter search query';
35                    return;
 my $on_page = 30;  
 my $skip = $o * $on_page;  
   
 my $cond = new Search::Estraier::Condition;  
 $cond->set_phrase( $search );  
 $cond->set_max( $on_page );  
 $cond->set_skip( $skip );  
   
 my $nres = $node->search($cond, 0);  
   
 my $max = 0;  
   
 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;  
36          }          }
37    
38            if (! $page) {
39                    $v->{status} = 'Error: no page number?';
40                    return;
41            }
42    
43          # for each document in results          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
44          for my $i ( 0 ... $nres->doc_num - 1 ) {          $v->{search} = $search || '';
45    
46                  my $rdoc = $nres->get_doc($i);          $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                  print "<li>";                  # for each document in results
103                    for my $i ( 0 ... $nres->doc_num - 1 ) {
104    
105                  print "<h1>", $rdoc->attr('@title'),"</h1>\n";                          my $rdoc = $nres->get_doc($i);
106                  print "<h2>", $rdoc->attr('source'),"</h2>\n";  
107                  print "", html_snippet( $rdoc->snippet ),"<br/>\n";                          $out .= '<div class="item">' .
108                  print "[", $skip + $i, "] ";                                  '<h1>' . $rdoc->attr('@title') . '</h1>' .
109                  print "<tt>", $rdoc->attr('@uri'),"</tt>";                                  '<p>' . html_snippet( $rdoc->snippet ) . '</p>' .
110                  print "</li>";                                  '<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          }          }
 } else {  
         die "error: ", $node->status,"\n";  
 }  
121    
122            $json = '<textarea id="json" style="display:none">' .
123                    $q->escapeHTML( JSON::Syck::Dump( $v ) ) .
124                    '</textarea>';
125    
126  print qq{          return ($out,$json);
         </ul>  
         </div>  
127    
128  </div>  }
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    
 </html>  
 };  
139    
140  } else {  } else {
141    
142          sub page_id {          my ($get_results, $json) = get_results( $q->param('search'), 1 );
                 my $page_id = time() . rand(99);  
                 warn "page_id = $page_id\n";  
                 return $page_id;  
         };  
143    
144          my $f = $q->path_info;          my $f = $q->path_info;
145          $f =~ s/\W+//g;          $f =~ s/\W+//g;
# Line 108  print qq{ Line 147  print qq{
147          $f .= '.html';          $f .= '.html';
148          open(my $s, $f)  || die "$f: $!";          open(my $s, $f)  || die "$f: $!";
149          while(<$s>) {          while(<$s>) {
                 no strict 'vars';  
150                  s/<%(.+?)%>/eval "$1"/ge;                  s/<%(.+?)%>/eval "$1"/ge;
151                  print;                  print;
152          }          }

Legend:
Removed from v.4  
changed lines
  Added in v.15

  ViewVC Help
Powered by ViewVC 1.1.26