]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtSearch.pm
wwwstream: show relative coderepo URLs correctly
[public-inbox.git] / lib / PublicInbox / ExtSearch.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Read-only external (detached) index for cross inbox search.
5 # This is a read-only counterpart to PublicInbox::ExtSearchIdx
6 # and behaves like PublicInbox::Inbox AND PublicInbox::Search
7 package PublicInbox::ExtSearch;
8 use strict;
9 use v5.10.1;
10 use PublicInbox::Over;
11 use PublicInbox::Inbox;
12 use PublicInbox::MiscSearch;
13 use DBI qw(:sql_types); # SQL_BLOB
14
15 # for ->reopen, ->mset, ->mset_to_artnums
16 use parent qw(PublicInbox::Search);
17
18 sub new {
19         my (undef, $topdir) = @_;
20         bless {
21                 topdir => $topdir,
22                 # xpfx => 'ei15'
23                 xpfx => "$topdir/ei".PublicInbox::Search::SCHEMA_VERSION
24         }, __PACKAGE__;
25 }
26
27 sub misc {
28         my ($self) = @_;
29         $self->{misc} //= PublicInbox::MiscSearch->new("$self->{xpfx}/misc");
30 }
31
32 sub search { $_[0] } # self
33
34 # overrides PublicInbox::Search::_xdb
35 sub _xdb {
36         my ($self) = @_;
37         $self->xdb_sharded;
38 }
39
40 # same as per-inbox ->over, for now...
41 sub over {
42         my ($self) = @_;
43         $self->{over} //= PublicInbox::Over->new("$self->{xpfx}/over.sqlite3");
44 }
45
46 sub git {
47         my ($self) = @_;
48         $self->{git} //= PublicInbox::Git->new("$self->{topdir}/ALL.git");
49 }
50
51 # returns a hashref of { $NEWSGROUP_NAME => $ART_NO } using the `xref3' table
52 sub nntp_xref_for { # NNTP only
53         my ($self, $xibx, $xsmsg) = @_;
54         my $dbh = over($self)->dbh;
55
56         my $sth = $dbh->prepare_cached(<<'', undef, 1);
57 SELECT ibx_id FROM inboxes WHERE eidx_key = ? LIMIT 1
58
59         $sth->execute($xibx->{newsgroup});
60         my $xibx_id = $sth->fetchrow_array // do {
61                 warn "W: `$xibx->{newsgroup}' not found in $self->{topdir}\n";
62                 return;
63         };
64
65         $sth = $dbh->prepare_cached(<<'', undef, 1);
66 SELECT docid FROM xref3 WHERE oidbin = ? AND xnum = ? AND ibx_id = ? LIMIT 1
67
68         $sth->bind_param(1, pack('H*', $xsmsg->{blob}), SQL_BLOB);
69
70         # NNTP::cmd_over can set {num} to zero according to RFC 3977 8.3.2
71         $sth->bind_param(2, $xsmsg->{num} || $xsmsg->{-orig_num});
72         $sth->bind_param(3, $xibx_id);
73         $sth->execute;
74         my $docid = $sth->fetchrow_array // do {
75                 warn <<EOF;
76 W: `$xibx->{newsgroup}:$xsmsg->{num}' not found in $self->{topdir}"
77 EOF
78                 return;
79         };
80
81         # LIMIT is number of newsgroups on server:
82         $sth = $dbh->prepare_cached(<<'', undef, 1);
83 SELECT ibx_id,xnum FROM xref3 WHERE docid = ? AND ibx_id != ?
84
85         $sth->execute($docid, $xibx_id);
86         my $rows = $sth->fetchall_arrayref;
87
88         my $eidx_key_sth = $dbh->prepare_cached(<<'', undef, 1);
89 SELECT eidx_key FROM inboxes WHERE ibx_id = ? LIMIT 1
90
91         my %xref = map {
92                 my ($ibx_id, $xnum) = @$_;
93
94                 $eidx_key_sth->execute($ibx_id);
95                 my $eidx_key = $eidx_key_sth->fetchrow_array;
96
97                 # only include if there's a newsgroup name
98                 $eidx_key && index($eidx_key, '/') >= 0 ?
99                         () : ($eidx_key => $xnum)
100         } @$rows;
101         $xref{$xibx->{newsgroup}} = $xsmsg->{num};
102         \%xref;
103 }
104
105 sub mm { undef }
106
107 sub altid_map { {} }
108
109 sub description {
110         my ($self) = @_;
111         ($self->{description} //=
112                 PublicInbox::Inbox::cat_desc("$self->{topdir}/description")) //
113                 '$EXTINDEX_DIR/description missing';
114 }
115
116 sub cloneurl { [] } # TODO
117
118 sub base_url { 'https://example.com/TODO/' }
119 sub nntp_url { [] }
120
121 no warnings 'once';
122 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
123 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
124 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
125 *modified = \&PublicInbox::Inbox::modified;
126 *recent = \&PublicInbox::Inbox::recent;
127
128 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
129 *isrch = *search;
130
131 1;