| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\User\CentralId; |
| 24 |
|
| 25 |
use MediaWiki\Block\HideUserUtils; |
| 26 |
use MediaWiki\Config\Config; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\User\UserIdentity; |
| 29 |
use MediaWiki\WikiMap\WikiMap; |
| 30 |
use Wikimedia\Rdbms\DBAccessObjectUtils; |
| 31 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 32 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class LocalIdLookup extends CentralIdLookup { |
| 45 |
|
| 46 |
private IConnectionProvider $dbProvider; |
| 47 |
private HideUserUtils $hideUserUtils; |
| 48 |
|
| 49 |
|
| 50 |
private $sharedDB; |
| 51 |
|
| 52 |
|
| 53 |
private $sharedTables; |
| 54 |
|
| 55 |
|
| 56 |
private $localDatabases; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( |
| 64 |
Config $config, |
| 65 |
IConnectionProvider $dbProvider, |
| 66 |
HideUserUtils $hideUserUtils |
| 67 |
) { |
| 68 |
$this->sharedDB = $config->get( MainConfigNames::SharedDB ); |
| 69 |
$this->sharedTables = $config->get( MainConfigNames::SharedTables ); |
| 70 |
$this->localDatabases = $config->get( MainConfigNames::LocalDatabases ); |
| 71 |
$this->dbProvider = $dbProvider; |
| 72 |
$this->hideUserUtils = $hideUserUtils; |
| 73 |
} |
| 74 |
|
| 75 |
public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool { |
| 76 |
|
| 77 |
if ( !$user->isRegistered() ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
return $this->sharedDB !== null && |
| 89 |
in_array( 'user', $this->sharedTables, true ) && |
| 90 |
in_array( $wikiId, $this->localDatabases, true ); |
| 91 |
} |
| 92 |
|
| 93 |
public function lookupCentralIds( |
| 94 |
array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL |
| 95 |
): array { |
| 96 |
if ( !$idToName ) { |
| 97 |
return []; |
| 98 |
} |
| 99 |
$audience = $this->checkAudience( $audience ); |
| 100 |
$db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags ); |
| 101 |
$queryBuilder = $db->newSelectQueryBuilder(); |
| 102 |
$queryBuilder |
| 103 |
->select( [ 'user_id', 'user_name' ] ) |
| 104 |
->from( 'user' ) |
| 105 |
->where( [ 'user_id' => array_map( 'intval', array_keys( $idToName ) ) ] ) |
| 106 |
->recency( $flags ); |
| 107 |
|
| 108 |
if ( $audience && !$audience->isAllowed( 'hideuser' ) ) { |
| 109 |
$this->hideUserUtils->addFieldToBuilder( $queryBuilder ); |
| 110 |
} |
| 111 |
|
| 112 |
$res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 113 |
foreach ( $res as $row ) { |
| 114 |
$idToName[$row->user_id] = empty( $row->hu_deleted ) ? $row->user_name : ''; |
| 115 |
} |
| 116 |
|
| 117 |
return $idToName; |
| 118 |
} |
| 119 |
|
| 120 |
public function lookupUserNames( |
| 121 |
array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL |
| 122 |
): array { |
| 123 |
if ( !$nameToId ) { |
| 124 |
return []; |
| 125 |
} |
| 126 |
|
| 127 |
$audience = $this->checkAudience( $audience ); |
| 128 |
$db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, $flags ); |
| 129 |
$queryBuilder = $db->newSelectQueryBuilder(); |
| 130 |
$queryBuilder |
| 131 |
->select( [ 'user_id', 'user_name' ] ) |
| 132 |
->from( 'user' ) |
| 133 |
->where( [ 'user_name' => array_map( 'strval', array_keys( $nameToId ) ) ] ) |
| 134 |
->recency( $flags ); |
| 135 |
|
| 136 |
if ( $audience && !$audience->isAllowed( 'hideuser' ) ) { |
| 137 |
$queryBuilder->andWhere( $this->hideUserUtils->getExpression( $db ) ); |
| 138 |
} |
| 139 |
|
| 140 |
$res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 141 |
foreach ( $res as $row ) { |
| 142 |
$nameToId[$row->user_name] = (int)$row->user_id; |
| 143 |
} |
| 144 |
|
| 145 |
return $nameToId; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
class_alias( LocalIdLookup::class, 'LocalIdLookup' ); |