X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchView.pm;h=30a310c9f58ceede52857d3b07d23668eaedcf6b;hb=e158d56a54d3c6d4890aa6ac4caa28a834279af0;hp=8751fca29654e7d51cfd6b952dc25c98e75ccff2;hpb=5d6c424d03a4057b129f4e5e099e378376a63e85;p=public-inbox.git diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index 8751fca2..30a310c9 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -6,16 +6,19 @@ 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 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->{cgi}); + my $q = PublicInbox::SearchQuery->new($ctx->{qp}); my $code = 200; # double the limit for expanded views: @@ -26,70 +29,74 @@ 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($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); } -sub dump_mset { - my ($res, $mset) = @_; +# display non-threaded search results similar to what users expect from +# regular WWW search engines: +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; 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. }. - $s->as_html . "\n"; + $s . "\n"; $$res .= "$pfx - by $f @ $ts UTC [$pct%]\n\n"; } + $$res .= search_nav_bot($mset, $q); + *noop; } 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; - "\n\nBad query: $err\n" . + $err = ascii_html($err); + "\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};
@@ -103,15 +110,13 @@ sub search_nav_top {
 	my $x = $q->{x};
 	if ($x eq '') {
 		my $t = $q->qs_html(x => 't');
-		$rv .= qq{summary|};
-		$rv .= qq{threaded}
+		$rv .= qq{summary|threaded}
 	} elsif ($q->{x} eq 't') {
 		my $s = $q->qs_html(x => '');
-		$rv .= qq{summary|};
-		$rv .= qq{threaded};
+		$rv .= qq{summary|threaded};
 	}
 	my $A = $q->qs_html(x => 'A', r => undef);
-	$rv .= qq{|Atom]};
+	$rv .= qq{|Atom feed]};
 }
 
 sub search_nav_bot {
@@ -121,26 +126,24 @@ 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) {
 		my $qs = $q->qs_html(o => $n);
-		$rv .= qq{, next}
+		$rv .= qq{, next}
 	}
 	if ($o > 0) {
 		$rv .= $n < $total ? '/' : ',      ';
 		my $p = $o - $LIM;
 		my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
-		$rv .= qq{prev};
+		$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 = $_; @@ -150,93 +153,71 @@ 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); - } - - my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir}); - my $state = { ctx => $ctx, anchor_idx => 0, pct => \%pct }; - $ctx->{searchview} = 1; - tdump_ent($fh, $git, $state, $_, 0) for $th->rootset; - Email::Address->purge_cache; - - $fh->write(search_nav_bot($mset, $q). "\n\n" . - foot($ctx). '
'); - - $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)); - }; + } else { # order by time (default for threaded view) + $th->order(*PublicInbox::View::sort_ts); } - 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} || ''; - qq{Back to index.\n$foot}; + 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);
+
+	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 $query = PublicInbox::Hval->new_oneline($q->{q}); - - my $qh = $query->as_html; - my $A = $q->qs_html(x => 'A', r => undef); - my $res = "$qh - search results" . - qq{! . - qq{} . - qq{}; - - $res .= qq{} if $q->{r}; + my $qh = ascii_html($q->{'q'}); + $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}) { - my $xh = PublicInbox::Hval->new_oneline($x)->as_html; - $res .= qq{}; + $x = ascii_html($x); + $extra .= qq{}; } - - $res .= qq{}; + $ctx->{-extra_form_html} = $extra; } 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); @@ -246,7 +227,8 @@ 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; } PublicInbox::Feed::end_feed($fh); } @@ -257,12 +239,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; } @@ -278,7 +261,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";