user/dev discussion of public-inbox itself
 help / color / Atom feed
6af151a4b82fc360779046899dbd1ad7647aea3c blob 6658 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
 
# Copyright (C) 2015 all contributors <meta@public-inbox.org>
# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
#
# Displays search results for the web interface
package PublicInbox::SearchView;
use strict;
use warnings;
use PublicInbox::SearchMsg;
use PublicInbox::Hval qw/ascii_html/;
use PublicInbox::View;
use PublicInbox::WwwAtomStream;
use PublicInbox::MID qw(mid2path mid_mime mid_clean mid_escape);
use Email::MIME;
require PublicInbox::Git;
require PublicInbox::SearchThread;
our $LIM = 50;

sub noop {}

sub sres_top_html {
	my ($ctx) = @_;
	my $q = PublicInbox::SearchQuery->new($ctx->{qp});
	my $code = 200;

	# double the limit for expanded views:
	my $opts = {
		limit => $LIM,
		offset => $q->{o},
		mset => 1,
		relevance => $q->{r},
	};
	my ($mset, $total);
	eval {
		$mset = $ctx->{srch}->query($q->{'q'}, $opts);
		$total = $mset->get_matches_estimated;
	};
	my $err = $@;
	ctx_prepare($q, $ctx);
	my $cb;
	if ($err) {
		$code = 400;
		$ctx->{-html_tip} = '<pre>'.err_txt($ctx, $err).'</pre><hr>';
		$cb = *noop;
	} elsif ($total == 0) {
		$code = 404;
		$ctx->{-html_tip} = "<pre>\n[No results found]</pre><hr>";
		$cb = *noop;
	} else {
		my $x = $q->{x};
		return adump($_[0], $mset, $q, $ctx) if $x eq 'A';

		$ctx->{-html_tip} = search_nav_top($mset, $q) . "\n\n";
		if ($x eq 't') {
			$cb = mset_thread($ctx, $mset, $q);
		} else {
			$cb = mset_summary($ctx, $mset, $q);
		}
	}
	PublicInbox::WwwStream->response($ctx, $code, $cb);
}

# display non-threaded search results similar to what users expect from
# regular WWW search engines:
sub mset_summary {
	my ($ctx, $mset, $q) = @_;

	my $total = $mset->get_matches_estimated;
	my $pad = length("$total");
	my $pfx = ' ' x $pad;
	my $res = \($ctx->{-html_tip});
	foreach my $m ($mset->items) {
		my $rank = sprintf("%${pad}d", $m->get_rank + 1);
		my $pct = $m->get_percent;
		my $smsg = PublicInbox::SearchMsg->load_doc($m->get_document);
		my $s = ascii_html($smsg->subject);
		my $f = ascii_html($smsg->from_name);
		my $ts = PublicInbox::View::fmt_ts($smsg->ts);
		my $mid = PublicInbox::Hval->new_msgid($smsg->mid)->{href};
		$$res .= qq{$rank. <b><a\nhref="$mid/">}.
			$s . "</a></b>\n";
		$$res .= "$pfx  - by $f @ $ts UTC [$pct%]\n\n";
	}
	$$res .= search_nav_bot($mset, $q);
	*noop;
}

sub err_txt {
	my ($ctx, $err) = @_;
	my $u = $ctx->{-inbox}->base_url($ctx->{env}) . '_/text/help/';
	$err =~ s/^\s*Exception:\s*//; # bad word to show users :P
	$err = ascii_html($err);
	"\nBad query: <b>$err</b>\n" .
		qq{See <a\nhref="$u">$u</a> for help on using search};
}

sub search_nav_top {
	my ($mset, $q) = @_;

	my $rv = "<pre>Search results ordered by [";
	if ($q->{r}) {
		my $d = $q->qs_html(r => 0);
		$rv .= qq{<a\nhref="?$d">date</a>|<b>relevance</b>};
	} else {
		my $d = $q->qs_html(r => 1);
		$rv .= qq{<b>date</b>|<a\nhref="?$d">relevance</a>};
	}

	$rv .= ']  view[';

	my $x = $q->{x};
	if ($x eq '') {
		my $t = $q->qs_html(x => 't');
		$rv .= qq{<b>summary</b>|<a\nhref="?$t">threaded</a>}
	} elsif ($q->{x} eq 't') {
		my $s = $q->qs_html(x => '');
		$rv .= qq{<a\nhref="?$s">summary</a>|<b>threaded</b>};
	}
	my $A = $q->qs_html(x => 'A', r => undef);
	$rv .= qq{|<a\nhref="?$A">Atom feed</a>]};
}

