ApiQueryBlocks.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Sep 10, 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. /**
  27. * Query module to enumerate all user blocks
  28. *
  29. * @ingroup API
  30. */
  31. class ApiQueryBlocks extends ApiQueryBase {
  32. public function __construct( ApiQuery $query, $moduleName ) {
  33. parent::__construct( $query, $moduleName, 'bk' );
  34. }
  35. public function execute() {
  36. $db = $this->getDB();
  37. $commentStore = new CommentStore( 'ipb_reason' );
  38. $params = $this->extractRequestParams();
  39. $this->requireMaxOneParameter( $params, 'users', 'ip' );
  40. $prop = array_flip( $params['prop'] );
  41. $fld_id = isset( $prop['id'] );
  42. $fld_user = isset( $prop['user'] );
  43. $fld_userid = isset( $prop['userid'] );
  44. $fld_by = isset( $prop['by'] );
  45. $fld_byid = isset( $prop['byid'] );
  46. $fld_timestamp = isset( $prop['timestamp'] );
  47. $fld_expiry = isset( $prop['expiry'] );
  48. $fld_reason = isset( $prop['reason'] );
  49. $fld_range = isset( $prop['range'] );
  50. $fld_flags = isset( $prop['flags'] );
  51. $result = $this->getResult();
  52. $this->addTables( 'ipblocks' );
  53. $this->addFields( [ 'ipb_auto', 'ipb_id', 'ipb_timestamp' ] );
  54. $this->addFieldsIf( [ 'ipb_address', 'ipb_user' ], $fld_user || $fld_userid );
  55. $this->addFieldsIf( 'ipb_by_text', $fld_by );
  56. $this->addFieldsIf( 'ipb_by', $fld_byid );
  57. $this->addFieldsIf( 'ipb_expiry', $fld_expiry );
  58. $this->addFieldsIf( [ 'ipb_range_start', 'ipb_range_end' ], $fld_range );
  59. $this->addFieldsIf( [ 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock',
  60. 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ],
  61. $fld_flags );
  62. if ( $fld_reason ) {
  63. $commentQuery = $commentStore->getJoin();
  64. $this->addTables( $commentQuery['tables'] );
  65. $this->addFields( $commentQuery['fields'] );
  66. $this->addJoinConds( $commentQuery['joins'] );
  67. }
  68. $this->addOption( 'LIMIT', $params['limit'] + 1 );
  69. $this->addTimestampWhereRange(
  70. 'ipb_timestamp',
  71. $params['dir'],
  72. $params['start'],
  73. $params['end']
  74. );
  75. // Include in ORDER BY for uniqueness
  76. $this->addWhereRange( 'ipb_id', $params['dir'], null, null );
  77. if ( !is_null( $params['continue'] ) ) {
  78. $cont = explode( '|', $params['continue'] );
  79. $this->dieContinueUsageIf( count( $cont ) != 2 );
  80. $op = ( $params['dir'] == 'newer' ? '>' : '<' );
  81. $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
  82. $continueId = (int)$cont[1];
  83. $this->dieContinueUsageIf( $continueId != $cont[1] );
  84. $this->addWhere( "ipb_timestamp $op $continueTimestamp OR " .
  85. "(ipb_timestamp = $continueTimestamp AND " .
  86. "ipb_id $op= $continueId)"
  87. );
  88. }
  89. if ( isset( $params['ids'] ) ) {
  90. $this->addWhereFld( 'ipb_id', $params['ids'] );
  91. }
  92. if ( isset( $params['users'] ) ) {
  93. $usernames = [];
  94. foreach ( (array)$params['users'] as $u ) {
  95. $usernames[] = $this->prepareUsername( $u );
  96. }
  97. $this->addWhereFld( 'ipb_address', $usernames );
  98. $this->addWhereFld( 'ipb_auto', 0 );
  99. }
  100. if ( isset( $params['ip'] ) ) {
  101. $blockCIDRLimit = $this->getConfig()->get( 'BlockCIDRLimit' );
  102. if ( IP::isIPv4( $params['ip'] ) ) {
  103. $type = 'IPv4';
  104. $cidrLimit = $blockCIDRLimit['IPv4'];
  105. $prefixLen = 0;
  106. } elseif ( IP::isIPv6( $params['ip'] ) ) {
  107. $type = 'IPv6';
  108. $cidrLimit = $blockCIDRLimit['IPv6'];
  109. $prefixLen = 3; // IP::toHex output is prefixed with "v6-"
  110. } else {
  111. $this->dieWithError( 'apierror-badip', 'param_ip' );
  112. }
  113. # Check range validity, if it's a CIDR
  114. list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
  115. if ( $ip !== false && $range !== false && $range < $cidrLimit ) {
  116. $this->dieWithError( [ 'apierror-cidrtoobroad', $type, $cidrLimit ] );
  117. }
  118. # Let IP::parseRange handle calculating $upper, instead of duplicating the logic here.
  119. list( $lower, $upper ) = IP::parseRange( $params['ip'] );
  120. # Extract the common prefix to any rangeblock affecting this IP/CIDR
  121. $prefix = substr( $lower, 0, $prefixLen + floor( $cidrLimit / 4 ) );
  122. # Fairly hard to make a malicious SQL statement out of hex characters,
  123. # but it is good practice to add quotes
  124. $lower = $db->addQuotes( $lower );
  125. $upper = $db->addQuotes( $upper );
  126. $this->addWhere( [
  127. 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
  128. 'ipb_range_start <= ' . $lower,
  129. 'ipb_range_end >= ' . $upper,
  130. 'ipb_auto' => 0
  131. ] );
  132. }
  133. if ( !is_null( $params['show'] ) ) {
  134. $show = array_flip( $params['show'] );
  135. /* Check for conflicting parameters. */
  136. if ( ( isset( $show['account'] ) && isset( $show['!account'] ) )
  137. || ( isset( $show['ip'] ) && isset( $show['!ip'] ) )
  138. || ( isset( $show['range'] ) && isset( $show['!range'] ) )
  139. || ( isset( $show['temp'] ) && isset( $show['!temp'] ) )
  140. ) {
  141. $this->dieWithError( 'apierror-show' );
  142. }
  143. $this->addWhereIf( 'ipb_user = 0', isset( $show['!account'] ) );
  144. $this->addWhereIf( 'ipb_user != 0', isset( $show['account'] ) );
  145. $this->addWhereIf( 'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show['!ip'] ) );
  146. $this->addWhereIf( 'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show['ip'] ) );
  147. $this->addWhereIf( 'ipb_expiry = ' .
  148. $db->addQuotes( $db->getInfinity() ), isset( $show['!temp'] ) );
  149. $this->addWhereIf( 'ipb_expiry != ' .
  150. $db->addQuotes( $db->getInfinity() ), isset( $show['temp'] ) );
  151. $this->addWhereIf( 'ipb_range_end = ipb_range_start', isset( $show['!range'] ) );
  152. $this->addWhereIf( 'ipb_range_end > ipb_range_start', isset( $show['range'] ) );
  153. }
  154. if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
  155. $this->addWhereFld( 'ipb_deleted', 0 );
  156. }
  157. # Filter out expired rows
  158. $this->addWhere( 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() ) );
  159. $res = $this->select( __METHOD__ );
  160. $count = 0;
  161. foreach ( $res as $row ) {
  162. if ( ++$count > $params['limit'] ) {
  163. // We've had enough
  164. $this->setContinueEnumParameter( 'continue', "$row->ipb_timestamp|$row->ipb_id" );
  165. break;
  166. }
  167. $block = [
  168. ApiResult::META_TYPE => 'assoc',
  169. ];
  170. if ( $fld_id ) {
  171. $block['id'] = (int)$row->ipb_id;
  172. }
  173. if ( $fld_user && !$row->ipb_auto ) {
  174. $block['user'] = $row->ipb_address;
  175. }
  176. if ( $fld_userid && !$row->ipb_auto ) {
  177. $block['userid'] = (int)$row->ipb_user;
  178. }
  179. if ( $fld_by ) {
  180. $block['by'] = $row->ipb_by_text;
  181. }
  182. if ( $fld_byid ) {
  183. $block['byid'] = (int)$row->ipb_by;
  184. }
  185. if ( $fld_timestamp ) {
  186. $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
  187. }
  188. if ( $fld_expiry ) {
  189. $block['expiry'] = ApiResult::formatExpiry( $row->ipb_expiry );
  190. }
  191. if ( $fld_reason ) {
  192. $block['reason'] = $commentStore->getComment( $row )->text;
  193. }
  194. if ( $fld_range && !$row->ipb_auto ) {
  195. $block['rangestart'] = IP::formatHex( $row->ipb_range_start );
  196. $block['rangeend'] = IP::formatHex( $row->ipb_range_end );
  197. }
  198. if ( $fld_flags ) {
  199. // For clarity, these flags use the same names as their action=block counterparts
  200. $block['automatic'] = (bool)$row->ipb_auto;
  201. $block['anononly'] = (bool)$row->ipb_anon_only;
  202. $block['nocreate'] = (bool)$row->ipb_create_account;
  203. $block['autoblock'] = (bool)$row->ipb_enable_autoblock;
  204. $block['noemail'] = (bool)$row->ipb_block_email;
  205. $block['hidden'] = (bool)$row->ipb_deleted;
  206. $block['allowusertalk'] = (bool)$row->ipb_allow_usertalk;
  207. }
  208. $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $block );
  209. if ( !$fit ) {
  210. $this->setContinueEnumParameter( 'continue', "$row->ipb_timestamp|$row->ipb_id" );
  211. break;
  212. }
  213. }
  214. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'block' );
  215. }
  216. protected function prepareUsername( $user ) {
  217. if ( !$user ) {
  218. $encParamName = $this->encodeParamName( 'users' );
  219. $this->dieWithError( [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $user ) ],
  220. "baduser_{$encParamName}"
  221. );
  222. }
  223. $name = User::isIP( $user )
  224. ? $user
  225. : User::getCanonicalName( $user, 'valid' );
  226. if ( $name === false ) {
  227. $encParamName = $this->encodeParamName( 'users' );
  228. $this->dieWithError( [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $user ) ],
  229. "baduser_{$encParamName}"
  230. );
  231. }
  232. return $name;
  233. }
  234. public function getAllowedParams() {
  235. $blockCIDRLimit = $this->getConfig()->get( 'BlockCIDRLimit' );
  236. return [
  237. 'start' => [
  238. ApiBase::PARAM_TYPE => 'timestamp'
  239. ],
  240. 'end' => [
  241. ApiBase::PARAM_TYPE => 'timestamp',
  242. ],
  243. 'dir' => [
  244. ApiBase::PARAM_TYPE => [
  245. 'newer',
  246. 'older'
  247. ],
  248. ApiBase::PARAM_DFLT => 'older',
  249. ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
  250. ],
  251. 'ids' => [
  252. ApiBase::PARAM_TYPE => 'integer',
  253. ApiBase::PARAM_ISMULTI => true
  254. ],
  255. 'users' => [
  256. ApiBase::PARAM_TYPE => 'user',
  257. ApiBase::PARAM_ISMULTI => true
  258. ],
  259. 'ip' => [
  260. ApiBase::PARAM_HELP_MSG => [
  261. 'apihelp-query+blocks-param-ip',
  262. $blockCIDRLimit['IPv4'],
  263. $blockCIDRLimit['IPv6'],
  264. ],
  265. ],
  266. 'limit' => [
  267. ApiBase::PARAM_DFLT => 10,
  268. ApiBase::PARAM_TYPE => 'limit',
  269. ApiBase::PARAM_MIN => 1,
  270. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  271. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  272. ],
  273. 'prop' => [
  274. ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
  275. ApiBase::PARAM_TYPE => [
  276. 'id',
  277. 'user',
  278. 'userid',
  279. 'by',
  280. 'byid',
  281. 'timestamp',
  282. 'expiry',
  283. 'reason',
  284. 'range',
  285. 'flags'
  286. ],
  287. ApiBase::PARAM_ISMULTI => true,
  288. ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
  289. ],
  290. 'show' => [
  291. ApiBase::PARAM_TYPE => [
  292. 'account',
  293. '!account',
  294. 'temp',
  295. '!temp',
  296. 'ip',
  297. '!ip',
  298. 'range',
  299. '!range',
  300. ],
  301. ApiBase::PARAM_ISMULTI => true
  302. ],
  303. 'continue' => [
  304. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  305. ],
  306. ];
  307. }
  308. protected function getExamplesMessages() {
  309. return [
  310. 'action=query&list=blocks'
  311. => 'apihelp-query+blocks-example-simple',
  312. 'action=query&list=blocks&bkusers=Alice|Bob'
  313. => 'apihelp-query+blocks-example-users',
  314. ];
  315. }
  316. public function getHelpUrls() {
  317. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Blocks';
  318. }
  319. }