]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchView.pm
hval: move PRE constant for wrapping UGC here
[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 #
4 # Displays search results for the web interface
5 package PublicInbox::SearchView;
6 use strict;
7 use warnings;
8 use PublicInbox::SearchMsg;
9 use PublicInbox::Hval;
10 use PublicInbox::View;
11 use PublicInbox::MID qw(mid2path mid_clean);
12 use Email::MIME;
13 our $LIM = 50;
14
15 sub sres_top_html {
16         my ($ctx) = @_;
17         my $q = PublicInbox::SearchQuery->new($ctx->{cgi});
18         my $code = 200;
19
20         # double the limit for expanded views:
21         my $opts = {
22                 limit => $LIM,
23                 offset => $q->{o},
24                 mset => 1,
25                 relevance => $q->{r},
26         };
27         my ($mset, $total);
28
29         eval {
30                 $mset = $ctx->{srch}->query($q->{q}, $opts);
31                 $total = $mset->get_matches_estimated;
32         };
33         my $err = $@;
34         my $res = html_start($q, $ctx) . PublicInbox::Hval::PRE;
35         if ($err) {
36                 $code = 400;
37                 $res .= err_txt($err) . "</pre><hr /><pre>" . foot($ctx);
38         } elsif ($total == 0) {
39                 $code = 404;
40                 $res .= "\n\n[No results found]</pre><hr /><pre>".foot($ctx);
41         } else {
42                 my $x = $q->{x};
43                 return sub { adump($_[0], $mset, $q, $ctx) } if ($x eq 'A');
44
45                 $res .= search_nav_top($mset, $q);
46                 if ($x eq 't') {
47                         return sub { tdump($_[0], $res, $mset, $q, $ctx) };
48                 }
49                 $res .= "\n\n";
50                 dump_mset(\$res, $mset);
51                 $res .= search_nav_bot($mset, $q) . "\n\n" . foot($ctx);
52         }
53
54         $res .= "</pre></body></html>";
55         [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]];
56 }
57
58 sub dump_mset {
59         my ($res, $mset) = @_;
60
61         my $total = $mset->get_matches_estimated;
62         my $pad = length("$total");
63         my $pfx = ' ' x $pad;
64         foreach my $m ($mset->items) {
65                 my $rank = sprintf("%${pad}d", $m->get_rank + 1);
66                 my $pct = $m->get_percent;
67                 my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
68                 my $s = PublicInbox::Hval->new_oneline($smsg->subject);
69                 my $f = $smsg->from_name;
70                 $f = PublicInbox::Hval->new_oneline($f)->as_html;
71                 my $ts = PublicInbox::View::fmt_ts($smsg->ts);
72                 my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->as_href;
73                 $$res .= qq{$rank. <b><a\nhref="$mid/">}.
74                         $s->as_html . "</a></b>\n";
75                 $$res .= "$pfx  - by $f @ $ts UTC [$pct%]\n\n";
76         }
77 }
78
79 sub err_txt {
80         my ($err) = @_;
81         my $u = 'http://xapian.org/docs/queryparser.html';
82         $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
83         $err = PublicInbox::Hval->new_oneline($err)->as_html;
84         "\n\nBad query: <b>$err</b>\n" .
85                 qq{See <a\nhref="$u">$u</a> for Xapian query syntax};
86 }
87
88 sub search_nav_top {
89         my ($mset, $q) = @_;
90
91         my $rv = "Search results ordered by [";
92         if ($q->{r}) {
93                 my $d = $q->qs_html(r => 0);
94                 $rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
95         } else {
96                 my $d = $q->qs_html(r => 1);
97                 $rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
98         }
99
100         $rv .= ']  view[';
101
102         my $x = $q->{x};
103         if ($x eq '') {
104                 my $t = $q->qs_html(x => 't');
105                 $rv .= qq{<b>summary</b>|};
106                 $rv .= qq{<a\nhref="?$t">threaded</a>}
107         } elsif ($q->{x} eq 't') {
108                 my $s = $q->qs_html(x => '');
109                 $rv .= qq{<a\nhref="?$s">summary</a>|};
110                 $rv .= qq{<b>threaded</b>};
111         }
112         my $A = $q->qs_html(x => 'A', r => undef);
113         $rv .= qq{|<a\nhref="?$A">Atom</a>};
114         $rv .= ']';
115 }
116
117 sub search_nav_bot {
118         my ($mset, $q) = @_;
119         my $total = $mset->get_matches_estimated;
120         my $nr = scalar $mset->items;
121         my $o = $q->{o};
122         my $end = $o + $nr;
123         my $beg = $o + 1;
124         my $rv = "<hr /><pre>Results $beg-$end of $total";
125         my $n = $o + $LIM;
126
127         if ($n < $total) {
128                 my $qs = $q->qs_html(o => $n);
129                 $rv .= qq{, <a\nhref="?$qs">next</a>}
130         }
131         if ($o > 0) {
132                 $rv .= $n < $total ? '/' : ',      ';
133                 my $p = $o - $LIM;
134                 my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
135                 $rv .= qq{<a\nhref="?$qs">prev</a>};
136         }
137         $rv;
138 }
139
140 sub tdump {
141         my ($cb, $res, $mset, $q, $ctx) = @_;
142         my $fh = $cb->([200, ['Content-Type'=>'text/html; charset=UTF-8']]);
143         $fh->write($res);
144         my %pct;
145         my @m = map {
146                 my $i = $_;
147                 my $m = PublicInbox::SearchMsg->load_doc($i->get_document);
148                 $pct{$m->mid} = $i->get_percent;
149                 $m = $m->mini_mime;
150                 $m;
151         } ($mset->items);
152
153         require PublicInbox::Thread;
154         my $th = PublicInbox::Thread->new(@m);
155         {
156                 no warnings 'once';
157                 $Mail::Thread::nosubject = 0;
158         }
159         $th->thread;
160         if ($q->{r}) {
161                 $th->order(sub {
162                         sort { (eval { $pct{$b->topmost->messageid} } || 0)
163                                         <=>
164                                 (eval { $pct{$a->topmost->messageid} } || 0)
165                         } @_;
166                 });
167         } else {
168                 no warnings 'once';
169                 $th->order(*PublicInbox::View::rsort_ts);
170         }
171
172         require PublicInbox::GitCatFile;
173         my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
174         my $state = { ctx => $ctx, anchor_idx => 0, pct => \%pct };
175         $ctx->{searchview} = 1;
176         tdump_ent($fh, $git, $state, $_, 0) for $th->rootset;
177         $git = undef;
178         Email::Address->purge_cache;
179
180         $fh->write(search_nav_bot($mset, $q). "\n\n" .
181                         foot($ctx). '</pre></body></html>');
182
183         $fh->close;
184 }
185
186 sub tdump_ent {
187         my ($fh, $git, $state, $node, $level) = @_;
188         return unless $node;
189         my $mime = $node->message;
190
191         if ($mime) {
192                 # lazy load the full message from mini_mime:
193                 my $mid = $mime->header('Message-ID');
194                 $mime = eval {
195                         my $path = mid2path(mid_clean($mid));
196                         Email::MIME->new($git->cat_file('HEAD:'.$path));
197                 };
198         }
199         if ($mime) {
200                 PublicInbox::View::index_entry($fh, $mime, $level, $state);
201         } else {
202                 my $mid = $node->messageid;
203                 $fh->write(PublicInbox::View::ghost_table('', $mid, $level));
204         }
205         tdump_ent($fh, $git, $state, $node->child, $level + 1);
206         tdump_ent($fh, $git, $state, $node->next, $level);
207 }
208
209 sub foot {
210         my ($ctx) = @_;
211         my $foot = $ctx->{footer} || '';
212         qq{Back to <a\nhref=".">index</a>.\n$foot};
213 }
214
215 sub html_start {
216         my ($q, $ctx) = @_;
217         my $query = PublicInbox::Hval->new_oneline($q->{q});
218
219         my $qh = $query->as_html;
220         my $A = $q->qs_html(x => 'A', r => undef);
221         my $res = "<html><head><title>$qh - search results</title>" .
222                 qq{<link\nrel=alternate\ntitle="Atom feed"\n} .
223                 qq!href="?$A"\ntype="application/atom+xml"/></head>! .
224                 qq{<body><form\naction="">} .
225                 qq{<input\nname=q\nvalue="$qh"\ntype=text />};
226
227         $res .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
228         if (my $x = $q->{x}) {
229                 my $xh = PublicInbox::Hval->new_oneline($x)->as_html;
230                 $res .= qq{<input\ntype=hidden\nname=x\nvalue="$xh" />};
231         }
232
233         $res .= qq{<input\ntype=submit\nvalue=search /></form>};
234 }
235
236 sub adump {
237         my ($cb, $mset, $q, $ctx) = @_;
238         my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
239         require PublicInbox::GitCatFile;
240         my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
241         my $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
242         my $x = PublicInbox::Hval->new_oneline($q->{q})->as_html;
243         $x = qq{$x - search results};
244         $feed_opts->{atomurl} = $feed_opts->{url} . '?'. $q->qs_html;
245         $feed_opts->{url} .= '?'. $q->qs_html(x => undef);
246         $x = PublicInbox::Feed::atom_header($feed_opts, $x);
247         $fh->write($x. PublicInbox::Feed::feed_updated());
248
249         for ($mset->items) {
250                 $x = PublicInbox::SearchMsg->load_doc($_->get_document)->mid;
251                 $x = mid2path($x);
252                 PublicInbox::Feed::add_to_feed($feed_opts, $fh, $x, $git);
253         }
254         $git = undef;
255         PublicInbox::Feed::end_feed($fh);
256 }
257
258 package PublicInbox::SearchQuery;
259 use strict;
260 use warnings;
261 use PublicInbox::Hval;
262
263 sub new {
264         my ($class, $cgi) = @_;
265         my $r = $cgi->param('r');
266         bless {
267                 q => $cgi->param('q'),
268                 x => $cgi->param('x') || '',
269                 o => int($cgi->param('o') || 0) || 0,
270                 r => (defined $r && $r ne '0'),
271         }, $class;
272 }
273
274 sub qs_html {
275         my ($self, %over) = @_;
276
277         if (keys %over) {
278                 my $tmp = bless { %$self }, ref($self);
279                 foreach my $k (keys %over) {
280                         $tmp->{$k} = $over{$k};
281                 }
282                 $self = $tmp;
283         }
284
285         my $q = PublicInbox::Hval->new($self->{q})->as_href;
286         $q =~ s/%20/+/g; # improve URL readability
287         my $qs = "q=$q";
288
289         if (my $o = $self->{o}) { # ignore o == 0
290                 $qs .= "&amp;o=$o";
291         }
292         if (my $r = $self->{r}) {
293                 $qs .= "&amp;r";
294         }
295         if (my $x = $self->{x}) {
296                 $qs .= "&amp;x=$x" if ($x eq 't' || $x eq 'A');
297         }
298         $qs;
299 }
300
301 1;