X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchView.pm;h=5a95a055eb3230c7cfd49db1317b0cfa11f5c9f9;hb=09e5f81e8259e7deffe0973b3547a5c77fbf3fc5;hp=80a2ff7c28e68ab9fe178919f481d24fcde8e708;hpb=8a770e2b8ec58bb3ac283aeaa97a4fddba3b03f9;p=public-inbox.git diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index 80a2ff7c..5a95a055 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -8,10 +8,11 @@ use warnings; use PublicInbox::SearchMsg; use PublicInbox::Hval qw/ascii_html/; use PublicInbox::View; -use PublicInbox::MID qw(mid2path mid_mime mid_clean); -use Email::MIME; +use PublicInbox::WwwAtomStream; +use PublicInbox::MID qw(mid2path mid_mime mid_clean mid_escape); +use PublicInbox::MIME; require PublicInbox::Git; -require PublicInbox::Thread; +require PublicInbox::SearchThread; our $LIM = 50; sub noop {} @@ -46,7 +47,7 @@ sub sres_top_html { $cb = *noop; } else { my $x = $q->{x}; - return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A'); + return adump($_[0], $mset, $q, $ctx) if $x eq 'A'; $ctx->{-html_tip} = search_nav_top($mset, $q) . "\n\n"; if ($x eq 't') { @@ -58,6 +59,17 @@ sub sres_top_html { 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-threaded search results similar to what users expect from # regular WWW search engines: sub mset_summary { @@ -67,14 +79,22 @@ sub mset_summary { my $pad = length("$total"); my $pfx = ' ' x $pad; my $res = \($ctx->{-html_tip}); + my $srch = $ctx->{srch}; 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 $smsg = load_doc_retry($srch, $m); + unless ($smsg) { + eval { + $m = "$m ".$m->get_docid . " expired\n"; + $ctx->{env}->{'psgi.errors'}->print($m); + }; + next; + } 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; + my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->{href}; $$res .= qq{$rank. }. $s . "\n"; $$res .= "$pfx - by $f @ $ts UTC [$pct%]\n\n"; @@ -85,12 +105,11 @@ sub mset_summary { sub err_txt { my ($ctx, $err) = @_; - my $u = '//xapian.org/docs/queryparser.html'; - $u = PublicInbox::Hval::prurl($ctx->{env}, $u); + my $u = $ctx->{-inbox}->base_url($ctx->{env}) . '_/text/help/'; $err =~ s/^\s*Exception:\s*//; # bad word to show users :P $err = ascii_html($err); "\nBad query: $err\n" . - qq{See $u for Xapian query syntax}; + qq{See $u for help on using search}; } sub search_nav_top { @@ -126,15 +145,21 @@ 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 = '
'; + if ($beg <= $end) { + $rv .= "Results $beg-$end of $total"; + $rv .= ' (estimated)' if $end != $total; + } else { + $rv .= "No more results, only $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 ? '/' : ', '; + $rv .= $n < $total ? '/' : ' '; my $p = $o - $LIM; my $qs = $q->qs_html(o => ($p > 0 ? $p : 0)); $rv .= qq{prev}; @@ -142,29 +167,27 @@ sub search_nav_bot { $rv .= ''; } +sub sort_relevance { + my ($pct) = @_; + sub { + [ sort { (eval { $pct->{$b->topmost->{id}} } || 0) + <=> + (eval { $pct->{$a->topmost->{id}} } || 0) + } @{$_[0]} ] }; +} + sub mset_thread { my ($ctx, $mset, $q) = @_; my %pct; - my @m = map { + my $msgs = $ctx->{srch}->retry_reopen(sub { [ map { my $i = $_; - my $m = PublicInbox::SearchMsg->load_doc($i->get_document); - $pct{$m->mid} = $i->get_percent; - $m = $m->mini_mime; - $m; - } ($mset->items); + my $smsg = PublicInbox::SearchMsg->load_doc($i->get_document); + $pct{$smsg->mid} = $i->get_percent; + $smsg; + } ($mset->items) ]}); - my $th = PublicInbox::Thread->new(@m); - $th->thread; - if ($q->{r}) { # order by relevance - $th->order(sub { - sort { (eval { $pct{$b->topmost->messageid} } || 0) - <=> - (eval { $pct{$a->topmost->messageid} } || 0) - } @_; - }); - } else { # order by time (default for threaded view) - $th->order(*PublicInbox::View::sort_ts); - } + my $rootset = PublicInbox::SearchThread::thread($msgs, + $q->{r} ? sort_relevance(\%pct) : *PublicInbox::View::sort_ts); my $skel = search_nav_bot($mset, $q). "
"; my $inbox = $ctx->{-inbox}; $ctx->{-upfx} = ''; @@ -176,21 +199,19 @@ sub mset_thread { $ctx->{prev_attr} = ''; $ctx->{prev_level} = 0; $ctx->{seen} = {}; - $ctx->{s_nr} = scalar(@m).'+ results'; + $ctx->{s_nr} = scalar(@$msgs).'+ results'; - PublicInbox::View::walk_thread($th, $ctx, + PublicInbox::View::walk_thread($rootset, $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; + $mime = $inbox->msg_by_smsg($mime) and last; } if ($mime) { - $mime = Email::MIME->new($mime); + $mime = PublicInbox::MIME->new($mime); return PublicInbox::View::index_entry($mime, $ctx, scalar @$msgs); } @@ -216,29 +237,25 @@ sub ctx_prepare { sub adump { my ($cb, $mset, $q, $ctx) = @_; - my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]); my $ibx = $ctx->{-inbox}; - my $feed_opts = PublicInbox::Feed::get_feedopts($ctx); - 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); - $x = PublicInbox::Feed::atom_header($feed_opts, $x); - $fh->write($x. PublicInbox::Feed::feed_updated()); - - for ($mset->items) { - $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid; - $x = mid2path($x); - my $s = PublicInbox::Feed::feed_entry($feed_opts, $x, $ibx); - $fh->write($s) if defined $s; - } - PublicInbox::Feed::end_feed($fh); + my @items = $mset->items; + $ctx->{search_query} = $q; + my $srch = $ctx->{srch}; + PublicInbox::WwwAtomStream->response($ctx, 200, sub { + while (my $x = shift @items) { + $x = load_doc_retry($srch, $x); + $x = $ibx->msg_by_smsg($x) and + return PublicInbox::MIME->new($x); + } + return undef; + }); } package PublicInbox::SearchQuery; use strict; use warnings; use PublicInbox::Hval; +use PublicInbox::MID qw(mid_escape); sub new { my ($class, $qp) = @_; @@ -263,7 +280,7 @@ sub qs_html { $self = $tmp; } - my $q = PublicInbox::Hval->new($self->{'q'})->as_href; + my $q = mid_escape($self->{'q'}); $q =~ s/%20/+/g; # improve URL readability my $qs = "q=$q";