Merge "(bug 19195) Make user IDs more readily available with the API"
authorReedy <reedy@wikimedia.org>
Thu, 10 May 2012 18:47:42 +0000 (18:47 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 10 May 2012 18:47:42 +0000 (18:47 +0000)
1  2 
includes/api/ApiBlock.php
includes/api/ApiQueryAllUsers.php
includes/api/ApiQueryUserInfo.php
includes/api/ApiQueryUsers.php
includes/api/ApiUnblock.php

@@@ -103,9 -103,11 +103,11 @@@ class ApiBlock extends ApiBase 
                        $res['expiry'] = $block->mExpiry == wfGetDB( DB_SLAVE )->getInfinity()
                                ? 'infinite'
                                : wfTimestamp( TS_ISO_8601, $block->mExpiry );
+                       $res['id'] = $block->getId();
                } else {
                        # should be unreachable
                        $res['expiry'] = '';
+                       $res['id'] = '';
                }
  
                $res['reason'] = $params['reason'];
                                ApiBase::PARAM_REQUIRED => true
                        ),
                        'token' => null,
 -                      'gettoken' => false,
 +                      'gettoken' => array(
 +                              ApiBase::PARAM_DFLT => false,
 +                              ApiBase::PARAM_DEPRECATED => true,
 +                      ),
                        'expiry' => 'never',
                        'reason' => null,
                        'anononly' => false,
        public function getParamDescription() {
                return array(
                        'user' => 'Username, IP address or IP range you want to block',
 -                      'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
 +                      'token' => 'A block token previously obtained through prop=info',
                        'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
                        'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
                        'reason' => 'Reason for block (optional)',
@@@ -34,16 -34,6 +34,16 @@@ class ApiQueryAllUsers extends ApiQuery
                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();
@@@ -67,8 -57,8 +67,8 @@@
                $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.
@@@ -78,7 -68,7 +78,7 @@@
  
                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'] ) ) {
                                        $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;
                                }
  
                                        'name' => $lastUser,
                                );
                                if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
+                                       $lastUserData['blockid'] = $row->ipb_id;
                                        $lastUserData['blockedby'] = $row->ipb_by_text;
+                                       $lastUserData['blockedbyid'] = $row->ipb_by;
                                        $lastUserData['blockreason'] = $row->ipb_reason;
                                        $lastUserData['blockexpiry'] = $row->ipb_expiry;
                                }
                                        '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' );
                        }
  
                                $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' );
                        }
                }
                        $fit = $result->addValue( array( 'query', $this->getModuleName() ),
                                null, $lastUserData );
                        if ( !$fit ) {
 -                              $this->setContinueEnumParameter( 'from',
 -                                      $this->keyToTitle( $lastUserData['name'] ) );
 +                              $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
                        }
                }
  
@@@ -50,7 -50,7 +50,7 @@@ class ApiQueryUserInfo extends ApiQuery
        }
  
        protected function getCurrentUserInfo() {
 -              global $wgRequest, $wgHiddenPrefs;
 +              global $wgHiddenPrefs;
                $user = $this->getUser();
                $result = $this->getResult();
                $vals = array();
  
                if ( isset( $this->prop['blockinfo'] ) ) {
                        if ( $user->isBlocked() ) {
-                               $vals['blockedby'] = User::whoIs( $user->blockedBy() );
+                               $block = $user->getBlock();
+                               $vals['blockid'] = $block->getId();
+                               $vals['blockedby'] = $block->getByName();
+                               $vals['blockedbyid'] = $block->getBy();
                                $vals['blockreason'] = $user->blockedFor();
                        }
                }
                }
  
                if ( isset( $this->prop['acceptlang'] ) ) {
 -                      $langs = $wgRequest->getAcceptLang();
 +                      $langs = $this->getRequest()->getAcceptLang();
                        $acceptLang = array();
                        foreach ( $langs as $lang => $val ) {
                                $r = array( 'q' => $val );
@@@ -61,10 -61,10 +61,10 @@@ class ApiQueryUsers extends ApiQueryBas
                return $this->tokenFunctions;
        }
  
 -       /**
 -        * @param $user User
 -        * @return String
 -        */
 +      /**
 +       * @param $user User
 +       * @return String
 +       */
        public static function getUserrightsToken( $user ) {
                global $wgUser;
                // Since the permissions check for userrights is non-trivial,
                                        $data[$name]['hidden'] = '';
                                }
                                if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
+                                       $data[$name]['blockid'] = $row->ipb_id;
                                        $data[$name]['blockedby'] = $row->ipb_by_text;
+                                       $data[$name]['blockedbyid'] = $row->ipb_by;
                                        $data[$name]['blockreason'] = $row->ipb_reason;
                                        $data[$name]['blockexpiry'] = $row->ipb_expiry;
                                }
@@@ -78,7 -78,9 +78,9 @@@ class ApiUnblock extends ApiBase 
                }
  
                $res['id'] = $block->getId();
-               $res['user'] = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
+               $target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
+               $res['user'] = $target;
+               $res['userid'] = $target instanceof User ? $target->getId() : 0;
                $res['reason'] = $params['reason'];
                $this->getResult()->addValue( null, $this->getModuleName(), $res );
        }
                        ),
                        'user' => null,
                        'token' => null,
 -                      'gettoken' => false,
 +                      'gettoken' => array(
 +                              ApiBase::PARAM_DFLT => false,
 +                              ApiBase::PARAM_DEPRECATED => true,
 +                      ),
                        'reason' => null,
                );
        }
                return array(
                        'id' => "ID of the block you want to unblock (obtained through list=blocks). Cannot be used together with {$p}user",
                        'user' => "Username, IP address or IP range you want to unblock. Cannot be used together with {$p}id",
 -                      'token' => "An unblock token previously obtained through the gettoken parameter or {$p}prop=info",
 +                      'token' => "An unblock token previously obtained through prop=info",
                        'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',
                        'reason' => 'Reason for unblock (optional)',
                );