1 # Copyright (C) 2015-2021 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(get_pct);
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->{ibx}->isrch or
34 return PublicInbox::WWW::need($ctx, 'Search');
35 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
41 $o = -($o + 1); # so [-1] is the last element, like Perl lists
45 # double the limit for expanded views:
53 my ($mset, $total, $err, $html);
56 my $query = $q->{'q'};
57 $srch->query_approxidate($ctx->{ibx}->git, $query);
58 $mset = $srch->mset($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->{ibx};
98 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
99 my @nums = @{$ibx->isrch->mset_to_artnums($mset)};
100 my %num2msg = map { $_->{num} => $_ } @{$ibx->over->get_all(@nums)};
101 my ($min, $max, %seen);
103 foreach my $m ($mset->items) {
104 my $num = shift @nums;
105 my $smsg = delete($num2msg{$num}) // do {
106 warn "$m $num expired\n";
109 my $mid = $smsg->{mid};
110 next if $seen{$mid}++;
111 $mid = mid_href($mid);
112 $ctx->{-t_max} //= $smsg->{ts};
113 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
114 my $pct = get_pct($m);
116 # only when sorting by relevance, ->items is always
117 # ordered descending:
121 my $s = ascii_html($smsg->{subject});
122 my $f = ascii_html(delete $smsg->{from_name});
124 obfuscate_addrs($obfs_ibx, $s);
125 obfuscate_addrs($obfs_ibx, $f);
127 my $date = fmt_ts($smsg->{ds});
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->{ibx}->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\nid=d\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>};
197 my $t = $q->qs_html(x => 't');
198 $rv .= qq{<b>summary</b>|<a\nhref="?$t">nested</a>}
199 } elsif ($x eq 't') {
200 my $s = $q->qs_html(x => '');
201 $rv .= qq{<a\nhref="?$s">summary</a>|<b>nested</b>};
202 $pfx = "thread overview <a\nhref=#t>below</a> | ";
204 my $A = $q->qs_html(x => 'A', r => undef);
205 $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
206 if ($ctx->{ibx}->isrch->has_threadid) {
207 $rv .= qq{\n${pfx}download mbox.gz: } .
208 # we set name=z w/o using it since it seems required for
209 # lynx (but works fine for w3m).
210 qq{<input\ntype=submit\nname=z\n} .
211 q{value="results only"/>} .
212 qq{|<input\ntype=submit\nname=x\n} .
213 q{value="full threads"/>};
214 } else { # BOFH needs to --reindex
215 $rv .= qq{\n${pfx}download: } .
216 qq{<input\ntype=submit\nname=z\nvalue="mbox.gz"/>}
218 $rv .= qq{</pre></form><pre>};
221 sub search_nav_bot { # also used by WwwListing for searching extindex miscidx
223 my $total = $mset->get_matches_estimated;
225 my $rv = '</pre><hr><pre id=t>';
227 my $off = $o < 0 ? -($o + 1) : $o;
228 my $end = $off + $mset->size;
232 my $approx = $end == $total ? '' : '~';
233 $rv .= "Results $beg-$end of $approx$total";
235 $rv .= "No more results, only $total";
237 my ($next, $join, $prev, $nd, $pd);
239 if ($o >= 0) { # sort descending
242 $next = $q->qs_html(o => $n, l => $l);
243 $nd = $q->{r} ? "[<= $q->{-min_pct}%]" : '(older)';
246 $join = $n < $total ? ' | ' : "\t";
248 $prev = $q->qs_html(o => ($p > 0 ? $p : 0));
249 $pd = $q->{r} ? "[>= $q->{-max_pct}%]" : '(newer)';
251 } else { # o < 0, sort ascending
255 $next = $q->qs_html(o => $n, l => $l);
256 $nd = $q->{r} ? "[<= $q->{-min_pct}%]" : '(newer)';
259 $join = -$n < $total ? ' | ' : "\t";
261 $prev = $q->qs_html(o => ($p < 0 ? $p : 0));
262 $pd = $q->{r} ? "[>= $q->{-max_pct}%]" : '(older)';
266 $rv .= qq{ <a\nhref="?$next"\nrel=next>next $nd</a>} if $next;
267 $rv .= $join if $join;
268 $rv .= qq{<a\nhref="?$prev"\nrel=prev>prev $pd</a>} if $prev;
270 my $rev = $q->qs_html(o => $o < 0 ? 0 : -1);
271 $rv .= qq{ | <a\nhref="?$rev">reverse</a>} .
272 q{ | sort options + mbox downloads } .
273 q{<a href=#d>above</a></pre>};
278 (eval { $b->topmost->{pct} } // 0) <=>
279 (eval { $a->topmost->{pct} } // 0)
284 my ($ctx, $mset, $q) = @_;
285 my $ibx = $ctx->{ibx};
286 my @pct = map { get_pct($_) } $mset->items;
287 my $msgs = $ibx->isrch->mset_to_smsg($ibx, $mset);
289 $_->{pct} = $pct[$i++] for @$msgs;
291 if ($r) { # for descriptions in search_nav_bot
292 $q->{-min_pct} = min(@pct);
293 $q->{-max_pct} = max(@pct);
295 my $rootset = PublicInbox::SearchThread::thread($msgs,
296 $r ? \&sort_relevance : \&PublicInbox::View::sort_ds,
298 my $skel = search_nav_bot($mset, $q).
299 "<pre>-- links below jump to the message on this page --\n";
302 $ctx->{anchor_idx} = 1;
303 $ctx->{cur_level} = 0;
304 $ctx->{skel} = \$skel;
305 $ctx->{mapping} = {};
306 $ctx->{searchview} = 1;
307 $ctx->{prev_attr} = '';
308 $ctx->{prev_level} = 0;
309 $ctx->{s_nr} = scalar(@$msgs).'+ results';
311 # reduce hash lookups in skel_dump
312 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
313 PublicInbox::View::walk_thread($rootset, $ctx,
314 \&PublicInbox::View::pre_thread);
316 # link $INBOX_DIR/description text to "recent" view around
317 # the newest message in this result set:
318 $ctx->{-t_max} = max(map { delete $_->{ts} } @$msgs);
320 @$msgs = reverse @$msgs if $r;
321 $ctx->{msgs} = $msgs;
322 PublicInbox::WwwStream::aresponse($ctx, 200, \&mset_thread_i);
325 # callback for PublicInbox::WwwStream::getline
327 my ($ctx, $eml) = @_;
328 $ctx->zmore($ctx->html_top) if exists $ctx->{-html_tip};
329 $eml and return PublicInbox::View::eml_entry($ctx, $eml);
330 my $smsg = shift @{$ctx->{msgs}} or
331 $ctx->zmore(${delete($ctx->{skel})});
339 $qh = ascii_html($qh);
340 $ctx->{-q_value_html} = $qh;
341 $ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
342 $ctx->{-title_html} = "$qh - search results";
344 $extra .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
345 if (my $x = $q->{x}) {
347 $extra .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
349 $ctx->{-extra_form_html} = $extra;
353 my ($cb, $mset, $q, $ctx) = @_;
354 $ctx->{ids} = $ctx->{ibx}->isrch->mset_to_artnums($mset);
355 $ctx->{search_query} = $q; # used by WwwAtomStream::atom_header
356 PublicInbox::WwwAtomStream->response($ctx, 200, \&adump_i);
359 # callback for PublicInbox::WwwAtomStream::getline
362 while (my $num = shift @{$ctx->{ids}}) {
363 my $smsg = eval { $ctx->{ibx}->over->get_art($num) } or next;