X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchView.pm;h=28d9ce5dd940ba337079ba39f2ae61fadec056b4;hb=3dac753c5c7bf3846d9aeff86815ecb69798b55e;hp=4fbf59ef2ca080d22db624c393d0db1dcc9084d6;hpb=87678710135973f72722258e171fc00f85c86ec8;p=public-inbox.git diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index 4fbf59ef..28d9ce5d 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -4,18 +4,17 @@ # Displays search results for the web interface package PublicInbox::SearchView; use strict; -use warnings; -use URI::Escape qw(uri_unescape uri_escape); +use v5.10.1; +use URI::Escape qw(uri_unescape); use PublicInbox::Smsg; use PublicInbox::Hval qw(ascii_html obfuscate_addrs mid_href); use PublicInbox::View; use PublicInbox::WwwAtomStream; +use PublicInbox::WwwStream qw(html_oneshot); use PublicInbox::SearchThread; -our $LIM = 200; +use PublicInbox::SearchQuery; my %rmap_inc; -my $noop = sub {}; - sub mbox_results { my ($ctx) = @_; my $q = PublicInbox::SearchQuery->new($ctx->{qp}); @@ -48,7 +47,7 @@ sub sres_top_html { relevance => $q->{r}, asc => $asc, }; - my ($mset, $total, $err, $cb); + my ($mset, $total, $err, $html); retry: eval { $mset = $srch->query($query, $opts); @@ -58,8 +57,7 @@ retry: ctx_prepare($q, $ctx); if ($err) { $code = 400; - $ctx->{-html_tip} = '
'.err_txt($ctx, $err).'

'; - $cb = $noop; + $html = '
'.err_txt($ctx, $err).'

'; } elsif ($total == 0) { if (defined($ctx->{-uxs_retried})) { # undo retry damage: @@ -70,19 +68,16 @@ retry: goto retry; } $code = 404; - $ctx->{-html_tip} = "
\n[No results found]

"; - $cb = $noop; + $html = "
\n[No results found]

"; } else { return adump($_[0], $mset, $q, $ctx) if $x eq 'A'; $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx); - if ($x eq 't') { - $cb = mset_thread($ctx, $mset, $q); - } else { - $cb = mset_summary($ctx, $mset, $q); - } + return mset_thread($ctx, $mset, $q) if $x eq 't'; + mset_summary($ctx, $mset, $q); # appends to {-html_tip} + $html = ''; } - PublicInbox::WwwStream->response($ctx, $code, $cb); + html_oneshot($ctx, $code); } # display non-nested search results similar to what users expect from @@ -122,7 +117,7 @@ sub mset_summary { $$res .= "$pfx - by $f @ $date UTC [$pct%]\n\n"; } $$res .= search_nav_bot($mset, $q); - $noop; + undef; } # shorten "/full/path/to/Foo/Bar.pm" to "Foo/Bar.pm" so error @@ -243,8 +238,8 @@ sub search_nav_bot { sub sort_relevance { [ sort { - (eval { $b->topmost->{smsg}->{pct} } // 0) <=> - (eval { $a->topmost->{smsg}->{pct} } // 0) + (eval { $b->topmost->{pct} } // 0) <=> + (eval { $a->topmost->{pct} } // 0) } @{$_[0]} ] } @@ -292,20 +287,17 @@ sub mset_thread { @$msgs = reverse @$msgs if $r; $ctx->{msgs} = $msgs; - \&mset_thread_i; + PublicInbox::WwwStream::aresponse($ctx, 200, \&mset_thread_i); } # callback for PublicInbox::WwwStream::getline sub mset_thread_i { - my ($nr, $ctx) = @_; - my $msgs = $ctx->{msgs} or return; - while (my $smsg = pop @$msgs) { - $ctx->{-inbox}->smsg_mime($smsg) or next; - return PublicInbox::View::index_entry($smsg, $ctx, - scalar @$msgs); - } - my ($skel) = delete @$ctx{qw(skel msgs)}; - $$skel .= "\n"; + my ($ctx, $eml) = @_; + $ctx->zmore($ctx->html_top) if exists $ctx->{-html_tip}; + $eml and return PublicInbox::View::eml_entry($ctx, $eml); + my $smsg = shift @{$ctx->{msgs}} or + $ctx->zmore(${delete($ctx->{skel})}); + $smsg; } sub ctx_prepare { @@ -329,7 +321,6 @@ sub adump { my ($cb, $mset, $q, $ctx) = @_; $ctx->{items} = [ $mset->items ]; $ctx->{search_query} = $q; # used by WwwAtomStream::atom_header - $ctx->{srch} = $ctx->{-inbox}->search; PublicInbox::WwwAtomStream->response($ctx, 200, \&adump_i); } @@ -337,58 +328,12 @@ sub adump { sub adump_i { my ($ctx) = @_; while (my $mi = shift @{$ctx->{items}}) { + my $srch = $ctx->{-inbox}->search(undef, $ctx) or return; my $smsg = eval { - PublicInbox::Smsg::from_mitem($mi, $ctx->{srch}); + PublicInbox::Smsg::from_mitem($mi, $srch); } or next; - $ctx->{-inbox}->smsg_mime($smsg) and return $smsg; - } -} - -package PublicInbox::SearchQuery; -use strict; -use warnings; -use URI::Escape qw(uri_escape); -use PublicInbox::MID qw(MID_ESC); - -sub new { - my ($class, $qp) = @_; - - my $r = $qp->{r}; - my ($l) = (($qp->{l} || '') =~ /([0-9]+)/); - $l = $LIM if !$l || $l > $LIM; - bless { - q => $qp->{'q'}, - x => $qp->{x} || '', - o => (($qp->{o} || '0') =~ /(-?[0-9]+)/), - l => $l, - r => (defined $r && $r ne '0'), - }, $class; -} - -sub qs_html { - my ($self, %override) = @_; - - if (scalar(keys(%override))) { - $self = bless { (%$self, %override) }, ref($self); - } - - my $q = uri_escape($self->{'q'}, MID_ESC); - $q =~ s/%20/+/g; # improve URL readability - my $qs = "q=$q"; - - if (my $o = $self->{o}) { # ignore o == 0 - $qs .= "&o=$o"; - } - if (my $l = $self->{l}) { - $qs .= "&l=$l" unless $l == $LIM; - } - if (my $r = $self->{r}) { - $qs .= "&r"; - } - if (my $x = $self->{x}) { - $qs .= "&x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm'); + return $smsg; } - $qs; } 1;