]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/AltId.pm
cleanup: use '$ibx' consistently when referring to Inbox refs
[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 # 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
17 # spec: TYPE:PREFIX:param1=value1&param2=value2&...
18 # The PREFIX will be a searchable boolean prefix in Xapian
19 # Example: serial:gmane:file=/path/to/altmsgmap.sqlite3
20 sub new {
21         my ($class, $ibx, $spec, $writable) = @_;
22         my ($type, $prefix, $query) = split(/:/, $spec, 3);
23         $type eq 'serial' or die "non-serial not supported, yet\n";
24
25         require PublicInbox::Msgmap;
26
27         my %params = map {
28                 my ($k, $v) = split(/=/, uri_unescape($_), 2);
29                 $v = '' unless defined $v;
30                 ($k, $v);
31         } split(/[&;]/, $query);
32         my $f = $params{file} or die "file: required for $type spec $spec\n";
33         unless (index($f, '/') == 0) {
34                 if (($ibx->{version} || 1) == 1) {
35                         $f = "$ibx->{mainrepo}/public-inbox/$f";
36                 } else {
37                         $f = "$ibx->{mainrepo}/$f";
38                 }
39         }
40         bless {
41                 filename => $f,
42                 writable => $writable,
43                 xprefix => 'X'.uc($prefix),
44         }, $class;
45 }
46
47 sub mm_alt {
48         my ($self) = @_;
49         $self->{mm_alt} ||= eval {
50                 my $f = $self->{filename};
51                 my $writable = $self->{writable};
52                 PublicInbox::Msgmap->new_file($f, $writable);
53         };
54 }
55
56 sub mid2alt {
57         my ($self, $mid) = @_;
58         $self->mm_alt->num_for($mid);
59 }
60
61 1;