ApiQueryBlocks.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /*
  3. * Created on Sep 10, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
  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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * Query module to enumerate all available pages.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryBlocks extends ApiQueryBase {
  34. var $users;
  35. public function __construct($query, $moduleName) {
  36. parent :: __construct($query, $moduleName, 'bk');
  37. }
  38. public function execute() {
  39. global $wgUser;
  40. $params = $this->extractRequestParams();
  41. if(isset($params['users']) && isset($params['ip']))
  42. $this->dieUsage('bkusers and bkip cannot be used together', 'usersandip');
  43. $prop = array_flip($params['prop']);
  44. $fld_id = isset($prop['id']);
  45. $fld_user = isset($prop['user']);
  46. $fld_by = isset($prop['by']);
  47. $fld_timestamp = isset($prop['timestamp']);
  48. $fld_expiry = isset($prop['expiry']);
  49. $fld_reason = isset($prop['reason']);
  50. $fld_range = isset($prop['range']);
  51. $fld_flags = isset($prop['flags']);
  52. $result = $this->getResult();
  53. $pageSet = $this->getPageSet();
  54. $titles = $pageSet->getTitles();
  55. $data = array();
  56. $this->addTables('ipblocks');
  57. if($fld_id)
  58. $this->addFields('ipb_id');
  59. if($fld_user)
  60. $this->addFields(array('ipb_address', 'ipb_user', 'ipb_auto'));
  61. if($fld_by)
  62. {
  63. $this->addTables('user');
  64. $this->addFields(array('ipb_by', 'user_name'));
  65. $this->addWhere('user_id = ipb_by');
  66. }
  67. if($fld_timestamp)
  68. $this->addFields('ipb_timestamp');
  69. if($fld_expiry)
  70. $this->addFields('ipb_expiry');
  71. if($fld_reason)
  72. $this->addFields('ipb_reason');
  73. if($fld_range)
  74. $this->addFields(array('ipb_range_start', 'ipb_range_end'));
  75. if($fld_flags)
  76. $this->addFields(array('ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk'));
  77. $this->addOption('LIMIT', $params['limit'] + 1);
  78. $this->addWhereRange('ipb_timestamp', $params['dir'], $params['start'], $params['end']);
  79. if(isset($params['ids']))
  80. $this->addWhereFld('ipb_id', $params['ids']);
  81. if(isset($params['users']))
  82. {
  83. foreach((array)$params['users'] as $u)
  84. $this->prepareUsername($u);
  85. $this->addWhereFld('ipb_address', $this->usernames);
  86. }
  87. if(isset($params['ip']))
  88. {
  89. list($ip, $range) = IP::parseCIDR($params['ip']);
  90. if($ip && $range)
  91. {
  92. # We got a CIDR range
  93. if($range < 16)
  94. $this->dieUsage('CIDR ranges broader than /16 are not accepted', 'cidrtoobroad');
  95. $lower = wfBaseConvert($ip, 10, 16, 8, false);
  96. $upper = wfBaseConvert($ip + pow(2, 32 - $range) - 1, 10, 16, 8, false);
  97. }
  98. else
  99. $lower = $upper = IP::toHex($params['ip']);
  100. $prefix = substr($lower, 0, 4);
  101. $this->addWhere(array(
  102. "ipb_range_start LIKE '$prefix%'",
  103. "ipb_range_start <= '$lower'",
  104. "ipb_range_end >= '$upper'"
  105. ));
  106. }
  107. if(!$wgUser->isAllowed('hideuser'))
  108. $this->addWhereFld('ipb_deleted', 0);
  109. // Purge expired entries on one in every 10 queries
  110. if(!mt_rand(0, 10))
  111. Block::purgeExpired();
  112. $res = $this->select(__METHOD__);
  113. $count = 0;
  114. while($row = $res->fetchObject())
  115. {
  116. if(++$count > $params['limit'])
  117. {
  118. // We've had enough
  119. $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ipb_timestamp));
  120. break;
  121. }
  122. $block = array();
  123. if($fld_id)
  124. $block['id'] = $row->ipb_id;
  125. if($fld_user && !$row->ipb_auto)
  126. $block['user'] = $row->ipb_address;
  127. if($fld_by)
  128. $block['by'] = $row->user_name;
  129. if($fld_timestamp)
  130. $block['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ipb_timestamp);
  131. if($fld_expiry)
  132. $block['expiry'] = Block::decodeExpiry($row->ipb_expiry, TS_ISO_8601);
  133. if($fld_reason)
  134. $block['reason'] = $row->ipb_reason;
  135. if($fld_range)
  136. {
  137. $block['rangestart'] = IP::hexToQuad($row->ipb_range_start);
  138. $block['rangeend'] = IP::hexToQuad($row->ipb_range_end);
  139. }
  140. if($fld_flags)
  141. {
  142. // For clarity, these flags use the same names as their action=block counterparts
  143. if($row->ipb_auto)
  144. $block['automatic'] = '';
  145. if($row->ipb_anon_only)
  146. $block['anononly'] = '';
  147. if($row->ipb_create_account)
  148. $block['nocreate'] = '';
  149. if($row->ipb_enable_autoblock)
  150. $block['autoblock'] = '';
  151. if($row->ipb_block_email)
  152. $block['noemail'] = '';
  153. if($row->ipb_deleted)
  154. $block['hidden'] = '';
  155. if($row->ipb_allow_usertalk)
  156. $block['allowusertalk'] = '';
  157. }
  158. $fit = $result->addValue(array('query', $this->getModuleName()), null, $block);
  159. if(!$fit)
  160. {
  161. $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ipb_timestamp));
  162. break;
  163. }
  164. }
  165. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'block');
  166. }
  167. protected function prepareUsername($user)
  168. {
  169. if(!$user)
  170. $this->dieUsage('User parameter may not be empty', 'param_user');
  171. $name = User::isIP($user)
  172. ? $user
  173. : User::getCanonicalName($user, 'valid');
  174. if($name === false)
  175. $this->dieUsage("User name {$user} is not valid", 'param_user');
  176. $this->usernames[] = $name;
  177. }
  178. public function getAllowedParams() {
  179. return array (
  180. 'start' => array(
  181. ApiBase :: PARAM_TYPE => 'timestamp'
  182. ),
  183. 'end' => array(
  184. ApiBase :: PARAM_TYPE => 'timestamp',
  185. ),
  186. 'dir' => array(
  187. ApiBase :: PARAM_TYPE => array(
  188. 'newer',
  189. 'older'
  190. ),
  191. ApiBase :: PARAM_DFLT => 'older'
  192. ),
  193. 'ids' => array(
  194. ApiBase :: PARAM_TYPE => 'integer',
  195. ApiBase :: PARAM_ISMULTI => true
  196. ),
  197. 'users' => array(
  198. ApiBase :: PARAM_ISMULTI => true
  199. ),
  200. 'ip' => null,
  201. 'limit' => array(
  202. ApiBase :: PARAM_DFLT => 10,
  203. ApiBase :: PARAM_TYPE => 'limit',
  204. ApiBase :: PARAM_MIN => 1,
  205. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  206. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  207. ),
  208. 'prop' => array(
  209. ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
  210. ApiBase :: PARAM_TYPE => array(
  211. 'id',
  212. 'user',
  213. 'by',
  214. 'timestamp',
  215. 'expiry',
  216. 'reason',
  217. 'range',
  218. 'flags'
  219. ),
  220. ApiBase :: PARAM_ISMULTI => true
  221. )
  222. );
  223. }
  224. public function getParamDescription() {
  225. return array (
  226. 'start' => 'The timestamp to start enumerating from',
  227. 'end' => 'The timestamp to stop enumerating at',
  228. 'dir' => 'The direction in which to enumerate',
  229. 'ids' => 'Pipe-separated list of block IDs to list (optional)',
  230. 'users' => 'Pipe-separated list of users to search for (optional)',
  231. 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
  232. 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted.'),
  233. 'limit' => 'The maximum amount of blocks to list',
  234. 'prop' => 'Which properties to get',
  235. );
  236. }
  237. public function getDescription() {
  238. return 'List all blocked users and IP addresses.';
  239. }
  240. protected function getExamples() {
  241. return array ( 'api.php?action=query&list=blocks',
  242. 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
  243. );
  244. }
  245. public function getVersion() {
  246. return __CLASS__ . ': $Id: ApiQueryBlocks.php 48213 2009-03-09 10:01:00Z aaron $';
  247. }
  248. }