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;
10 use PublicInbox::View;
11 use PublicInbox::MID qw(mid2path mid_clean);
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 = PublicInbox::Hval->new_oneline($smsg->subject);
72 my $f = $smsg->from_name;
73 $f = PublicInbox::Hval->new_oneline($f)->as_html;
74 my $ts = PublicInbox::View::fmt_ts($smsg->ts);
75 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
76 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
77 $s->as_html . "</a></b>\n";
78 $$res .= "$pfx - by $f @ $ts UTC [$pct%]\n\n";
84 my $u = '//xapian.org/docs/queryparser.html';
85 $u = PublicInbox::Hval::prurl($ctx->{cgi}->{env}, $u);
86 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
87 $err = PublicInbox::Hval->new_oneline($err)->as_html;
88 "\n\nBad query: <b>$err</b>\n" .
89 qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
95 my $rv = "Search results ordered by [";
97 my $d = $q->qs_html(r => 0);
98 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
100 my $d = $q->qs_html(r => 1);
101 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
108 my $t = $q->qs_html(x => 't');
109 $rv .= qq{<b>summary</b>|<a\nhref="?$t">threaded</a>}
110 } elsif ($q->{x} eq 't') {
111 my $s = $q->qs_html(x => '');
112 $rv .= qq{<a\nhref="?$s">summary</a>|<b>threaded</b>};
114 my $A = $q->qs_html(x => 'A', r => undef);
115 $rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
120 my $total = $mset->get_matches_estimated;
121 my $nr = scalar $mset->items;
125 my $rv = "<hr /><pre>Results $beg-$end of $total";
129 my $qs = $q->qs_html(o => $n);
130 $rv .= qq{, <a\nhref="?$qs">next</a>}
133 $rv .= $n < $total ? '/' : ', ';
135 my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
136 $rv .= qq{<a\nhref="?$qs">prev</a>};
142 my ($cb, $res, $mset, $q, $ctx) = @_;
143 my $fh = $cb->([200, ['Content-Type'=>'text/html; charset=UTF-8']]);
144 $fh->write($res .= '</pre>');
148 my $m = PublicInbox::SearchMsg->load_doc($i->get_document);
149 $pct{$m->mid} = $i->get_percent;
154 require PublicInbox::Thread;
155 my $th = PublicInbox::Thread->new(@m);
158 $Mail::Thread::nosubject = 0;
163 sort { (eval { $pct{$b->topmost->messageid} } || 0)
165 (eval { $pct{$a->topmost->messageid} } || 0)
170 $th->order(*PublicInbox::View::rsort_ts);
173 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
180 $ctx->{searchview} = 1;
181 tdump_ent($fh, $git, $state, $_, 0) for $th->rootset;
182 PublicInbox::View::thread_adj_level($fh, $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 ($fh, $git, $state, $node, $level) = @_;
194 my $mime = $node->message;
197 # lazy load the full message from mini_mime:
198 my $mid = $mime->header('Message-ID');
200 my $path = mid2path(mid_clean($mid));
201 Email::MIME->new($git->cat_file('HEAD:'.$path));
206 PublicInbox::View::thread_adj_level($fh, $state, $level);
207 PublicInbox::View::index_entry($fh, $mime, $level, $state);
208 $fh->write($end) if $end;
210 my $mid = $node->messageid;
211 PublicInbox::View::ghost_flush($fh, $state, '', $mid, $level);
213 tdump_ent($fh, $git, $state, $node->child, $level + 1);
214 tdump_ent($fh, $git, $state, $node->next, $level);
219 my $foot = $ctx->{footer} || '';
220 qq{Back to <a\nhref=".">index</a>.\n$foot};
225 my $query = PublicInbox::Hval->new_oneline($q->{q});
227 my $qh = $query->as_html;
228 my $A = $q->qs_html(x => 'A', r => undef);
229 my $res = '<html><head>' . PublicInbox::Hval::STYLE .
230 "<title>$qh - search results</title>" .
231 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
232 qq!href="?$A"\ntype="application/atom+xml"/></head>! .
233 qq{<body><form\naction="">} .
234 qq{<input\nname=q\nvalue="$qh"\ntype=text />};
236 $res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
237 if (my $x = $q->{x}) {
238 my $xh = PublicInbox::Hval->new_oneline($x)->as_html;
239 $res .= qq{<input\ntype=hidden\nname=x\nvalue="$xh" />};
242 $res .= qq{<input\ntype=submit\nvalue=search /></form>};
246 my ($cb, $mset, $q, $ctx) = @_;
247 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
248 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
249 my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
250 my $x = PublicInbox::Hval->new_oneline($q->{q})->as_html;
251 $x = qq{$x - search results};
252 $feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
253 $feed_opts->{url} .= '?'. $q->qs_html(x => undef);
254 $x = PublicInbox::Feed::atom_header($feed_opts, $x);
255 $fh->write($x. PublicInbox::Feed::feed_updated());
258 $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
260 PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
262 PublicInbox::Feed::end_feed($fh);
265 package PublicInbox::SearchQuery;
268 use PublicInbox::Hval;
271 my ($class, $cgi) = @_;
272 my $r = $cgi->param('r');
274 q => $cgi->param('q'),
275 x => $cgi->param('x') || '',
276 o => int($cgi->param('o') || 0) || 0,
277 r => (defined $r && $r ne '0'),
282 my ($self, %over) = @_;
285 my $tmp = bless { %$self }, ref($self);
286 foreach my $k (keys %over) {
287 $tmp->{$k} = $over{$k};
292 my $q = PublicInbox::Hval->new_oneline($self->{q})->as_href;
293 $q =~ s/%20/+/g; # improve URL readability
296 if (my $o = $self->{o}) { # ignore o == 0
299 if (my $r = $self->{r}) {
302 if (my $x = $self->{x}) {
303 $qs .= "&x=$x" if ($x eq 't' || $x eq 'A');