Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.96% |
48 / 49 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| LocalIdLookup | |
100.00% |
48 / 48 |
|
100.00% |
6 / 6 |
23 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| isAttached | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| isWikiSharingOurUsers | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
| lookupCentralIds | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
6 | |||
| lookupUserNamesWithFilter | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
8 | |||
| getScope | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\User\CentralId; |
| 8 | |
| 9 | use MediaWiki\Block\HideUserUtils; |
| 10 | use MediaWiki\Config\Config; |
| 11 | use MediaWiki\MainConfigNames; |
| 12 | use MediaWiki\User\UserIdentity; |
| 13 | use MediaWiki\WikiMap\WikiMap; |
| 14 | use Wikimedia\Rdbms\DBAccessObjectUtils; |
| 15 | use Wikimedia\Rdbms\IConnectionProvider; |
| 16 | use Wikimedia\Rdbms\IDBAccessObject; |
| 17 | |
| 18 | /** |
| 19 | * A CentralIdLookup provider that just uses local IDs. Useful if the wiki |
| 20 | * isn't part of a cluster or you're using shared user tables. |
| 21 | * |
| 22 | * @note Shared user table support expects that all wikis involved have |
| 23 | * $wgSharedDB and $wgSharedTables set, and that all wikis involved in the |
| 24 | * sharing are listed in $wgLocalDatabases, and that no wikis not involved in |
| 25 | * the sharing are listed in $wgLocalDatabases. |
| 26 | * @since 1.27 |
| 27 | */ |
| 28 | class LocalIdLookup extends CentralIdLookup { |
| 29 | |
| 30 | private readonly ?string $sharedDB; |
| 31 | |
| 32 | /** @var string[] */ |
| 33 | private readonly array $sharedTables; |
| 34 | |
| 35 | /** @var string[] */ |
| 36 | private readonly array $localDatabases; |
| 37 | |
| 38 | public function __construct( |
| 39 | Config $config, |
| 40 | private readonly IConnectionProvider $dbProvider, |
| 41 | private readonly HideUserUtils $hideUserUtils, |
| 42 | ) { |
| 43 | $this->sharedDB = $config->get( MainConfigNames::SharedDB ); |
| 44 | $this->sharedTables = $config->get( MainConfigNames::SharedTables ); |
| 45 | $this->localDatabases = $config->get( MainConfigNames::LocalDatabases ); |
| 46 | } |
| 47 | |
| 48 | /** @inheritDoc */ |
| 49 | public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool { |
| 50 | // If the user has no ID, it can't be attached |
| 51 | if ( !$user->isRegistered() ) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | return $this->isWikiSharingOurUsers( $wikiId ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Check if the specified wiki shares a user table with the current wiki |
| 60 | * |
| 61 | * @param string|false $wikiId |
| 62 | * @return bool |
| 63 | */ |
| 64 | private function isWikiSharingOurUsers( $wikiId ) { |
| 65 | // Easy case, the local wiki |
| 66 | if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) { |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | // Assume that shared user tables are set up as described above, if |
| 71 | // they're being used at all. |
| 72 | return $this->sharedDB !== null && |
| 73 | in_array( 'user', $this->sharedTables, true ) && |
| 74 | in_array( $wikiId, $this->localDatabases, true ); |
| 75 | } |
| 76 | |
| 77 | /** @inheritDoc */ |
| 78 | public function lookupCentralIds( |
| 79 | array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL |
| 80 | ): array { |
| 81 | if ( !$idToName ) { |
| 82 | return []; |
| 83 | } |
| 84 | $audience = $this->checkAudience( $audience ); |
| 85 | $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags ); |
| 86 | $queryBuilder = $db->newSelectQueryBuilder(); |
| 87 | $queryBuilder |
| 88 | ->select( [ 'user_id', 'user_name' ] ) |
| 89 | ->from( 'user' ) |
| 90 | ->where( [ 'user_id' => array_map( 'intval', array_keys( $idToName ) ) ] ) |
| 91 | ->recency( $flags ); |
| 92 | |
| 93 | if ( $audience && !$audience->isAllowed( 'hideuser' ) ) { |
| 94 | $this->hideUserUtils->addFieldToBuilder( $queryBuilder ); |
| 95 | } |
| 96 | |
| 97 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 98 | foreach ( $res as $row ) { |
| 99 | $idToName[$row->user_id] = empty( $row->hu_deleted ) ? $row->user_name : ''; |
| 100 | } |
| 101 | |
| 102 | return $idToName; |
| 103 | } |
| 104 | |
| 105 | /** @inheritDoc */ |
| 106 | public function lookupUserNamesWithFilter( |
| 107 | array $nameToId, $filter, $audience = self::AUDIENCE_PUBLIC, |
| 108 | $flags = IDBAccessObject::READ_NORMAL, |
| 109 | $wikiId = UserIdentity::LOCAL |
| 110 | ): array { |
| 111 | if ( !$nameToId ) { |
| 112 | return []; |
| 113 | } |
| 114 | if ( ( $filter === self::FILTER_ATTACHED || $filter === self::FILTER_OWNED ) |
| 115 | && !$this->isWikiSharingOurUsers( $wikiId ) |
| 116 | ) { |
| 117 | // No users pass the filter |
| 118 | return $nameToId; |
| 119 | } |
| 120 | |
| 121 | $audience = $this->checkAudience( $audience ); |
| 122 | $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags ); |
| 123 | $queryBuilder = $db->newSelectQueryBuilder(); |
| 124 | $queryBuilder |
| 125 | ->select( [ 'user_id', 'user_name' ] ) |
| 126 | ->from( 'user' ) |
| 127 | ->where( [ 'user_name' => array_map( 'strval', array_keys( $nameToId ) ) ] ) |
| 128 | ->recency( $flags ); |
| 129 | |
| 130 | if ( $audience && !$audience->isAllowed( 'hideuser' ) ) { |
| 131 | $queryBuilder->andWhere( $this->hideUserUtils->getExpression( $db ) ); |
| 132 | } |
| 133 | |
| 134 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 135 | foreach ( $res as $row ) { |
| 136 | $nameToId[$row->user_name] = (int)$row->user_id; |
| 137 | } |
| 138 | |
| 139 | return $nameToId; |
| 140 | } |
| 141 | |
| 142 | /** @inheritDoc */ |
| 143 | public function getScope(): string { |
| 144 | return $this->getProviderId() . ':' |
| 145 | . strtr( $this->sharedDB ?? WikiMap::getCurrentWikiId(), [ ':' => '-' ] ); |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | /** @deprecated class alias since 1.41 */ |
| 151 | class_alias( LocalIdLookup::class, 'LocalIdLookup' ); |