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