/[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 14 by dpavlin, Wed Aug 16 17:57:28 2006 UTC revision 17 by dpavlin, Wed Aug 16 22:26:45 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    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);  #warn "config = ", dump($config);
29    
30  if ($q->path_info() eq '/snippet') {  sub json {
31            return
32            '<textarea id="json" style="display:none">' .
33            $q->escapeHTML( JSON::Syck::Dump( $v ) ) .
34            '</textarea>';
35    }
36    
37  print qq{  sub get_results {
38  <HTML>          my ($search, $page) = @_;
 <HEAD>  
 <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>  
 };  
39    
40  my $node = new Search::Estraier::Node(%{ $config->{estraier} });          warn "get_results( $search , $page )\n";
41    
42  my $o = $q->param('index') || 0;          if (! $search || $search =~ m/^\s*$/) {
43  my $search = $q->param('q');                  $v->{status} = 'Enter search query';
44  $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);                  return('', json);
   
 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, ( $config->{estraier}->{depth} || 0 ) );  
   
 my $max = 0;  
   
 if (defined($nres)) {  
         $max = $nres->hits;  
         my $time = $nres->hint('TIME');  
         print qq{  
                 <div id="status_update" style="display:none;">  
                         Got <b>$max</b> results for <tt>$search</tt> in <em>$time s</em>  
                 </div>  
         };  
   
         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;  
45          }          }
46    
47          sub attr_regex {          if (! $page) {
48                  my ($rdoc,$attr) = @_;                  $v->{status} = 'Error: no page number?';
49                  my $text = $rdoc->attr( $attr );                  return('', json);
50                  return unless defined($text);          }
51    
52                  if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
53                          my $do = '$text =~ ' . $r . ';';          $v->{search} = $search || '';
54                          eval $do;  
55                          if ($@) {          $v->{page} = $page;
56                                  warn "eval $do failed: $@\n";  
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                  }                  }
                 return $text;  
         }  
95    
96          # for each document in results                  sub attr_regex {
97          for my $i ( 0 ... $nres->doc_num - 1 ) {                          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);                          my $rdoc = $nres->get_doc($i);
115    
116                  print "<li>";                          $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                  print "<h1>", $rdoc->attr('@title'),"</h1>\n";          } else {
128                  print "<h2>", attr_regex( $rdoc, 'source' ),"</h2>\n";                  $out .= 'error: ' . $node->status;
                 print "<p>", html_snippet( $rdoc->snippet ),"</p>\n";  
                 my $uri = attr_regex( $rdoc, '@uri' );  
                 print qq{<a href="$uri"><tt>$uri</tt></a> },  
                         attr_regex( $rdoc, '@mdate' );  
                 print " [", $skip + $i + 1, "]";  
                 print "</li>";  
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;

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

  ViewVC Help
Powered by ViewVC 1.1.26