--- no_pager/index.cgi 2006/08/16 00:29:08 9 +++ no_pager/index.cgi 2006/08/18 09:41:35 28 @@ -6,6 +6,7 @@ use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use Search::Estraier; use YAML::Syck; +use JSON::Syck; use Data::Dump qw/dump/; my $q = new CGI::Simple; @@ -13,99 +14,193 @@ my $config = LoadFile('config.yml'); -#warn "config = ", dump($config); +my $v = { + search => '', + hits => 0, + page => 0, + max_page => 0, + time => '', + id => time() . rand(99), +}; -if ($q->path_info() eq '/snippet') { +my $json; -print qq{ - - - - - - - -
-
- -
+ $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/); + $v->{search} = $search; -
+ $v->{page} = $page; + + my $node = new Search::Estraier::Node(%{ $config->{estraier} }); + + my $on_page = 30; + my $skip = ( $page - 1 ) * $on_page; + + my $cond = new Search::Estraier::Condition; + $cond->set_phrase( $search ); + $cond->set_max( $on_page * $page ); ## FIXME * $page is needed by hest 1.3.8 + $cond->set_skip( $skip ); + $cond->set_order( $p->{sort} ) if ($p->{sort}); + + my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) ); + + my $out; + + if (defined($nres)) { + + $v->{hits} = $nres->hits; + $v->{time} = $nres->hint('TIME'); + + if ($v->{hits} == 0) { + $v->{status} = qq{No results for your search.}; + return next_page(); + } elsif ($nres->doc_num == 0) { + $v->{status} = qq{Error getting results for page $page.}; + return next_page('No results found.'); + } + + $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page ); + + $v->{status} = qq{ + Got $v->{hits} results for $v->{search} + in $v->{time} s + }; + + 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 || '' ) . '' . + $q->escapeHTML( $hit || '' ) . '' . + $q->escapeHTML( $post || ''); + } + return $out; + } + + sub attr_regex { + my ($rdoc,$attr) = @_; + my $text = $rdoc->attr( $attr ); + return unless defined($text); + + if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) { + my $do = '$text =~ ' . $r . ';'; + eval $do; + if ($@) { + warn "eval $do failed: $@\n"; + } + } + return $text; + } + + my @template; + open(my $t, 'result.html') || die "result.html: $!"; + while(<$t>) { + push @template, $_; + } + close($t); + + # for each document in results + for my $i ( 0 ... $nres->doc_num - 1 ) { + + my $rdoc = $nres->get_doc($i); + my $uri = attr_regex( $rdoc, '@uri' ); + my $nr = $skip + $i + 1; + + map { + my $l = $_; + $l =~ s/<%(.+?)%>/eval "$1"/ge; + $out .= $l; + } @template; + + } + + } else { + $out .= 'error: ' . $node->status; + } + + if ($v->{page} == $v->{max_page}) { + $out .= next_page('
All results shown'); + } else { + $out .= next_page( + '
Loading results...
', + 'If you are using the scroll bar, release the mouse to see more results.' + ); + } + + return $out; + +} + +if ($q->path_info() eq '/snippet') { + + print get_results( + search => $q->param('search') || '', + page => $q->param('page') || 0, + sort => $q->param('sort') || undef, + ); - -}; } else { - sub page_id { - my $page_id = time() . rand(99); - warn "page_id = $page_id\n"; - return $page_id; - }; + my $get_results = get_results( + search => $q->param('search') || '', + page => 1, + sort => $q->param('sort') || undef, + ); my $f = $q->path_info; $f =~ s/\W+//g; @@ -113,10 +208,9 @@ $f .= '.html'; open(my $s, $f) || die "$f: $!"; while(<$s>) { - no strict 'vars'; s/<%(.+?)%>/eval "$1"/ge; print; } - close($f); + close($s); }