r49013 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r49012‎ | r49013 | r49014 >
Date:16:39, 29 March 2009
Author:catrope
Status:ok
Tags:
Comment:
Remove ugly userrights-CentralAuth hack introduced in r48970 and friends:
* Make UserrightsForm::doSaveUserGroups(), addLogEntry() and helpers non-static again so CentralAuth can override them; remove the short-lived UserRightsLogEntry hook
* Let UserrightsForm::fetchUser() return a WikiErrorMsg on failure
* In ApiUserrights, use an instance of the UserrightsPage class rather than calling its methods statically. This also enables interwiki userrights in this module
* Add some messages to ApiBase::$messageMap
Modified paths:
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/api/ApiUserrights.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialUserrights.php (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -1440,12 +1440,6 @@
14411441 'remove-self' => array( removable groups from self )
14421442 )
14431443
1444 -'UserRightsLogEntry': before a log entry is added by UserrightsPage::addLogEntry(). If you use this hook to add your own log entries, you can return false to prevent the usual log entry from being added
1445 -$user: User or UserrightsProxy object
1446 -$oldGroups: Array of groups the user was a member of before the change
1447 -$newGroups: Array of groups the user is a member of after the change
1448 -$reason: User-provided summary
1449 -
14501444 'UserRetrieveNewTalks': called when retrieving "You have new messages!" message(s)
14511445 $user: user retrieving new talks messages
14521446 $talks: array of new talks page(s)
Index: trunk/phase3/includes/api/ApiUserrights.php
@@ -42,17 +42,21 @@
4343 $params = $this->extractRequestParams();
4444 if(is_null($params['user']))
4545 $this->dieUsageMsg(array('missingparam', 'user'));
46 - $user = User::newFromName($params['user']);
47 - if($user->isAnon())
48 - $this->dieUsageMsg(array('nosuchuser', $params['user']));
4946 if(is_null($params['token']))
5047 $this->dieUsageMsg(array('missingparam', 'token'));
 48+
 49+ $form = new UserrightsPage;
 50+ $user = $form->fetchUser($params['user']);
 51+ if($user instanceof WikiErrorMsg)
 52+ $this->dieUsageMsg(array_merge(
 53+ (array)$user->getMessageKey(),
 54+ $user->getMessageArgs()));
