]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchView.pm
www: implement hybrid flat+thread conversation view
[public-inbox.git] / lib / PublicInbox / SearchView.pm
index a51acbb4eaaa8933a8211216bbd8ee18827f21c4..fbef4116e9623f2838f17cf0c4fc31c9ff31acaf 100644 (file)
@@ -1,19 +1,22 @@
 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+#
+# Displays search results for the web interface
 package PublicInbox::SearchView;
 use strict;
 use warnings;
 use PublicInbox::SearchMsg;
-use PublicInbox::Hval;
+use PublicInbox::Hval qw/ascii_html/;
 use PublicInbox::View;
-use PublicInbox::MID qw(mid2path mid_clean);
+use PublicInbox::MID qw(mid2path mid_mime);
 use Email::MIME;
-use POSIX qw/strftime/;
+require PublicInbox::Git;
+require PublicInbox::Thread;
 our $LIM = 50;
 
 sub sres_top_html {
        my ($ctx) = @_;
-       my $q = PublicInbox::SearchQuery->new($ctx->{cgi});
+       my $q = PublicInbox::SearchQuery->new($ctx->{qp});
        my $code = 200;
 
        # double the limit for expanded views:
@@ -30,10 +33,10 @@ sub sres_top_html {
                $total = $mset->get_matches_estimated;
        };
        my $err = $@;
-       my $res = html_start($q, $ctx) . PublicInbox::View::PRE_WRAP;
+       my $res = html_start($q, $ctx) . '<pre>';
        if ($err) {
                $code = 400;
-               $res .= err_txt($err) . "</pre><hr /><pre>" . foot($ctx);
+               $res .= err_txt($ctx, $err) . "</pre><hr /><pre>" . foot($ctx);
        } elsif ($total == 0) {
                $code = 404;
                $res .= "\n\n[No results found]</pre><hr /><pre>".foot($ctx);
@@ -41,19 +44,21 @@ sub sres_top_html {
                my $x = $q->{x};
                return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A');
 
-               $res .= search_nav_top($mset, $q);
+               $res .= search_nav_top($mset, $q) . "\n\n";
                if ($x eq 't') {
                        return sub { tdump($_[0], $res, $mset, $q, $ctx) };
                }
-               $res .= "\n\n";
                dump_mset(\$res, $mset);
-               $res .= search_nav_bot($mset, $q) . "\n\n" . foot($ctx);
+               $res .= '</pre>' . search_nav_bot($mset, $q) .
+                       "\n\n" . foot($ctx);
        }
 
        $res .= "</pre></body></html>";
        [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
 }
 
+# display non-threaded search results similar to what users expect from
+# regular WWW search engines:
 sub dump_mset {
        my ($res, $mset) = @_;
 
@@ -64,22 +69,22 @@ sub dump_mset {
                my $rank = sprintf("%${pad}d", $m->get_rank + 1);
                my $pct = $m->get_percent;
                my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
-               my $s = PublicInbox::Hval->new_oneline($smsg->subject);
-               my $f = $smsg->from_name;
-               $f = PublicInbox::Hval->new_oneline($f)->as_html;
-               my $d = strftime('%Y-%m-%d %H:%M', gmtime($smsg->ts));
+               my $s = ascii_html($smsg->subject);
+               my $f = ascii_html($smsg->from_name);
+               my $ts = PublicInbox::View::fmt_ts($smsg->ts);
                my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
                $$res .= qq{$rank. <b><a\nhref="$mid/">}.
-                       $s->as_html . "</a></b>\n";
-               $$res .= "$pfx  - by $f @ $d UTC [$pct%]\n\n";
+                       $s . "</a></b>\n";
+               $$res .= "$pfx  - by $f @ $ts UTC [$pct%]\n\n";
        }
 }
 
 sub err_txt {
-       my ($err) = @_;
-       my $u = 'http://xapian.org/docs/queryparser.html';
+       my ($ctx, $err) = @_;
+       my $u = '//xapian.org/docs/queryparser.html';
+       $u = PublicInbox::Hval::prurl($ctx->{cgi}->{env}, $u);
        $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
-       $err = PublicInbox::Hval->new_oneline($err)->as_html;
+       $err = ascii_html($err);
        "\n\nBad query: <b>$err</b>\n" .
                qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
 }
@@ -101,16 +106,13 @@ sub search_nav_top {
        my $x = $q->{x};
        if ($x eq '') {
                my $t = $q->qs_html(x => 't');
-               $rv .= qq{<b>summary</b>|};
-               $rv .= qq{<a\nhref="?$t">threaded</a>}
+               $rv .= qq{<b>summary</b>|<a\nhref="?$t">threaded</a>}
        } elsif ($q->{x} eq 't') {
                my $s = $q->qs_html(x => '');
-               $rv .= qq{<a\nhref="?$s">summary</a>|};
-               $rv .= qq{<b>threaded</b>};
+               $rv .= qq{<a\nhref="?$s">summary</a>|<b>threaded</b>};
        }
        my $A = $q->qs_html(x => 'A', r => undef);
-       $rv .= qq{|<a\nhref="?$A">Atom</a>};
-       $rv .= ']';
+       $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
 }
 
 sub search_nav_bot {
@@ -125,13 +127,13 @@ sub search_nav_bot {
 
        if ($n < $total) {
                my $qs = $q->qs_html(o => $n);
-               $rv .= qq{, <a\nhref="?$qs">next</a>}
+               $rv .= qq{, <a\nhref="?$qs"\nrel=next>next</a>}
        }
        if ($o > 0) {
                $rv .= $n < $total ? '/' : ',      ';
                my $p = $o - $LIM;
                my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
-               $rv .= qq{<a\nhref="?$qs">prev</a>};
+               $rv .= qq{<a\nhref="?$qs"\nrel=prev>prev</a>};
        }
        $rv;
 }
@@ -139,7 +141,7 @@ sub search_nav_bot {
 sub tdump {
        my ($cb, $res, $mset, $q, $ctx) = @_;
        my $fh = $cb->([200, ['Content-Type'=>'text/html; charset=UTF-8']]);
-       $fh->write($res);
+       $fh->write($res .= '</pre>');
        my %pct;
        my @m = map {
                my $i = $_;
@@ -149,62 +151,46 @@ sub tdump {
                $m;
        } ($mset->items);
 
-       require PublicInbox::Thread;
        my $th = PublicInbox::Thread->new(@m);
-       {
-               no warnings 'once';
-               $Mail::Thread::nosubject = 0;
-       }
        $th->thread;
-       if ($q->{r}) {
+       if ($q->{r}) { # order by relevance
                $th->order(sub {
                        sort { (eval { $pct{$b->topmost->messageid} } || 0)
                                        <=>
                                (eval { $pct{$a->topmost->messageid} } || 0)
                        } @_;
                });
-       } else {
-               no warnings 'once';
-               $th->order(*PublicInbox::View::rsort_ts);
+       } else { # order by time (default for threaded view)
+               $th->order(*PublicInbox::View::sort_ts);
        }
-
-       require PublicInbox::GitCatFile;
-       my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
-       my $state = { ctx => $ctx, anchor_idx => 0, pct => \%pct };
+       my $skel = '';
+       my $state = {
+               -inbox => $ctx->{-inbox},
+               anchor_idx => 1,
+               ctx => $ctx,
+               cur_level => 0,
+               dst => \$skel,
+               fh => $fh,
+               mapping => {},
+               pct => \%pct,
+               prev_attr => '',
+               prev_level => 0,
+               seen => {},
+               srch => $ctx->{srch},
+               upfx => './',
+       };
        $ctx->{searchview} = 1;
-       tdump_ent($fh, $git, $state, $_, 0) for $th->rootset;
-       $git = undef;
-       Email::Address->purge_cache;
+       PublicInbox::View::walk_thread($th, $state,
+               *PublicInbox::View::pre_thread);
+
+       PublicInbox::View::thread_entry($state, $_, 0) for @m;
 
-       $fh->write(search_nav_bot($mset, $q). "\n\n" .
+       $fh->write(search_nav_bot($mset, $q). "\n\n" . $skel . "\n" .
                        foot($ctx). '</pre></body></html>');
 
        $fh->close;
 }
 
-sub tdump_ent {
-       my ($fh, $git, $state, $node, $level) = @_;
-       return unless $node;
-       my $mime = $node->message;
-
-       if ($mime) {
-               # lazy load the full message from mini_mime:
-               my $mid = $mime->header('Message-ID');
-               $mime = eval {
-                       my $path = mid2path(mid_clean($mid));
-                       Email::MIME->new($git->cat_file('HEAD:'.$path));
-               };
-       }
-       if ($mime) {
-               PublicInbox::View::index_entry($fh, $mime, $level, $state);
-       } else {
-               my $mid = $node->messageid;
-               $fh->write(PublicInbox::View::ghost_table('', $mid, $level));
-       }
-       tdump_ent($fh, $git, $state, $node->child, $level + 1);
-       tdump_ent($fh, $git, $state, $node->next, $level);
-}
-
 sub foot {
        my ($ctx) = @_;
        my $foot = $ctx->{footer} || '';
@@ -213,11 +199,10 @@ sub foot {
 
 sub html_start {
        my ($q, $ctx) = @_;
-       my $query = PublicInbox::Hval->new_oneline($q->{q});
-
-       my $qh = $query->as_html;
+       my $qh = ascii_html($q->{'q'});
        my $A = $q->qs_html(x => 'A', r => undef);
-       my $res = "<html><head><title>$qh - search results</title>" .
+       my $res = '<html><head>' . PublicInbox::Hval::STYLE .
+               "<title>$qh - search results</title>" .
                qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
                qq!href="?$A"\ntype="application/atom+xml"/></head>! .
                qq{<body><form\naction="">} .
@@ -225,8 +210,8 @@ sub html_start {
 
        $res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
        if (my $x = $q->{x}) {
-               my $xh = PublicInbox::Hval->new_oneline($x)->as_html;
-               $res .= qq{<input\ntype=hidden\nname=x\nvalue="$xh" />};
+               $x = ascii_html($x);
+               $res .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
        }
 
        $res .= qq{<input\ntype=submit\nvalue=search /></form>};
@@ -235,10 +220,9 @@ sub html_start {
 sub adump {
        my ($cb, $mset, $q, $ctx) = @_;
        my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
-       require PublicInbox::GitCatFile;
-       my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
+       my $ibx = $ctx->{-inbox};
        my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
-       my $x = PublicInbox::Hval->new_oneline($q->{q})->as_html;
+       my $x = ascii_html($q->{'q'});
        $x = qq{$x - search results};
        $feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
        $feed_opts->{url} .= '?'. $q->qs_html(x => undef);
@@ -248,9 +232,9 @@ sub adump {
        for ($mset->items) {
                $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
                $x = mid2path($x);
-               PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
+               my $s = PublicInbox::Feed::feed_entry($feed_opts, $x, $ibx);
+               $fh->write($s) if defined $s;
        }
-       $git = undef;
        PublicInbox::Feed::end_feed($fh);
 }
 
@@ -260,12 +244,13 @@ use warnings;
 use PublicInbox::Hval;
 
 sub new {
-       my ($class, $cgi) = @_;
-       my $r = $cgi->param('r');
+       my ($class, $qp) = @_;
+
+       my $r = $qp->{r};
        bless {
-               q => $cgi->param('q'),
-               x => $cgi->param('x') || '',
-               o => int($cgi->param('o') || 0) || 0,
+               q => $qp->{'q'},
+               x => $qp->{x} || '',
+               o => (($qp->{o} || '0') =~ /(\d+)/),
                r => (defined $r && $r ne '0'),
        }, $class;
 }
@@ -281,7 +266,7 @@ sub qs_html {
                $self = $tmp;
        }
 
-       my $q = PublicInbox::Hval->new($self->{q})->as_href;
+       my $q = PublicInbox::Hval->new($self->{'q'})->as_href;
        $q =~ s/%20/+/g; # improve URL readability
        my $qs = "q=$q";