Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 125 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryGlobalUserInfo | |
0.00% |
0 / 125 |
|
0.00% |
0 / 5 |
1332 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 90 |
|
0.00% |
0 / 1 |
930 | |||
getCacheMode | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getAllowedParams | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Created on Jan 30, 2010 |
4 | * |
5 | * CentralAuth extension |
6 | * |
7 | * Copyright (C) 2010 Roan Kattouw roan DOT kattouw AT gmail DOT com |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 2 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., |
21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
22 | * http://www.gnu.org/copyleft/gpl.html |
23 | */ |
24 | |
25 | namespace MediaWiki\Extension\CentralAuth\Api; |
26 | |
27 | use MediaWiki\Api\ApiBase; |
28 | use MediaWiki\Api\ApiQuery; |
29 | use MediaWiki\Api\ApiQueryBase; |
30 | use MediaWiki\Extension\CentralAuth\User\CentralAuthUser; |
31 | use MediaWiki\ParamValidator\TypeDef\UserDef; |
32 | use MediaWiki\User\UserNameUtils; |
33 | use MediaWiki\WikiMap\WikiMap; |
34 | use Wikimedia\ParamValidator\ParamValidator; |
35 | |
36 | /** |
37 | * Query module to list global user info and attachments |
38 | * |
39 | * @ingroup API |
40 | * @ingroup Extensions |
41 | */ |
42 | class ApiQueryGlobalUserInfo extends ApiQueryBase { |
43 | |
44 | private UserNameUtils $userNameUtils; |
45 | |
46 | public function __construct( |
47 | ApiQuery $query, |
48 | string $moduleName, |
49 | UserNameUtils $userNameUtils |
50 | ) { |
51 | parent::__construct( $query, $moduleName, 'gui' ); |
52 | $this->userNameUtils = $userNameUtils; |
53 | } |
54 | |
55 | public function execute() { |
56 | $params = $this->extractRequestParams(); |
57 | $prop = array_flip( (array)$params['prop'] ); |
58 | if ( $params['user'] === null && $params['id'] === null ) { |
59 | $params['user'] = $this->getUser()->getName(); |
60 | } |
61 | |
62 | if ( $params['user'] === null ) { |
63 | $user = CentralAuthUser::newFromId( $params['id'] ); |
64 | if ( $user === false ) { |
65 | $this->dieWithError( [ 'apierror-invaliduserid', wfEscapeWikiText( $params['id'] ) ] ); |
66 | } |
67 | } else { |
68 | $username = $this->userNameUtils->getCanonical( $params['user'] ); |
69 | if ( $username === false ) { |
70 | $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] ); |
71 | } |
72 | $user = CentralAuthUser::getInstanceByName( $username ); |
73 | } |
74 | |
75 | // Add basic info |
76 | $result = $this->getResult(); |
77 | $data = []; |
78 | $userExists = $user->exists(); |
79 | |
80 | if ( $userExists && |
81 | ( $user->getHiddenLevelInt() === CentralAuthUser::HIDDEN_LEVEL_NONE || |
82 | $this->getAuthority()->isAllowed( 'centralauth-suppress' ) ) |
83 | ) { |
84 | // The global user exists and it's not hidden or the current user is allowed to see it |
85 | $data['home'] = $user->getHomeWiki(); |
86 | $data['id'] = $user->getId(); |
87 | $data['registration'] = wfTimestamp( TS_ISO_8601, $user->getRegistration() ); |
88 | $data['name'] = $user->getName(); |
89 | |
90 | if ( $user->isLocked() ) { |
91 | $data['locked'] = true; |
92 | } |
93 | if ( $user->isHidden() ) { |
94 | $data['hidden'] = true; |
95 | } |
96 | } else { |
97 | // The user doesn't exist or we pretend it doesn't if it's hidden |
98 | $data['missing'] = true; |
99 | |
100 | // If we are pretending that the user doesn't exist because it is hidden, |
101 | // do not add any more information |
102 | $userExists = false; |
103 | } |
104 | $result->addValue( 'query', $this->getModuleName(), $data ); |
105 | |
106 | // Add requested info |
107 | if ( $userExists && isset( $prop['groups'] ) ) { |
108 | $groups = $user->getGlobalGroups(); |
109 | $result->setIndexedTagName( $groups, 'g' ); |
110 | $result->addValue( [ 'query', $this->getModuleName() ], 'groups', $groups ); |
111 | } |
112 | if ( $userExists && isset( $prop['rights'] ) ) { |
113 | $rights = $user->getGlobalRights(); |
114 | $result->setIndexedTagName( $rights, 'r' ); |
115 | $result->addValue( [ 'query', $this->getModuleName() ], 'rights', $rights ); |
116 | } |
117 | |
118 | $attachedAccounts = null; |
119 | if ( $userExists && ( isset( $prop['merged'] ) || isset( $prop['editcount'] ) ) ) { |
120 | $attachedAccounts = $user->queryAttached(); |
121 | } |
122 | |
123 | if ( $userExists && isset( $prop['merged'] ) ) { |
124 | foreach ( $attachedAccounts as $account ) { |
125 | $dbname = $account['wiki']; |
126 | $wiki = WikiMap::getWiki( $dbname ); |
127 | $a = [ |
128 | 'wiki' => $dbname, |
129 | 'url' => $wiki->getCanonicalServer(), |
130 | 'id' => intval( $account['id'] ), |
131 | 'timestamp' => wfTimestamp( TS_ISO_8601, $account['attachedTimestamp'] ), |
132 | 'method' => $account['attachedMethod'], |
133 | 'editcount' => intval( $account['editCount'] ), |
134 | 'registration' => wfTimestamp( TS_ISO_8601, $account['registration'] ), |
135 | ]; |
136 | if ( $account['groupMemberships'] ) { |
137 | $a['groups'] = array_keys( $account['groupMemberships'] ); |
138 | $result->setIndexedTagName( $a['groups'], 'group' ); |
139 | } |
140 | |
141 | if ( $account['blocked'] ) { |
142 | $a['blocked'] = [ |
143 | 'expiry' => $this->getLanguage()->formatExpiry( |
144 | $account['block-expiry'], TS_ISO_8601 ), |
145 | 'reason' => $account['block-reason'] |
146 | ]; |
147 | } |
148 | $result->addValue( [ 'query', $this->getModuleName(), 'merged' ], null, $a ); |
149 | } |
150 | $result->addIndexedTagName( [ 'query', $this->getModuleName(), 'merged' ], 'account' ); |
151 | } |
152 | if ( $userExists && isset( $prop['editcount'] ) ) { |
153 | $editcount = 0; |
154 | foreach ( $attachedAccounts as $account ) { |
155 | $editcount += $account['editCount']; |
156 | } |
157 | $result->addValue( 'query', $this->getModuleName(), [ 'editcount' => $editcount ] ); |
158 | } |
159 | if ( isset( $prop['unattached'] ) ) { |
160 | $accounts = $user->queryUnattached(); |
161 | foreach ( $accounts as $account ) { |
162 | $a = [ |
163 | 'wiki' => $account['wiki'], |
164 | 'editcount' => $account['editCount'], |
165 | 'registration' => wfTimestamp( TS_ISO_8601, $account['registration'] ), |
166 | ]; |
167 | |
168 | if ( $account['groupMemberships'] ) { |
169 | $a['groups'] = array_keys( $account['groupMemberships'] ); |
170 | $result->setIndexedTagName( $a['groups'], 'group' ); |
171 | } |
172 | |
173 | if ( $account['blocked'] ) { |
174 | $a['blocked'] = [ |
175 | 'expiry' => $this->getLanguage()->formatExpiry( |
176 | $account['block-expiry'], TS_ISO_8601 ), |
177 | 'reason' => $account['block-reason'] |
178 | ]; |
179 | } |
180 | $result->addValue( [ 'query', $this->getModuleName(), 'unattached' ], null, $a ); |
181 | } |
182 | $result->addIndexedTagName( |
183 | [ 'query', $this->getModuleName(), 'unattached' ], 'account' |
184 | ); |
185 | } |
186 | } |
187 | |
188 | /** @inheritDoc */ |
189 | public function getCacheMode( $params ) { |
190 | if ( $params['user'] !== null || $params['id'] !== null ) { |
191 | // URL determines user, public caching is fine |
192 | return 'public'; |
193 | } else { |
194 | // Code will fall back to the context user, don't cache |
195 | return 'private'; |
196 | } |
197 | } |
198 | |
199 | /** @inheritDoc */ |
200 | public function getAllowedParams() { |
201 | return [ |
202 | 'user' => [ |
203 | ParamValidator::PARAM_TYPE => 'user', |
204 | UserDef::PARAM_ALLOWED_USER_TYPES => [ |
205 | 'name', |
206 | 'temp', |
207 | 'interwiki', |
208 | ], |
209 | ], |
210 | 'id' => [ |
211 | ParamValidator::PARAM_TYPE => 'integer', |
212 | ], |
213 | 'prop' => [ |
214 | ParamValidator::PARAM_TYPE => [ |
215 | 'groups', |
216 | 'rights', |
217 | 'merged', |
218 | 'unattached', |
219 | 'editcount' |
220 | ], |
221 | ParamValidator::PARAM_ISMULTI => true, |
222 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
223 | ] |
224 | ]; |
225 | } |
226 | |
227 | /** @inheritDoc */ |
228 | protected function getExamplesMessages() { |
229 | return [ |
230 | 'action=query&meta=globaluserinfo' |
231 | => 'apihelp-query+globaluserinfo-example-1', |
232 | 'action=query&meta=globaluserinfo&guiuser=Example&guiprop=groups|merged|unattached' |
233 | => 'apihelp-query+globaluserinfo-example-2', |
234 | ]; |
235 | } |
236 | } |