]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/AltId.pm
f8aa4cb8cf71a244d369fc671bf3639d635acdb4
[public-inbox.git] / lib / PublicInbox / AltId.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 package PublicInbox::AltId;
5 use strict;
6 use warnings;
7 use URI::Escape qw(uri_unescape);
8
9 # spec: TYPE:PREFIX:param1=value1&param2=value2&...
10 # Example: serial:gmane:file=/path/to/altmsgmap.sqlite3
11 sub new {
12         my ($class, $inbox, $spec, $writable) = @_;
13         my ($type, $prefix, $query) = split(/:/, $spec, 3);
14         $type eq 'serial' or die "non-serial not supported, yet\n";
15
16         require PublicInbox::Msgmap;
17
18         my %params = map {
19                 my ($k, $v) = split(/=/, uri_unescape($_), 2);
20                 $v = '' unless defined $v;
21                 ($k, $v);
22         } split(/[&;]/, $query);
23         my $f = $params{file} or die "file: required for $type spec $spec\n";
24         unless (index($f, '/') == 0) {
25                 if (($inbox->{version} || 1) == 1) {
26                         $f = "$inbox->{mainrepo}/public-inbox/$f";
27                 } else {
28                         $f = "$inbox->{mainrepo}/$f";
29                 }
30         }
31         bless {
32                 filename => $f,
33                 writable => $writable,
34                 xprefix => 'X'.uc($prefix),
35         }, $class;
36 }
37
38 sub mm_alt {
39         my ($self) = @_;
40         $self->{mm_alt} ||= eval {
41                 my $f = $self->{filename};
42                 my $writable = $self->{filename};
43                 PublicInbox::Msgmap->new_file($f, $writable);
44         };
45 }
46
47 sub mid2alt {
48         my ($self, $mid) = @_;
49         $self->mm_alt->num_for($mid);
50 }
51
52 1;