]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/AltId.pm
inbox: add ->version method
[public-inbox.git] / lib / PublicInbox / AltId.pm
1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Used for giving serial numbers to messages.  This can be tied to
5 # the msgmap for live updates to living lists (see
6 # PublicInbox::Filters::RubyLang), or kept separate for imports
7 # of defunct NNTP groups (e.g. scripts/xhdr-num2mid)
8 #
9 # Introducing NEW uses of serial numbers is discouraged because of
10 # it leads to reliance on centralization.  However, being able
11 # to use existing serial numbers is beneficial.
12 package PublicInbox::AltId;
13 use strict;
14 use warnings;
15 use URI::Escape qw(uri_unescape);
16 use PublicInbox::Msgmap;
17
18 # spec: TYPE:PREFIX:param1=value1&param2=value2&...
19 # The PREFIX will be a searchable boolean prefix in Xapian
20 # Example: serial:gmane:file=/path/to/altmsgmap.sqlite3
21 sub new {
22         my ($class, $ibx, $spec, $writable) = @_;
23         my ($type, $prefix, $query) = split(/:/, $spec, 3);
24         $type eq 'serial' or die "non-serial not supported, yet\n";
25
26         my %params = map {
27                 my ($k, $v) = split(/=/, uri_unescape($_), 2);
28                 $v = '' unless defined $v;
29                 ($k, $v);
30         } split(/[&;]/, $query);
31         my $f = $params{file} or die "file: required for $type spec $spec\n";
32         unless (index($f, '/') == 0) {
33                 if ($ibx->version == 1) {
34                         $f = "$ibx->{inboxdir}/public-inbox/$f";
35                 } else {
36                         $f = "$ibx->{inboxdir}/$f";
37                 }
38         }
39         bless {
40                 filename => $f,
41                 writable => $writable,
42                 xprefix => 'X'.uc($prefix),
43         }, $class;
44 }
45
46 sub mm_alt {
47         my ($self) = @_;
48         $self->{mm_alt} ||= eval {
49                 my $f = $self->{filename};
50                 my $writable = $self->{writable};
51                 PublicInbox::Msgmap->new_file($f, $writable);
52         };
53 }
54
55 sub mid2alt {
56         my ($self, $mid) = @_;
57         $self->mm_alt->num_for($mid);
58 }
59
60 1;