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 URI::Escape qw(uri_unescape);
10 use PublicInbox::Hval qw(ascii_html obfuscate_addrs mid_href);
11 use PublicInbox::View;
12 use PublicInbox::WwwAtomStream;
13 use PublicInbox::WwwStream qw(html_oneshot);
14 use PublicInbox::SearchThread;
15 use PublicInbox::SearchQuery;
16 use PublicInbox::Search qw(mdocid);
21 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
22 if ($ctx->{env}->{'psgi.input'}->read(my $buf, 3)) {
23 $q->{t} = 1 if $buf =~ /\Ax=[^0]/;
25 require PublicInbox::Mbox;
26 $q->{x} eq 'm' ? PublicInbox::Mbox::mbox_all($ctx, $q) :
32 my $srch = $ctx->{-inbox}->search or
33 return PublicInbox::WWW::need($ctx, 'Search');
34 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
36 my $query = $q->{'q'};
41 $o = -($o + 1); # so [-1] is the last element, like Perl lists
45 # double the limit for expanded views:
54 my ($mset, $total, $err, $html);
57 $mset = $srch->query($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)};
101 foreach my $m ($mset->items) {
102 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
103 my $pct = get_pct($m);
104 my $num = shift @nums;
105 my $smsg = delete($num2msg{$num}) or do {
107 $m = "$m $num expired\n";
108 $ctx->{env}->{'psgi.errors'}->print($m);
112 my $s = ascii_html($smsg->{subject});
113 my $f = ascii_html($smsg->{from_name});
115 obfuscate_addrs($obfs_ibx, $s);
116 obfuscate_addrs($obfs_ibx, $f);
118 my $date = PublicInbox::View::fmt_ts($smsg->{ds});
119 my $mid = mid_href($smsg->{mid});
120 $s = '(no subject)' if $s eq '';
121 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
123 $$res .= "$pfx - by $f @ $date UTC [$pct%]\n\n";
125 $$res .= search_nav_bot($mset, $q);
129 # shorten "/full/path/to/Foo/Bar.pm" to "Foo/Bar.pm" so error
130 # messages don't reveal FS layout info in case people use non-standard
134 if (my $short = $rmap_inc{$full}) {
136 } elsif (!scalar(keys %rmap_inc) && -e $full) {
137 %rmap_inc = map {; "$INC{$_}" => $_ } keys %INC;
138 # fall back to basename as last resort
139 $rmap_inc{$full} // (split('/', $full))[-1];
146 my ($ctx, $err) = @_;
147 my $u = $ctx->{-inbox}->base_url($ctx->{env}) . '_/text/help/';
148 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
149 $err =~ s!(\S+)!path2inc($1)!sge;
150 $err = ascii_html($err);
151 "\nBad query: <b>$err</b>\n" .
152 qq{See <a\nhref="$u">$u</a> for help on using search};
156 my ($mset, $q, $ctx) = @_;
157 my $m = $q->qs_html(x => 'm', r => undef, t => undef);
158 my $rv = qq{<form\naction="?$m"\nmethod="post"><pre>};
159 my $initial_q = $ctx->{-uxs_retried};
160 if (defined $initial_q) {
161 my $rewritten = $q->{'q'};
162 utf8::decode($initial_q);
163 utf8::decode($rewritten);
164 $initial_q = ascii_html($initial_q);
165 $rewritten = ascii_html($rewritten);
166 $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
167 $rv .= " returned no results, used:\n";
168 $rv .= " <b>$rewritten</b>\n instead\n\n";
171 $rv .= 'Search results ordered by [';
173 my $d = $q->qs_html(r => 0);
174 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
176 my $d = $q->qs_html(r => 1);
177 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
184 my $t = $q->qs_html(x => 't');
185 $rv .= qq{<b>summary</b>|<a\nhref="?$t">nested</a>}
186 } elsif ($q->{x} eq 't') {
187 my $s = $q->qs_html(x => '');
188 $rv .= qq{<a\nhref="?$s">summary</a>|<b>nested</b>};
190 my $A = $q->qs_html(x => 'A', r => undef);
191 $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
192 if ($ctx->{-inbox}->search->has_threadid) {
193 $rv .= qq{\n\t\t\tdownload mbox.gz: } .
194 # we set name=z w/o using it since it seems required for
195 # lynx (but works fine for w3m).
196 qq{<input\ntype=submit\nname=z\n} .
197 q{value="results only"/>} .
198 qq{|<input\ntype=submit\nname=x\n} .
199 q{value="full threads"/>};
200 } else { # BOFH needs to --reindex
201 $rv .= qq{\n\t\t\t\t\t\tdownload: } .
202 qq{<input\ntype=submit\nname=z\nvalue="mbox.gz"/>}
204 $rv .= qq{</pre></form><pre>};
209 my $total = $mset->get_matches_estimated;
211 my $rv = '</pre><hr><pre id=t>';
213 my $off = $o < 0 ? -($o + 1) : $o;
214 my $end = $off + $mset->size;
218 $rv .= "Results $beg-$end of $total";
219 $rv .= ' (estimated)' if $end != $total;
221 $rv .= "No more results, only $total";
223 my ($next, $join, $prev);
225 if ($o >= 0) { # sort descending
228 $next = $q->qs_html(o => $n, l => $l);
231 $join = $n < $total ? '/' : ' ';
233 $prev = $q->qs_html(o => ($p > 0 ? $p : 0));
235 } else { # o < 0, sort ascending
239 $next = $q->qs_html(o => $n, l => $l);
242 $join = -$n < $total ? '/' : ' ';
244 $prev = $q->qs_html(o => ($p < 0 ? $p : 0));
248 $rv .= qq{ <a\nhref="?$next"\nrel=next>next</a>} if $next;
249 $rv .= $join if $join;
250 $rv .= qq{<a\nhref="?$prev"\nrel=prev>prev</a>} if $prev;
252 my $rev = $q->qs_html(o => $o < 0 ? 0 : -1);
253 $rv .= qq{ | <a\nhref="?$rev">reverse results</a></pre>};
258 (eval { $b->topmost->{pct} } // 0) <=>
259 (eval { $a->topmost->{pct} } // 0)
264 # Capped at "99%" since "100%" takes an extra column in the
265 # thread skeleton view. <xapian/mset.h> says the value isn't
266 # very meaningful, anyways.
267 my $n = $_[0]->get_percent;
272 my ($ctx, $mset, $q) = @_;
273 my $ibx = $ctx->{-inbox};
274 my $nshard = $ibx->search->{nshard} // 1;
275 my %pct = map { mdocid($nshard, $_) => get_pct($_) } $mset->items;
276 my $msgs = $ibx->over->get_all(keys %pct);
277 $_->{pct} = $pct{$_->{num}} for @$msgs;
279 my $rootset = PublicInbox::SearchThread::thread($msgs,
280 $r ? \&sort_relevance : \&PublicInbox::View::sort_ds,
282 my $skel = search_nav_bot($mset, $q). "<pre>";
284 $ctx->{anchor_idx} = 1;
285 $ctx->{cur_level} = 0;
286 $ctx->{skel} = \$skel;
287 $ctx->{mapping} = {};
288 $ctx->{searchview} = 1;
289 $ctx->{prev_attr} = '';
290 $ctx->{prev_level} = 0;
291 $ctx->{s_nr} = scalar(@$msgs).'+ results';
293 # reduce hash lookups in skel_dump
294 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
295 PublicInbox::View::walk_thread($rootset, $ctx,
296 \&PublicInbox::View::pre_thread);
298 @$msgs = reverse @$msgs if $r;
299 $ctx->{msgs} = $msgs;
300 PublicInbox::WwwStream::aresponse($ctx, 200, \&mset_thread_i);
303 # callback for PublicInbox::WwwStream::getline
305 my ($ctx, $eml) = @_;
306 $ctx->zmore($ctx->html_top) if exists $ctx->{-html_tip};
307 $eml and return PublicInbox::View::eml_entry($ctx, $eml);
308 my $smsg = shift @{$ctx->{msgs}} or
309 $ctx->zmore(${delete($ctx->{skel})});
317 $qh = ascii_html($qh);
318 $ctx->{-q_value_html} = $qh;
319 $ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
320 $ctx->{-title_html} = "$qh - search results";
322 $extra .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
323 if (my $x = $q->{x}) {
325 $extra .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
327 $ctx->{-extra_form_html} = $extra;
331 my ($cb, $mset, $q, $ctx) = @_;
332 $ctx->{ids} = $ctx->{-inbox}->search->mset_to_artnums($mset);
333 $ctx->{search_query} = $q; # used by WwwAtomStream::atom_header
334 PublicInbox::WwwAtomStream->response($ctx, 200, \&adump_i);
337 # callback for PublicInbox::WwwAtomStream::getline
340 while (my $num = shift @{$ctx->{ids}}) {
341 my $smsg = eval { $ctx->{-inbox}->over->get_art($num) } or next;