PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/api/ApiQueryAllUsers.php

https://bitbucket.org/brunodefraine/mediawiki
PHP | 380 lines | 281 code | 52 blank | 47 comment | 48 complexity | 3e0f3d2b1e4ba7f3fb49fdb834f1598c MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on July 7, 2007
  6. *
  7. * Copyright Š 2007 Yuri Astrakhan <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. /**
  27. * Query module to enumerate all registered users.
  28. *
  29. * @ingroup API
  30. */
  31. class ApiQueryAllUsers extends ApiQueryBase {
  32. public function __construct( $query, $moduleName ) {
  33. parent::__construct( $query, $moduleName, 'au' );
  34. }
  35. public function execute() {
  36. $db = $this->getDB();
  37. $params = $this->extractRequestParams();
  38. $prop = $params['prop'];
  39. if ( !is_null( $prop ) ) {
  40. $prop = array_flip( $prop );
  41. $fld_blockinfo = isset( $prop['blockinfo'] );
  42. $fld_editcount = isset( $prop['editcount'] );
  43. $fld_groups = isset( $prop['groups'] );
  44. $fld_rights = isset( $prop['rights'] );
  45. $fld_registration = isset( $prop['registration'] );
  46. $fld_implicitgroups = isset( $prop['implicitgroups'] );
  47. } else {
  48. $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = $fld_implicitgroups = false;
  49. }
  50. $limit = $params['limit'];
  51. $this->addTables( 'user' );
  52. $useIndex = true;
  53. $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
  54. $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
  55. $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
  56. # MySQL doesn't seem to use 'equality propagation' here, so like the
  57. # ActiveUsers special page, we have to use rc_user_text for some cases.
  58. $userFieldToSort = $params['activeusers'] ? 'rc_user_text' : 'user_name';
  59. $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
  60. if ( !is_null( $params['prefix'] ) ) {
  61. $this->addWhere( $userFieldToSort .
  62. $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
  63. }
  64. if ( !is_null( $params['rights'] ) ) {
  65. $groups = array();
  66. foreach( $params['rights'] as $r ) {
  67. $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
  68. }
  69. $groups = array_unique( $groups );
  70. if ( is_null( $params['group'] ) ) {
  71. $params['group'] = $groups;
  72. } else {
  73. $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
  74. }
  75. }
  76. if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
  77. $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
  78. }
  79. if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
  80. $useIndex = false;
  81. // Filter only users that belong to a given group
  82. $this->addTables( 'user_groups', 'ug1' );
  83. $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
  84. 'ug1.ug_group' => $params['group'] ) ) ) );
  85. }
  86. if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
  87. $useIndex = false;
  88. // Filter only users don't belong to a given group
  89. $this->addTables( 'user_groups', 'ug1' );
  90. if ( count( $params['excludegroup'] ) == 1 ) {
  91. $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
  92. } else {
  93. $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR ) );
  94. }
  95. $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
  96. array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
  97. )
  98. ) );
  99. $this->addWhere( 'ug1.ug_user IS NULL' );
  100. }
  101. if ( $params['witheditsonly'] ) {
  102. $this->addWhere( 'user_editcount > 0' );
  103. }
  104. $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
  105. if ( $fld_groups || $fld_rights ) {
  106. // Show the groups the given users belong to
  107. // request more than needed to avoid not getting all rows that belong to one user
  108. $groupCount = count( User::getAllGroups() );
  109. $sqlLimit = $limit + $groupCount + 1;
  110. $this->addTables( 'user_groups', 'ug2' );
  111. $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
  112. $this->addFields( 'ug2.ug_group ug_group2' );
  113. } else {
  114. $sqlLimit = $limit + 1;
  115. }
  116. if ( $params['activeusers'] ) {
  117. global $wgActiveUserDays;
  118. $this->addTables( 'recentchanges' );
  119. $this->addJoinConds( array( 'recentchanges' => array(
  120. 'INNER JOIN', 'rc_user_text=user_name'
  121. ) ) );
  122. $this->addFields( 'COUNT(*) AS recentedits' );
  123. $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
  124. $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 );
  125. $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
  126. $this->addOption( 'GROUP BY', $userFieldToSort );
  127. }
  128. $this->addOption( 'LIMIT', $sqlLimit );
  129. $this->addFields( array(
  130. 'user_name',
  131. 'user_id'
  132. ) );
  133. $this->addFieldsIf( 'user_editcount', $fld_editcount );
  134. $this->addFieldsIf( 'user_registration', $fld_registration );
  135. if ( $useIndex ) {
  136. $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
  137. }
  138. $res = $this->select( __METHOD__ );
  139. $count = 0;
  140. $lastUserData = false;
  141. $lastUser = false;
  142. $result = $this->getResult();
  143. //
  144. // This loop keeps track of the last entry.
  145. // For each new row, if the new row is for different user then the last, the last entry is added to results.
  146. // Otherwise, the group of the new row is appended to the last entry.
  147. // The setContinue... is more complex because of this, and takes into account the higher sql limit
  148. // to make sure all rows that belong to the same user are received.
  149. foreach ( $res as $row ) {
  150. $count++;
  151. if ( $lastUser !== $row->user_name ) {
  152. // Save the last pass's user data
  153. if ( is_array( $lastUserData ) ) {
  154. $fit = $result->addValue( array( 'query', $this->getModuleName() ),
  155. null, $lastUserData );
  156. $lastUserData = null;
  157. if ( !$fit ) {
  158. $this->setContinueEnumParameter( 'from',
  159. $this->keyToTitle( $lastUserData['name'] ) );
  160. break;
  161. }
  162. }
  163. if ( $count > $limit ) {
  164. // We've reached the one extra which shows that there are additional pages to be had. Stop here...
  165. $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
  166. break;
  167. }
  168. // Record new user's data
  169. $lastUser = $row->user_name;
  170. $lastUserData = array(
  171. 'userid' => $row->user_id,
  172. 'name' => $lastUser,
  173. );
  174. if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
  175. $lastUserData['blockedby'] = $row->ipb_by_text;
  176. $lastUserData['blockreason'] = $row->ipb_reason;
  177. $lastUserData['blockexpiry'] = $row->ipb_expiry;
  178. }
  179. if ( $row->ipb_deleted ) {
  180. $lastUserData['hidden'] = '';
  181. }
  182. if ( $fld_editcount ) {
  183. $lastUserData['editcount'] = intval( $row->user_editcount );
  184. }
  185. if ( $params['activeusers'] ) {
  186. $lastUserData['recenteditcount'] = intval( $row->recentedits );
  187. }
  188. if ( $fld_registration ) {
  189. $lastUserData['registration'] = $row->user_registration ?
  190. wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
  191. }
  192. }
  193. if ( $sqlLimit == $count ) {
  194. // BUG! database contains group name that User::getAllGroups() does not return
  195. // TODO: should handle this more gracefully
  196. ApiBase::dieDebug( __METHOD__,
  197. 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
  198. }
  199. $lastUserObj = User::newFromName( $lastUser );
  200. // Add user's group info
  201. if ( $fld_groups ) {
  202. if ( !isset( $lastUserData['groups'] ) && $lastUserObj ) {
  203. $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
  204. }
  205. if ( !is_null( $row->ug_group2 ) ) {
  206. $lastUserData['groups'][] = $row->ug_group2;
  207. }
  208. $result->setIndexedTagName( $lastUserData['groups'], 'g' );
  209. }
  210. if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
  211. $lastUserData['implicitgroups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
  212. $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
  213. }
  214. if ( $fld_rights ) {
  215. if ( !isset( $lastUserData['rights'] ) && $lastUserObj ) {
  216. $lastUserData['rights'] = User::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
  217. }
  218. if ( !is_null( $row->ug_group2 ) ) {
  219. $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
  220. User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
  221. }
  222. $result->setIndexedTagName( $lastUserData['rights'], 'r' );
  223. }
  224. }
  225. if ( is_array( $lastUserData ) ) {
  226. $fit = $result->addValue( array( 'query', $this->getModuleName() ),
  227. null, $lastUserData );
  228. if ( !$fit ) {
  229. $this->setContinueEnumParameter( 'from',
  230. $this->keyToTitle( $lastUserData['name'] ) );
  231. }
  232. }
  233. $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
  234. }
  235. public function getCacheMode( $params ) {
  236. return 'anon-public-user-private';
  237. }
  238. public function getAllowedParams() {
  239. $userGroups = User::getAllGroups();
  240. return array(
  241. 'from' => null,
  242. 'to' => null,
  243. 'prefix' => null,
  244. 'dir' => array(
  245. ApiBase::PARAM_DFLT => 'ascending',
  246. ApiBase::PARAM_TYPE => array(
  247. 'ascending',
  248. 'descending'
  249. ),
  250. ),
  251. 'group' => array(
  252. ApiBase::PARAM_TYPE => $userGroups,
  253. ApiBase::PARAM_ISMULTI => true,
  254. ),
  255. 'excludegroup' => array(
  256. ApiBase::PARAM_TYPE => $userGroups,
  257. ApiBase::PARAM_ISMULTI => true,
  258. ),
  259. 'rights' => array(
  260. ApiBase::PARAM_TYPE => User::getAllRights(),
  261. ApiBase::PARAM_ISMULTI => true,
  262. ),
  263. 'prop' => array(
  264. ApiBase::PARAM_ISMULTI => true,
  265. ApiBase::PARAM_TYPE => array(
  266. 'blockinfo',
  267. 'groups',
  268. 'implicitgroups',
  269. 'rights',
  270. 'editcount',
  271. 'registration'
  272. )
  273. ),
  274. 'limit' => array(
  275. ApiBase::PARAM_DFLT => 10,
  276. ApiBase::PARAM_TYPE => 'limit',
  277. ApiBase::PARAM_MIN => 1,
  278. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  279. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  280. ),
  281. 'witheditsonly' => false,
  282. 'activeusers' => false,
  283. );
  284. }
  285. public function getParamDescription() {
  286. global $wgActiveUserDays;
  287. return array(
  288. 'from' => 'The user name to start enumerating from',
  289. 'to' => 'The user name to stop enumerating at',
  290. 'prefix' => 'Search for all users that begin with this value',
  291. 'dir' => 'Direction to sort in',
  292. 'group' => 'Limit users to given group name(s)',
  293. 'excludegroup' => 'Exclude users in given group name(s)',
  294. 'rights' => 'Limit users to given right(s)',
  295. 'prop' => array(
  296. 'What pieces of information to include.',
  297. ' blockinfo - Adds the information about a current block on the user',
  298. ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
  299. ' implicitgroups - Lists all the groups the user is automatically in',
  300. ' rights - Lists rights that the user has',
  301. ' editcount - Adds the edit count of the user',
  302. ' registration - Adds the timestamp of when the user registered if available (may be blank)',
  303. ),
  304. 'limit' => 'How many total user names to return',
  305. 'witheditsonly' => 'Only list users who have made edits',
  306. 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
  307. );
  308. }
  309. public function getDescription() {
  310. return 'Enumerate all registered users';
  311. }
  312. public function getPossibleErrors() {
  313. return array_merge( parent::getPossibleErrors(), array(
  314. array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
  315. ) );
  316. }
  317. public function getExamples() {
  318. return array(
  319. 'api.php?action=query&list=allusers&aufrom=Y',
  320. );
  321. }
  322. public function getHelpUrls() {
  323. return 'https://www.mediawiki.org/wiki/API:Allusers';
  324. }
  325. public function getVersion() {
  326. return __CLASS__ . ': $Id$';
  327. }
  328. }