1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # Displays search results for the web interface
5 package PublicInbox::SearchView;
8 use PublicInbox::SearchMsg;
9 use PublicInbox::Hval qw/ascii_html/;
10 use PublicInbox::View;
11 use PublicInbox::MID qw(mid2path mid_clean mid_mime);
13 require PublicInbox::Git;
18 my $q = PublicInbox::SearchQuery->new($ctx->{cgi});
21 # double the limit for expanded views:
31 $mset = $ctx->{srch}->query($q->{q}, $opts);
32 $total = $mset->get_matches_estimated;
35 my $res = html_start($q, $ctx) . '<pre>';
38 $res .= err_txt($ctx, $err) . "</pre><hr /><pre>" . foot($ctx);
39 } elsif ($total == 0) {
41 $res .= "\n\n[No results found]</pre><hr /><pre>".foot($ctx);
44 return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A');
46 $res .= search_nav_top($mset, $q) . "\n\n";
48 return sub { tdump($_[0], $res, $mset, $q, $ctx) };
50 dump_mset(\$res, $mset);
51 $res .= '</pre>' . search_nav_bot($mset, $q) .
55 $res .= "</pre></body></html>";
56 [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
59 # display non-threaded search results similar to what users expect from
60 # regular WWW search engines:
62 my ($res, $mset) = @_;
64 my $total = $mset->get_matches_estimated;
65 my $pad = length("$total");
67 foreach my $m ($mset->items) {
68 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
69 my $pct = $m->get_percent;
70 my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
71 my $s = ascii_html($smsg->subject);
72 my $f = ascii_html($smsg->from_name);
73 my $ts = PublicInbox::View::fmt_ts($smsg->ts);
74 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
75 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
77 $$res .= "$pfx - by $f @ $ts UTC [$pct%]\n\n";
83 my $u = '//xapian.org/docs/queryparser.html';
84 $u = PublicInbox::Hval::prurl($ctx->{cgi}->{env}, $u);
85 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
86 $err = ascii_html($err);
87 "\n\nBad query: <b>$err</b>\n" .
88 qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
94 my $rv = "Search results ordered by [";
96 my $d = $q->qs_html(r => 0);
97 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
99 my $d = $q->qs_html(r => 1);
100 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
107 my $t = $q->qs_html(x => 't');
108 $rv .= qq{<b>summary</b>|<a\nhref="?$t">threaded</a>}
109 } elsif ($q->{x} eq 't') {
110 my $s = $q->qs_html(x => '');
111 $rv .= qq{<a\nhref="?$s">summary</a>|<b>threaded</b>};
113 my $A = $q->qs_html(x => 'A', r => undef);
114 $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
119 my $total = $mset->get_matches_estimated;
120 my $nr = scalar $mset->items;
124 my $rv = "<hr /><pre>Results $beg-$end of $total";
128 my $qs = $q->qs_html(o => $n);
129 $rv .= qq{, <a\nhref="?$qs">next</a>}
132 $rv .= $n < $total ? '/' : ', ';
134 my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
135 $rv .= qq{<a\nhref="?$qs">prev</a>};
141 my ($cb, $res, $mset, $q, $ctx) = @_;
142 my $fh = $cb->([200, ['Content-Type'=>'text/html; charset=UTF-8']]);
143 $fh->write($res .= '</pre>');
147 my $m = PublicInbox::SearchMsg->load_doc($i->get_document);
148 $pct{$m->mid} = $i->get_percent;
153 require PublicInbox::Thread;
154 my $th = PublicInbox::Thread->new(@m);
157 $Mail::Thread::nosubject = 0;
162 sort { (eval { $pct{$b->topmost->messageid} } || 0)
164 (eval { $pct{$a->topmost->messageid} } || 0)
169 $th->order(*PublicInbox::View::rsort_ts);
172 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
180 $ctx->{searchview} = 1;
181 tdump_ent($git, $state, $_, 0) for $th->rootset;
182 PublicInbox::View::thread_adj_level($state, 0);
183 Email::Address->purge_cache;
185 $fh->write(search_nav_bot($mset, $q). "\n\n" .
186 foot($ctx). '</pre></body></html>');
192 my ($git, $state, $node, $level) = @_;
194 my $mime = $node->message;
197 # lazy load the full message from mini_mime:
198 my $mid = mid_mime($mime);
200 my $path = mid2path(mid_clean($mid));
201 Email::MIME->new($git->cat_file('HEAD:'.$path));
205 my $end = PublicInbox::View::thread_adj_level($state, $level);
206 PublicInbox::View::index_entry($mime, $level, $state);
207 $state->{fh}->write($end) if $end;
209 my $mid = $node->messageid;
210 PublicInbox::View::ghost_flush($state, '', $mid, $level);
212 tdump_ent($git, $state, $node->child, $level + 1);
213 tdump_ent($git, $state, $node->next, $level);
218 my $foot = $ctx->{footer} || '';
219 qq{Back to <a\nhref=".">index</a>.\n$foot};
224 my $qh = ascii_html($q->{'q'});
225 my $A = $q->qs_html(x => 'A', r => undef);
226 my $res = '<html><head>' . PublicInbox::Hval::STYLE .
227 "<title>$qh - search results</title>" .
228 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
229 qq!href="?$A"\ntype="application/atom+xml"/></head>! .
230 qq{<body><form\naction="">} .
231 qq{<input\nname=q\nvalue="$qh"\ntype=text />};
233 $res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
234 if (my $x = $q->{x}) {
236 $res .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
239 $res .= qq{<input\ntype=submit\nvalue=search /></form>};
243 my ($cb, $mset, $q, $ctx) = @_;
244 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
245 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
246 my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
247 my $x = ascii_html($q->{'q'});
248 $x = qq{$x - search results};
249 $feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
250 $feed_opts->{url} .= '?'. $q->qs_html(x => undef);
251 $x = PublicInbox::Feed::atom_header($feed_opts, $x);
252 $fh->write($x. PublicInbox::Feed::feed_updated());
255 $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
257 PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
259 PublicInbox::Feed::end_feed($fh);
262 package PublicInbox::SearchQuery;
265 use PublicInbox::Hval;
268 my ($class, $cgi) = @_;
269 my $r = $cgi->param('r');
271 q => $cgi->param('q'),
272 x => $cgi->param('x') || '',
273 o => int($cgi->param('o') || 0) || 0,
274 r => (defined $r && $r ne '0'),
279 my ($self, %over) = @_;
282 my $tmp = bless { %$self }, ref($self);
283 foreach my $k (keys %over) {
284 $tmp->{$k} = $over{$k};
289 my $q = PublicInbox::Hval->new($self->{'q'})->as_href;
290 $q =~ s/%20/+/g; # improve URL readability
293 if (my $o = $self->{o}) { # ignore o == 0
296 if (my $r = $self->{r}) {
299 if (my $x = $self->{x}) {
300 $qs .= "&x=$x" if ($x eq 't' || $x eq 'A');