1 # Copyright (C) 2015-2018 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;
23 my $q = PublicInbox::SearchQuery->new($ctx->{qp});
25 require PublicInbox::Mbox;
26 return PublicInbox::Mbox::mbox_all($ctx, $q->{'q'}) if $x eq 'm';
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:
53 my ($mset, $total, $err, $cb);
56 $mset = $srch->query($query, $opts);
57 $total = $mset->get_matches_estimated;
60 ctx_prepare($q, $ctx);
63 $ctx->{-html_tip} = '<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 $ctx->{-html_tip} = "<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);
82 $cb = mset_thread($ctx, $mset, $q);
84 $cb = mset_summary($ctx, $mset, $q);
87 PublicInbox::WwwStream->response($ctx, $code, $cb);
90 # allow undef for individual doc loads...
92 my ($srch, $mitem) = @_;
95 $srch->retry_reopen(sub {
96 PublicInbox::SearchMsg->load_doc($mitem->get_document)
101 # display non-nested search results similar to what users expect from
102 # regular WWW search engines:
104 my ($ctx, $mset, $q) = @_;
106 my $total = $mset->get_matches_estimated;
107 my $pad = length("$total");
108 my $pfx = ' ' x $pad;
109 my $res = \($ctx->{-html_tip});
110 my $ibx = $ctx->{-inbox};
111 my $srch = $ibx->search;
112 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
113 foreach my $m ($mset->items) {
114 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
115 my $pct = $m->get_percent;
116 my $smsg = load_doc_retry($srch, $m);
119 $m = "$m ".$m->get_docid . " expired\n";
120 $ctx->{env}->{'psgi.errors'}->print($m);
124 my $s = ascii_html($smsg->subject);
125 my $f = ascii_html($smsg->from_name);
127 obfuscate_addrs($obfs_ibx, $s);
128 obfuscate_addrs($obfs_ibx, $f);
130 my $date = PublicInbox::View::fmt_ts($smsg->ds);
131 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->{href};
132 $s = '(no subject)' if $s eq '';
133 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
135 $$res .= "$pfx - by $f @ $date UTC [$pct%]\n\n";
137 $$res .= search_nav_bot($mset, $q);
142 my ($ctx, $err) = @_;
143 my $u = $ctx->{-inbox}->base_url($ctx->{env}) . '_/text/help/';
144 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
145 $err = ascii_html($err);
146 "\nBad query: <b>$err</b>\n" .
147 qq{See <a\nhref="$u">$u</a> for help on using search};
151 my ($mset, $q, $ctx) = @_;
152 my $m = $q->qs_html(x => 'm', r => undef);
153 my $rv = qq{<form\naction="?$m"\nmethod="post"><pre>};
154 my $initial_q = $ctx->{-uxs_retried};
155 if (defined $initial_q) {
156 my $rewritten = $q->{'q'};
157 utf8::decode($initial_q);
158 utf8::decode($rewritten);
159 $initial_q = ascii_html($initial_q);
160 $rewritten = ascii_html($rewritten);
161 $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
162 $rv .= " returned no results, used:\n";
163 $rv .= " <b>$rewritten</b>\n instead\n\n";
166 $rv .= 'Search results ordered by [';
168 my $d = $q->qs_html(r => 0);
169 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
171 my $d = $q->qs_html(r => 1);
172 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
179 my $t = $q->qs_html(x => 't');
180 $rv .= qq{<b>summary</b>|<a\nhref="?$t">nested</a>}
181 } elsif ($q->{x} eq 't') {
182 my $s = $q->qs_html(x => '');
183 $rv .= qq{<a\nhref="?$s">summary</a>|<b>nested</b>};
185 my $A = $q->qs_html(x => 'A', r => undef);
186 $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
187 $rv .= qq{\n\t\t\t\t\t\tdownload: };
188 $rv .= qq{<input\ntype=submit\nvalue="mbox.gz"/></pre></form><pre>};
193 my $total = $mset->get_matches_estimated;
195 my $rv = '</pre><hr><pre id=t>';
197 my $off = $o < 0 ? -($o + 1) : $o;
198 my $end = $off + $mset->size;
202 $rv .= "Results $beg-$end of $total";
203 $rv .= ' (estimated)' if $end != $total;
205 $rv .= "No more results, only $total";
207 my ($next, $join, $prev);
209 if ($o >= 0) { # sort descending
212 $next = $q->qs_html(o => $n, l => $l);
215 $join = $n < $total ? '/' : ' ';
217 $prev = $q->qs_html(o => ($p > 0 ? $p : 0));
219 } else { # o < 0, sort ascending
223 $next = $q->qs_html(o => $n, l => $l);
226 $join = -$n < $total ? '/' : ' ';
228 $prev = $q->qs_html(o => ($p < 0 ? $p : 0));
232 $rv .= qq{ <a\nhref="?$next"\nrel=next>next</a>} if $next;
233 $rv .= $join if $join;
234 $rv .= qq{<a\nhref="?$prev"\nrel=prev>prev</a>} if $prev;
236 my $rev = $q->qs_html(o => $o < 0 ? 0 : -1);
237 $rv .= qq{ | <a\nhref="?$rev">reverse results</a></pre>};
243 [ sort { (eval { $pct->{$b->topmost->{id}} } || 0)
245 (eval { $pct->{$a->topmost->{id}} } || 0)
250 my ($ctx, $mset, $q) = @_;
252 my $ibx = $ctx->{-inbox};
253 my $msgs = $ibx->search->retry_reopen(sub { [ map {
255 my $smsg = PublicInbox::SearchMsg->load_doc($i->get_document);
256 $pct{$smsg->mid} = $i->get_percent;
258 } ($mset->items) ]});
260 my $rootset = PublicInbox::SearchThread::thread($msgs,
261 $r ? sort_relevance(\%pct) : *PublicInbox::View::sort_ds,
263 my $skel = search_nav_bot($mset, $q). "<pre>";
265 $ctx->{anchor_idx} = 1;
266 $ctx->{cur_level} = 0;
267 $ctx->{dst} = \$skel;
268 $ctx->{mapping} = {};
270 $ctx->{prev_attr} = '';
271 $ctx->{prev_level} = 0;
272 $ctx->{s_nr} = scalar(@$msgs).'+ results';
274 # reduce hash lookups in skel_dump
275 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
276 PublicInbox::View::walk_thread($rootset, $ctx,
277 *PublicInbox::View::pre_thread);
279 @$msgs = reverse @$msgs if $r;
283 while (my $m = pop @$msgs) {
284 $smsg = $ibx->smsg_mime($m) and last;
287 return PublicInbox::View::index_entry($smsg, $ctx,
299 $qh = ascii_html($qh);
300 $ctx->{-q_value_html} = $qh;
301 $ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
302 $ctx->{-title_html} = "$qh - search results";
304 $extra .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
305 if (my $x = $q->{x}) {
307 $extra .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
309 $ctx->{-extra_form_html} = $extra;
313 my ($cb, $mset, $q, $ctx) = @_;
314 my $ibx = $ctx->{-inbox};
315 my @items = $mset->items;
316 $ctx->{search_query} = $q;
317 my $srch = $ibx->search;
318 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
319 while (my $x = shift @items) {
320 $x = load_doc_retry($srch, $x);
321 $x = $ibx->smsg_mime($x) and return $x;
327 package PublicInbox::SearchQuery;
330 use URI::Escape qw(uri_escape);
331 use PublicInbox::Hval;
332 use PublicInbox::MID qw(MID_ESC);
335 my ($class, $qp) = @_;
338 my ($l) = (($qp->{l} || '') =~ /([0-9]+)/);
339 $l = $LIM if !$l || $l > $LIM;
343 o => (($qp->{o} || '0') =~ /(-?[0-9]+)/),
345 r => (defined $r && $r ne '0'),
350 my ($self, %over) = @_;
353 my $tmp = bless { %$self }, ref($self);
354 foreach my $k (keys %over) {
355 $tmp->{$k} = $over{$k};
360 my $q = uri_escape($self->{'q'}, MID_ESC);
361 $q =~ s/%20/+/g; # improve URL readability
364 if (my $o = $self->{o}) { # ignore o == 0
367 if (my $l = $self->{l}) {
368 $qs .= "&l=$l" unless $l == $LIM;
370 if (my $r = $self->{r}) {
373 if (my $x = $self->{x}) {
374 $qs .= "&x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm');