1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Displays search results for the web interface
5 package PublicInbox::SearchView;
8 use List::Util qw(min max);
9 use URI::Escape qw(uri_unescape);
10 use PublicInbox::Smsg;
11 use PublicInbox::Hval qw(ascii_html obfuscate_addrs mid_href fmt_ts);
12 use PublicInbox::View;
13 use PublicInbox::WwwAtomStream;
14 use PublicInbox::WwwStream qw(html_oneshot);
15 use PublicInbox::SearchThread;
16 use PublicInbox::SearchQuery;
17 use PublicInbox::Search qw(mdocid);
22 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
23 if ($ctx->{env}->{'psgi.input'}->read(my $buf, 3)) {
24 $q->{t} = 1 if $buf =~ /\Ax=[^0]/;
26 require PublicInbox::Mbox;
27 $q->{x} eq 'm' ? PublicInbox::Mbox::mbox_all($ctx, $q) :
33 my $srch = $ctx->{-inbox}->search or
34 return PublicInbox::WWW::need($ctx, 'Search');
35 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
37 my $query = $q->{'q'};
42 $o = -($o + 1); # so [-1] is the last element, like Perl lists
46 # double the limit for expanded views:
55 my ($mset, $total, $err, $html);
58 $mset = $srch->query($query, $opts);
59 $total = $mset->get_matches_estimated;
62 ctx_prepare($q, $ctx);
65 $html = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
66 } elsif ($total == 0) {
67 if (defined($ctx->{-uxs_retried})) {
69 $q->{'q'} = $ctx->{-uxs_retried};
70 } elsif (index($q->{'q'}, '%') >= 0) {
71 $ctx->{-uxs_retried} = $q->{'q'};
72 $q->{'q'} = uri_unescape($q->{'q'});
76 $html = "<pre>\n[No results found]</pre><hr>";
78 return adump($_[0], $mset, $q, $ctx) if $x eq 'A';
80 $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx);
81 return mset_thread($ctx, $mset, $q) if $x eq 't';
82 mset_summary($ctx, $mset, $q); # appends to {-html_tip}
85 html_oneshot($ctx, $code);
88 # display non-nested search results similar to what users expect from
89 # regular WWW search engines:
91 my ($ctx, $mset, $q) = @_;
93 my $total = $mset->get_matches_estimated;
94 my $pad = length("$total");
96 my $res = \($ctx->{-html_tip});
97 my $ibx = $ctx->{-inbox};
98 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
99 my @nums = @{$ibx->search->mset_to_artnums($mset)};
100 my %num2msg = map { $_->{num} => $_ } @{$ibx->over->get_all(@nums)};
103 foreach my $m ($mset->items) {
104 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
105 my $pct = get_pct($m);
106 my $num = shift @nums;
107 my $smsg = delete($num2msg{$num}) or do {
109 $m = "$m $num expired\n";
110 $ctx->{env}->{'psgi.errors'}->print($m);
114 $ctx->{-t_max} //= $smsg->{ts};
116 # only when sorting by relevance, ->items is always
117 # ordered descending:
121 my $s = ascii_html($smsg->{subject});
122 my $f = ascii_html($smsg->{from_name});
124 obfuscate_addrs($obfs_ibx, $s);
125 obfuscate_addrs($obfs_ibx, $f);
127 my $date = fmt_ts($smsg->{ds});
128 my $mid = mid_href($smsg->{mid});
129 $s = '(no subject)' if $s eq '';
130 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
132 $$res .= "$pfx - by $f @ $date UTC [$pct%]\n\n";
134 if ($q->{r}) { # for descriptions in search_nav_bot
135 $q->{-min_pct} = $min;
136 $q->{-max_pct} = $max;
138 $$res .= search_nav_bot($mset, $q);
142 # shorten "/full/path/to/Foo/Bar.pm" to "Foo/Bar.pm" so error
143 # messages don't reveal FS layout info in case people use non-standard
147 if (my $short = $rmap_inc{$full}) {
149 } elsif (!scalar(keys %rmap_inc) && -e $full) {
150 %rmap_inc = map {; "$INC{$_}" => $_ } keys %INC;
151 # fall back to basename as last resort
152 $rmap_inc{$full} // (split('/', $full))[-1];
159 my ($ctx, $err) = @_;
160 my $u = $ctx->{-inbox}->base_url($ctx->{env}) . '_/text/help/';
161 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
162 $err =~ s!(\S+)!path2inc($1)!sge;
163 $err = ascii_html($err);
164 "\nBad query: <b>$err</b>\n" .
165 qq{See <a\nhref="$u">$u</a> for help on using search};
169 my ($mset, $q, $ctx) = @_;
170 my $m = $q->qs_html(x => 'm', r => undef, t => undef);
171 my $rv = qq{<form\naction="?$m"\nmethod="post"><pre>};
172 my $initial_q = $ctx->{-uxs_retried};
173 if (defined $initial_q) {
174 my $rewritten = $q->{'q'};
175 utf8::decode($initial_q);
176 utf8::decode($rewritten);
177 $initial_q = ascii_html($initial_q);
178 $rewritten = ascii_html($rewritten);
179 $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
180 $rv .= " returned no results, used:\n";
181 $rv .= " <b>$rewritten</b>\n instead\n\n";
184 $rv .= 'Search results ordered by [';
186 my $d = $q->qs_html(r => 0);
187 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
189 my $d = $q->qs_html(r => 1);
190 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
197 my $t = $q->qs_html(x => 't');
198 $rv .= qq{<b>summary</b>|<a\nhref="?$t">nested</a>}
199 } elsif ($q->{x} eq 't') {
200 my $s = $q->qs_html(x => '');
201 $rv .= qq{<a\nhref="?$s">summary</a>|<b>nested</b>};
203 my $A = $q->qs_html(x => 'A', r => undef);
204 $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
205 if ($ctx->{-inbox}->search->has_threadid) {
206 $rv .= qq{\n\t\t\tdownload mbox.gz: } .
207 # we set name=z w/o using it since it seems required for
208 # lynx (but works fine for w3m).
209 qq{<input\ntype=submit\nname=z\n} .
210 q{value="results only"/>} .
211 qq{|<input\ntype=submit\nname=x\n} .
212 q{value="full threads"/>};
213 } else { # BOFH needs to --reindex
214 $rv .= qq{\n\t\t\t\t\t\tdownload: } .
215 qq{<input\ntype=submit\nname=z\nvalue="mbox.gz"/>}
217 $rv .= qq{</pre></form><pre>};
222 my $total = $mset->get_matches_estimated;
224 my $rv = '</pre><hr><pre id=t>';
226 my $off = $o < 0 ? -($o + 1) : $o;
227 my $end = $off + $mset->size;
231 my $approx = $end == $total ? '' : '~';
232 $rv .= "Results $beg-$end of $approx$total";
234 $rv .= "No more results, only $total";
236 my ($next, $join, $prev, $nd, $pd);
238 if ($o >= 0) { # sort descending
241 $next = $q->qs_html(o => $n, l => $l);
242 $nd = $q->{r} ? "[<= $q->{-min_pct}%]" : '(older)';
245 $join = $n < $total ? ' | ' : "\t";
247 $prev = $q->qs_html(o => ($p > 0 ? $p : 0));
248 $pd = $q->{r} ? "[>= $q->{-max_pct}%]" : '(newer)';
250 } else { # o < 0, sort ascending
254 $next = $q->qs_html(o => $n, l => $l);
255 $nd = $q->{r} ? "[<= $q->{-min_pct}%]" : '(newer)';
258 $join = -$n < $total ? ' | ' : "\t";
260 $prev = $q->qs_html(o => ($p < 0 ? $p : 0));
261 $pd = $q->{r} ? "[>= $q->{-max_pct}%]" : '(older)';
265 $rv .= qq{ <a\nhref="?$next"\nrel=next>next $nd</a>} if $next;
266 $rv .= $join if $join;
267 $rv .= qq{<a\nhref="?$prev"\nrel=prev>prev $pd</a>} if $prev;
269 my $rev = $q->qs_html(o => $o < 0 ? 0 : -1);
270 $rv .= qq{ | <a\nhref="?$rev">reverse</a></pre>};
275 (eval { $b->topmost->{pct} } // 0) <=>
276 (eval { $a->topmost->{pct} } // 0)
281 # Capped at "99%" since "100%" takes an extra column in the
282 # thread skeleton view. <xapian/mset.h> says the value isn't
283 # very meaningful, anyways.
284 my $n = $_[0]->get_percent;
289 my ($ctx, $mset, $q) = @_;
290 my $ibx = $ctx->{-inbox};
291 my $nshard = $ibx->search->{nshard} // 1;
292 my %pct = map { mdocid($nshard, $_) => get_pct($_) } $mset->items;
293 my $msgs = $ibx->over->get_all(keys %pct);
294 $_->{pct} = $pct{$_->{num}} for @$msgs;
296 if ($r) { # for descriptions in search_nav_bot
297 my @pct = values %pct;
298 $q->{-min_pct} = min(@pct);
299 $q->{-max_pct} = max(@pct);
301 my $rootset = PublicInbox::SearchThread::thread($msgs,
302 $r ? \&sort_relevance : \&PublicInbox::View::sort_ds,
304 my $skel = search_nav_bot($mset, $q). "<pre>";
306 $ctx->{anchor_idx} = 1;
307 $ctx->{cur_level} = 0;
308 $ctx->{skel} = \$skel;
309 $ctx->{mapping} = {};
310 $ctx->{searchview} = 1;
311 $ctx->{prev_attr} = '';
312 $ctx->{prev_level} = 0;
313 $ctx->{s_nr} = scalar(@$msgs).'+ results';
315 # reduce hash lookups in skel_dump
316 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
317 PublicInbox::View::walk_thread($rootset, $ctx,
318 \&PublicInbox::View::pre_thread);
320 # link $INBOX_DIR/description text to "recent" view around
321 # the newest message in this result set:
322 $ctx->{-t_max} = max(map { delete $_->{ts} } @$msgs);
324 @$msgs = reverse @$msgs if $r;
325 $ctx->{msgs} = $msgs;
326 PublicInbox::WwwStream::aresponse($ctx, 200, \&mset_thread_i);
329 # callback for PublicInbox::WwwStream::getline
331 my ($ctx, $eml) = @_;
332 $ctx->zmore($ctx->html_top) if exists $ctx->{-html_tip};
333 $eml and return PublicInbox::View::eml_entry($ctx, $eml);
334 my $smsg = shift @{$ctx->{msgs}} or
335 $ctx->zmore(${delete($ctx->{skel})});
343 $qh = ascii_html($qh);
344 $ctx->{-q_value_html} = $qh;
345 $ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
346 $ctx->{-title_html} = "$qh - search results";
348 $extra .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
349 if (my $x = $q->{x}) {
351 $extra .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
353 $ctx->{-extra_form_html} = $extra;
357 my ($cb, $mset, $q, $ctx) = @_;
358 $ctx->{ids} = $ctx->{-inbox}->search->mset_to_artnums($mset);
359 $ctx->{search_query} = $q; # used by WwwAtomStream::atom_header
360 PublicInbox::WwwAtomStream->response($ctx, 200, \&adump_i);
363 # callback for PublicInbox::WwwAtomStream::getline
366 while (my $num = shift @{$ctx->{ids}}) {
367 my $smsg = eval { $ctx->{-inbox}->over->get_art($num) } or next;