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