#
# Displays search results for the web interface
package PublicInbox::SearchView;
use strict;
use warnings;
+use URI::Escape qw(uri_unescape uri_escape);
use PublicInbox::SearchMsg;
-use PublicInbox::Hval qw/ascii_html/;
+use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
use PublicInbox::View;
use PublicInbox::WwwAtomStream;
-use PublicInbox::MID qw(mid2path mid_mime mid_clean mid_escape);
+use PublicInbox::MID qw(mid2path mid_mime mid_clean mid_escape MID_ESC);
use PublicInbox::MIME;
require PublicInbox::Git;
require PublicInbox::SearchThread;
-our $LIM = 50;
+our $LIM = 200;
sub noop {}
+sub mbox_results {
+ my ($ctx) = @_;
+ my $q = PublicInbox::SearchQuery->new($ctx->{qp});
+ my $x = $q->{x};
+ return PublicInbox::Mbox::mbox_all($ctx, $q->{'q'}) if $x eq 'm';
+ sres_top_html($ctx);
+}
+
sub sres_top_html {
my ($ctx) = @_;
my $q = PublicInbox::SearchQuery->new($ctx->{qp});
- my $code = 200;
+ my $x = $q->{x};
+ my $query = $q->{'q'};
+ my $code = 200;
# double the limit for expanded views:
my $opts = {
limit => $LIM,
@@ -29,27 +40,34 @@ sub sres_top_html {
mset => 1,
relevance => $q->{r},
};
- my ($mset, $total);
+ my ($mset, $total, $err, $cb);
+retry:
eval {
- $mset = $ctx->{srch}->query($q->{'q'}, $opts);
+ $mset = $ctx->{srch}->query($query, $opts);
$total = $mset->get_matches_estimated;
};
- my $err = $@;
+ $err = $@;
ctx_prepare($q, $ctx);
- my $cb;
if ($err) {
$code = 400;
$ctx->{-html_tip} = ''.err_txt($ctx, $err).'
';
$cb = *noop;
} elsif ($total == 0) {
+ if (defined($ctx->{-uxs_retried})) {
+ # undo retry damage:
+ $q->{'q'} = $ctx->{-uxs_retried};
+ } elsif (index($q->{'q'}, '%') >= 0) {
+ $ctx->{-uxs_retried} = $q->{'q'};
+ $q->{'q'} = uri_unescape($q->{'q'});
+ goto retry;
+ }
$code = 404;
$ctx->{-html_tip} = "\n[No results found]
";
$cb = *noop;
} else {
- my $x = $q->{x};
return adump($_[0], $mset, $q, $ctx) if $x eq 'A';
- $ctx->{-html_tip} = search_nav_top($mset, $q) . "\n\n";
+ $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx);
if ($x eq 't') {
$cb = mset_thread($ctx, $mset, $q);
} else {
@@ -70,7 +88,7 @@ sub load_doc_retry {
}
}
-# display non-threaded search results similar to what users expect from
+# display non-nested search results similar to what users expect from
# regular WWW search engines:
sub mset_summary {
my ($ctx, $mset, $q) = @_;
@@ -80,6 +98,8 @@ sub mset_summary {
my $pfx = ' ' x $pad;
my $res = \($ctx->{-html_tip});
my $srch = $ctx->{srch};
+ my $ibx = $ctx->{-inbox};
+ my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
foreach my $m ($mset->items) {
my $rank = sprintf("%${pad}d", $m->get_rank + 1);
my $pct = $m->get_percent;
@@ -93,11 +113,15 @@ sub mset_summary {
}
my $s = ascii_html($smsg->subject);
my $f = ascii_html($smsg->from_name);
- my $ts = PublicInbox::View::fmt_ts($smsg->ts);
+ if ($obfs_ibx) {
+ obfuscate_addrs($obfs_ibx, $s);
+ obfuscate_addrs($obfs_ibx, $f);
+ }
+ my $date = PublicInbox::View::fmt_ts($smsg->ds);
my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->{href};
$$res .= qq{$rank. }.
$s . "\n";
- $$res .= "$pfx - by $f @ $ts UTC [$pct%]\n\n";
+ $$res .= "$pfx - by $f @ $date UTC [$pct%]\n\n";
}
$$res .= search_nav_bot($mset, $q);
*noop;
@@ -113,9 +137,22 @@ sub err_txt {
}
sub search_nav_top {
- my ($mset, $q) = @_;
+ my ($mset, $q, $ctx) = @_;
+ my $m = $q->qs_html(x => 'm', r => undef);
+ my $rv = qq{};
}
sub search_nav_bot {
@@ -145,15 +184,21 @@ sub search_nav_bot {
my $o = $q->{o};
my $end = $o + $nr;
my $beg = $o + 1;
- my $rv = "
Results $beg-$end of $total";
+ my $rv = '
';
+ if ($beg <= $end) {
+ $rv .= "Results $beg-$end of $total";
+ $rv .= ' (estimated)' if $end != $total;
+ } else {
+ $rv .= "No more results, only $total";
+ }
my $n = $o + $LIM;
if ($n < $total) {
my $qs = $q->qs_html(o => $n);
- $rv .= qq{, next}
+ $rv .= qq{ next}
}
if ($o > 0) {
- $rv .= $n < $total ? '/' : ', ';
+ $rv .= $n < $total ? '/' : ' ';
my $p = $o - $LIM;
my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
$rv .= qq{prev};
@@ -173,15 +218,17 @@ sub sort_relevance {
sub mset_thread {
my ($ctx, $mset, $q) = @_;
my %pct;
- my $msgs = $ctx->{srch}->retry_reopen(sub { [ map {
+ my $srch = $ctx->{srch};
+ my $msgs = $srch->retry_reopen(sub { [ map {
my $i = $_;
my $smsg = PublicInbox::SearchMsg->load_doc($i->get_document);
$pct{$smsg->mid} = $i->get_percent;
$smsg;
} ($mset->items) ]});
-
+ my $r = $q->{r};
my $rootset = PublicInbox::SearchThread::thread($msgs,
- $q->{r} ? sort_relevance(\%pct) : *PublicInbox::View::sort_ts);
+ $r ? sort_relevance(\%pct) : *PublicInbox::View::sort_ds,
+ $srch);
my $skel = search_nav_bot($mset, $q). "";
my $inbox = $ctx->{-inbox};
$ctx->{-upfx} = '';
@@ -195,13 +242,16 @@ sub mset_thread {
$ctx->{seen} = {};
$ctx->{s_nr} = scalar(@$msgs).'+ results';
+ # reduce hash lookups in skel_dump
+ $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
PublicInbox::View::walk_thread($rootset, $ctx,
*PublicInbox::View::pre_thread);
+ @$msgs = reverse @$msgs if $r;
my $mime;
sub {
return unless $msgs;
- while ($mime = shift @$msgs) {
+ while ($mime = pop @$msgs) {
$mime = $inbox->msg_by_smsg($mime) and last;
}
if ($mime) {
@@ -216,7 +266,9 @@ sub mset_thread {
sub ctx_prepare {
my ($q, $ctx) = @_;
- my $qh = ascii_html($q->{'q'});
+ my $qh = $q->{'q'};
+ utf8::decode($qh);
+ $qh = ascii_html($qh);
$ctx->{-q_value_html} = $qh;
$ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
$ctx->{-title_html} = "$qh - search results";
@@ -248,8 +300,9 @@ sub adump {
package PublicInbox::SearchQuery;
use strict;
use warnings;
+use URI::Escape qw(uri_escape);
use PublicInbox::Hval;
-use PublicInbox::MID qw(mid_escape);
+use PublicInbox::MID qw(MID_ESC);
sub new {
my ($class, $qp) = @_;
@@ -274,7 +327,7 @@ sub qs_html {
$self = $tmp;
}
- my $q = mid_escape($self->{'q'});
+ my $q = uri_escape($self->{'q'}, MID_ESC);
$q =~ s/%20/+/g; # improve URL readability
my $qs = "q=$q";
@@ -285,7 +338,7 @@ sub qs_html {
$qs .= "&r";
}
if (my $x = $self->{x}) {
- $qs .= "&x=$x" if ($x eq 't' || $x eq 'A');
+ $qs .= "&x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm');
}
$qs;
}