5155 if(!$wgUser->matchEditToken($params['token'], $user->getName()))
5256 $this->dieUsageMsg(array('sessionfailure'));
5357
5458 $r['user'] = $user->getName();
5559 list($r['added'], $r['removed']) =
56 - UserrightsPage::doSaveUserGroups(
 60+ $form->doSaveUserGroups(
5761 $user, (array)$params['add'],
5862 (array)$params['remove'], $params['reason']);
5963
@@ -71,9 +75,7 @@
7276
7377 public function getAllowedParams() {
7478 return array (
75 - 'user' => array(
76 - ApiBase :: PARAM_TYPE => 'user'
77 - ),
 79+ 'user' => null,
7880 'add' => array(
7981 ApiBase :: PARAM_TYPE => User::getAllGroups(),
8082 ApiBase :: PARAM_ISMULTI => true
Index: trunk/phase3/includes/specials/SpecialUserrights.php
@@ -134,7 +134,8 @@
135135 global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
136136
137137 $user = $this->fetchUser( $username );
138 - if( !$user ) {
 138+ if( $user instanceof WikiErrorMsg ) {
 139+ $wgOut->addWikiMsgArray($user->getMessageKey(), $user->getMessageArgs());
139140 return;
140141 }
141142
@@ -153,7 +154,7 @@
154155 $removegroup[] = $group;
155156 }
156157 }
157 - self::doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
 158+ $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
158159 }
159160
160161 /**
@@ -165,7 +166,7 @@
166167 * @param $reason String: reason for group change
167168 * @return Array: Tuple of added, then removed groups
168169 */
169 - static function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
 170+ function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
170171 global $wgUser;
171172
172173 // Validate input set...
@@ -206,7 +207,7 @@
207208 wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
208209
209210 if( $newGroups != $oldGroups ) {
210 - self::addLogEntry( $user, $oldGroups, $newGroups, $reason );
 211+ $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
211212 }
212213 return array( $add, $remove );
213214 }
@@ -215,22 +216,15 @@
216217 /**
217218 * Add a rights log entry for an action.
218219 */
219 - static function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
220 - // Just overriding addLogEntry in a subclass would be cleaner,
221 - // but that requires PHP 5.3 (late static bindings)
222 - if( !wfRunHooks( 'UserRightsLogEntry', array( $user, $oldGroups,
223 - $newGroups, $reason ) ) ) {
224 - return;
225 - }
226 -
 220+ function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
227221 $log = new LogPage( 'rights' );
228222
229223 $log->addEntry( 'rights',
230224 $user->getUserPage(),
231225 $reason,
232226 array(
233 - self::makeGroupNameListForLog( $oldGroups ),
234 - self::makeGroupNameListForLog( $newGroups )
 227+ $this->makeGroupNameListForLog( $oldGroups ),
 228+ $this->makeGroupNameListForLog( $newGroups )
235229 )
236230 );
237231 }
@@ -243,7 +237,8 @@
244238 global $wgOut;
245239
246240 $user = $this->fetchUser( $username );
247 - if( !$user ) {
 241+ if( $user instanceof WikiErrorMsg ) {
 242+ $wgOut->addWikiMsgArray($user->getMessageKey(), $user->getMessageArgs());
248243 return;
249244 }
250245
@@ -261,10 +256,10 @@
262257 * return a user (or proxy) object for manipulating it.
263258 *
264259 * Side effects: error output for invalid access
265 - * @return mixed User, UserRightsProxy, or null
 260+ * @return mixed User, UserRightsProxy, or WikiErrorMsg
266261 */
267262 function fetchUser( $username ) {
268 - global $wgOut, $wgUser, $wgUserrightsInterwikiDelimiter;
 263+ global $wgUser, $wgUserrightsInterwikiDelimiter;
269264
270265 $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
271266 if( count( $parts ) < 2 ) {
@@ -274,18 +269,15 @@
275270 list( $name, $database ) = array_map( 'trim', $parts );
276271
277272 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
278 - $wgOut->addWikiMsg( 'userrights-no-interwiki' );
279 - return null;
 273+ return new WikiErrorMsg( 'userrights-no-interwiki' );
280274 }
281275 if( !UserRightsProxy::validDatabase( $database ) ) {
282 - $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
283 - return null;
 276+ return new WikiErrorMsg( 'userrights-nodatabase', $database );
284277 }
285278 }
286279
287280 if( $name == '' ) {
288 - $wgOut->addWikiMsg( 'nouserspecified' );
289 - return false;
 281+ return new WikiErrorMsg( 'nouserspecified' );
290282 }
291283
292284 if( $name{0} == '#' ) {
@@ -300,8 +292,7 @@
301293 }
302294
303295 if( !$name ) {
304 - $wgOut->addWikiMsg( 'noname' );
305 - return null;
 296+ return new WikiErrorMsg( 'noname' );
306297 }
307298 }
308299
@@ -312,14 +303,13 @@
313304 }
314305
315306 if( !$user || $user->isAnon() ) {
316 - $wgOut->addWikiMsg( 'nosuchusershort', $username );
317 - return null;
 307+ return new WikiErrorMsg( 'nosuchusershort', $username );
318308 }
319309
320310 return $user;
321311 }
322312
323 - static function makeGroupNameList( $ids ) {
 313+ function makeGroupNameList( $ids ) {
324314 if( empty( $ids ) ) {
325315 return wfMsgForContent( 'rightsnone' );
326316 } else {
@@ -327,11 +317,11 @@
328318 }
329319 }
330320
331 - static function makeGroupNameListForLog( $ids ) {
 321+ function makeGroupNameListForLog( $ids ) {
332322 if( empty( $ids ) ) {
333323 return '';
334324 } else {
335 - return self::makeGroupNameList( $ids );
 325+ return $this->makeGroupNameList( $ids );
336326 }
337327 }
338328

Follow-up revisions

RevisionCommit summaryAuthorDate
r49014Followup to r49013: forgot to commit ApiBase.phpcatrope16:40, 29 March 2009
r49015CentralAuth changes for r49013catrope16:42, 29 March 2009

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r48970Redo r48746 (API userrights, reverted in r48909 and r48910) in a way that doe...catrope19:08, 28 March 2009

Status & tagging log