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;
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:
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);
[$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) = @_;
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 $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";
+ $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};
}
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;
}
$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);
}
-
- my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
my $state = {
ctx => $ctx,
anchor_idx => 0,
pct => \%pct,
- cur_level => 0
+ cur_level => 0,
+ -inbox => $ctx->{-inbox},
+ fh => $fh,
};
$ctx->{searchview} = 1;
- tdump_ent($fh, $git, $state, $_, 0) for $th->rootset;
- PublicInbox::View::thread_adj_level($fh, $state, 0);
- Email::Address->purge_cache;
+ PublicInbox::View::walk_thread($th, $state, *tdump_ent);
+ PublicInbox::View::thread_adj_level($state, 0);
$fh->write(search_nav_bot($mset, $q). "\n\n" .
foot($ctx). '</pre></body></html>');
}
sub tdump_ent {
- my ($fh, $git, $state, $node, $level) = @_;
- return unless $node;
+ my ($state, $level, $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));
- };
+ my $mid = mid_mime($mime);
+ $mime = eval { $state->{-inbox}->msg_by_mid($mid) } and
+ $mime = Email::MIME->new($mime);
}
if ($mime) {
- my $end =
- PublicInbox::View::thread_adj_level($fh, $state, $level);
- PublicInbox::View::index_entry($fh, $mime, $level, $state);
- $fh->write($end) if $end;
+ my $end = PublicInbox::View::thread_adj_level($state, $level);
+ PublicInbox::View::index_entry($mime, $level, $state);
+ $state->{fh}->write($end) if $end;
} else {
my $mid = $node->messageid;
- PublicInbox::View::ghost_flush($fh, $state, '', $mid, $level);
+ PublicInbox::View::ghost_flush($state, '', $mid, $level);
}
- tdump_ent($fh, $git, $state, $node->child, $level + 1);
- tdump_ent($fh, $git, $state, $node->next, $level);
}
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>' . PublicInbox::Hval::STYLE .
"<title>$qh - search results</title>" .
$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>};
sub adump {
my ($cb, $mset, $q, $ctx) = @_;
my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
- my $git = $ctx->{git} ||= PublicInbox::Git->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);
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;
}
PublicInbox::Feed::end_feed($fh);
}
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;
}
$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";