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 uri_escape);
9 use PublicInbox::SearchMsg;
10 use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
11 use PublicInbox::View;
12 use PublicInbox::WwwAtomStream;
13 use PublicInbox::SearchThread;
21 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
23 require PublicInbox::Mbox;
24 return PublicInbox::Mbox::mbox_all($ctx, $q->{'q'}) if $x eq 'm';
30 my $srch = $ctx->{-inbox}->search or
31 return PublicInbox::WWW::need($ctx, 'Search');
32 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
34 my $query = $q->{'q'};
39 $o = -($o + 1); # so [-1] is the last element, like Perl lists
43 # double the limit for expanded views:
51 my ($mset, $total, $err, $cb);
54 $mset = $srch->query($query, $opts);
55 $total = $mset->get_matches_estimated;
58 ctx_prepare($q, $ctx);
61 $ctx->{-html_tip} = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
63 } elsif ($total == 0) {
64 if (defined($ctx->{-uxs_retried})) {
66 $q->{'q'} = $ctx->{-uxs_retried};
67 } elsif (index($q->{'q'}, '%') >= 0) {
68 $ctx->{-uxs_retried} = $q->{'q'};
69 $q->{'q'} = uri_unescape($q->{'q'});
73 $ctx->{-html_tip} = "<pre>\n[No results found]</pre><hr>";
76 return adump($_[0], $mset, $q, $ctx) if $x eq 'A';
78 $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx);
80 $cb = mset_thread($ctx, $mset, $q);
82 $cb = mset_summary($ctx, $mset, $q);
85 PublicInbox::WwwStream->response($ctx, $code, $cb);
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 $srch = $ibx->search;
99 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
100 foreach my $m ($mset->items) {
101 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
102 my $pct = get_pct($m);
103 my $smsg = PublicInbox::SearchMsg::from_mitem($m, $srch);
106 $m = "$m ".$m->get_docid . " expired\n";
107 $ctx->{env}->{'psgi.errors'}->print($m);
111 my $s = ascii_html($smsg->{subject});
112 my $f = ascii_html($smsg->{from_name});
114 obfuscate_addrs($obfs_ibx, $s);
115 obfuscate_addrs($obfs_ibx, $f);
117 my $date = PublicInbox::View::fmt_ts($smsg->{ds});
118 my $mid = PublicInbox::Hval->new_msgid($smsg->{mid})->{href};
119 $s = '(no subject)' if $s eq '';
120 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
122 $$res .= "$pfx - by $f @ $date UTC [$pct%]\n\n";
124 $$res .= search_nav_bot($mset, $q);
128 # shorten "/full/path/to/Foo/Bar.pm" to "Foo/Bar.pm" so error
129 # messages don't reveal FS layout info in case people use non-standard
133 if (my $short = $rmap_inc{$full}) {
135 } elsif (!scalar(keys %rmap_inc) && -e $full) {
136 %rmap_inc = map {; "$INC{$_}" => $_ } keys %INC;
137 # fall back to basename as last resort
138 $rmap_inc{$full} // (split('/', $full))[-1];
145 my ($ctx, $err) = @_;
146 my $u = $ctx->{-inbox}->base_url($ctx->{env}) . '_/text/help/';
147 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
148 $err =~ s!(\S+)!path2inc($1)!sge;
149 $err = ascii_html($err);
150 "\nBad query: <b>$err</b>\n" .
151 qq{See <a\nhref="$u">$u</a> for help on using search};
155 my ($mset, $q, $ctx) = @_;
156 my $m = $q->qs_html(x => 'm', r => undef);
157 my $rv = qq{<form\naction="?$m"\nmethod="post"><pre>};
158 my $initial_q = $ctx->{-uxs_retried};
159 if (defined $initial_q) {
160 my $rewritten = $q->{'q'};
161 utf8::decode($initial_q);
162 utf8::decode($rewritten);
163 $initial_q = ascii_html($initial_q);
164 $rewritten = ascii_html($rewritten);
165 $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
166 $rv .= " returned no results, used:\n";
167 $rv .= " <b>$rewritten</b>\n instead\n\n";
170 $rv .= 'Search results ordered by [';
172 my $d = $q->qs_html(r => 0);
173 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
175 my $d = $q->qs_html(r => 1);
176 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
183 my $t = $q->qs_html(x => 't');
184 $rv .= qq{<b>summary</b>|<a\nhref="?$t">nested</a>}
185 } elsif ($q->{x} eq 't') {
186 my $s = $q->qs_html(x => '');
187 $rv .= qq{<a\nhref="?$s">summary</a>|<b>nested</b>};
189 my $A = $q->qs_html(x => 'A', r => undef);
190 $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
191 $rv .= qq{\n\t\t\t\t\t\tdownload: };
192 $rv .= qq{<input\ntype=submit\nvalue="mbox.gz"/></pre></form><pre>};
197 my $total = $mset->get_matches_estimated;
199 my $rv = '</pre><hr><pre id=t>';
201 my $off = $o < 0 ? -($o + 1) : $o;
202 my $end = $off + $mset->size;
206 $rv .= "Results $beg-$end of $total";
207 $rv .= ' (estimated)' if $end != $total;
209 $rv .= "No more results, only $total";
211 my ($next, $join, $prev);
213 if ($o >= 0) { # sort descending
216 $next = $q->qs_html(o => $n, l => $l);
219 $join = $n < $total ? '/' : ' ';
221 $prev = $q->qs_html(o => ($p > 0 ? $p : 0));
223 } else { # o < 0, sort ascending
227 $next = $q->qs_html(o => $n, l => $l);
230 $join = -$n < $total ? '/' : ' ';
232 $prev = $q->qs_html(o => ($p < 0 ? $p : 0));
236 $rv .= qq{ <a\nhref="?$next"\nrel=next>next</a>} if $next;
237 $rv .= $join if $join;
238 $rv .= qq{<a\nhref="?$prev"\nrel=prev>prev</a>} if $prev;
240 my $rev = $q->qs_html(o => $o < 0 ? 0 : -1);
241 $rv .= qq{ | <a\nhref="?$rev">reverse results</a></pre>};
246 (eval { $b->topmost->{smsg}->{pct} } // 0) <=>
247 (eval { $a->topmost->{smsg}->{pct} } // 0)
252 # Capped at "99%" since "100%" takes an extra column in the
253 # thread skeleton view. <xapian/mset.h> says the value isn't
254 # very meaningful, anyways.
255 my $n = $_[0]->get_percent;
263 my $smsg = PublicInbox::SearchMsg::from_mitem($mi);
264 $smsg->{pct} = get_pct($mi);
270 my ($ctx, $mset, $q) = @_;
271 my $msgs = $ctx->{-inbox}->search->retry_reopen(\&load_msgs, $mset);
273 my $rootset = PublicInbox::SearchThread::thread($msgs,
274 $r ? \&sort_relevance : \&PublicInbox::View::sort_ds,
276 my $skel = search_nav_bot($mset, $q). "<pre>";
278 $ctx->{anchor_idx} = 1;
279 $ctx->{cur_level} = 0;
280 $ctx->{skel} = \$skel;
281 $ctx->{mapping} = {};
282 $ctx->{searchview} = 1;
283 $ctx->{prev_attr} = '';
284 $ctx->{prev_level} = 0;
285 $ctx->{s_nr} = scalar(@$msgs).'+ results';
287 # reduce hash lookups in skel_dump
288 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
289 PublicInbox::View::walk_thread($rootset, $ctx,
290 \&PublicInbox::View::pre_thread);
292 @$msgs = reverse @$msgs if $r;
293 $ctx->{msgs} = $msgs;
297 # callback for PublicInbox::WwwStream::getline
300 my $msgs = $ctx->{msgs} or return;
301 while (my $smsg = pop @$msgs) {
302 $ctx->{-inbox}->smsg_mime($smsg) or next;
303 return PublicInbox::View::index_entry($smsg, $ctx,
306 my ($skel) = delete @$ctx{qw(skel msgs)};
307 $$skel .= "\n</pre>";
314 $qh = ascii_html($qh);
315 $ctx->{-q_value_html} = $qh;
316 $ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
317 $ctx->{-title_html} = "$qh - search results";
319 $extra .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
320 if (my $x = $q->{x}) {
322 $extra .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
324 $ctx->{-extra_form_html} = $extra;
328 my ($cb, $mset, $q, $ctx) = @_;
329 $ctx->{items} = [ $mset->items ];
330 $ctx->{search_query} = $q; # used by WwwAtomStream::atom_header
331 $ctx->{srch} = $ctx->{-inbox}->search;
332 PublicInbox::WwwAtomStream->response($ctx, 200, \&adump_i);
335 # callback for PublicInbox::WwwAtomStream::getline
338 while (my $mi = shift @{$ctx->{items}}) {
340 PublicInbox::SearchMsg::from_mitem($mi, $ctx->{srch});
342 $ctx->{-inbox}->smsg_mime($smsg) and return $smsg;
346 package PublicInbox::SearchQuery;
349 use URI::Escape qw(uri_escape);
350 use PublicInbox::MID qw(MID_ESC);
353 my ($class, $qp) = @_;
356 my ($l) = (($qp->{l} || '') =~ /([0-9]+)/);
357 $l = $LIM if !$l || $l > $LIM;
361 o => (($qp->{o} || '0') =~ /(-?[0-9]+)/),
363 r => (defined $r && $r ne '0'),
368 my ($self, %over) = @_;
371 my $tmp = bless { %$self }, ref($self);
372 foreach my $k (keys %over) {
373 $tmp->{$k} = $over{$k};
378 my $q = uri_escape($self->{'q'}, MID_ESC);
379 $q =~ s/%20/+/g; # improve URL readability
382 if (my $o = $self->{o}) { # ignore o == 0
385 if (my $l = $self->{l}) {
386 $qs .= "&l=$l" unless $l == $LIM;
388 if (my $r = $self->{r}) {
391 if (my $x = $self->{x}) {
392 $qs .= "&x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm');