Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.78% |
44 / 45 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
LocalIdLookup | |
100.00% |
44 / 44 |
|
100.00% |
4 / 4 |
18 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
isAttached | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
lookupCentralIds | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
6 | |||
lookupUserNames | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | /** |
3 | * A central user id lookup service implementation |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
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 | * A CentralIdLookup provider that just uses local IDs. Useful if the wiki |
36 | * isn't part of a cluster or you're using shared user tables. |
37 | * |
38 | * @note Shared user table support expects that all wikis involved have |
39 | * $wgSharedDB and $wgSharedTables set, and that all wikis involved in the |
40 | * sharing are listed in $wgLocalDatabases, and that no wikis not involved in |
41 | * the sharing are listed in $wgLocalDatabases. |
42 | * @since 1.27 |
43 | */ |
44 | class LocalIdLookup extends CentralIdLookup { |
45 | |
46 | private IConnectionProvider $dbProvider; |
47 | private HideUserUtils $hideUserUtils; |
48 | |
49 | /** @var string|null */ |
50 | private $sharedDB; |
51 | |
52 | /** @var string[] */ |
53 | private $sharedTables; |
54 | |
55 | /** @var string[] */ |
56 | private $localDatabases; |
57 | |
58 | /** |
59 | * @param Config $config |
60 | * @param IConnectionProvider $dbProvider |
61 | * @param HideUserUtils $hideUserUtils |
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 | // If the user has no ID, it can't be attached |
77 | if ( !$user->isRegistered() ) { |
78 | return false; |
79 | } |
80 | |
81 | // Easy case, we're checking locally |
82 | if ( !$wikiId || WikiMap::isCurrentWikiId( $wikiId ) ) { |
83 | return true; |
84 | } |
85 | |
86 | // Assume that shared user tables are set up as described above, if |
87 | // they're being used at all. |
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 | /** @deprecated class alias since 1.41 */ |
150 | class_alias( LocalIdLookup::class, 'LocalIdLookup' ); |