00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( 'ApiQueryBase.php' );
00029 }
00030
00036 class ApiQueryUsers extends ApiQueryBase {
00037
00038 public function __construct( $query, $moduleName ) {
00039 parent :: __construct( $query, $moduleName, 'us' );
00040 }
00041
00048 protected function getTokenFunctions() {
00049
00050 if ( isset( $this->tokenFunctions ) )
00051 return $this->tokenFunctions;
00052
00053
00054 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) )
00055 return array();
00056
00057 $this->tokenFunctions = array(
00058 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
00059 );
00060 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
00061 return $this->tokenFunctions;
00062 }
00063
00064 public static function getUserrightsToken( $user )
00065 {
00066 global $wgUser;
00067
00068
00069 return $wgUser->editToken( $user->getName() );
00070 }
00071
00072 public function execute() {
00073 $params = $this->extractRequestParams();
00074 $result = $this->getResult();
00075 $r = array();
00076
00077 if ( !is_null( $params['prop'] ) ) {
00078 $this->prop = array_flip( $params['prop'] );
00079 } else {
00080 $this->prop = array();
00081 }
00082
00083 $users = (array)$params['users'];
00084 $goodNames = $done = array();
00085 $result = $this->getResult();
00086
00087 foreach ( $users as $u ) {
00088 $n = User::getCanonicalName( $u );
00089 if ( $n === false || $n === '' )
00090 {
00091 $vals = array( 'name' => $u, 'invalid' => '' );
00092 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00093 null, $vals );
00094 if ( !$fit )
00095 {
00096 $this->setContinueEnumParameter( 'users',
00097 implode( '|', array_diff( $users, $done ) ) );
00098 $goodNames = array();
00099 break;
00100 }
00101 $done[] = $u;
00102 }
00103 else
00104 $goodNames[] = $n;
00105 }
00106
00107 if ( count( $goodNames ) )
00108 {
00109 $db = $this->getDb();
00110 $this->addTables( 'user', 'u1' );
00111 $this->addFields( 'u1.*' );
00112 $this->addWhereFld( 'u1.user_name', $goodNames );
00113
00114 if ( isset( $this->prop['groups'] ) ) {
00115 $this->addTables( 'user_groups' );
00116 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=u1.user_id' ) ) );
00117 $this->addFields( 'ug_group' );
00118 }
00119 if ( isset( $this->prop['blockinfo'] ) ) {
00120 $this->addTables( 'ipblocks' );
00121 $this->addTables( 'user', 'u2' );
00122 $u2 = $this->getAliasedName( 'user', 'u2' );
00123 $this->addJoinConds( array(
00124 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
00125 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
00126 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
00127 }
00128
00129 $data = array();
00130 $res = $this->select( __METHOD__ );
00131 while ( ( $r = $db->fetchObject( $res ) ) ) {
00132 $user = User::newFromRow( $r );
00133 $name = $user->getName();
00134 $data[$name]['name'] = $name;
00135 if ( isset( $this->prop['editcount'] ) )
00136 $data[$name]['editcount'] = intval( $user->getEditCount() );
00137 if ( isset( $this->prop['registration'] ) )
00138 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
00139 if ( isset( $this->prop['groups'] ) && !is_null( $r->ug_group ) )
00140
00141 $data[$name]['groups'][] = $r->ug_group;
00142 if ( isset( $this->prop['blockinfo'] ) && !is_null( $r->blocker_name ) ) {
00143 $data[$name]['blockedby'] = $r->blocker_name;
00144 $data[$name]['blockreason'] = $r->ipb_reason;
00145 }
00146 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() )
00147 $data[$name]['emailable'] = '';
00148
00149 if ( isset( $this->prop['gender'] ) ) {
00150 $gender = $user->getOption( 'gender' );
00151 if ( strval( $gender ) === '' ) {
00152 $gender = 'unknown';
00153 }
00154 $data[$name]['gender'] = $gender;
00155 }
00156
00157 if ( !is_null( $params['token'] ) )
00158 {
00159 $tokenFunctions = $this->getTokenFunctions();
00160 foreach ( $params['token'] as $t )
00161 {
00162 $val = call_user_func( $tokenFunctions[$t], $user );
00163 if ( $val === false )
00164 $this->setWarning( "Action '$t' is not allowed for the current user" );
00165 else
00166 $data[$name][$t . 'token'] = $val;
00167 }
00168 }
00169 }
00170 }
00171
00172 foreach ( $goodNames as $u ) {
00173 if ( !isset( $data[$u] ) ) {
00174 $data[$u] = array( 'name' => $u );
00175 $urPage = new UserrightsPage;
00176 $iwUser = $urPage->fetchUser( $u );
00177 if ( $iwUser instanceof UserRightsProxy ) {
00178 $data[$u]['interwiki'] = '';
00179 if ( !is_null( $params['token'] ) )
00180 {
00181 $tokenFunctions = $this->getTokenFunctions();
00182 foreach ( $params['token'] as $t )
00183 {
00184 $val = call_user_func( $tokenFunctions[$t], $iwUser );
00185 if ( $val === false )
00186 $this->setWarning( "Action '$t' is not allowed for the current user" );
00187 else
00188 $data[$u][$t . 'token'] = $val;
00189 }
00190 }
00191 } else
00192 $data[$u]['missing'] = '';
00193 } else {
00194 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) )
00195 $this->getResult()->setIndexedTagName( $data[$u]['groups'], 'g' );
00196 }
00197 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00198 null, $data[$u] );
00199 if ( !$fit )
00200 {
00201 $this->setContinueEnumParameter( 'users',
00202 implode( '|', array_diff( $users, $done ) ) );
00203 break;
00204 }
00205 $done[] = $u;
00206 }
00207 return $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
00208 }
00209
00210 public function getCacheMode( $params ) {
00211 if ( isset( $params['token'] ) ) {
00212 return 'private';
00213 } else {
00214 return 'public';
00215 }
00216 }
00217
00218 public function getAllowedParams() {
00219 return array (
00220 'prop' => array (
00221 ApiBase :: PARAM_DFLT => null,
00222 ApiBase :: PARAM_ISMULTI => true,
00223 ApiBase :: PARAM_TYPE => array (
00224 'blockinfo',
00225 'groups',
00226 'editcount',
00227 'registration',
00228 'emailable',
00229 'gender',
00230 )
00231 ),
00232 'users' => array(
00233 ApiBase :: PARAM_ISMULTI => true
00234 ),
00235 'token' => array(
00236 ApiBase :: PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
00237 ApiBase :: PARAM_ISMULTI => true
00238 ),
00239 );
00240 }
00241
00242 public function getParamDescription() {
00243 return array (
00244 'prop' => array(
00245 'What pieces of information to include',
00246 ' blockinfo - tags if the user is blocked, by whom, and for what reason',
00247 ' groups - lists all the groups the user belongs to',
00248 ' editcount - adds the user\'s edit count',
00249 ' registration - adds the user\'s registration timestamp',
00250 ' emailable - tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
00251 ' gender - tags the gender of the user. Returns "male", "female", or "unknown"',
00252 ),
00253 'users' => 'A list of users to obtain the same information for',
00254 'token' => 'Which tokens to obtain for each user',
00255 );
00256 }
00257
00258 public function getDescription() {
00259 return 'Get information about a list of users';
00260 }
00261
00262 protected function getExamples() {
00263 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
00264 }
00265
00266 public function getVersion() {
00267 return __CLASS__ . ': $Id: ApiQueryUsers.php 69932 2010-07-26 08:03:21Z tstarling $';
00268 }
00269 }