1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::SearchView;
6 use PublicInbox::SearchMsg;
9 use POSIX qw/strftime/;
14 my $cgi = $ctx->{cgi};
16 # $q ||= $cgi->param('q');
17 my $o = int($cgi->param('o') || 0);
18 my $r = $cgi->param('r');
19 $r = (defined $r && $r ne '0');
20 my $opts = { limit => $LIM, offset => $o, mset => 1, relevance => $r };
23 $mset = $ctx->{srch}->query($q, $opts);
24 $total = $mset->get_matches_estimated;
27 my $query = PublicInbox::Hval->new_oneline($q);
28 my $qh = $query->as_html;
29 my $res = "<html><head><title>$qh - search results</title></head>" .
30 qq{<body><form\naction="">} .
31 qq{<input\nname=q\nvalue="$qh"\ntype=text />};
33 $res .= qq{<input\ntype=hidden\nname=r />} if $r;
35 $res .= qq{<input\ntype=submit\nvalue=search /></form>} .
36 PublicInbox::View::PRE_WRAP;
38 my $foot = $ctx->{footer} || '';
39 $foot = qq{Back to <a\nhref=".">index</a>.};
41 my $u = 'http://xapian.org/docs/queryparser.html';
43 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
44 $err = PublicInbox::Hval->new_oneline($err)->as_html;
45 $res .= "\n\nBad query: <b>$err</b>\n";
46 $res .= qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
47 $res .= "</pre><hr /><pre>$foot";
48 } elsif ($total == 0) {
50 $res .= "\n\n[No results found]</pre><hr /><pre>$foot";
53 $q =~ s/%20/+/g; # improve URL readability
55 $qp .= "&o=$o" if $o;
57 $res .= "Search results ordered by [";
59 $res .= qq{<a\nhref="$qp">date</a>|<b>relevance</b>};
62 $res .= qq{<b>date</b>|<a\nhref="$qp">relevance</a>};
66 dump_mset(\$res, $mset);
67 my $nr = scalar $mset->items;
70 $res .= "<hr /><pre>";
71 $res .= "Results $beg-$end of $total";
75 $qp = "q=$q&o=$n";
76 $qp .= "&r" if $r;
77 $res .= qq{, <a\nhref="?$qp">next</a>}
80 $res .= $n < $total ? '/' : ', ';
83 $qp .= "&o=$p" if $p > 0;
84 $qp .= "&r" if $r;
85 $res .= qq{<a\nhref="?$qp">prev</a>};
87 $res .= "\n\n" . $foot;
90 $res .= "</pre></body></html>";
91 [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
95 my ($res, $mset) = @_;
97 my $total = $mset->get_matches_estimated;
98 my $pad = length("$total");
100 foreach my $m ($mset->items) {
101 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
102 my $pct = $m->get_percent;
103 my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
104 my $s = PublicInbox::Hval->new_oneline($smsg->subject);
105 my $f = $smsg->from_name;
106 $f = PublicInbox::Hval->new_oneline($f)->as_html;
107 my $d = strftime('%Y-%m-%d %H:%M', gmtime($smsg->ts));
108 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
109 $$res .= qq{$rank. <b><a\nhref="$mid/t/#u">}.
110 $s->as_html . "</a></b>\n";
111 $$res .= "$pfx - by $f @ $d UTC [$pct%]\n\n";