sub search_nav_bot {
	my ($mset, $q) = @_;
	my $total = $mset->get_matches_estimated;
	my $nr = scalar $mset->items;
	my $o = $q->{o};
	my $end = $o + $nr;
	my $beg = $o + 1;
	my $rv = "</pre><hr><pre>Results $beg-$end of $total";
	my $n = $o + $LIM;

	if ($n < $total) {
		my $qs = $q->qs_html(o => $n);
		$rv .= qq{, <a\nhref="?$qs"\nrel=next>next</a>}
	}
	if ($o > 0) {
		$rv .= $n < $total ? '/' : ',      ';
		my $p = $o - $LIM;
		my $qs = $q->qs_html(o => ($p > 0 ? $p : 0));
		$rv .= qq{<a\nhref="?$qs"\nrel=prev>prev</a>};
	}
	$rv .= '</pre>';
}

sub mset_thread {
	my ($ctx, $mset, $q) = @_;
	my %pct;
	my @m = map {
		my $i = $_;
		my $m = PublicInbox::SearchMsg->load_doc($i->get_document);
		$pct{$m->mid} = $i->get_percent;
		$m;
	} ($mset->items);

	my $th = PublicInbox::SearchThread->new(\@m);
	$th->thread;
	if ($q->{r}) { # order by relevance
		$th->order(sub {
			[ sort { (eval { $pct{$b->topmost->{id}} } || 0)
					<=>
				(eval { $pct{$a->topmost->{id}} } || 0)
			} @{$_[0]} ];
		});
	} else { # order by time (default for threaded view)
		$th->order(*PublicInbox::View::sort_ts);
	}
	my $skel = search_nav_bot($mset, $q). "<pre>";
	my $inbox = $ctx->{-inbox};
	$ctx->{-upfx} = '';
	$ctx->{anchor_idx} = 1;
	$ctx->{cur_level} = 0;
	$ctx->{dst} = \$skel;
	$ctx->{mapping} = {};
	$ctx->{pct} = \%pct;
	$ctx->{prev_attr} = '';
	$ctx->{prev_level} = 0;
	$ctx->{seen} = {};
	$ctx->{s_nr} = scalar(@m).'+ results';

	PublicInbox::View::walk_thread($th, $ctx,
		*PublicInbox::View::pre_thread);

	my $msgs = \@m;
	my $mime;
	sub {
		return unless $msgs;
		while ($mime = shift @$msgs) {
			$mime = $inbox->msg_by_smsg($mime) and last;
		}
		if ($mime) {
			$mime = Email::MIME->new($mime);
			return PublicInbox::View::index_entry($mime, $ctx,
				scalar @$msgs);
		}
		$msgs = undef;
		$skel .= "\n</pre>";
	};
}

sub ctx_prepare {
	my ($q, $ctx) = @_;
	my $qh = ascii_html($q->{'q'});
	$ctx->{-q_value_html} = $qh;
	$ctx->{-atom} = '?'.$q->qs_html(x => 'A', r => undef);
	$ctx->{-title_html} = "$qh - search results";
	my $extra = '';
	$extra .= qq{<input\ntype=hidden\nname=r />} if $q->{r};
	if (my $x = $q->{x}) {
		$x = ascii_html($x);
		$extra .= qq{<input\ntype=hidden\nname=x\nvalue="$x" />};
	}
	$ctx->{-extra_form_html} = $extra;
}

sub adump {
	my ($cb, $mset, $q, $ctx) = @_;
	my $ibx = $ctx->{-inbox};
	my @items = $mset->items;
	$ctx->{search_query} = $q;
	PublicInbox::WwwAtomStream->response($ctx, 200, sub {
		while (my $x = shift @items) {
			$x = PublicInbox::SearchMsg->load_doc($x->get_document);
			$x = $ibx->msg_by_smsg($x) and
					return Email::MIME->new($x);
		}
		return undef;
	});
}

package PublicInbox::SearchQuery;
use strict;
use warnings;
use PublicInbox::Hval;
use PublicInbox::MID qw(mid_escape);

sub new {
	my ($class, $qp) = @_;

	my $r = $qp->{r};
	bless {
		q => $qp->{'q'},
		x => $qp->{x} || '',
		o => (($qp->{o} || '0') =~ /(\d+)/),
		r => (defined $r && $r ne '0'),
	}, $class;
}

sub qs_html {
	my ($self, %over) = @_;

	if (keys %over) {
		my $tmp = bless { %$self }, ref($self);
		foreach my $k (keys %over) {
			$tmp->{$k} = $over{$k};
		}
		$self = $tmp;
	}

	my $q = mid_escape($self->{'q'});
	$q =~ s/%20/+/g; # improve URL readability
	my $qs = "q=$q";

	if (my $o = $self->{o}) { # ignore o == 0
		$qs .= "&amp;o=$o";
	}
	if (my $r = $self->{r}) {
		$qs .= "&amp;r";
	}
	if (my $x = $self->{x}) {
		$qs .= "&amp;x=$x" if ($x eq 't' || $x eq 'A');
	}
	$qs;
}

1;
debug log:

solving 6af151a ...
found 6af151a in https://80x24.org/public-inbox.git

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git