]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchView.pm
searchview: factor out dump_mset subroutine
[public-inbox.git] / lib / PublicInbox / SearchView.pm
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;
4 use strict;
5 use warnings;
6 use PublicInbox::SearchMsg;
7 use PublicInbox::Hval;
8 use PublicInbox::View;
9 use POSIX qw/strftime/;
10 our $LIM = 25;
11
12 sub sres_top_html {
13         my ($ctx, $q) = @_;
14         my $cgi = $ctx->{cgi};
15         # $q ||= $cgi->param('q');
16         my $o = int($cgi->param('o') || 0);
17         my $r = $cgi->param('r');
18         $r = (defined $r && $r ne '0');
19         my $opts = { limit => $LIM, offset => $o, mset => 1, relevance => $r };
20         my $mset = $ctx->{srch}->query($q, $opts);
21         my $total = $mset->get_matches_estimated;
22         my $query = PublicInbox::Hval->new_oneline($q);
23         my $qh = $query->as_html;
24         my $res = "<html><head><title>$qh - search results</title></head>" .
25                   qq{<body><form\naction="">} .
26                   qq{<input\nname=q\nvalue="$qh"\ntype=text />};
27
28         $res .= qq{<input\ntype=hidden\nname=r />} if $r;
29
30         $res .= qq{<input\ntype=submit\nvalue=search /></form>} .
31                   PublicInbox::View::PRE_WRAP;
32
33         my $foot = $ctx->{footer} || '';
34         $foot = qq{Back to <a\nhref=".">index</a>.};
35         if ($total == 0) {
36                 $res .= "\n\n[No results found]</pre><hr /><pre>$foot";
37         } else {
38                 $q = $query->as_href;
39                 $q =~ s/%20/+/g; # improve URL readability
40                 my $qp = "?q=$q";
41                 $qp .= "&amp;o=$o" if $o;
42
43                 $res .= "Search results ordered by [";
44                 if ($r) {
45                         $res .= qq{<a\nhref="$qp">date</a>|<b>relevance</b>};
46                 } else {
47                         $qp .= '&amp;r';
48                         $res .= qq{<b>date</b>|<a\nhref="$qp">relevance</a>};
49                 }
50                 $res .= "]\n\n";
51
52                 dump_mset(\$res, $mset);
53                 my $nr = scalar $mset->items;
54                 my $end = $o + $nr;
55                 my $beg = $o + 1;
56                 $res .= "<hr /><pre>";
57                 $res .= "Results $beg-$end of $total";
58
59                 my $n = $o + $LIM;
60                 if ($n < $total) {
61                         $qp = "q=$q&amp;o=$n";
62                         $qp .= "&amp;r" if $r;
63                         $res .= qq{, <a\nhref="?$qp">next</a>}
64                 }
65                 if ($o > 0) {
66                         $res .= $n < $total ? '/' : ',      ';
67                         my $p = $o - $LIM;
68                         $qp = "q=$q";
69                         $qp .= "&amp;o=$p" if $p > 0;
70                         $qp .= "&amp;r" if $r;
71                         $res .= qq{<a\nhref="?$qp">prev</a>};
72                 }
73                 $res .= "\n\n" . $foot;
74         }
75
76         $res .= "</pre></body></html>";
77         [200, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
78 }
79
80 sub dump_mset {
81         my ($res, $mset) = @_;
82
83         my $total = $mset->get_matches_estimated;
84         my $pad = length("$total");
85         my $pfx = ' ' x $pad;
86         foreach my $m ($mset->items) {
87                 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
88                 my $pct = $m->get_percent;
89                 my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
90                 my $s = PublicInbox::Hval->new_oneline($smsg->subject);
91                 my $f = $smsg->from_name;
92                 $f = PublicInbox::Hval->new_oneline($f)->as_html;
93                 my $d = strftime('%Y-%m-%d %H:%M', gmtime($smsg->ts));
94                 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
95                 $$res .= qq{$rank. <b><a\nhref="$mid/t/#u">}.
96                         $s->as_html . "</a></b>\n";
97                 $$res .= "$pfx  - by $f @ $d UTC [$pct%]\n\n";
98         }
99 }
100
101 1;