MediaWiki  REL1_20
ApiQueryUsers.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryUsers extends ApiQueryBase {
00033 
00034         private $tokenFunctions, $prop;
00035 
00036         public function __construct( $query, $moduleName ) {
00037                 parent::__construct( $query, $moduleName, 'us' );
00038         }
00039 
00046         protected function getTokenFunctions() {
00047                 // Don't call the hooks twice
00048                 if ( isset( $this->tokenFunctions ) ) {
00049                         return $this->tokenFunctions;
00050                 }
00051 
00052                 // If we're in JSON callback mode, no tokens can be obtained
00053                 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
00054                         return array();
00055                 }
00056 
00057                 $this->tokenFunctions = array(
00058                         'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
00059                 );
00060                 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
00061                 return $this->tokenFunctions;
00062         }
00063 
00068         public static function getUserrightsToken( $user ) {
00069                 global $wgUser;
00070                 // Since the permissions check for userrights is non-trivial,
00071                 // don't bother with it here
00072                 return $wgUser->getEditToken( $user->getName() );
00073         }
00074 
00075         public function execute() {
00076                 $params = $this->extractRequestParams();
00077 
00078                 if ( !is_null( $params['prop'] ) ) {
00079                         $this->prop = array_flip( $params['prop'] );
00080                 } else {
00081                         $this->prop = array();
00082                 }
00083 
00084                 $users = (array)$params['users'];
00085                 $goodNames = $done = array();
00086                 $result = $this->getResult();
00087                 // Canonicalize user names
00088                 foreach ( $users as $u ) {
00089                         $n = User::getCanonicalName( $u );
00090                         if ( $n === false || $n === '' ) {
00091                                 $vals = array( 'name' => $u, 'invalid' => '' );
00092                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00093                                                 null, $vals );
00094                                 if ( !$fit ) {
00095                                         $this->setContinueEnumParameter( 'users',
00096                                                         implode( '|', array_diff( $users, $done ) ) );
00097                                         $goodNames = array();
00098                                         break;
00099                                 }
00100                                 $done[] = $u;
00101                         } else {
00102                                 $goodNames[] = $n;
00103                         }
00104                 }
00105 
00106                 $result = $this->getResult();
00107 
00108                 if ( count( $goodNames ) ) {
00109                         $this->addTables( 'user' );
00110                         $this->addFields( User::selectFields() );
00111                         $this->addWhereFld( 'user_name', $goodNames );
00112 
00113                         if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
00114                                 $this->addTables( 'user_groups' );
00115                                 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=user_id' ) ) );
00116                                 $this->addFields( 'ug_group' );
00117                         }
00118 
00119                         $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
00120 
00121                         $data = array();
00122                         $res = $this->select( __METHOD__ );
00123 
00124                         foreach ( $res as $row ) {
00125                                 $user = User::newFromRow( $row );
00126                                 $name = $user->getName();
00127 
00128                                 $data[$name]['userid'] = $user->getId();
00129                                 $data[$name]['name'] = $name;
00130 
00131                                 if ( isset( $this->prop['editcount'] ) ) {
00132                                         $data[$name]['editcount'] = intval( $user->getEditCount() );
00133                                 }
00134 
00135                                 if ( isset( $this->prop['registration'] ) ) {
00136                                         $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
00137                                 }
00138 
00139                                 if ( isset( $this->prop['groups'] ) ) {
00140                                         if ( !isset( $data[$name]['groups'] ) ) {
00141                                                 $data[$name]['groups'] = $user->getAutomaticGroups();
00142                                         }
00143 
00144                                         if ( !is_null( $row->ug_group ) ) {
00145                                                 // This row contains only one group, others will be added from other rows
00146                                                 $data[$name]['groups'][] = $row->ug_group;
00147                                         }
00148                                 }
00149 
00150                                 if ( isset( $this->prop['implicitgroups'] ) && !isset( $data[$name]['implicitgroups'] ) ) {
00151                                         $data[$name]['implicitgroups'] =  $user->getAutomaticGroups();
00152                                 }
00153 
00154                                 if ( isset( $this->prop['rights'] ) ) {
00155                                         if ( !isset( $data[$name]['rights'] ) ) {
00156                                                 $data[$name]['rights'] = User::getGroupPermissions( $user->getAutomaticGroups() );
00157                                         }
00158 
00159                                         if ( !is_null( $row->ug_group ) ) {
00160                                                 $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
00161                                                         User::getGroupPermissions( array( $row->ug_group ) ) ) );
00162                                         }
00163                                 }
00164                                 if ( $row->ipb_deleted ) {
00165                                         $data[$name]['hidden'] = '';
00166                                 }
00167                                 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
00168                                         $data[$name]['blockid'] = $row->ipb_id;
00169                                         $data[$name]['blockedby'] = $row->ipb_by_text;
00170                                         $data[$name]['blockedbyid'] = $row->ipb_by;
00171                                         $data[$name]['blockreason'] = $row->ipb_reason;
00172                                         $data[$name]['blockexpiry'] = $row->ipb_expiry;
00173                                 }
00174 
00175                                 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
00176                                         $data[$name]['emailable'] = '';
00177                                 }
00178 
00179                                 if ( isset( $this->prop['gender'] ) ) {
00180                                         $gender = $user->getOption( 'gender' );
00181                                         if ( strval( $gender ) === '' ) {
00182                                                 $gender = 'unknown';
00183                                         }
00184                                         $data[$name]['gender'] = $gender;
00185                                 }
00186 
00187                                 if ( !is_null( $params['token'] ) ) {
00188                                         $tokenFunctions = $this->getTokenFunctions();
00189                                         foreach ( $params['token'] as $t ) {
00190                                                 $val = call_user_func( $tokenFunctions[$t], $user );
00191                                                 if ( $val === false ) {
00192                                                         $this->setWarning( "Action '$t' is not allowed for the current user" );
00193                                                 } else {
00194                                                         $data[$name][$t . 'token'] = $val;
00195                                                 }
00196                                         }
00197                                 }
00198                         }
00199                 }
00200 
00201                 // Second pass: add result data to $retval
00202                 foreach ( $goodNames as $u ) {
00203                         if ( !isset( $data[$u] ) ) {
00204                                 $data[$u] = array( 'name' => $u );
00205                                 $urPage = new UserrightsPage;
00206                                 $iwUser = $urPage->fetchUser( $u );
00207 
00208                                 if ( $iwUser instanceof UserRightsProxy ) {
00209                                         $data[$u]['interwiki'] = '';
00210 
00211                                         if ( !is_null( $params['token'] ) ) {
00212                                                 $tokenFunctions = $this->getTokenFunctions();
00213 
00214                                                 foreach ( $params['token'] as $t ) {
00215                                                         $val = call_user_func( $tokenFunctions[$t], $iwUser );
00216                                                         if ( $val === false ) {
00217                                                                 $this->setWarning( "Action '$t' is not allowed for the current user" );
00218                                                         } else {
00219                                                                 $data[$u][$t . 'token'] = $val;
00220                                                         }
00221                                                 }
00222                                         }
00223                                 } else {
00224                                         $data[$u]['missing'] = '';
00225                                 }
00226                         } else {
00227                                 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
00228                                         $result->setIndexedTagName( $data[$u]['groups'], 'g' );
00229                                 }
00230                                 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
00231                                         $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
00232                                 }
00233                                 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
00234                                         $result->setIndexedTagName( $data[$u]['rights'], 'r' );
00235                                 }
00236                         }
00237 
00238                         $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00239                                         null, $data[$u] );
00240                         if ( !$fit ) {
00241                                 $this->setContinueEnumParameter( 'users',
00242                                                 implode( '|', array_diff( $users, $done ) ) );
00243                                 break;
00244                         }
00245                         $done[] = $u;
00246                 }
00247                 return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
00248         }
00249 
00257         public static function getAutoGroups( $user ) {
00258                 wfDeprecated( __METHOD__, '1.20' );
00259 
00260                 return $user->getAutomaticGroups();
00261         }
00262 
00263         public function getCacheMode( $params ) {
00264                 if ( isset( $params['token'] ) ) {
00265                         return 'private';
00266                 } else {
00267                         return 'anon-public-user-private';
00268                 }
00269         }
00270 
00271         public function getAllowedParams() {
00272                 return array(
00273                         'prop' => array(
00274                                 ApiBase::PARAM_DFLT => null,
00275                                 ApiBase::PARAM_ISMULTI => true,
00276                                 ApiBase::PARAM_TYPE => array(
00277                                         'blockinfo',
00278                                         'groups',
00279                                         'implicitgroups',
00280                                         'rights',
00281                                         'editcount',
00282                                         'registration',
00283                                         'emailable',
00284                                         'gender',
00285                                 )
00286                         ),
00287                         'users' => array(
00288                                 ApiBase::PARAM_ISMULTI => true
00289                         ),
00290                         'token' => array(
00291                                 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
00292                                 ApiBase::PARAM_ISMULTI => true
00293                         ),
00294                 );
00295         }
00296 
00297         public function getParamDescription() {
00298                 return array(
00299                         'prop' => array(
00300                                 'What pieces of information to include',
00301                                 '  blockinfo      - Tags if the user is blocked, by whom, and for what reason',
00302                                 '  groups         - Lists all the groups the user(s) belongs to',
00303                                 '  implicitgroups - Lists all the groups a user is automatically a member of',
00304                                 '  rights         - Lists all the rights the user(s) has',
00305                                 '  editcount      - Adds the user\'s edit count',
00306                                 '  registration   - Adds the user\'s registration timestamp',
00307                                 '  emailable      - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
00308                                 '  gender         - Tags the gender of the user. Returns "male", "female", or "unknown"',
00309                         ),
00310                         'users' => 'A list of users to obtain the same information for',
00311                         'token' => 'Which tokens to obtain for each user',
00312                 );
00313         }
00314 
00315         public function getResultProperties() {
00316                 $props = array(
00317                         '' => array(
00318                                 'userid' => array(
00319                                         ApiBase::PROP_TYPE => 'integer',
00320                                         ApiBase::PROP_NULLABLE => true
00321                                 ),
00322                                 'name' => 'string',
00323                                 'invalid' => 'boolean',
00324                                 'hidden' => 'boolean',
00325                                 'interwiki' => 'boolean',
00326                                 'missing' => 'boolean'
00327                         ),
00328                         'editcount' => array(
00329                                 'editcount' => array(
00330                                         ApiBase::PROP_TYPE => 'integer',
00331                                         ApiBase::PROP_NULLABLE => true
00332                                 )
00333                         ),
00334                         'registration' => array(
00335                                 'registration' => array(
00336                                         ApiBase::PROP_TYPE => 'timestamp',
00337                                         ApiBase::PROP_NULLABLE => true
00338                                 )
00339                         ),
00340                         'blockinfo' => array(
00341                                 'blockid' => array(
00342                                         ApiBase::PROP_TYPE => 'integer',
00343                                         ApiBase::PROP_NULLABLE => true
00344                                 ),
00345                                 'blockedby' => array(
00346                                         ApiBase::PROP_TYPE => 'string',
00347                                         ApiBase::PROP_NULLABLE => true
00348                                 ),
00349                                 'blockedbyid' => array(
00350                                         ApiBase::PROP_TYPE => 'integer',
00351                                         ApiBase::PROP_NULLABLE => true
00352                                 ),
00353                                 'blockedreason' => array(
00354                                         ApiBase::PROP_TYPE => 'string',
00355                                         ApiBase::PROP_NULLABLE => true
00356                                 ),
00357                                 'blockedexpiry' => array(
00358                                         ApiBase::PROP_TYPE => 'timestamp',
00359                                         ApiBase::PROP_NULLABLE => true
00360                                 )
00361                         ),
00362                         'emailable' => array(
00363                                 'emailable' => 'boolean'
00364                         ),
00365                         'gender' => array(
00366                                 'gender' => array(
00367                                         ApiBase::PROP_TYPE => array(
00368                                                 'male',
00369                                                 'female',
00370                                                 'unknown'
00371                                         ),
00372                                         ApiBase::PROP_NULLABLE => true
00373                                 )
00374                         )
00375                 );
00376 
00377                 self::addTokenProperties( $props, $this->getTokenFunctions() );
00378 
00379                 return $props;
00380         }
00381 
00382         public function getDescription() {
00383                 return 'Get information about a list of users';
00384         }
00385 
00386         public function getExamples() {
00387                 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
00388         }
00389 
00390         public function getHelpUrls() {
00391                 return 'https://www.mediawiki.org/wiki/API:Users';
00392         }
00393 
00394         public function getVersion() {
00395                 return __CLASS__ . ': $Id$';
00396         }
00397 }