Merge "(bug 19195) Make user IDs more readily available with the API"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
index 0f2db10..01cb15a 100644 (file)
@@ -34,6 +34,16 @@ class ApiQueryAllUsers extends ApiQueryBase {
                parent::__construct( $query, $moduleName, 'au' );
        }
 
+       /**
+        * This function converts the user name to a canonical form
+        * which is stored in the database.
+        * @param String $name
+        * @return String
+        */
+       private function getCanonicalUserName( $name ) {
+               return str_replace( '_', ' ', $name );
+       }
+
        public function execute() {
                $db = $this->getDB();
                $params = $this->extractRequestParams();
@@ -57,8 +67,8 @@ class ApiQueryAllUsers extends ApiQueryBase {
                $useIndex = true;
 
                $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
-               $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
-               $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
+               $from = is_null( $params['from'] ) ? null : $this->getCanonicalUserName( $params['from'] );
+               $to = is_null( $params['to'] ) ? null : $this->getCanonicalUserName( $params['to'] );
 
                # MySQL doesn't seem to use 'equality propagation' here, so like the
                # ActiveUsers special page, we have to use rc_user_text for some cases.
@@ -68,7 +78,7 @@ class ApiQueryAllUsers extends ApiQueryBase {
 
                if ( !is_null( $params['prefix'] ) ) {
                        $this->addWhere( $userFieldToSort .
-                               $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
+                               $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
                }
 
                if ( !is_null( $params['rights'] ) ) {
@@ -190,15 +200,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;
                                }
 
@@ -237,17 +246,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' );
                        }
 
@@ -256,13 +271,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' );
                        }
                }
@@ -271,8 +293,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'] );
                        }
                }