]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchView.pm
searchview: implement Atom feed for search results
[public-inbox.git] / lib / PublicInbox / SearchView.pm
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;
4 use strict;
5 use warnings;
6 use PublicInbox::SearchMsg;
7 use PublicInbox::Hval;
8 use PublicInbox::View;
9 use PublicInbox::MID qw(mid2path mid_clean);
10 use Email::MIME;
11 use POSIX qw/strftime/;
12 our $LIM = 50;
13
14 sub sres_top_html {
15         my ($ctx) = @_;
16         my $q = PublicInbox::SearchQuery->new($ctx->{cgi});
17         my $code = 200;
18
19         # double the limit for expanded views:
20         my $opts = {
21                 limit => $LIM,
22                 offset => $q->{o},
23                 mset => 1,
24                 relevance => $q->{r},
25         };
26         my ($mset, $total);
27
28         eval {
29                 $mset = $ctx->{srch}->query($q->{q}, $opts);
30                 $total = $mset->get_matches_estimated;
31         };
32         my $err = $@;
33         my $res = html_start($q, $ctx) . PublicInbox::View::PRE_WRAP;
34         if ($err) {
35                 $code = 400;
36                 $res .= err_txt($err) . "</pre><hr /><pre>" . foot($ctx);
37         } elsif ($total == 0) {
38                 $code = 404;
39                 $res .= "\n\n[No results found]</pre><hr /><pre>".foot($ctx);
40         } else {
41                 my $x = $q->{x};
42                 return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A');
43
44                 $res .= search_nav_top($mset, $q);
45                 if ($x eq 't') {
46                         return sub { tdump($_[0], $res, $mset, $q, $ctx) };
47                 }
48                 $res .= "\n\n";
49                 dump_mset(\$res, $mset);
50                 $res .= search_nav_bot($mset, $q) . "\n\n" . foot($ctx);
51         }
52
53         $res .= "</pre></body></html>";
54         [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
55 }
56
57 sub dump_mset {
58         my ($res, $mset) = @_;
59
60         my $total = $mset->get_matches_estimated;
61         my $pad = length("$total");
62         my $pfx = ' ' x $pad;
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";
75         }
76 }
77
78 sub err_txt {
79         my ($err) = @_;
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};
85 }
86
87 sub search_nav_top {
88         my ($mset, $q) = @_;
89
90         my $rv = "Search results ordered by [";
91         if ($q->{r}) {
92                 my $d = $q->qs_html(r => 0);
93                 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
94         } else {
95                 my $d = $q->qs_html(r => 1);
96                 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
97         }
98
99         $rv .= ']  view[';
100
101         my $x = $q->{x};
102         if ($x eq '') {
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>};
110         }
111         my $A = $q->qs_html(x => 'A');
112         $rv .= qq{|<a\nhref="?$A">Atom</a>};
113         $rv .= ']';
114 }
115
116 sub search_nav_bot {
117         my ($mset, $q) = @_;
118         my $total = $mset->get_matches_estimated;
119         my $nr = scalar $mset->items;
120         my $o = $q->{o};
121         my $end = $o + $nr;
122         my $beg = $o + 1;
123         my $rv = "<hr /><pre>Results $beg-$end of $total";
124         my $n = $o + $LIM;
125
126         if ($n < $total) {
127                 my $qs = $q->qs_html(o => $n);
128                 $rv .= qq{, <a\nhref="?$qs">next</a>}
129         }
130         if ($o > 0) {
131                 $rv .= $n < $total ? '/' : ',      ';
132                 my $p = $o - $LIM;
133                 my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
134                 $rv .= qq{<a\nhref="?$qs">prev</a>};
135         }
136         $rv;
137 }
138
139 sub tdump {
140         my ($cb, $res, $mset, $q, $ctx) = @_;
141         my $fh = $cb->([200, ['Content-Type'=>'text/html; charset=UTF-8']]);
142         $fh->write($res);
143         my %pct;
144         my @m = map {
145                 my $i = $_;
146                 my $m = PublicInbox::SearchMsg->load_doc($i->get_document);
147                 $pct{$m->mid} = $i->get_percent;
148                 $m = $m->mini_mime;
149                 $m;
150         } ($mset->items);
151
152         require PublicInbox::Thread;
153         my $th = PublicInbox::Thread->new(@m);
154         {
155                 no warnings 'once';
156                 $Mail::Thread::nosubject = 0;
157         }
158         $th->thread;
159         if ($q->{r}) {
160                 $th->order(sub {
161                         sort { (eval { $pct{$b->topmost->messageid} } || 0)
162                                         <=>
163                                 (eval { $pct{$a->topmost->messageid} } || 0)
164                         } @_;
165                 });
166         } else {
167                 no warnings 'once';
168                 $th->order(*PublicInbox::View::rsort_ts);
169         }
170
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;
176         $git = undef;
177         Email::Address->purge_cache;
178
179         $fh->write(search_nav_bot($mset, $q). "\n\n" .
180                         foot($ctx). '</pre></body></html>');
181
182         $fh->close;
183 }
184
185 sub tdump_ent {
186         my ($fh, $git, $state, $node, $level) = @_;
187         return unless $node;
188         my $mime = $node->message;
189
190         if ($mime) {
191                 # lazy load the full message from mini_mime:
192                 my $mid = $mime->header('Message-ID');
193                 $mime = eval {
194                         my $path = mid2path(mid_clean($mid));
195                         Email::MIME->new($git->cat_file('HEAD:'.$path));
196                 };
197         }
198         if ($mime) {
199                 PublicInbox::View::index_entry($fh, $mime, $level, $state);
200         } else {
201                 my $mid = $node->messageid;
202                 $fh->write(PublicInbox::View::ghost_table('', $mid, $level));
203         }
204         tdump_ent($fh, $git, $state, $node->child, $level + 1);
205         tdump_ent($fh, $git, $state, $node->next, $level);
206 }
207
208 sub foot {
209         my ($ctx) = @_;
210         my $foot = $ctx->{footer} || '';
211         qq{Back to <a\nhref=".">index</a>.\n$foot};
212 }
213
214 sub html_start {
215         my ($q, $ctx) = @_;
216         my $query = PublicInbox::Hval->new_oneline($q->{q});
217
218         my $qh = $query->as_html;
219         my $A = $q->qs_html(x => 'A');
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 />};
225
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" />};
230         }
231
232         $res .= qq{<input\ntype=submit\nvalue=search /></form>};
233 }
234
235 sub adump {
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());
247
248         for ($mset->items) {
249                 $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
250                 $x = mid2path($x);
251                 PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
252         }
253         $git = undef;
254         PublicInbox::Feed::end_feed($fh);
255 }
256
257 package PublicInbox::SearchQuery;
258 use strict;
259 use warnings;
260 use fields qw(q o t x r);
261 use PublicInbox::Hval;
262
263 sub new {
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');
271
272         $self;
273 }
274
275 sub qs_html {
276         my ($self, %over) = @_;
277
278         if (keys %over) {
279                 my $tmp = fields::new(ref($self));
280                 %$tmp = %$self;
281                 foreach my $k (keys %over) {
282                         $tmp->{$k} = $over{$k};
283                 }
284                 $self = $tmp;
285         }
286
287         my $q = PublicInbox::Hval->new($self->{q})->as_href;
288         $q =~ s/%20/+/g; # improve URL readability
289         my $qs = "q=$q";
290
291         if (my $o = $self->{o}) { # ignore o == 0
292                 $qs .= "&amp;o=$o";
293         }
294         if (my $r = $self->{r}) {
295                 $qs .= "&amp;r";
296         }
297         if (my $x = $self->{x}) {
298                 $qs .= "&amp;x=$x" if ($x eq 't' || $x eq 'A');
299         }
300         $qs;
301 }
302
303 1;