# Copyright (C) 2015 all contributors # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt) package PublicInbox::SearchView; use strict; use warnings; use PublicInbox::SearchMsg; use PublicInbox::Hval; use PublicInbox::View; use POSIX qw/strftime/; our $LIM = 25; sub sres_top_html { my ($ctx, $q) = @_; my $cgi = $ctx->{cgi}; my $code = 200; # $q ||= $cgi->param('q'); my $o = int($cgi->param('o') || 0); my $r = $cgi->param('r'); $r = (defined $r && $r ne '0'); my $opts = { limit => $LIM, offset => $o, mset => 1, relevance => $r }; my ($mset, $total); eval { $mset = $ctx->{srch}->query($q, $opts); $total = $mset->get_matches_estimated; }; my $err = $@; my $query = PublicInbox::Hval->new_oneline($q); my $qh = $query->as_html; my $res = "
$foot"; } elsif ($total == 0) { $code = 404; $res .= "\n\n[No results found]
$foot"; } else { $q = $query->as_href; $q =~ s/%20/+/g; # improve URL readability my $qp = "?q=$q"; $qp .= "&o=$o" if $o; $res .= "Search results ordered by ["; if ($r) { $res .= qq{date|relevance}; } else { $qp .= '&r'; $res .= qq{date|relevance}; } $res .= "]\n\n"; dump_mset(\$res, $mset); my $nr = scalar $mset->items; my $end = $o + $nr; my $beg = $o + 1; $res .= ""; $res .= "Results $beg-$end of $total"; my $n = $o + $LIM; if ($n < $total) { $qp = "q=$q&o=$n"; $qp .= "&r" if $r; $res .= qq{, next} } if ($o > 0) { $res .= $n < $total ? '/' : ', '; my $p = $o - $LIM; $qp = "q=$q"; $qp .= "&o=$p" if $p > 0; $qp .= "&r" if $r; $res .= qq{prev}; } $res .= "\n\n" . $foot; } $res .= ""; [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]]; } sub dump_mset { my ($res, $mset) = @_; my $total = $mset->get_matches_estimated; my $pad = length("$total"); my $pfx = ' ' x $pad; foreach my $m ($mset->items) { my $rank = sprintf("%${pad}d", $m->get_rank + 1); my $pct = $m->get_percent; my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document); my $s = PublicInbox::Hval->new_oneline($smsg->subject); my $f = $smsg->from_name; $f = PublicInbox::Hval->new_oneline($f)->as_html; my $d = strftime('%Y-%m-%d %H:%M', gmtime($smsg->ts)); my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href; $$res .= qq{$rank. }. $s->as_html . "\n"; $$res .= "$pfx - by $f @ $d UTC [$pct%]\n\n"; } } 1;