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:
54 my ($mset, $total, $err, $html);
57 $mset = $srch->mset($query, $opts);
58 $total = $mset->get_matches_estimated;
61 ctx_prepare($q, $ctx);
64 $html = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
65 } elsif ($total == 0) {
66 if (defined($ctx->{-uxs_retried})) {
68 $q->{'q'} = $ctx->{-uxs_retried};
69 } elsif (index($q->{'q'}, '%') >= 0) {
70 $ctx->{-uxs_retried} = $q->{'q'};
71 $q->{'q'} = uri_unescape($q->{'q'});
75 $html = "<pre>\n[No results found]</pre><hr>";
77 return adump($_[0], $mset, $q, $ctx) if $x eq 'A';
79 $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx);
80 return mset_thread($ctx, $mset, $q) if $x eq 't';
81 mset_summary($ctx, $mset, $q); # appends to {-html_tip}
84 html_oneshot($ctx, $code);
87 # display non-nested search results similar to what users expect from
88 # regular WWW search engines:
90 my ($ctx, $mset, $q) = @_;
92 my $total = $mset->get_matches_estimated;
93 my $pad = length("$total");
95 my $res = \($ctx->{-html_tip});
96 my $ibx = $ctx->{-inbox};
97 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
98 my @nums = @{$ibx->search->mset_to_artnums($mset)};
99 my %num2msg = map { $_->{num} => $_ } @{$ibx->over->get_all(@nums)};
102 foreach my $m ($mset->items) {
103 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
104 my $pct = get_pct($m);
105 my $num = shift @nums;
106 my $smsg = delete($num2msg{$num}) or do {
108 $m = "$m $num expired\n";
109 $ctx->{env}->{'psgi.errors'}->print($m);
113 $ctx->{-t_max} //= $smsg->{ts};
115 # only when sorting by relevance, ->items is always
116 # ordered descending:
120 my $s = ascii_html($smsg->{subject});
121 my $f = ascii_html($smsg->{from_name});
123 obfuscate_addrs($obfs_ibx, $s);
124 obfuscate_addrs($obfs_ibx, $f);
126 my $date = fmt_ts($smsg->{ds});
127 my $mid = mid_href($smsg->{mid});
128 $s = '(no subject)' if $s eq '';
129 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
131 $$res .= "$pfx - by $f @ $date UTC [$pct%]\n\n";
133 if ($q->{r}) { # for descriptions in search_nav_bot
134 $q->{-min_pct} = $min;
135 $q->{-max_pct} = $max;
137 $$res .= search_nav_bot($mset, $q);
141 # shorten "/full/path/to/Foo/Bar.pm" to "Foo/Bar.pm" so error
142 # messages don't reveal FS layout info in case people use non-standard
146 if (my $short = $rmap_inc{$full}) {
148 } elsif (!scalar(keys %rmap_inc) && -e $full) {
149 %rmap_inc = map {; "$INC{$_}" => $_ } keys %INC;
150 # fall back to basename as last resort
151 $rmap_inc{$full} // (split('/', $full))[-1];
158 my ($ctx, $err) = @_;
159 my $u = $ctx->{-inbox}->base_url($ctx->{env}) . '_/text/help/';
160 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
161 $err =~ s!(\S+)!path2inc($1)!sge;
162 $err = ascii_html($err);
163 "\nBad query: <b>$err</b>\n" .
164 qq{See <a\nhref="$u">$u</a> for help on using search};
168 my ($mset, $q, $ctx) = @_;
169 my $m = $q->qs_html(x => 'm', r => undef, t => undef);
170 my $rv = qq{<form\naction="?$m"\nmethod="post"><pre>};
171 my $initial_q = $ctx->{-uxs_retried};
172 if (defined $initial_q) {
173 my $rewritten = $q->{'q'};
174 utf8::decode($initial_q);
175 utf8::decode($rewritten);
176 $initial_q = ascii_html($initial_q);
177 $rewritten = ascii_html($rewritten);
178 $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
179 $rv .= " returned no results, used:\n";
180 $rv .= " <b>$rewritten</b>\n instead\n\n";
183 $rv .= 'Search results ordered by [';
185 my $d = $q->qs_html(r => 0);
186 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
188 my $d = $q->qs_html(r => 1);
189 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
196 my $t = $q->qs_html(x => 't');
197 $rv .= qq{<b>summary</b>|<a\nhref="?$t">nested</a>}
198 } elsif ($q->{x} eq 't') {
199 my $s = $q->qs_html(x => '');
200 $rv .= qq{<a\nhref="?$s">summary</a>|<b>nested</b>};
202 my $A = $q->qs_html(x => 'A', r => undef);
203 $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
204 if ($ctx->{-inbox}->search->has_threadid) {
205 $rv .= qq{\n\t\t\tdownload mbox.gz: } .
206 # we set name=z w/o using it since it seems required for
207 # lynx (but works fine for w3m).
208 qq{<input\ntype=submit\nname=z\n} .
209 q{value="results only"/>} .
210 qq{|<input\ntype=submit\nname=x\n} .
211 q{value="full threads"/>};
212 } else { # BOFH needs to --reindex
213 $rv .= qq{\n\t\t\t\t\t\tdownload: } .
214 qq{<input\ntype=submit\nname=z\nvalue="mbox.gz"/>}
216 $rv .= qq{</pre></form><pre>};
221 my $total = $mset->get_matches_estimated;
223 my $rv = '</pre><hr><pre id=t>';
225 my $off = $o < 0 ? -($o + 1) : $o;
226 my $end = $off + $mset->size;
230 my $approx = $end == $total ? '' : '~';
231 $rv .= "Results $beg-$end of $approx$total";
233 $rv .= "No more results, only $total";
235 my ($next, $join, $prev, $nd, $pd);
237 if ($o >= 0) { # sort descending
240 $next = $q->qs_html(o => $n, l => $l);
241 $nd = $q->{r} ? "[<= $q->{-min_pct}%]" : '(older)';
244 $join = $n < $total ? ' | ' : "\t";
246 $prev = $q->qs_html(o => ($p > 0 ? $p : 0));
247 $pd = $q->{r} ? "[>= $q->{-max_pct}%]" : '(newer)';
249 } else { # o < 0, sort ascending
253 $next = $q->qs_html(o => $n, l => $l);
254 $nd = $q->{r} ? "[<= $q->{-min_pct}%]" : '(newer)';
257 $join = -$n < $total ? ' | ' : "\t";
259 $prev = $q->qs_html(o => ($p < 0 ? $p : 0));
260 $pd = $q->{r} ? "[>= $q->{-max_pct}%]" : '(older)';
264 $rv .= qq{ <a\nhref="?$next"\nrel=next>next $nd</a>} if $next;
265 $rv .= $join if $join;
266 $rv .= qq{<a\nhref="?$prev"\nrel=prev>prev $pd</a>} if $prev;
268 my $rev = $q->qs_html(o => $o < 0 ? 0 : -1);
269 $rv .= qq{ | <a\nhref="?$rev">reverse</a></pre>};
274 (eval { $b->topmost->{pct} } // 0) <=>
275 (eval { $a->topmost->{pct} } // 0)
280 # Capped at "99%" since "100%" takes an extra column in the
281 # thread skeleton view. <xapian/mset.h> says the value isn't
282 # very meaningful, anyways.
283 my $n = $_[0]->get_percent;
288 my ($ctx, $mset, $q) = @_;
289 my $ibx = $ctx->{-inbox};
290 my $nshard = $ibx->search->{nshard} // 1;
291 my %pct = map { mdocid($nshard, $_) => get_pct($_) } $mset->items;
292 my $msgs = $ibx->over->get_all(keys %pct);
293 $_->{pct} = $pct{$_->{num}} for @$msgs;
295 if ($r) { # for descriptions in search_nav_bot
296 my @pct = values %pct;
297 $q->{-min_pct} = min(@pct);
298 $q->{-max_pct} = max(@pct);
300 my $rootset = PublicInbox::SearchThread::thread($msgs,
301 $r ? \&sort_relevance : \&PublicInbox::View::sort_ds,
303 my $skel = search_nav_bot($mset, $q). "<pre>";
305 $ctx->{anchor_idx} = 1;
306 $ctx->{cur_level} = 0;
307 $ctx->{skel} = \$skel;
308 $ctx->{mapping} = {};
309 $ctx->{searchview} = 1;
310 $ctx->{prev_attr} = '';
311 $ctx->{prev_level} = 0;
312 $ctx->{s_nr} = scalar(@$msgs).'+ results';
314 # reduce hash lookups in skel_dump
315 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
316 PublicInbox::View::walk_thread($rootset, $ctx,
317 \&PublicInbox::View::pre_thread);
319 # link $INBOX_DIR/description text to "recent" view around
320 # the newest message in this result set:
321 $ctx->{-t_max} = max(map { delete $_->{ts} } @$msgs);
323 @$msgs = reverse @$msgs if $r;
324 $ctx->{msgs} = $msgs;
325 PublicInbox::WwwStream::aresponse($ctx, 200, \&mset_thread_i);
328 # callback for PublicInbox::WwwStream::getline
330 my ($ctx, $eml) = @_;
331 $ctx->zmore($ctx->html_top) if exists $ctx->{-html_tip};
332 $eml and return PublicInbox::View::eml_entry($ctx, $eml);
333 my $smsg = shift @{$ctx->{msgs}} or
334 $ctx->zmore(${delete($ctx->{skel})});
342 $qh = ascii_html($qh);
343 $ctx->{-q_value_html} = $qh;
344 $ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
345 $ctx->{-title_html} = "$qh - search results";
347 $extra .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
348 if (my $x = $q->{x}) {
350 $extra .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
352 $ctx->{-extra_form_html} = $extra;
356 my ($cb, $mset, $q, $ctx) = @_;
357 $ctx->{ids} = $ctx->{-inbox}->search->mset_to_artnums($mset);
358 $ctx->{search_query} = $q; # used by WwwAtomStream::atom_header
359 PublicInbox::WwwAtomStream->response($ctx, 200, \&adump_i);
362 # callback for PublicInbox::WwwAtomStream::getline
365 while (my $num = shift @{$ctx->{ids}}) {
366 my $smsg = eval { $ctx->{-inbox}->over->get_art($num) } or next;