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($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]];
60 my ($res, $mset) = @_;
62 my $total = $mset->get_matches_estimated;
63 my $pad = length("$total");
65 foreach my $m ($mset->items) {
66 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
67 my $pct = $m->get_percent;
68 my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
69 my $s = PublicInbox::Hval->new_oneline($smsg->subject);
70 my $f = $smsg->from_name;
71 $f = PublicInbox::Hval->new_oneline($f)->as_html;
72 my $ts = PublicInbox::View::fmt_ts($smsg->ts);
73 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
74 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
75 $s->as_html . "</a></b>\n";
76 $$res .= "$pfx - by $f @ $ts UTC [$pct%]\n\n";
82 my $u = 'http://xapian.org/docs/queryparser.html';
83 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
84 $err = PublicInbox::Hval->new_oneline($err)->as_html;
85 "\n\nBad query: <b>$err</b>\n" .
86 qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
92 my $rv = "Search results ordered by [";
94 my $d = $q->qs_html(r => 0);
95 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
97 my $d = $q->qs_html(r => 1);
98 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
105 my $t = $q->qs_html(x => 't');
106 $rv .= qq{<b>summary</b>|};
107 $rv .= qq{<a\nhref="?$t">threaded</a>}
108 } elsif ($q->{x} eq 't') {
109 my $s = $q->qs_html(x => '');
110 $rv .= qq{<a\nhref="?$s">summary</a>|};
111 $rv .= qq{<b>threaded</b>};
113 my $A = $q->qs_html(x => 'A', r => undef);
114 $rv .= qq{|<a\nhref="?$A">Atom</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']]);
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});
173 my $state = { ctx => $ctx, anchor_idx => 0, pct => \%pct };
174 $ctx->{searchview} = 1;
175 tdump_ent($fh, $git, $state, $_, 0) for $th->rootset;
176 Email::Address->purge_cache;
178 $fh->write(search_nav_bot($mset, $q). "\n\n" .
179 foot($ctx). '</pre></body></html>');
185 my ($fh, $git, $state, $node, $level) = @_;
187 my $mime = $node->message;
190 # lazy load the full message from mini_mime:
191 my $mid = $mime->header('Message-ID');
193 my $path = mid2path(mid_clean($mid));
194 Email::MIME->new($git->cat_file('HEAD:'.$path));
198 PublicInbox::View::index_entry($fh, $mime, $level, $state);
200 my $mid = $node->messageid;
201 $fh->write(PublicInbox::View::ghost_table('', $mid, $level));
203 tdump_ent($fh, $git, $state, $node->child, $level + 1);
204 tdump_ent($fh, $git, $state, $node->next, $level);
209 my $foot = $ctx->{footer} || '';
210 qq{Back to <a\nhref=".">index</a>.\n$foot};
215 my $query = PublicInbox::Hval->new_oneline($q->{q});
217 my $qh = $query->as_html;
218 my $A = $q->qs_html(x => 'A', r => undef);
219 my $res = "<html><head><title>$qh - search results</title>" .
220 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
221 qq!href="?$A"\ntype="application/atom+xml"/></head>! .
222 qq{<body><form\naction="">} .
223 qq{<input\nname=q\nvalue="$qh"\ntype=text />};
225 $res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
226 if (my $x = $q->{x}) {
227 my $xh = PublicInbox::Hval->new_oneline($x)->as_html;
228 $res .= qq{<input\ntype=hidden\nname=x\nvalue="$xh" />};
231 $res .= qq{<input\ntype=submit\nvalue=search /></form>};
235 my ($cb, $mset, $q, $ctx) = @_;
236 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
237 my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
238 my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
239 my $x = PublicInbox::Hval->new_oneline($q->{q})->as_html;
240 $x = qq{$x - search results};
241 $feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
242 $feed_opts->{url} .= '?'. $q->qs_html(x => undef);
243 $x = PublicInbox::Feed::atom_header($feed_opts, $x);
244 $fh->write($x. PublicInbox::Feed::feed_updated());
247 $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
249 PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
251 PublicInbox::Feed::end_feed($fh);
254 package PublicInbox::SearchQuery;
257 use PublicInbox::Hval;
260 my ($class, $cgi) = @_;
261 my $r = $cgi->param('r');
263 q => $cgi->param('q'),
264 x => $cgi->param('x') || '',
265 o => int($cgi->param('o') || 0) || 0,
266 r => (defined $r && $r ne '0'),
271 my ($self, %over) = @_;
274 my $tmp = bless { %$self }, ref($self);
275 foreach my $k (keys %over) {
276 $tmp->{$k} = $over{$k};
281 my $q = PublicInbox::Hval->new($self->{q})->as_href;
282 $q =~ s/%20/+/g; # improve URL readability
285 if (my $o = $self->{o}) { # ignore o == 0
288 if (my $r = $self->{r}) {
291 if (my $x = $self->{x}) {
292 $qs .= "&x=$x" if ($x eq 't' || $x eq 'A');