MediaWiki
REL1_23
|
00001 <?php 00032 class ApiQueryUserInfo extends ApiQueryBase { 00033 00034 private $prop = array(); 00035 00036 public function __construct( $query, $moduleName ) { 00037 parent::__construct( $query, $moduleName, 'ui' ); 00038 } 00039 00040 public function execute() { 00041 $params = $this->extractRequestParams(); 00042 $result = $this->getResult(); 00043 00044 if ( !is_null( $params['prop'] ) ) { 00045 $this->prop = array_flip( $params['prop'] ); 00046 } 00047 00048 $r = $this->getCurrentUserInfo(); 00049 $result->addValue( 'query', $this->getModuleName(), $r ); 00050 } 00051 00052 protected function getCurrentUserInfo() { 00053 global $wgHiddenPrefs; 00054 $user = $this->getUser(); 00055 $result = $this->getResult(); 00056 $vals = array(); 00057 $vals['id'] = intval( $user->getId() ); 00058 $vals['name'] = $user->getName(); 00059 00060 if ( $user->isAnon() ) { 00061 $vals['anon'] = ''; 00062 } 00063 00064 if ( isset( $this->prop['blockinfo'] ) ) { 00065 if ( $user->isBlocked() ) { 00066 $block = $user->getBlock(); 00067 $vals['blockid'] = $block->getId(); 00068 $vals['blockedby'] = $block->getByName(); 00069 $vals['blockedbyid'] = $block->getBy(); 00070 $vals['blockreason'] = $user->blockedFor(); 00071 } 00072 } 00073 00074 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) { 00075 $vals['messages'] = ''; 00076 } 00077 00078 if ( isset( $this->prop['groups'] ) ) { 00079 $vals['groups'] = $user->getEffectiveGroups(); 00080 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty 00081 } 00082 00083 if ( isset( $this->prop['implicitgroups'] ) ) { 00084 $vals['implicitgroups'] = $user->getAutomaticGroups(); 00085 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty 00086 } 00087 00088 if ( isset( $this->prop['rights'] ) ) { 00089 // User::getRights() may return duplicate values, strip them 00090 $vals['rights'] = array_values( array_unique( $user->getRights() ) ); 00091 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty 00092 } 00093 00094 if ( isset( $this->prop['changeablegroups'] ) ) { 00095 $vals['changeablegroups'] = $user->changeableGroups(); 00096 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' ); 00097 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' ); 00098 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' ); 00099 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' ); 00100 } 00101 00102 if ( isset( $this->prop['options'] ) ) { 00103 $vals['options'] = $user->getOptions(); 00104 } 00105 00106 if ( isset( $this->prop['preferencestoken'] ) && 00107 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) && 00108 $user->isAllowed( 'editmyoptions' ) 00109 ) { 00110 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() ); 00111 } 00112 00113 if ( isset( $this->prop['editcount'] ) ) { 00114 // use intval to prevent null if a non-logged-in user calls 00115 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount 00116 $vals['editcount'] = intval( $user->getEditCount() ); 00117 } 00118 00119 if ( isset( $this->prop['ratelimits'] ) ) { 00120 $vals['ratelimits'] = $this->getRateLimits(); 00121 } 00122 00123 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) { 00124 $vals['realname'] = $user->getRealName(); 00125 } 00126 00127 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) { 00128 if ( isset( $this->prop['email'] ) ) { 00129 $vals['email'] = $user->getEmail(); 00130 $auth = $user->getEmailAuthenticationTimestamp(); 00131 if ( !is_null( $auth ) ) { 00132 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth ); 00133 } 00134 } 00135 } 00136 00137 if ( isset( $this->prop['registrationdate'] ) ) { 00138 $regDate = $user->getRegistration(); 00139 if ( $regDate !== false ) { 00140 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate ); 00141 } 00142 } 00143 00144 if ( isset( $this->prop['acceptlang'] ) ) { 00145 $langs = $this->getRequest()->getAcceptLang(); 00146 $acceptLang = array(); 00147 foreach ( $langs as $lang => $val ) { 00148 $r = array( 'q' => $val ); 00149 ApiResult::setContent( $r, $lang ); 00150 $acceptLang[] = $r; 00151 } 00152 $result->setIndexedTagName( $acceptLang, 'lang' ); 00153 $vals['acceptlang'] = $acceptLang; 00154 } 00155 00156 return $vals; 00157 } 00158 00159 protected function getRateLimits() { 00160 global $wgRateLimits; 00161 $user = $this->getUser(); 00162 if ( !$user->isPingLimitable() ) { 00163 return array(); // No limits 00164 } 00165 00166 // Find out which categories we belong to 00167 $categories = array(); 00168 if ( $user->isAnon() ) { 00169 $categories[] = 'anon'; 00170 } else { 00171 $categories[] = 'user'; 00172 } 00173 if ( $user->isNewbie() ) { 00174 $categories[] = 'ip'; 00175 $categories[] = 'subnet'; 00176 if ( !$user->isAnon() ) { 00177 $categories[] = 'newbie'; 00178 } 00179 } 00180 $categories = array_merge( $categories, $user->getGroups() ); 00181 00182 // Now get the actual limits 00183 $retval = array(); 00184 foreach ( $wgRateLimits as $action => $limits ) { 00185 foreach ( $categories as $cat ) { 00186 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) { 00187 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] ); 00188 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] ); 00189 } 00190 } 00191 } 00192 00193 return $retval; 00194 } 00195 00196 public function getAllowedParams() { 00197 return array( 00198 'prop' => array( 00199 ApiBase::PARAM_DFLT => null, 00200 ApiBase::PARAM_ISMULTI => true, 00201 ApiBase::PARAM_TYPE => array( 00202 'blockinfo', 00203 'hasmsg', 00204 'groups', 00205 'implicitgroups', 00206 'rights', 00207 'changeablegroups', 00208 'options', 00209 'preferencestoken', 00210 'editcount', 00211 'ratelimits', 00212 'email', 00213 'realname', 00214 'acceptlang', 00215 'registrationdate' 00216 ) 00217 ) 00218 ); 00219 } 00220 00221 public function getParamDescription() { 00222 return array( 00223 'prop' => array( 00224 'What pieces of information to include', 00225 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason', 00226 ' hasmsg - Adds a tag "message" if the current user has pending messages', 00227 ' groups - Lists all the groups the current user belongs to', 00228 ' implicitgroups - Lists all the groups the current user is automatically a member of', 00229 ' rights - Lists all the rights the current user has', 00230 ' changeablegroups - Lists the groups the current user can add to and remove from', 00231 ' options - Lists all preferences the current user has set', 00232 ' preferencestoken - Get a token to change current user\'s preferences', 00233 ' editcount - Adds the current user\'s edit count', 00234 ' ratelimits - Lists all rate limits applying to the current user', 00235 ' realname - Adds the user\'s real name', 00236 ' email - Adds the user\'s email address and email authentication date', 00237 ' acceptlang - Echoes the Accept-Language header sent by ' . 00238 'the client in a structured format', 00239 ' registrationdate - Adds the user\'s registration date', 00240 ) 00241 ); 00242 } 00243 00244 public function getResultProperties() { 00245 return array( 00246 ApiBase::PROP_LIST => false, 00247 '' => array( 00248 'id' => 'integer', 00249 'name' => 'string', 00250 'anon' => 'boolean' 00251 ), 00252 'blockinfo' => array( 00253 'blockid' => array( 00254 ApiBase::PROP_TYPE => 'integer', 00255 ApiBase::PROP_NULLABLE => true 00256 ), 00257 'blockedby' => array( 00258 ApiBase::PROP_TYPE => 'string', 00259 ApiBase::PROP_NULLABLE => true 00260 ), 00261 'blockedbyid' => array( 00262 ApiBase::PROP_TYPE => 'integer', 00263 ApiBase::PROP_NULLABLE => true 00264 ), 00265 'blockedreason' => array( 00266 ApiBase::PROP_TYPE => 'string', 00267 ApiBase::PROP_NULLABLE => true 00268 ) 00269 ), 00270 'hasmsg' => array( 00271 'messages' => 'boolean' 00272 ), 00273 'preferencestoken' => array( 00274 'preferencestoken' => 'string' 00275 ), 00276 'editcount' => array( 00277 'editcount' => 'integer' 00278 ), 00279 'realname' => array( 00280 'realname' => array( 00281 ApiBase::PROP_TYPE => 'string', 00282 ApiBase::PROP_NULLABLE => true 00283 ) 00284 ), 00285 'email' => array( 00286 'email' => 'string', 00287 'emailauthenticated' => array( 00288 ApiBase::PROP_TYPE => 'timestamp', 00289 ApiBase::PROP_NULLABLE => true 00290 ) 00291 ), 00292 'registrationdate' => array( 00293 'registrationdate' => array( 00294 ApiBase::PROP_TYPE => 'timestamp', 00295 ApiBase::PROP_NULLABLE => true 00296 ) 00297 ) 00298 ); 00299 } 00300 00301 public function getDescription() { 00302 return 'Get information about the current user.'; 00303 } 00304 00305 public function getExamples() { 00306 return array( 00307 'api.php?action=query&meta=userinfo', 00308 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg', 00309 ); 00310 } 00311 00312 public function getHelpUrls() { 00313 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui'; 00314 } 00315 }