(bug 33602) list=allusers throws exceptions with invalid names
authorSzymon Świerkosz <beau@adres.pl>
Sun, 15 Apr 2012 08:26:33 +0000 (10:26 +0200)
committerSzymon Świerkosz <beau@adres.pl>
Sun, 15 Apr 2012 08:37:03 +0000 (10:37 +0200)
Some MediaWiki installations have invalid user names in user table
(most notable example: en.wikipedia). This commit DOES NOT resolve
the issue completely, it only makes the API behave more gracefully.

I have replaced User::newFromName call with User::newFromId, so the
User object should be always constructed. In case it was not, an
empty array will be provided instead of null when returning user
rights. I have removed keyToTitle calls for ContinueEnumParameter
as user names in the database are stored without underscores
(en.wikipedia has of course one user with _ in the database...).
This should fix invalidtitle error thrown.

There is one issue I don't know how to solve. When API outputs in
query-continue aufrom value with invalid user name, subsequent
call with aufrom set will return an error, because input parameters
'from' and 'to' are passed to keyToTitle method too. Should I
replace it with simple str_replace('_', ' ')?

Change-Id: I7d115e734cb8c93dcf6dc3b98bdbc81975951273

includes/api/ApiQueryAllUsers.php

index ac112ef..3c19c18 100644 (file)
@@ -190,15 +190,14 @@ class ApiQueryAllUsers extends ApiQueryBase {
                                        $lastUserData = null;
 
                                        if ( !$fit ) {
-                                               $this->setContinueEnumParameter( 'from',
-                                                               $this->keyToTitle( $lastUserData['name'] ) );
+                                               $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
                                                break;
                                        }
                                }
 
                                if ( $count > $limit ) {
                                        // We've reached the one extra which shows that there are additional pages to be had. Stop here...
-                                       $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
+                                       $this->setContinueEnumParameter( 'from', $row->user_name );
                                        break;
                                }
 
@@ -235,17 +234,23 @@ class ApiQueryAllUsers extends ApiQueryBase {
                                        'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
                        }
 
-                       $lastUserObj = User::newFromName( $lastUser );
+                       $lastUserObj = User::newFromId( $row->user_id );
 
                        // Add user's group info
                        if ( $fld_groups ) {
-                               if ( !isset( $lastUserData['groups'] ) && $lastUserObj ) {
-                                       $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
+                               if ( !isset( $lastUserData['groups'] ) ) {
+                                       if ( $lastUserObj ) {
+                                               $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
+                                       } else {
+                                               // This should not normally happen
+                                               $lastUserData['groups'] = array();
+                                       }
                                }
 
                                if ( !is_null( $row->ug_group2 ) ) {
                                        $lastUserData['groups'][] = $row->ug_group2;
                                }
+
                                $result->setIndexedTagName( $lastUserData['groups'], 'g' );
                        }
 
@@ -254,13 +259,20 @@ class ApiQueryAllUsers extends ApiQueryBase {
                                $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
                        }
                        if ( $fld_rights ) {
-                               if ( !isset( $lastUserData['rights'] ) && $lastUserObj ) {
-                                       $lastUserData['rights'] =  User::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
+                               if ( !isset( $lastUserData['rights'] ) ) {
+                                       if ( $lastUserObj ) {
+                                               $lastUserData['rights'] =  User::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
+                                       } else {
+                                               // This should not normally happen
+                                               $lastUserData['rights'] = array();
+                                       }
                                }
+
                                if ( !is_null( $row->ug_group2 ) ) {
                                        $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
                                                User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
                                }
+
                                $result->setIndexedTagName( $lastUserData['rights'], 'r' );
                        }
                }
@@ -269,8 +281,7 @@ class ApiQueryAllUsers extends ApiQueryBase {
                        $fit = $result->addValue( array( 'query', $this->getModuleName() ),
                                null, $lastUserData );
                        if ( !$fit ) {
-                               $this->setContinueEnumParameter( 'from',
-                                       $this->keyToTitle( $lastUserData['name'] ) );
+                               $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
                        }
                }