X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchView.pm;h=30a310c9f58ceede52857d3b07d23668eaedcf6b;hb=e158d56a54d3c6d4890aa6ac4caa28a834279af0;hp=fbef4116e9623f2838f17cf0c4fc31c9ff31acaf;hpb=5c5dfb7d5558ff248477fa57aef08e2a8bbd9800;p=public-inbox.git diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index fbef4116..30a310c9 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -8,12 +8,14 @@ use warnings; use PublicInbox::SearchMsg; use PublicInbox::Hval qw/ascii_html/; use PublicInbox::View; -use PublicInbox::MID qw(mid2path mid_mime); +use PublicInbox::MID qw(mid2path mid_mime mid_clean); use Email::MIME; require PublicInbox::Git; require PublicInbox::Thread; our $LIM = 50; +sub noop {} + sub sres_top_html { my ($ctx) = @_; my $q = PublicInbox::SearchQuery->new($ctx->{qp}); @@ -27,44 +29,44 @@ sub sres_top_html { relevance => $q->{r}, }; my ($mset, $total); - eval { - $mset = $ctx->{srch}->query($q->{q}, $opts); + $mset = $ctx->{srch}->query($q->{'q'}, $opts); $total = $mset->get_matches_estimated; }; my $err = $@; - my $res = html_start($q, $ctx) . '
';
+	ctx_prepare($q, $ctx);
+	my $cb;
 	if ($err) {
 		$code = 400;
-		$res .= err_txt($ctx, $err) . "

" . foot($ctx);
+		$ctx->{-html_tip} = '
'.err_txt($ctx, $err).'

'; + $cb = *noop; } elsif ($total == 0) { $code = 404; - $res .= "\n\n[No results found]

".foot($ctx);
+		$ctx->{-html_tip} = "
\n[No results found]

"; + $cb = *noop; } else { my $x = $q->{x}; return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A'); - $res .= search_nav_top($mset, $q) . "\n\n"; + $ctx->{-html_tip} = search_nav_top($mset, $q) . "\n\n"; if ($x eq 't') { - return sub { tdump($_[0], $res, $mset, $q, $ctx) }; + $cb = mset_thread($ctx, $mset, $q); + } else { + $cb = mset_summary($ctx, $mset, $q); } - dump_mset(\$res, $mset); - $res .= '
' . search_nav_bot($mset, $q) . - "\n\n" . foot($ctx); } - - $res .= ""; - [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]]; + PublicInbox::WwwStream->response($ctx, $code, $cb); } # display non-threaded search results similar to what users expect from # regular WWW search engines: -sub dump_mset { - my ($res, $mset) = @_; +sub mset_summary { + my ($ctx, $mset, $q) = @_; my $total = $mset->get_matches_estimated; my $pad = length("$total"); my $pfx = ' ' x $pad; + my $res = \($ctx->{-html_tip}); foreach my $m ($mset->items) { my $rank = sprintf("%${pad}d", $m->get_rank + 1); my $pct = $m->get_percent; @@ -77,6 +79,8 @@ sub dump_mset { $s . "\n"; $$res .= "$pfx - by $f @ $ts UTC [$pct%]\n\n"; } + $$res .= search_nav_bot($mset, $q); + *noop; } sub err_txt { @@ -85,14 +89,14 @@ sub err_txt { $u = PublicInbox::Hval::prurl($ctx->{cgi}->{env}, $u); $err =~ s/^\s*Exception:\s*//; # bad word to show users :P $err = ascii_html($err); - "\n\nBad query: $err\n" . + "\nBad query: $err\n" . qq{See $u for Xapian query syntax}; } sub search_nav_top { my ($mset, $q) = @_; - my $rv = "Search results ordered by ["; + my $rv = "
Search results ordered by [";
 	if ($q->{r}) {
 		my $d = $q->qs_html(r => 0);
 		$rv .= qq{date|relevance};
@@ -122,7 +126,7 @@ 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 = "

Results $beg-$end of $total";
 	my $n = $o + $LIM;
 
 	if ($n < $total) {
@@ -135,13 +139,11 @@ sub search_nav_bot {
 		my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
 		$rv .= qq{prev};
 	}
-	$rv;
+	$rv .= '
'; } -sub tdump { - my ($cb, $res, $mset, $q, $ctx) = @_; - my $fh = $cb->([200, ['Content-Type'=>'text/html; charset=UTF-8']]); - $fh->write($res .= '
'); +sub mset_thread { + my ($ctx, $mset, $q) = @_; my %pct; my @m = map { my $i = $_; @@ -163,58 +165,51 @@ sub tdump { } else { # order by time (default for threaded view) $th->order(*PublicInbox::View::sort_ts); } - 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; - PublicInbox::View::walk_thread($th, $state, + my $skel = search_nav_bot($mset, $q). "
";
+	my $inbox = $ctx->{-inbox};
+	$ctx->{-upfx} = '';
+	$ctx->{anchor_idx} = 1;
+	$ctx->{cur_level} = 0;
+	$ctx->{dst} = \$skel;
+	$ctx->{mapping} = {};
+	$ctx->{pct} = \%pct;
+	$ctx->{prev_attr} = '';
+	$ctx->{prev_level} = 0;
+	$ctx->{seen} = {};
+
+	PublicInbox::View::walk_thread($th, $ctx,
 		*PublicInbox::View::pre_thread);
 
-	PublicInbox::View::thread_entry($state, $_, 0) for @m;
-
-	$fh->write(search_nav_bot($mset, $q). "\n\n" . $skel . "\n" .
-			foot($ctx). '
'); - - $fh->close; -} - -sub foot { - my ($ctx) = @_; - my $foot = $ctx->{footer} || ''; - qq{Back to index.\n$foot}; + my $msgs = \@m; + my $mime; + sub { + return unless $msgs; + while ($mime = shift @$msgs) { + my $mid = mid_clean(mid_mime($mime)); + $mime = $inbox->msg_by_mid($mid) and last; + } + if ($mime) { + $mime = Email::MIME->new($mime); + return PublicInbox::View::index_entry($mime, $ctx); + } + $msgs = undef; + $skel .= "\n"; + }; } -sub html_start { +sub ctx_prepare { my ($q, $ctx) = @_; my $qh = ascii_html($q->{'q'}); - my $A = $q->qs_html(x => 'A', r => undef); - my $res = '' . PublicInbox::Hval::STYLE . - "$qh - search results" . - qq{! . - qq{} . - qq{}; - - $res .= qq{} if $q->{r}; + $ctx->{-q_value_html} = $qh; + $ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef); + $ctx->{-title_html} = "$qh - search results"; + my $extra = ''; + $extra .= qq{} if $q->{r}; if (my $x = $q->{x}) { $x = ascii_html($x); - $res .= qq{}; + $extra .= qq{}; } - - $res .= qq{}; + $ctx->{-extra_form_html} = $extra; } sub adump {