1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::SearchView;
6 use PublicInbox::SearchMsg;
9 use PublicInbox::MID qw(mid2path mid_clean);
11 use POSIX qw/strftime/;
16 my $q = PublicInbox::SearchQuery->new($ctx->{cgi});
19 # double the limit for expanded views:
29 $mset = $ctx->{srch}->query($q->{q}, $opts);
30 $total = $mset->get_matches_estimated;
33 my $res = html_start($q, $ctx) . PublicInbox::View::PRE_WRAP;
36 $res .= err_txt($err) . "</pre><hr /><pre>" . foot($ctx);
37 } elsif ($total == 0) {
39 $res .= "\n\n[No results found]</pre><hr /><pre>".foot($ctx);
42 return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A');
44 $res .= search_nav_top($mset, $q);
46 return sub { tdump($_[0], $res, $mset, $q, $ctx) };
49 dump_mset(\$res, $mset);
50 $res .= search_nav_bot($mset, $q) . "\n\n" . foot($ctx);
53 $res .= "</pre></body></html>";
54 [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
58 my ($res, $mset) = @_;
60 my $total = $mset->get_matches_estimated;
61 my $pad = length("$total");
63 foreach my $m ($mset->items) {
64 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
65 my $pct = $m->get_percent;
66 my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
67 my $s = PublicInbox::Hval->new_oneline($smsg->subject);
68 my $f = $smsg->from_name;
69 $f = PublicInbox::Hval->new_oneline($f)->as_html;
70 my $d = strftime('%Y-%m-%d %H:%M', gmtime($smsg->ts));
71 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
72 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
73 $s->as_html . "</a></b>\n";
74 $$res .= "$pfx - by $f @ $d UTC [$pct%]\n\n";
80 my $u = 'http://xapian.org/docs/queryparser.html';
81 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
82 $err = PublicInbox::Hval->new_oneline($err)->as_html;
83 "\n\nBad query: <b>$err</b>\n" .
84 qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
90 my $rv = "Search results ordered by [";
92 my $d = $q->qs_html(r => 0);
93 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
95 my $d = $q->qs_html(r => 1);
96 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
103 my $t = $q->qs_html(x => 't');
104 $rv .= qq{<b>summary</b>|};
105 $rv .= qq{<a\nhref="?$t">threaded</a>}
106 } elsif ($q->{x} eq 't') {
107 my $s = $q->qs_html(x => '');
108 $rv .= qq{<a\nhref="?$s">summary</a>|};
109 $rv .= qq{<b>threaded</b>};
111 my $A = $q->qs_html(x => 'A', r => undef);
112 $rv .= qq{|<a\nhref="?$A">Atom</a>};
118 my $total = $mset->get_matches_estimated;
119 my $nr = scalar $mset->items;
123 my $rv = "<hr /><pre>Results $beg-$end of $total";
127 my $qs = $q->qs_html(o => $n);
128 $rv .= qq{, <a\nhref="?$qs">next</a>}
131 $rv .= $n < $total ? '/' : ', ';
133 my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
134 $rv .= qq{<a\nhref="?$qs">prev</a>};
140 my ($cb, $res, $mset, $q, $ctx) = @_;
141 my $fh = $cb->([200, ['Content-Type'=>'text/html; charset=UTF-8']]);
146 my $m = PublicInbox::SearchMsg->load_doc($i->get_document);
147 $pct{$m->mid} = $i->get_percent;
152 require PublicInbox::Thread;
153 my $th = PublicInbox::Thread->new(@m);
156 $Mail::Thread::nosubject = 0;
161 sort { (eval { $pct{$b->topmost->messageid} } || 0)
163 (eval { $pct{$a->topmost->messageid} } || 0)
168 $th->order(*PublicInbox::View::rsort_ts);
171 require PublicInbox::GitCatFile;
172 my $git = PublicInbox::GitCatFile->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;
177 Email::Address->purge_cache;
179 $fh->write(search_nav_bot($mset, $q). "\n\n" .
180 foot($ctx). '</pre></body></html>');
186 my ($fh, $git, $state, $node, $level) = @_;
188 my $mime = $node->message;
191 # lazy load the full message from mini_mime:
192 my $mid = $mime->header('Message-ID');
194 my $path = mid2path(mid_clean($mid));
195 Email::MIME->new($git->cat_file('HEAD:'.$path));
199 PublicInbox::View::index_entry($fh, $mime, $level, $state);
201 my $mid = $node->messageid;
202 $fh->write(PublicInbox::View::ghost_table('', $mid, $level));
204 tdump_ent($fh, $git, $state, $node->child, $level + 1);
205 tdump_ent($fh, $git, $state, $node->next, $level);
210 my $foot = $ctx->{footer} || '';
211 qq{Back to <a\nhref=".">index</a>.\n$foot};
216 my $query = PublicInbox::Hval->new_oneline($q->{q});
218 my $qh = $query->as_html;
219 my $A = $q->qs_html(x => 'A', r => undef);
220 my $res = "<html><head><title>$qh - search results</title>" .
221 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
222 qq!href="?$A"\ntype="application/atom+xml"/></head>! .
223 qq{<body><form\naction="">} .
224 qq{<input\nname=q\nvalue="$qh"\ntype=text />};
226 $res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
227 if (my $x = $q->{x}) {
228 my $xh = PublicInbox::Hval->new_oneline($x)->as_html;
229 $res .= qq{<input\ntype=hidden\nname=x\nvalue="$xh" />};
232 $res .= qq{<input\ntype=submit\nvalue=search /></form>};
236 my ($cb, $mset, $q, $ctx) = @_;
237 my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
238 require PublicInbox::GitCatFile;
239 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
240 my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
241 my $x = PublicInbox::Hval->new_oneline($q->{q})->as_html;
242 $x = qq{$x - search results};
243 $feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
244 $feed_opts->{url} .= '?'. $q->qs_html(x => undef);
245 $x = PublicInbox::Feed::atom_header($feed_opts, $x);
246 $fh->write($x. PublicInbox::Feed::feed_updated());
249 $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
251 PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
254 PublicInbox::Feed::end_feed($fh);
257 package PublicInbox::SearchQuery;
260 use fields qw(q o t x r);
261 use PublicInbox::Hval;
264 my ($class, $cgi) = @_;
265 my $self = fields::new($class);
266 $self->{q} = $cgi->param('q');
267 $self->{x} = $cgi->param('x') || '';
268 $self->{o} = int($cgi->param('o') || 0) || 0;
269 my $r = $cgi->param('r');
270 $self->{r} = (defined $r && $r ne '0');
276 my ($self, %over) = @_;
279 my $tmp = fields::new(ref($self));
281 foreach my $k (keys %over) {
282 $tmp->{$k} = $over{$k};
287 my $q = PublicInbox::Hval->new($self->{q})->as_href;
288 $q =~ s/%20/+/g; # improve URL readability
291 if (my $o = $self->{o}) { # ignore o == 0
294 if (my $r = $self->{r}) {
297 if (my $x = $self->{x}) {
298 $qs .= "&x=$x" if ($x eq 't' || $x eq 'A');