MediaWiki  REL1_19
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( '*' );
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'] = self::getAutoGroups( $user );
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'] =  self::getAutoGroups( $user );
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]['blockedby'] = $row->ipb_by_text;
00169                                         $data[$name]['blockreason'] = $row->ipb_reason;
00170                                         $data[$name]['blockexpiry'] = $row->ipb_expiry;
00171                                 }
00172 
00173                                 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
00174                                         $data[$name]['emailable'] = '';
00175                                 }
00176 
00177                                 if ( isset( $this->prop['gender'] ) ) {
00178                                         $gender = $user->getOption( 'gender' );
00179                                         if ( strval( $gender ) === '' ) {
00180                                                 $gender = 'unknown';
00181                                         }
00182                                         $data[$name]['gender'] = $gender;
00183                                 }
00184 
00185                                 if ( !is_null( $params['token'] ) ) {
00186                                         $tokenFunctions = $this->getTokenFunctions();
00187                                         foreach ( $params['token'] as $t ) {
00188                                                 $val = call_user_func( $tokenFunctions[$t], $user );
00189                                                 if ( $val === false ) {
00190                                                         $this->setWarning( "Action '$t' is not allowed for the current user" );
00191                                                 } else {
00192                                                         $data[$name][$t . 'token'] = $val;
00193                                                 }
00194                                         }
00195                                 }
00196                         }
00197                 }
00198 
00199                 // Second pass: add result data to $retval
00200                 foreach ( $goodNames as $u ) {
00201                         if ( !isset( $data[$u] ) ) {
00202                                 $data[$u] = array( 'name' => $u );
00203                                 $urPage = new UserrightsPage;
00204                                 $iwUser = $urPage->fetchUser( $u );
00205 
00206                                 if ( $iwUser instanceof UserRightsProxy ) {
00207                                         $data[$u]['interwiki'] = '';
00208 
00209                                         if ( !is_null( $params['token'] ) ) {
00210                                                 $tokenFunctions = $this->getTokenFunctions();
00211 
00212                                                 foreach ( $params['token'] as $t ) {
00213                                                         $val = call_user_func( $tokenFunctions[$t], $iwUser );
00214                                                         if ( $val === false ) {
00215                                                                 $this->setWarning( "Action '$t' is not allowed for the current user" );
00216                                                         } else {
00217                                                                 $data[$u][$t . 'token'] = $val;
00218                                                         }
00219                                                 }
00220                                         }
00221                                 } else {
00222                                         $data[$u]['missing'] = '';
00223                                 }
00224                         } else {
00225                                 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
00226                                         $result->setIndexedTagName( $data[$u]['groups'], 'g' );
00227                                 }
00228                                 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
00229                                         $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
00230                                 }
00231                                 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
00232                                         $result->setIndexedTagName( $data[$u]['rights'], 'r' );
00233                                 }
00234                         }
00235 
00236                         $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00237                                         null, $data[$u] );
00238                         if ( !$fit ) {
00239                                 $this->setContinueEnumParameter( 'users',
00240                                                 implode( '|', array_diff( $users, $done ) ) );
00241                                 break;
00242                         }
00243                         $done[] = $u;
00244                 }
00245                 return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
00246         }
00247 
00253         public static function getAutoGroups( $user ) {
00254                 $groups = array();
00255                 $groups[] = '*';
00256 
00257                 if ( !$user->isAnon() ) {
00258                         $groups[] = 'user';
00259                 }
00260 
00261                 return array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
00262         }
00263 
00264         public function getCacheMode( $params ) {
00265                 if ( isset( $params['token'] ) ) {
00266                         return 'private';
00267                 } else {
00268                         return 'anon-public-user-private';
00269                 }
00270         }
00271 
00272         public function getAllowedParams() {
00273                 return array(
00274                         'prop' => array(
00275                                 ApiBase::PARAM_DFLT => null,
00276                                 ApiBase::PARAM_ISMULTI => true,
00277                                 ApiBase::PARAM_TYPE => array(
00278                                         'blockinfo',
00279                                         'groups',
00280                                         'implicitgroups',
00281                                         'rights',
00282                                         'editcount',
00283                                         'registration',
00284                                         'emailable',
00285                                         'gender',
00286                                 )
00287                         ),
00288                         'users' => array(
00289                                 ApiBase::PARAM_ISMULTI => true
00290                         ),
00291                         'token' => array(
00292                                 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
00293                                 ApiBase::PARAM_ISMULTI => true
00294                         ),
00295                 );
00296         }
00297 
00298         public function getParamDescription() {
00299                 return array(
00300                         'prop' => array(
00301                                 'What pieces of information to include',
00302                                 '  blockinfo      - Tags if the user is blocked, by whom, and for what reason',
00303                                 '  groups         - Lists all the groups the user(s) belongs to',
00304                                 '  implicitgroups - Lists all the groups a user is automatically a member of',
00305                                 '  rights         - Lists all the rights the user(s) has',
00306                                 '  editcount      - Adds the user\'s edit count',
00307                                 '  registration   - Adds the user\'s registration timestamp',
00308                                 '  emailable      - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
00309                                 '  gender         - Tags the gender of the user. Returns "male", "female", or "unknown"',
00310                         ),
00311                         'users' => 'A list of users to obtain the same information for',
00312                         'token' => 'Which tokens to obtain for each user',
00313                 );
00314         }
00315 
00316         public function getDescription() {
00317                 return 'Get information about a list of users';
00318         }
00319 
00320         public function getExamples() {
00321                 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
00322         }
00323 
00324         public function getHelpUrls() {
00325                 return 'https://www.mediawiki.org/wiki/API:Users';
00326         }
00327 
00328         public function getVersion() {
00329                 return __CLASS__ . ': $Id$';
00330         }
00331 }