1 # Copyright (C) 2015-2019 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::MID qw(MID_ESC);
14 use PublicInbox::MIME;
15 require PublicInbox::Git;
16 require PublicInbox::SearchThread;
24 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
26 require PublicInbox::Mbox;
27 return PublicInbox::Mbox::mbox_all($ctx, $q->{'q'}) if $x eq 'm';
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, $cb);
57 $mset = $srch->query($query, $opts);
58 $total = $mset->get_matches_estimated;
61 ctx_prepare($q, $ctx);
64 $ctx->{-html_tip} = '<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 $ctx->{-html_tip} = "<pre>\n[No results found]</pre><hr>";
79 return adump($_[0], $mset, $q, $ctx) if $x eq 'A';
81 $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx);
83 $cb = mset_thread($ctx, $mset, $q);
85 $cb = mset_summary($ctx, $mset, $q);
88 PublicInbox::WwwStream->response($ctx, $code, $cb);
91 # allow undef for individual doc loads...
93 my ($srch, $mitem) = @_;
96 $srch->retry_reopen(sub {
97 PublicInbox::SearchMsg->load_doc($mitem->get_document)
102 # display non-nested search results similar to what users expect from
103 # regular WWW search engines:
105 my ($ctx, $mset, $q) = @_;
107 my $total = $mset->get_matches_estimated;
108 my $pad = length("$total");
109 my $pfx = ' ' x $pad;
110 my $res = \($ctx->{-html_tip});
111 my $ibx = $ctx->{-inbox};
112 my $srch = $ibx->search;
113 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
114 foreach my $m ($mset->items) {
115 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
116 my $pct = $m->get_percent;
117 my $smsg = load_doc_retry($srch, $m);
120 $m = "$m ".$m->get_docid . " expired\n";
121 $ctx->{env}->{'psgi.errors'}->print($m);
125 my $s = ascii_html($smsg->subject);
126 my $f = ascii_html($smsg->from_name);
128 obfuscate_addrs($obfs_ibx, $s);
129 obfuscate_addrs($obfs_ibx, $f);
131 my $date = PublicInbox::View::fmt_ts($smsg->ds);
132 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->{href};
133 $s = '(no subject)' if $s eq '';
134 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
136 $$res .= "$pfx - by $f @ $date UTC [$pct%]\n\n";
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);
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 $rv .= qq{\n\t\t\t\t\t\tdownload: };
206 $rv .= qq{<input\ntype=submit\nvalue="mbox.gz"/></pre></form><pre>};
211 my $total = $mset->get_matches_estimated;
213 my $rv = '</pre><hr><pre id=t>';
215 my $off = $o < 0 ? -($o + 1) : $o;
216 my $end = $off + $mset->size;
220 $rv .= "Results $beg-$end of $total";
221 $rv .= ' (estimated)' if $end != $total;
223 $rv .= "No more results, only $total";
225 my ($next, $join, $prev);
227 if ($o >= 0) { # sort descending
230 $next = $q->qs_html(o => $n, l => $l);
233 $join = $n < $total ? '/' : ' ';
235 $prev = $q->qs_html(o => ($p > 0 ? $p : 0));
237 } else { # o < 0, sort ascending
241 $next = $q->qs_html(o => $n, l => $l);
244 $join = -$n < $total ? '/' : ' ';
246 $prev = $q->qs_html(o => ($p < 0 ? $p : 0));
250 $rv .= qq{ <a\nhref="?$next"\nrel=next>next</a>} if $next;
251 $rv .= $join if $join;
252 $rv .= qq{<a\nhref="?$prev"\nrel=prev>prev</a>} if $prev;
254 my $rev = $q->qs_html(o => $o < 0 ? 0 : -1);
255 $rv .= qq{ | <a\nhref="?$rev">reverse results</a></pre>};
261 [ sort { (eval { $pct->{$b->topmost->{id}} } || 0)
263 (eval { $pct->{$a->topmost->{id}} } || 0)
268 my ($ctx, $mset, $q) = @_;
270 my $ibx = $ctx->{-inbox};
271 my $msgs = $ibx->search->retry_reopen(sub { [ map {
273 my $smsg = PublicInbox::SearchMsg->load_doc($i->get_document);
274 $pct{$smsg->mid} = $i->get_percent;
276 } ($mset->items) ]});
278 my $rootset = PublicInbox::SearchThread::thread($msgs,
279 $r ? sort_relevance(\%pct) : *PublicInbox::View::sort_ds,
281 my $skel = search_nav_bot($mset, $q). "<pre>";
283 $ctx->{anchor_idx} = 1;
284 $ctx->{cur_level} = 0;
285 $ctx->{dst} = \$skel;
286 $ctx->{mapping} = {};
288 $ctx->{prev_attr} = '';
289 $ctx->{prev_level} = 0;
290 $ctx->{s_nr} = scalar(@$msgs).'+ results';
292 # reduce hash lookups in skel_dump
293 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
294 PublicInbox::View::walk_thread($rootset, $ctx,
295 *PublicInbox::View::pre_thread);
297 @$msgs = reverse @$msgs if $r;
301 while (my $m = pop @$msgs) {
302 $smsg = $ibx->smsg_mime($m) and last;
305 return PublicInbox::View::index_entry($smsg, $ctx,
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 my $ibx = $ctx->{-inbox};
333 my @items = $mset->items;
334 $ctx->{search_query} = $q;
335 my $srch = $ibx->search;
336 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
337 while (my $x = shift @items) {
338 $x = load_doc_retry($srch, $x);
339 $x = $ibx->smsg_mime($x) and return $x;
345 package PublicInbox::SearchQuery;
348 use URI::Escape qw(uri_escape);
349 use PublicInbox::Hval;
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');