]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ExtSearch.pm
git: move cloneurl + description reading here
[public-inbox.git] / lib / PublicInbox / ExtSearch.pm
index 20ec322475de99808b3ba80113dbdb9a924292a1..fa49a1d0182f75d53e4408d26fdc4f2997f6ef6e 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # Read-only external (detached) index for cross inbox search.
@@ -9,7 +9,6 @@ use strict;
 use v5.10.1;
 use PublicInbox::Over;
 use PublicInbox::Inbox;
-use File::Spec ();
 use PublicInbox::MiscSearch;
 use DBI qw(:sql_types); # SQL_BLOB
 
@@ -17,13 +16,13 @@ use DBI qw(:sql_types); # SQL_BLOB
 use parent qw(PublicInbox::Search);
 
 sub new {
-       my (undef, $topdir) = @_;
-       $topdir = File::Spec->canonpath($topdir);
+       my ($class, $topdir) = @_;
        bless {
                topdir => $topdir,
+               -primary_address => 'unknown@example.com',
                # xpfx => 'ei15'
                xpfx => "$topdir/ei".PublicInbox::Search::SCHEMA_VERSION
-       }, __PACKAGE__;
+       }, $class;
 }
 
 sub misc {
@@ -31,27 +30,24 @@ sub misc {
        $self->{misc} //= PublicInbox::MiscSearch->new("$self->{xpfx}/misc");
 }
 
-sub search { $_[0] } # self
-
-# overrides PublicInbox::Search::_xdb
-sub _xdb {
-       my ($self) = @_;
-       $self->xdb_sharded;
-}
-
 # same as per-inbox ->over, for now...
 sub over {
        my ($self) = @_;
-       $self->{over} //= PublicInbox::Over->new("$self->{xpfx}/over.sqlite3");
+       $self->{over} //= do {
+               PublicInbox::Inbox::_cleanup_later($self);
+               PublicInbox::Over->new("$self->{xpfx}/over.sqlite3");
+       };
 }
 
 sub git {
        my ($self) = @_;
-       $self->{git} //= PublicInbox::Git->new("$self->{topdir}/ALL.git");
+       $self->{git} //= do {
+               PublicInbox::Inbox::_cleanup_later($self);
+               PublicInbox::Git->new("$self->{topdir}/ALL.git");
+       };
 }
 
-# returns an arrayref of [ $NEWSGROUP_NAME:$ART_NO ] using
-# the `xref3' table
+# returns a hashref of { $NEWSGROUP_NAME => $ART_NO } using the `xref3' table
 sub nntp_xref_for { # NNTP only
        my ($self, $xibx, $xsmsg) = @_;
        my $dbh = over($self)->dbh;
@@ -68,8 +64,10 @@ SELECT ibx_id FROM inboxes WHERE eidx_key = ? LIMIT 1
        $sth = $dbh->prepare_cached(<<'', undef, 1);
 SELECT docid FROM xref3 WHERE oidbin = ? AND xnum = ? AND ibx_id = ? LIMIT 1
 
-       $sth->bind_param(1, pack('H*', $xsmsg->{blob}), SQL_BLOB);
-       $sth->bind_param(2, $xsmsg->{num});
+       $sth->bind_param(1, $xsmsg->oidbin, SQL_BLOB);
+
+       # NNTP::cmd_over can set {num} to zero according to RFC 3977 8.3.2
+       $sth->bind_param(2, $xsmsg->{num} || $xsmsg->{-orig_num});
        $sth->bind_param(3, $xibx_id);
        $sth->execute;
        my $docid = $sth->fetchrow_array // do {
@@ -81,9 +79,9 @@ EOF
 
        # LIMIT is number of newsgroups on server:
        $sth = $dbh->prepare_cached(<<'', undef, 1);
-SELECT ibx_id,xnum FROM xref3 WHERE docid = ?
+SELECT ibx_id,xnum FROM xref3 WHERE docid = ? AND ibx_id != ?
 
-       $sth->execute($docid);
+       $sth->execute($docid, $xibx_id);
        my $rows = $sth->fetchall_arrayref;
 
        my $eidx_key_sth = $dbh->prepare_cached(<<'', undef, 1);
@@ -91,18 +89,16 @@ SELECT eidx_key FROM inboxes WHERE ibx_id = ? LIMIT 1
 
        my %xref = map {
                my ($ibx_id, $xnum) = @$_;
-               if ($ibx_id == $xibx_id) {
-                       ();
-               } else {
-                       $eidx_key_sth->execute($ibx_id);
-                       my $eidx_key = $eidx_key_sth->fetchrow_array;
-
-                       # only include if there's a newsgroup name
-                       $eidx_key && index($eidx_key, '/') >= 0 ?
-                               () : ($eidx_key => $xnum)
-               }
+
+               $eidx_key_sth->execute($ibx_id);
+               my $eidx_key = $eidx_key_sth->fetchrow_array;
+
+               # only include if there's a newsgroup name
+               $eidx_key && index($eidx_key, '/') >= 0 ?
+                       () : ($eidx_key => $xnum)
        } @$rows;
-       [ map { "$_:$xref{$_}" } sort keys %xref ]; # match NNTP LIST order
+       $xref{$xibx->{newsgroup}} = $xsmsg->{num};
+       \%xref;
 }
 
 sub mm { undef }
@@ -112,22 +108,25 @@ sub altid_map { {} }
 sub description {
        my ($self) = @_;
        ($self->{description} //=
-               PublicInbox::Inbox::cat_desc("$self->{topdir}/description")) //
+               PublicInbox::Git::cat_desc("$self->{topdir}/description")) //
                '$EXTINDEX_DIR/description missing';
 }
 
-sub cloneurl { [] } # TODO
+sub search {
+       PublicInbox::Inbox::_cleanup_later($_[0]);
+       $_[0];
+}
 
-sub base_url { 'https://example.com/TODO/' }
-sub nntp_url { [] }
+sub thing_type { 'external index' }
 
 no warnings 'once';
+*base_url = \&PublicInbox::Inbox::base_url;
 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
 *modified = \&PublicInbox::Inbox::modified;
-*recent = \&PublicInbox::Inbox::recent;
 
 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
+*isrch = \&search;
 
 1;