]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchView.pm
searchview: error description for invalid queries
[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         my $code = 200;
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 };
21         my ($mset, $total);
22         eval {
23                 $mset = $ctx->{srch}->query($q, $opts);
24                 $total = $mset->get_matches_estimated;
25         };
26         my $err = $@;
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 />};
32
33         $res .= qq{<input\ntype=hidden\nname=r />} if $r;
34
35         $res .= qq{<input\ntype=submit\nvalue=search /></form>} .
36                   PublicInbox::View::PRE_WRAP;
37
38         my $foot = $ctx->{footer} || '';
39         $foot = qq{Back to <a\nhref=".">index</a>.};
40         if ($err) {
41                 my $u = 'http://xapian.org/docs/queryparser.html';
42                 $code = 400;
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) {
49                 $code = 404;
50                 $res .= "\n\n[No results found]</pre><hr /><pre>$foot";
51         } else {
52                 $q = $query->as_href;
53                 $q =~ s/%20/+/g; # improve URL readability
54                 my $qp = "?q=$q";
55                 $qp .= "&amp;o=$o" if $o;
56
57                 $res .= "Search results ordered by [";
58                 if ($r) {
59                         $res .= qq{<a\nhref="$qp">date</a>|<b>relevance</b>};
60                 } else {
61                         $qp .= '&amp;r';
62                         $res .= qq{<b>date</b>|<a\nhref="$qp">relevance</a>};
63                 }
64                 $res .= "]\n\n";
65
66                 dump_mset(\$res, $mset);
67                 my $nr = scalar $mset->items;
68                 my $end = $o + $nr;
69                 my $beg = $o + 1;
70                 $res .= "<hr /><pre>";
71                 $res .= "Results $beg-$end of $total";
72
73                 my $n = $o + $LIM;
74                 if ($n < $total) {
75                         $qp = "q=$q&amp;o=$n";
76                         $qp .= "&amp;r" if $r;
77                         $res .= qq{, <a\nhref="?$qp">next</a>}
78                 }
79                 if ($o > 0) {
80                         $res .= $n < $total ? '/' : ',      ';
81                         my $p = $o - $LIM;
82                         $qp = "q=$q";
83                         $qp .= "&amp;o=$p" if $p > 0;
84                         $qp .= "&amp;r" if $r;
85                         $res .= qq{<a\nhref="?$qp">prev</a>};
86                 }
87                 $res .= "\n\n" . $foot;
88         }
89
90         $res .= "</pre></body></html>";
91         [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
92 }
93
94 sub dump_mset {
95         my ($res, $mset) = @_;
96
97         my $total = $mset->get_matches_estimated;
98         my $pad = length("$total");
99         my $pfx = ' ' x $pad;
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";
112         }
113 }
114
115 1;