]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchView.pm
treewide: "require" + "use" cleanup and docs
[public-inbox.git] / lib / PublicInbox / SearchView.pm
index 6f07279bd72c21d05aeb3527fc0af02b7785bcdc..0d2e71fda534d4819ae23813516d4df9b74f7c45 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Displays search results for the web interface
@@ -10,11 +10,9 @@ use PublicInbox::SearchMsg;
 use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
 use PublicInbox::View;
 use PublicInbox::WwwAtomStream;
-use PublicInbox::MID qw(MID_ESC);
-use PublicInbox::MIME;
-require PublicInbox::Git;
-require PublicInbox::SearchThread;
+use PublicInbox::SearchThread;
 our $LIM = 200;
+my %rmap_inc;
 
 sub noop {}
 
@@ -87,17 +85,6 @@ retry:
        PublicInbox::WwwStream->response($ctx, $code, $cb);
 }
 
-# allow undef for individual doc loads...
-sub load_doc_retry {
-       my ($srch, $mitem) = @_;
-
-       eval {
-               $srch->retry_reopen(sub {
-                       PublicInbox::SearchMsg->load_doc($mitem->get_document)
-               });
-       }
-}
-
 # display non-nested search results similar to what users expect from
 # regular WWW search engines:
 sub mset_summary {
@@ -112,8 +99,8 @@ sub mset_summary {
        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;
-               my $smsg = load_doc_retry($srch, $m);
+               my $pct = get_pct($m);
+               my $smsg = PublicInbox::SearchMsg::from_mitem($m, $srch);
                unless ($smsg) {
                        eval {
                                $m = "$m ".$m->get_docid . " expired\n";
@@ -138,10 +125,27 @@ sub mset_summary {
        *noop;
 }
 
+# shorten "/full/path/to/Foo/Bar.pm" to "Foo/Bar.pm" so error
+# messages don't reveal FS layout info in case people use non-standard
+# installation paths
+sub path2inc ($) {
+       my $full = $_[0];
+       if (my $short = $rmap_inc{$full}) {
+               return $short;
+       } elsif (!scalar(keys %rmap_inc) && -e $full) {
+               %rmap_inc = map {; "$INC{$_}" => $_ } keys %INC;
+               # fall back to basename as last resort
+               $rmap_inc{$full} // (split('/', $full))[-1];
+       } else {
+               $full;
+       }
+}
+
 sub err_txt {
        my ($ctx, $err) = @_;
        my $u = $ctx->{-inbox}->base_url($ctx->{env}) . '_/text/help/';
        $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
+       $err =~ s!(\S+)!path2inc($1)!sge;
        $err = ascii_html($err);
        "\nBad query: <b>$err</b>\n" .
                qq{See <a\nhref="$u">$u</a> for help on using search};
@@ -238,27 +242,36 @@ sub search_nav_bot {
 }
 
 sub sort_relevance {
-       my ($pct) = @_;
-       sub {
-               [ sort { (eval { $pct->{$b->topmost->{id}} } || 0)
-                               <=>
-                       (eval { $pct->{$a->topmost->{id}} } || 0)
-       } @{$_[0]} ] };
+       [ sort {
+               (eval { $b->topmost->{smsg}->{pct} } // 0) <=>
+               (eval { $a->topmost->{smsg}->{pct} } // 0)
+       } @{$_[0]} ]
+}
+
+sub get_pct ($) {
+       # Capped at "99%" since "100%" takes an extra column in the
+       # thread skeleton view.  <xapian/mset.h> says the value isn't
+       # very meaningful, anyways.
+       my $n = $_[0]->get_percent;
+       $n > 99 ? 99 : $n;
+}
+
+sub load_msgs {
+       my ($mset) = @_;
+       [ map {
+               my $mi = $_;
+               my $smsg = PublicInbox::SearchMsg::from_mitem($mi);
+               $smsg->{pct} = get_pct($mi);
+               $smsg;
+       } ($mset->items) ]
 }
 
 sub mset_thread {
        my ($ctx, $mset, $q) = @_;
-       my %pct;
-       my $ibx = $ctx->{-inbox};
-       my $msgs = $ibx->search->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 $msgs = $ctx->{-inbox}->search->retry_reopen(\&load_msgs, $mset);
        my $r = $q->{r};
        my $rootset = PublicInbox::SearchThread::thread($msgs,
-               $r ? sort_relevance(\%pct) : *PublicInbox::View::sort_ds,
+               $r ? \&sort_relevance : \&PublicInbox::View::sort_ds,
                $ctx);
        my $skel = search_nav_bot($mset, $q). "<pre>";
        $ctx->{-upfx} = '';
@@ -266,7 +279,7 @@ sub mset_thread {
        $ctx->{cur_level} = 0;
        $ctx->{dst} = \$skel;
        $ctx->{mapping} = {};
-       $ctx->{pct} = \%pct;
+       $ctx->{searchview} = 1;
        $ctx->{prev_attr} = '';
        $ctx->{prev_level} = 0;
        $ctx->{s_nr} = scalar(@$msgs).'+ results';
@@ -277,19 +290,21 @@ sub mset_thread {
                *PublicInbox::View::pre_thread);
 
        @$msgs = reverse @$msgs if $r;
-       sub {
-               return unless $msgs;
-               my $smsg;
-               while (my $m = pop @$msgs) {
-                       $smsg = $ibx->smsg_mime($m) and last;
-               }
-               if ($smsg) {
-                       return PublicInbox::View::index_entry($smsg, $ctx,
-                               scalar @$msgs);
-               }
-               $msgs = undef;
-               $skel .= "\n</pre>";
-       };
+       $ctx->{msgs} = $msgs;
+       \&mset_thread_i;
+}
+
+# callback for PublicInbox::WwwStream::getline
+sub mset_thread_i {
+       my ($nr, $ctx) = @_;
+       my $msgs = $ctx->{msgs} or return;
+       while (my $smsg = pop @$msgs) {
+               $ctx->{-inbox}->smsg_mime($smsg) or next;
+               return PublicInbox::View::index_entry($smsg, $ctx,
+                                                       scalar @$msgs);
+       }
+       my ($skel) = delete @$ctx{qw(dst msgs)};
+       $$skel .= "\n</pre>";
 }
 
 sub ctx_prepare {
@@ -311,24 +326,27 @@ sub ctx_prepare {
 
 sub adump {
        my ($cb, $mset, $q, $ctx) = @_;
-       my $ibx = $ctx->{-inbox};
-       my @items = $mset->items;
-       $ctx->{search_query} = $q;
-       my $srch = $ibx->search;
-       PublicInbox::WwwAtomStream->response($ctx, 200, sub {
-               while (my $x = shift @items) {
-                       $x = load_doc_retry($srch, $x);
-                       $x = $ibx->smsg_mime($x) and return $x;
-               }
-               return undef;
-       });
+       $ctx->{items} = [ $mset->items ];
+       $ctx->{search_query} = $q; # used by WwwAtomStream::atom_header
+       $ctx->{srch} = $ctx->{-inbox}->search;
+       PublicInbox::WwwAtomStream->response($ctx, 200, \&adump_i);
+}
+
+# callback for PublicInbox::WwwAtomStream::getline
+sub adump_i {
+       my ($ctx) = @_;
+       while (my $mi = shift @{$ctx->{items}}) {
+               my $smsg = eval {
+                       PublicInbox::SearchMsg::from_mitem($mi, $ctx->{srch});
+               } or next;
+               $ctx->{-inbox}->smsg_mime($smsg) and return $smsg;
+       }
 }
 
 package PublicInbox::SearchQuery;
 use strict;
 use warnings;
 use URI::Escape qw(uri_escape);
-use PublicInbox::Hval;
 use PublicInbox::MID qw(MID_ESC);
 
 sub new {