]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchView.pm
searchquery: split off from searchview
[public-inbox.git] / lib / PublicInbox / SearchView.pm
index 249cf53926df75eb6ec22aa91ca1525b62c7803f..28d9ce5dd940ba337079ba39f2ae61fadec056b4 100644 (file)
@@ -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} = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
-               $cb = $noop;
+               $html = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
        } elsif ($total == 0) {
                if (defined($ctx->{-uxs_retried})) {
                        # undo retry damage:
@@ -70,19 +68,16 @@ retry:
                        goto retry;
                }
                $code = 404;
-               $ctx->{-html_tip} = "<pre>\n[No results found]</pre><hr>";
-               $cb = $noop;
+               $html = "<pre>\n[No results found]</pre><hr>";
        } 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
@@ -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</pre>";
+       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;
                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 .= "&amp;o=$o";
-       }
-       if (my $l = $self->{l}) {
-               $qs .= "&amp;l=$l" unless $l == $LIM;
-       }
-       if (my $r = $self->{r}) {
-               $qs .= "&amp;r";
-       }
-       if (my $x = $self->{x}) {
-               $qs .= "&amp;x=$x" if ($x eq 't' || $x eq 'A' || $x eq 'm');
-       }
-       $qs;
-}
-
 1;