PageRenderTime 79ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/api/ApiQueryUsers.php

https://github.com/daevid/MWFork
PHP | 332 lines | 238 code | 42 blank | 52 comment | 47 complexity | b8516720ac0efa3ac8df844aba05a4c6 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on July 30, 2007
  6. *
  7. * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. if ( !defined( 'MEDIAWIKI' ) ) {
  27. // Eclipse helper - will be ignored in production
  28. require_once( 'ApiQueryBase.php' );
  29. }
  30. /**
  31. * Query module to get information about a list of users
  32. *
  33. * @ingroup API
  34. */
  35. class ApiQueryUsers extends ApiQueryBase {
  36. private $tokenFunctions, $prop;
  37. public function __construct( $query, $moduleName ) {
  38. parent::__construct( $query, $moduleName, 'us' );
  39. }
  40. /**
  41. * Get an array mapping token names to their handler functions.
  42. * The prototype for a token function is func($user)
  43. * it should return a token or false (permission denied)
  44. * @return Array tokenname => function
  45. */
  46. protected function getTokenFunctions() {
  47. // Don't call the hooks twice
  48. if ( isset( $this->tokenFunctions ) ) {
  49. return $this->tokenFunctions;
  50. }
  51. // If we're in JSON callback mode, no tokens can be obtained
  52. if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
  53. return array();
  54. }
  55. $this->tokenFunctions = array(
  56. 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
  57. );
  58. wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
  59. return $this->tokenFunctions;
  60. }
  61. /**
  62. * @param $user User
  63. * @return String
  64. */
  65. public static function getUserrightsToken( $user ) {
  66. global $wgUser;
  67. // Since the permissions check for userrights is non-trivial,
  68. // don't bother with it here
  69. return $wgUser->editToken( $user->getName() );
  70. }
  71. public function execute() {
  72. $params = $this->extractRequestParams();
  73. if ( !is_null( $params['prop'] ) ) {
  74. $this->prop = array_flip( $params['prop'] );
  75. } else {
  76. $this->prop = array();
  77. }
  78. $users = (array)$params['users'];
  79. $goodNames = $done = array();
  80. $result = $this->getResult();
  81. // Canonicalize user names
  82. foreach ( $users as $u ) {
  83. $n = User::getCanonicalName( $u );
  84. if ( $n === false || $n === '' ) {
  85. $vals = array( 'name' => $u, 'invalid' => '' );
  86. $fit = $result->addValue( array( 'query', $this->getModuleName() ),
  87. null, $vals );
  88. if ( !$fit ) {
  89. $this->setContinueEnumParameter( 'users',
  90. implode( '|', array_diff( $users, $done ) ) );
  91. $goodNames = array();
  92. break;
  93. }
  94. $done[] = $u;
  95. } else {
  96. $goodNames[] = $n;
  97. }
  98. }
  99. $result = $this->getResult();
  100. if ( count( $goodNames ) ) {
  101. $this->addTables( 'user' );
  102. $this->addFields( '*' );
  103. $this->addWhereFld( 'user_name', $goodNames );
  104. if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
  105. $this->addTables( 'user_groups' );
  106. $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=user_id' ) ) );
  107. $this->addFields( 'ug_group' );
  108. }
  109. $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
  110. $data = array();
  111. $res = $this->select( __METHOD__ );
  112. foreach ( $res as $row ) {
  113. $user = User::newFromRow( $row );
  114. $name = $user->getName();
  115. $data[$name]['userid'] = $user->getId();
  116. $data[$name]['name'] = $name;
  117. if ( isset( $this->prop['editcount'] ) ) {
  118. $data[$name]['editcount'] = intval( $user->getEditCount() );
  119. }
  120. if ( isset( $this->prop['registration'] ) ) {
  121. $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
  122. }
  123. if ( isset( $this->prop['groups'] ) ) {
  124. if ( !isset( $data[$name]['groups'] ) ) {
  125. $data[$name]['groups'] = self::getAutoGroups( $user );
  126. }
  127. if ( !is_null( $row->ug_group ) ) {
  128. // This row contains only one group, others will be added from other rows
  129. $data[$name]['groups'][] = $row->ug_group;
  130. }
  131. }
  132. if ( isset( $this->prop['rights'] ) ) {
  133. if ( !isset( $data[$name]['rights'] ) ) {
  134. $data[$name]['rights'] = User::getGroupPermissions( $user->getAutomaticGroups() );
  135. }
  136. if ( !is_null( $row->ug_group ) ) {
  137. $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
  138. User::getGroupPermissions( array( $row->ug_group ) ) ) );
  139. }
  140. }
  141. if ( $row->ipb_deleted ) {
  142. $data[$name]['hidden'] = '';
  143. }
  144. if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
  145. $data[$name]['blockedby'] = $row->ipb_by_text;
  146. $data[$name]['blockreason'] = $row->ipb_reason;
  147. $data[$name]['blockexpiry'] = $row->ipb_expiry;
  148. }
  149. if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
  150. $data[$name]['emailable'] = '';
  151. }
  152. if ( isset( $this->prop['gender'] ) ) {
  153. $gender = $user->getOption( 'gender' );
  154. if ( strval( $gender ) === '' ) {
  155. $gender = 'unknown';
  156. }
  157. $data[$name]['gender'] = $gender;
  158. }
  159. if ( !is_null( $params['token'] ) ) {
  160. $tokenFunctions = $this->getTokenFunctions();
  161. foreach ( $params['token'] as $t ) {
  162. $val = call_user_func( $tokenFunctions[$t], $user );
  163. if ( $val === false ) {
  164. $this->setWarning( "Action '$t' is not allowed for the current user" );
  165. } else {
  166. $data[$name][$t . 'token'] = $val;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. // Second pass: add result data to $retval
  173. foreach ( $goodNames as $u ) {
  174. if ( !isset( $data[$u] ) ) {
  175. $data[$u] = array( 'name' => $u );
  176. $urPage = new UserrightsPage;
  177. $iwUser = $urPage->fetchUser( $u );
  178. if ( $iwUser instanceof UserRightsProxy ) {
  179. $data[$u]['interwiki'] = '';
  180. if ( !is_null( $params['token'] ) ) {
  181. $tokenFunctions = $this->getTokenFunctions();
  182. foreach ( $params['token'] as $t ) {
  183. $val = call_user_func( $tokenFunctions[$t], $iwUser );
  184. if ( $val === false ) {
  185. $this->setWarning( "Action '$t' is not allowed for the current user" );
  186. } else {
  187. $data[$u][$t . 'token'] = $val;
  188. }
  189. }
  190. }
  191. } else {
  192. $data[$u]['missing'] = '';
  193. }
  194. } else {
  195. if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
  196. $result->setIndexedTagName( $data[$u]['groups'], 'g' );
  197. }
  198. if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
  199. $result->setIndexedTagName( $data[$u]['rights'], 'r' );
  200. }
  201. }
  202. $fit = $result->addValue( array( 'query', $this->getModuleName() ),
  203. null, $data[$u] );
  204. if ( !$fit ) {
  205. $this->setContinueEnumParameter( 'users',
  206. implode( '|', array_diff( $users, $done ) ) );
  207. break;
  208. }
  209. $done[] = $u;
  210. }
  211. return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
  212. }
  213. /**
  214. * Gets all the groups that a user is automatically a member of (implicit groups)
  215. * @param $user User
  216. * @return array
  217. */
  218. public static function getAutoGroups( $user ) {
  219. $groups = array();
  220. $groups[] = '*';
  221. if ( !$user->isAnon() ) {
  222. $groups[] = 'user';
  223. }
  224. $builtGroups = array();
  225. foreach( array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) ) as $i => $group ) {
  226. $builtGroups[$i] = array( 'implicit' => '' );
  227. ApiResult::setContent( $builtGroups[$i], $group );
  228. }
  229. return $builtGroups;
  230. }
  231. public function getCacheMode( $params ) {
  232. if ( isset( $params['token'] ) ) {
  233. return 'private';
  234. } else {
  235. return 'anon-public-user-private';
  236. }
  237. }
  238. public function getAllowedParams() {
  239. return array(
  240. 'prop' => array(
  241. ApiBase::PARAM_DFLT => null,
  242. ApiBase::PARAM_ISMULTI => true,
  243. ApiBase::PARAM_TYPE => array(
  244. 'blockinfo',
  245. 'groups',
  246. 'rights',
  247. 'editcount',
  248. 'registration',
  249. 'emailable',
  250. 'gender',
  251. )
  252. ),
  253. 'users' => array(
  254. ApiBase::PARAM_ISMULTI => true
  255. ),
  256. 'token' => array(
  257. ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
  258. ApiBase::PARAM_ISMULTI => true
  259. ),
  260. );
  261. }
  262. public function getParamDescription() {
  263. return array(
  264. 'prop' => array(
  265. 'What pieces of information to include',
  266. ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
  267. ' groups - Lists all the groups the user(s) belongs to',
  268. ' rights - Lists all the rights the user(s) has',
  269. ' editcount - Adds the user\'s edit count',
  270. ' registration - Adds the user\'s registration timestamp',
  271. ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
  272. ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
  273. ),
  274. 'users' => 'A list of users to obtain the same information for',
  275. 'token' => 'Which tokens to obtain for each user',
  276. );
  277. }
  278. public function getDescription() {
  279. return 'Get information about a list of users';
  280. }
  281. protected function getExamples() {
  282. return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
  283. }
  284. public function getHelpUrls() {
  285. return 'http://www.mediawiki.org/wiki/API:Users';
  286. }
  287. public function getVersion() {
  288. return __CLASS__ . ': $Id: ApiQueryUsers.php 92477 2011-07-18 21:26:33Z reedy $';
  289. }
  290. }