]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtMsg.pm
search: disable Message-ID compression in Xapian
[public-inbox.git] / lib / PublicInbox / ExtMsg.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::ExtMsg;
4 use strict;
5 use warnings;
6 use URI::Escape qw(uri_escape_utf8);
7 use PublicInbox::Hval;
8 use PublicInbox::MID qw/mid_compress mid2path/;
9
10 sub ext_msg {
11         my ($ctx) = @_;
12         my $pi_config = $ctx->{pi_config};
13         my $listname = $ctx->{listname};
14         my $mid = $ctx->{mid};
15
16         eval { require PublicInbox::Search };
17         my $have_xap = $@ ? 0 : 1;
18         my @nox;
19
20         foreach my $k (keys %$pi_config) {
21                 $k =~ /\Apublicinbox\.([A-Z0-9a-z-]+)\.url\z/ or next;
22                 my $list = $1;
23                 next if $list eq $listname;
24
25                 my $git_dir = $pi_config->{"publicinbox.$list.mainrepo"};
26                 defined $git_dir or next;
27
28                 my $url = $pi_config->{"publicinbox.$list.url"};
29                 defined $url or next;
30
31                 $url =~ s!/+\z!!;
32
33                 # try to find the URL with Xapian to avoid forking
34                 if ($have_xap) {
35                         my $doc_id = eval {
36                                 my $s = PublicInbox::Search->new($git_dir);
37                                 $s->find_unique_doc_id('mid', $mid);
38                         };
39                         if ($@) {
40                                 # xapian not configured for this repo
41                         } else {
42                                 # maybe we found it!
43                                 return r302($url, $mid) if (defined $doc_id);
44
45                                 # no point in trying the fork fallback if we
46                                 # know Xapian is up-to-date but missing the
47                                 # message in the current repo
48                                 next;
49                         }
50                 }
51
52                 # queue up for forking after we've tried Xapian on all of them
53                 push @nox, { git_dir => $git_dir, url => $url };
54         }
55
56         # Xapian not installed or configured for some repos
57         my $path = "HEAD:" . mid2path($mid);
58
59         foreach my $n (@nox) {
60                 my @cmd = ('git', "--git-dir=$n->{git_dir}", 'cat-file',
61                            '-t', $path);
62                 my $pid = open my $fh, '-|';
63                 defined $pid or die "fork failed: $!\n";
64
65                 if ($pid == 0) {
66                         open STDERR, '>', '/dev/null'; # ignore errors
67                         exec @cmd or die "exec failed: $!\n";
68                 } else {
69                         my $type = eval { local $/; <$fh> };
70                         close $fh;
71                         if ($? == 0 && $type eq "blob\n") {
72                                 return r302($n->{url}, $mid);
73                         }
74                 }
75         }
76
77         # Fall back to external repos
78
79         [404, ['Content-Type'=>'text/plain'], ['Not found']];
80 }
81
82 # Redirect to another public-inbox which is mapped by $pi_config
83 sub r302 {
84         my ($url, $mid) = @_;
85         $url .= '/' . uri_escape_utf8($mid) . '/';
86         [ 302,
87           [ 'Location' => $url, 'Content-Type' => 'text/plain' ],
88           [ "Redirecting to\n$url\n" ] ]
89 }
90
91 1;