]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Inbox.pm
view: speed up homepage loading time with date clamp
[public-inbox.git] / lib / PublicInbox / Inbox.pm
1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Represents a public-inbox (which may have multiple mailing addresses)
5 package PublicInbox::Inbox;
6 use strict;
7 use warnings;
8 use PublicInbox::Git;
9 use PublicInbox::MID qw(mid2path);
10 use Devel::Peek qw(SvREFCNT);
11 use PublicInbox::MIME;
12
13 my $cleanup_timer;
14 eval {
15         $cleanup_timer = 'disabled';
16         require PublicInbox::EvCleanup;
17         $cleanup_timer = undef; # OK if we get here
18 };
19
20 my $CLEANUP = {}; # string(inbox) -> inbox
21 sub cleanup_task () {
22         $cleanup_timer = undef;
23         for my $ibx (values %$CLEANUP) {
24                 foreach my $f (qw(git mm search)) {
25                         delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
26                 }
27         }
28         $CLEANUP = {};
29 }
30
31 sub _cleanup_later ($) {
32         my ($self) = @_;
33         return unless PublicInbox::EvCleanup::enabled();
34         $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
35         $CLEANUP->{"$self"} = $self;
36 }
37
38 sub _set_uint ($$$) {
39         my ($opts, $field, $default) = @_;
40         my $val = $opts->{$field};
41         if (defined $val) {
42                 $val = $val->[-1] if ref($val) eq 'ARRAY';
43                 $val = undef if $val !~ /\A\d+\z/;
44         }
45         $opts->{$field} = $val || $default;
46 }
47
48 sub _set_limiter ($$$) {
49         my ($self, $pi_config, $pfx) = @_;
50         my $lkey = "-${pfx}_limiter";
51         $self->{$lkey} ||= eval {
52                 # full key is: publicinbox.$NAME.httpbackendmax
53                 my $mkey = $pfx.'max';
54                 my $val = $self->{$mkey} or return;
55                 my $lim;
56                 if ($val =~ /\A\d+\z/) {
57                         require PublicInbox::Qspawn;
58                         $lim = PublicInbox::Qspawn::Limiter->new($val);
59                 } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
60                         $lim = $pi_config->limiter($val);
61                         warn "$mkey limiter=$val not found\n" if !$lim;
62                 } else {
63                         warn "$mkey limiter=$val not understood\n";
64                 }
65                 $lim;
66         }
67 }
68
69 sub new {
70         my ($class, $opts) = @_;
71         my $v = $opts->{address} ||= 'public-inbox@example.com';
72         my $p = $opts->{-primary_address} = ref($v) eq 'ARRAY' ? $v->[0] : $v;
73         $opts->{domain} = ($p =~ /\@(\S+)\z/) ? $1 : 'localhost';
74         my $pi_config = delete $opts->{-pi_config};
75         _set_limiter($opts, $pi_config, 'httpbackend');
76         _set_uint($opts, 'feedmax', 25);
77         $opts->{nntpserver} ||= $pi_config->{'publicinbox.nntpserver'};
78         my $dir = $opts->{mainrepo};
79         if (defined $dir && -f "$dir/inbox.lock") {
80                 $opts->{version} = 2;
81         }
82         bless $opts, $class;
83 }
84
85 sub git_part {
86         my ($self, $part) = @_;
87         ($self->{version} || 1) == 2 or return;
88         $self->{"$part.git"} ||= eval {
89                 my $git_dir = "$self->{mainrepo}/git/$part.git";
90                 my $g = PublicInbox::Git->new($git_dir);
91                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
92                 # no cleanup needed, we never cat-file off this, only clone
93                 $g;
94         };
95 }
96
97 sub git {
98         my ($self) = @_;
99         $self->{git} ||= eval {
100                 my $git_dir = $self->{mainrepo};
101                 $git_dir .= '/all.git' if (($self->{version} || 1) == 2);
102                 my $g = PublicInbox::Git->new($git_dir);
103                 $g->{-httpbackend_limiter} = $self->{-httpbackend_limiter};
104                 _cleanup_later($self);
105                 $g;
106         };
107 }
108
109 sub max_git_part {
110         my ($self) = @_;
111         my $v = $self->{version};
112         return unless defined($v) && $v == 2;
113         my $part = $self->{-max_git_part};
114         my $changed = git($self)->alternates_changed;
115         if (!defined($part) || $changed) {
116                 $self->git->cleanup if $changed;
117                 my $gits = "$self->{mainrepo}/git";
118                 if (opendir my $dh, $gits) {
119                         my $max = -1;
120                         while (defined(my $git_dir = readdir($dh))) {
121                                 $git_dir =~ m!\A(\d+)\.git\z! or next;
122                                 $max = $1 if $1 > $max;
123                         }
124                         $part = $self->{-max_git_part} = $max if $max >= 0;
125                 } else {
126                         warn "opendir $gits failed: $!\n";
127                 }
128         }
129         $part;
130 }
131
132 sub mm {
133         my ($self) = @_;
134         $self->{mm} ||= eval {
135                 require PublicInbox::Msgmap;
136                 _cleanup_later($self);
137                 my $dir = $self->{mainrepo};
138                 if (($self->{version} || 1) >= 2) {
139                         PublicInbox::Msgmap->new_file("$dir/msgmap.sqlite3");
140                 } else {
141                         PublicInbox::Msgmap->new($dir);
142                 }
143         };
144 }
145
146 sub search {
147         my ($self) = @_;
148         $self->{search} ||= eval {
149                 _cleanup_later($self);
150                 PublicInbox::Search->new($self, $self->{altid});
151         };
152 }
153
154 sub try_cat {
155         my ($path) = @_;
156         my $rv = '';
157         if (open(my $fh, '<', $path)) {
158                 local $/;
159                 $rv = <$fh>;
160         }
161         $rv;
162 }
163
164 sub description {
165         my ($self) = @_;
166         my $desc = $self->{description};
167         return $desc if defined $desc;
168         $desc = try_cat("$self->{mainrepo}/description");
169         local $/ = "\n";
170         chomp $desc;
171         $desc =~ s/\s+/ /smg;
172         $desc = '($REPO_DIR/description missing)' if $desc eq '';
173         $self->{description} = $desc;
174 }
175
176 sub cloneurl {
177         my ($self) = @_;
178         my $url = $self->{cloneurl};
179         return $url if $url;
180         $url = try_cat("$self->{mainrepo}/cloneurl");
181         my @url = split(/\s+/s, $url);
182         local $/ = "\n";
183         chomp @url;
184         $self->{cloneurl} = \@url;
185 }
186
187 sub base_url {
188         my ($self, $env) = @_;
189         if ($env) { # PSGI env
190                 my $scheme = $env->{'psgi.url_scheme'};
191                 my $host_port = $env->{HTTP_HOST} ||
192                         "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
193                 my $url = "$scheme://$host_port". ($env->{SCRIPT_NAME} || '/');
194                 # for mount in Plack::Builder
195                 $url .= '/' if $url !~ m!/\z!;
196                 $url .= $self->{name} . '/';
197         } else {
198                 # either called from a non-PSGI environment (e.g. NNTP/POP3)
199                 $self->{-base_url} ||= do {
200                         my $url = $self->{url} or return undef;
201                         # expand protocol-relative URLs to HTTPS if we're
202                         # not inside a web server
203                         $url = "https:$url" if $url =~ m!\A//!;
204                         $url .= '/' if $url !~ m!/\z!;
205                         $url;
206                 };
207         }
208 }
209
210 sub nntp_url {
211         my ($self) = @_;
212         $self->{-nntp_url} ||= do {
213                 # no checking for nntp_usable here, we can point entirely
214                 # to non-local servers or users run by a different user
215                 my $ns = $self->{nntpserver};
216                 my $group = $self->{newsgroup};
217                 my @urls;
218                 if ($ns && $group) {
219                         $ns = [ $ns ] if ref($ns) ne 'ARRAY';
220                         @urls = map {
221                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
222                                 $u .= '/' if $u !~ m!/\z!;
223                                 $u.$group;
224                         } @$ns;
225                 }
226
227                 my $mirrors = $self->{nntpmirror};
228                 if ($mirrors) {
229                         my @m;
230                         foreach (@$mirrors) {
231                                 my $u = m!\Anntps?://! ? $_ : "nntp://$_";
232                                 if ($u =~ m!\Anntps?://[^/]+/?\z!) {
233                                         if ($group) {
234                                                 $u .= '/' if $u !~ m!/\z!;
235                                                 $u .= $group;
236                                         } else {
237                                                 warn
238 "publicinbox.$self->{name}.nntpmirror=$_ missing newsgroup name\n";
239                                         }
240                                 }
241                                 # else: allow full URLs like:
242                                 # nntp://news.example.com/alt.example
243                                 push @m, $u;
244                         }
245                         my %seen = map { $_ => 1 } @urls;
246                         foreach my $u (@m) {
247                                 next if $seen{$u};
248                                 $seen{$u} = 1;
249                                 push @urls, $u;
250                         }
251                 }
252                 \@urls;
253         };
254 }
255
256 sub nntp_usable {
257         my ($self) = @_;
258         my $ret = $self->mm && $self->search;
259         $self->{mm} = $self->{search} = undef;
260         $ret;
261 }
262
263 sub msg_by_path ($$;$) {
264         my ($self, $path, $ref) = @_;
265         # TODO: allow other refs:
266         my $str = git($self)->cat_file('HEAD:'.$path, $ref);
267         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
268         $str;
269 }
270
271 sub msg_by_smsg ($$;$) {
272         my ($self, $smsg, $ref) = @_;
273
274         # ghosts may have undef smsg (from SearchThread.node) or
275         # no {blob} field (from each_smsg_by_mid)
276         return unless defined $smsg;
277         defined(my $blob = $smsg->{blob}) or return;
278
279         my $str = git($self)->cat_file($blob, $ref);
280         $$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s if $str;
281         $str;
282 }
283
284 sub smsg_mime {
285         my ($self, $smsg, $ref) = @_;
286         if (my $s = msg_by_smsg($self, $smsg, $ref)) {
287                 $smsg->{mime} = PublicInbox::MIME->new($s);
288                 return $smsg;
289         }
290 }
291
292 sub path_check {
293         my ($self, $path) = @_;
294         git($self)->check('HEAD:'.$path);
295 }
296
297 sub mid2num($$) {
298         my ($self, $mid) = @_;
299         my $mm = mm($self) or return;
300         $mm->num_for($mid);
301 }
302
303 sub smsg_by_mid ($$) {
304         my ($self, $mid) = @_;
305         my $srch = search($self) or return;
306         # favor the Message-ID we used for the NNTP article number:
307         my $num = mid2num($self, $mid);
308         defined $num ? $srch->lookup_article($num) : undef;
309 }
310
311 sub msg_by_mid ($$;$) {
312         my ($self, $mid, $ref) = @_;
313         my $srch = search($self) or
314                 return msg_by_path($self, mid2path($mid), $ref);
315         my $smsg = smsg_by_mid($self, $mid);
316         $smsg ? msg_by_smsg($self, $smsg, $ref) : undef;
317 }
318
319 1;