]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchView.pm
use "Atom feed" consistently in headers/footers
[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 #
4 # Displays search results for the web interface
5 package PublicInbox::SearchView;
6 use strict;
7 use warnings;
8 use PublicInbox::SearchMsg;
9 use PublicInbox::Hval;
10 use PublicInbox::View;
11 use PublicInbox::MID qw(mid2path mid_clean);
12 use Email::MIME;
13 require PublicInbox::Git;
14 our $LIM = 50;
15
16 sub sres_top_html {
17         my ($ctx) = @_;
18         my $q = PublicInbox::SearchQuery->new($ctx->{cgi});
19         my $code = 200;
20
21         # double the limit for expanded views:
22         my $opts = {
23                 limit => $LIM,
24                 offset => $q->{o},
25                 mset => 1,
26                 relevance => $q->{r},
27         };
28         my ($mset, $total);
29
30         eval {
31                 $mset = $ctx->{srch}->query($q->{q}, $opts);
32                 $total = $mset->get_matches_estimated;
33         };
34         my $err = $@;
35         my $res = html_start($q, $ctx) . '<pre>';
36         if ($err) {
37                 $code = 400;
38                 $res .= err_txt($err) . "</pre><hr /><pre>" . foot($ctx);
39         } elsif ($total == 0) {
40                 $code = 404;
41                 $res .= "\n\n[No results found]</pre><hr /><pre>".foot($ctx);
42         } else {
43                 my $x = $q->{x};
44                 return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A');
45
46                 $res .= search_nav_top($mset, $q) . "\n\n";
47                 if ($x eq 't') {
48                         return sub { tdump($_[0], $res, $mset, $q, $ctx) };
49                 }
50                 dump_mset(\$res, $mset);
51                 $res .= '</pre>' . search_nav_bot($mset, $q) .
52                         "\n\n" . foot($ctx);
53         }
54
55         $res .= "</pre></body></html>";
56         [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
57 }
58
59 sub dump_mset {
60         my ($res, $mset) = @_;
61
62         my $total = $mset->get_matches_estimated;
63         my $pad = length("$total");
64         my $pfx = ' ' x $pad;
65         foreach my $m ($mset->items) {
66                 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
67                 my $pct = $m->get_percent;
68                 my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
69                 my $s = PublicInbox::Hval->new_oneline($smsg->subject);
70                 my $f = $smsg->from_name;
71                 $f = PublicInbox::Hval->new_oneline($f)->as_html;
72                 my $ts = PublicInbox::View::fmt_ts($smsg->ts);
73                 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
74                 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
75                         $s->as_html . "</a></b>\n";
76                 $$res .= "$pfx  - by $f @ $ts UTC [$pct%]\n\n";
77         }
78 }
79
80 sub err_txt {
81         my ($err) = @_;
82         my $u = 'http://xapian.org/docs/queryparser.html';
83         $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
84         $err = PublicInbox::Hval->new_oneline($err)->as_html;
85         "\n\nBad query: <b>$err</b>\n" .
86                 qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
87 }
88
89 sub search_nav_top {
90         my ($mset, $q) = @_;
91
92         my $rv = "Search results ordered by [";
93         if ($q->{r}) {
94                 my $d = $q->qs_html(r => 0);
95                 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
96         } else {
97                 my $d = $q->qs_html(r => 1);
98                 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
99         }
100
101         $rv .= ']  view[';
102
103         my $x = $q->{x};
104         if ($x eq '') {
105                 my $t = $q->qs_html(x => 't');
106                 $rv .= qq{<b>summary</b>|<a\nhref="?$t">threaded</a>}
107         } elsif ($q->{x} eq 't') {
108                 my $s = $q->qs_html(x => '');
109                 $rv .= qq{<a\nhref="?$s">summary</a>|<b>threaded</b>};
110         }
111         my $A = $q->qs_html(x => 'A', r => undef);
112         $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
113 }
114
115 sub search_nav_bot {
116         my ($mset, $q) = @_;
117         my $total = $mset->get_matches_estimated;
118         my $nr = scalar $mset->items;
119         my $o = $q->{o};
120         my $end = $o + $nr;
121         my $beg = $o + 1;
122         my $rv = "<hr /><pre>Results $beg-$end of $total";
123         my $n = $o + $LIM;
124
125         if ($n < $total) {
126                 my $qs = $q->qs_html(o => $n);
127                 $rv .= qq{, <a\nhref="?$qs">next</a>}
128         }
129         if ($o > 0) {
130                 $rv .= $n < $total ? '/' : ',      ';
131                 my $p = $o - $LIM;
132                 my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
133                 $rv .= qq{<a\nhref="?$qs">prev</a>};
134         }
135         $rv;
136 }
137
138 sub tdump {
139         my ($cb, $res, $mset, $q, $ctx) = @_;
140         my $fh = $cb->([200, ['Content-Type'=>'text/html; charset=UTF-8']]);
141         $fh->write($res);
142         my %pct;
143         my @m = map {
144                 my $i = $_;
145                 my $m = PublicInbox::SearchMsg->load_doc($i->get_document);
146                 $pct{$m->mid} = $i->get_percent;
147                 $m = $m->mini_mime;
148                 $m;
149         } ($mset->items);
150
151         require PublicInbox::Thread;
152         my $th = PublicInbox::Thread->new(@m);
153         {
154                 no warnings 'once';
155                 $Mail::Thread::nosubject = 0;
156         }
157         $th->thread;
158         if ($q->{r}) {
159                 $th->order(sub {
160                         sort { (eval { $pct{$b->topmost->messageid} } || 0)
161                                         <=>
162                                 (eval { $pct{$a->topmost->messageid} } || 0)
163                         } @_;
164                 });
165         } else {
166                 no warnings 'once';
167                 $th->order(*PublicInbox::View::rsort_ts);
168         }
169
170         my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
171         my $state = { ctx => $ctx, anchor_idx => 0, pct => \%pct };
172         $ctx->{searchview} = 1;
173         tdump_ent($fh, $git, $state, $_, 0) for $th->rootset;
174         Email::Address->purge_cache;
175
176         $fh->write(search_nav_bot($mset, $q). "\n\n" .
177                         foot($ctx). '</pre></body></html>');
178
179         $fh->close;
180 }
181
182 sub tdump_ent {
183         my ($fh, $git, $state, $node, $level) = @_;
184         return unless $node;
185         my $mime = $node->message;
186
187         if ($mime) {
188                 # lazy load the full message from mini_mime:
189                 my $mid = $mime->header('Message-ID');
190                 $mime = eval {
191                         my $path = mid2path(mid_clean($mid));
192                         Email::MIME->new($git->cat_file('HEAD:'.$path));
193                 };
194         }
195         if ($mime) {
196                 PublicInbox::View::index_entry($fh, $mime, $level, $state);
197         } else {
198                 my $mid = $node->messageid;
199                 $fh->write(PublicInbox::View::ghost_table('', $mid, $level));
200         }
201         tdump_ent($fh, $git, $state, $node->child, $level + 1);
202         tdump_ent($fh, $git, $state, $node->next, $level);
203 }
204
205 sub foot {
206         my ($ctx) = @_;
207         my $foot = $ctx->{footer} || '';
208         qq{Back to <a\nhref=".">index</a>.\n$foot};
209 }
210
211 sub html_start {
212         my ($q, $ctx) = @_;
213         my $query = PublicInbox::Hval->new_oneline($q->{q});
214
215         my $qh = $query->as_html;
216         my $A = $q->qs_html(x => 'A', r => undef);
217         my $res = "<html><head><title>$qh - search results</title>" .
218                 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
219                 qq!href="?$A"\ntype="application/atom+xml"/></head>! .
220                 qq{<body><form\naction="">} .
221                 qq{<input\nname=q\nvalue="$qh"\ntype=text />};
222
223         $res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
224         if (my $x = $q->{x}) {
225                 my $xh = PublicInbox::Hval->new_oneline($x)->as_html;
226                 $res .= qq{<input\ntype=hidden\nname=x\nvalue="$xh" />};
227         }
228
229         $res .= qq{<input\ntype=submit\nvalue=search /></form>};
230 }
231
232 sub adump {
233         my ($cb, $mset, $q, $ctx) = @_;
234         my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
235         my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
236         my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
237         my $x = PublicInbox::Hval->new_oneline($q->{q})->as_html;
238         $x = qq{$x - search results};
239         $feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
240         $feed_opts->{url} .= '?'. $q->qs_html(x => undef);
241         $x = PublicInbox::Feed::atom_header($feed_opts, $x);
242         $fh->write($x. PublicInbox::Feed::feed_updated());
243
244         for ($mset->items) {
245                 $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
246                 $x = mid2path($x);
247                 PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
248         }
249         PublicInbox::Feed::end_feed($fh);
250 }
251
252 package PublicInbox::SearchQuery;
253 use strict;
254 use warnings;
255 use PublicInbox::Hval;
256
257 sub new {
258         my ($class, $cgi) = @_;
259         my $r = $cgi->param('r');
260         bless {
261                 q => $cgi->param('q'),
262                 x => $cgi->param('x') || '',
263                 o => int($cgi->param('o') || 0) || 0,
264                 r => (defined $r && $r ne '0'),
265         }, $class;
266 }
267
268 sub qs_html {
269         my ($self, %over) = @_;
270
271         if (keys %over) {
272                 my $tmp = bless { %$self }, ref($self);
273                 foreach my $k (keys %over) {
274                         $tmp->{$k} = $over{$k};
275                 }
276                 $self = $tmp;
277         }
278
279         my $q = PublicInbox::Hval->new($self->{q})->as_href;
280         $q =~ s/%20/+/g; # improve URL readability
281         my $qs = "q=$q";
282
283         if (my $o = $self->{o}) { # ignore o == 0
284                 $qs .= "&amp;o=$o";
285         }
286         if (my $r = $self->{r}) {
287                 $qs .= "&amp;r";
288         }
289         if (my $x = $self->{x}) {
290                 $qs .= "&amp;x=$x" if ($x eq 't' || $x eq 'A');
291         }
292         $qs;
293 }
294
295 1;