41 private $blockActionInfo;
44 private $blockRestrictionStore;
47 private $commentStore;
63 parent::__construct( $query, $moduleName,
'bk' );
64 $this->blockActionInfo = $blockActionInfo;
65 $this->blockRestrictionStore = $blockRestrictionStore;
66 $this->commentStore = $commentStore;
74 $prop = array_fill_keys( $params[
'prop'],
true );
75 $fld_id = isset( $prop[
'id'] );
76 $fld_user = isset( $prop[
'user'] );
77 $fld_userid = isset( $prop[
'userid'] );
78 $fld_by = isset( $prop[
'by'] );
79 $fld_byid = isset( $prop[
'byid'] );
80 $fld_timestamp = isset( $prop[
'timestamp'] );
81 $fld_expiry = isset( $prop[
'expiry'] );
82 $fld_reason = isset( $prop[
'reason'] );
83 $fld_range = isset( $prop[
'range'] );
84 $fld_flags = isset( $prop[
'flags'] );
85 $fld_restrictions = isset( $prop[
'restrictions'] );
90 $this->
addFields( [
'ipb_auto',
'ipb_id',
'ipb_timestamp' ] );
92 $this->
addFieldsIf( [
'ipb_address',
'ipb_user' ], $fld_user || $fld_userid );
93 if ( $fld_by || $fld_byid ) {
95 $this->
addFields( [
'actor_user',
'actor_name' ] );
96 $this->
addJoinConds( [
'actor' => [
'JOIN',
'actor_id=ipb_by_actor' ] ] );
99 $this->
addFieldsIf( [
'ipb_range_start',
'ipb_range_end' ], $fld_range );
100 $this->
addFieldsIf( [
'ipb_anon_only',
'ipb_create_account',
'ipb_enable_autoblock',
101 'ipb_block_email',
'ipb_deleted',
'ipb_allow_usertalk',
'ipb_sitewide' ],
103 $this->
addFieldsIf(
'ipb_sitewide', $fld_restrictions );
106 $commentQuery = $this->commentStore->getJoin(
'ipb_reason' );
107 $this->
addTables( $commentQuery[
'tables'] );
108 $this->
addFields( $commentQuery[
'fields'] );
112 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
120 $this->
addWhereRange(
'ipb_id', $params[
'dir'],
null,
null );
122 if ( $params[
'continue'] !==
null ) {
123 $cont = explode(
'|', $params[
'continue'] );
125 $op = ( $params[
'dir'] ==
'newer' ?
'>' :
'<' );
126 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
127 $continueId = (int)$cont[1];
129 $this->
addWhere(
"ipb_timestamp $op $continueTimestamp OR " .
130 "(ipb_timestamp = $continueTimestamp AND " .
131 "ipb_id $op= $continueId)"
135 if ( $params[
'ids'] ) {
138 if ( $params[
'users'] ) {
139 $this->
addWhereFld(
'ipb_address', $params[
'users'] );
142 if ( $params[
'ip'] !==
null ) {
143 $blockCIDRLimit = $this->
getConfig()->get( MainConfigNames::BlockCIDRLimit );
144 if ( IPUtils::isIPv4( $params[
'ip'] ) ) {
146 $cidrLimit = $blockCIDRLimit[
'IPv4'];
148 } elseif ( IPUtils::isIPv6( $params[
'ip'] ) ) {
150 $cidrLimit = $blockCIDRLimit[
'IPv6'];
156 # Check range validity, if it's a CIDR
157 list( $ip, $range ) = IPUtils::parseCIDR( $params[
'ip'] );
158 if ( $ip !==
false && $range !==
false && $range < $cidrLimit ) {
162 # Let IPUtils::parseRange handle calculating $upper, instead of duplicating the logic here.
163 list( $lower, $upper ) = IPUtils::parseRange( $params[
'ip'] );
165 # Extract the common prefix to any rangeblock affecting this IP/CIDR
166 $prefix = substr( $lower, 0, $prefixLen + (
int)floor( $cidrLimit / 4 ) );
168 # Fairly hard to make a malicious SQL statement out of hex characters,
169 # but it is good practice to add quotes
170 $lower = $db->addQuotes( $lower );
171 $upper = $db->addQuotes( $upper );
174 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
175 'ipb_range_start <= ' . $lower,
176 'ipb_range_end >= ' . $upper,
181 if ( $params[
'show'] !==
null ) {
182 $show = array_fill_keys( $params[
'show'],
true );
185 if ( ( isset( $show[
'account'] ) && isset( $show[
'!account'] ) )
186 || ( isset( $show[
'ip'] ) && isset( $show[
'!ip'] ) )
187 || ( isset( $show[
'range'] ) && isset( $show[
'!range'] ) )
188 || ( isset( $show[
'temp'] ) && isset( $show[
'!temp'] ) )
193 $this->
addWhereIf(
'ipb_user = 0', isset( $show[
'!account'] ) );
194 $this->
addWhereIf(
'ipb_user != 0', isset( $show[
'account'] ) );
195 $this->
addWhereIf(
'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show[
'!ip'] ) );
196 $this->
addWhereIf(
'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show[
'ip'] ) );
198 $db->addQuotes( $db->getInfinity() ), isset( $show[
'!temp'] ) );
200 $db->addQuotes( $db->getInfinity() ), isset( $show[
'temp'] ) );
201 $this->
addWhereIf(
'ipb_range_end = ipb_range_start', isset( $show[
'!range'] ) );
202 $this->
addWhereIf(
'ipb_range_end > ipb_range_start', isset( $show[
'range'] ) );
205 if ( !$this->
getAuthority()->isAllowed(
'hideuser' ) ) {
209 # Filter out expired rows
210 $this->
addWhere(
'ipb_expiry > ' . $db->addQuotes( $db->timestamp() ) );
215 if ( $fld_restrictions ) {
216 $restrictions = $this->getRestrictionData(
$res, $params[
'limit'] );
220 foreach (
$res as $row ) {
221 if ( ++$count > $params[
'limit'] ) {
227 ApiResult::META_TYPE =>
'assoc',
230 $block[
'id'] = (int)$row->ipb_id;
232 if ( $fld_user && !$row->ipb_auto ) {
233 $block[
'user'] = $row->ipb_address;
235 if ( $fld_userid && !$row->ipb_auto ) {
236 $block[
'userid'] = (int)$row->ipb_user;
239 $block[
'by'] = $row->actor_name;
242 $block[
'byid'] = (int)$row->actor_user;
244 if ( $fld_timestamp ) {
245 $block[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
248 $block[
'expiry'] = ApiResult::formatExpiry( $row->ipb_expiry );
251 $block[
'reason'] = $this->commentStore->getComment(
'ipb_reason', $row )->text;
253 if ( $fld_range && !$row->ipb_auto ) {
254 $block[
'rangestart'] = IPUtils::formatHex( $row->ipb_range_start );
255 $block[
'rangeend'] = IPUtils::formatHex( $row->ipb_range_end );
259 $block[
'automatic'] = (bool)$row->ipb_auto;
260 $block[
'anononly'] = (bool)$row->ipb_anon_only;
261 $block[
'nocreate'] = (bool)$row->ipb_create_account;
262 $block[
'autoblock'] = (bool)$row->ipb_enable_autoblock;
263 $block[
'noemail'] = (bool)$row->ipb_block_email;
264 $block[
'hidden'] = (bool)$row->ipb_deleted;
265 $block[
'allowusertalk'] = (bool)$row->ipb_allow_usertalk;
266 $block[
'partial'] = !(bool)$row->ipb_sitewide;
269 if ( $fld_restrictions ) {
270 $block[
'restrictions'] = [];
271 if ( !$row->ipb_sitewide && isset( $restrictions[$row->ipb_id] ) ) {
272 $block[
'restrictions'] = $restrictions[$row->ipb_id];
276 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $block );
282 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'block' );
293 private function getRestrictionData(
IResultWrapper $result, $limit ) {
296 foreach ( $result as $row ) {
297 if ( ++$count <= $limit && !$row->ipb_sitewide ) {
298 $partialIds[] = (int)$row->ipb_id;
302 $restrictions = $this->blockRestrictionStore->loadByBlockId( $partialIds );
307 'ns' =>
'namespaces',
309 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
310 $keys[
'action'] =
'actions';
313 foreach ( $restrictions as $restriction ) {
314 $key =
$keys[$restriction->getType()];
315 $id = $restriction->getBlockId();
316 switch ( $restriction->getType() ) {
319 '@phan-var \MediaWiki\Block\Restriction\PageRestriction $restriction';
320 $value = [
'id' => $restriction->getValue() ];
321 if ( $restriction->getTitle() ) {
326 $value = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
329 $value = $restriction->getValue();
332 if ( !isset( $data[$id][$key] ) ) {
333 $data[$id][$key] = [];
336 $data[$id][$key][] = $value;
343 $blockCIDRLimit = $this->
getConfig()->get( MainConfigNames::BlockCIDRLimit );
347 ParamValidator::PARAM_TYPE =>
'timestamp'
350 ParamValidator::PARAM_TYPE =>
'timestamp',
353 ParamValidator::PARAM_TYPE => [
357 ParamValidator::PARAM_DEFAULT =>
'older',
360 'newer' =>
'api-help-paramvalue-direction-newer',
361 'older' =>
'api-help-paramvalue-direction-older',
365 ParamValidator::PARAM_TYPE =>
'integer',
366 ParamValidator::PARAM_ISMULTI =>
true
369 ParamValidator::PARAM_TYPE =>
'user',
370 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'cidr' ],
371 ParamValidator::PARAM_ISMULTI =>
true
375 'apihelp-query+blocks-param-ip',
376 $blockCIDRLimit[
'IPv4'],
377 $blockCIDRLimit[
'IPv6'],
381 ParamValidator::PARAM_DEFAULT => 10,
382 ParamValidator::PARAM_TYPE =>
'limit',
383 IntegerDef::PARAM_MIN => 1,
388 ParamValidator::PARAM_DEFAULT =>
'id|user|by|timestamp|expiry|reason|flags',
389 ParamValidator::PARAM_TYPE => [
402 ParamValidator::PARAM_ISMULTI =>
true,
406 ParamValidator::PARAM_TYPE => [
416 ParamValidator::PARAM_ISMULTI =>
true
426 'action=query&list=blocks'
427 =>
'apihelp-query+blocks-example-simple',
428 'action=query&list=blocks&bkusers=Alice|Bob'
429 =>
'apihelp-query+blocks-example-users',
434 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Blocks';
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
const LIMIT_BIG1
Fast query, standard limit.
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
const LIMIT_BIG2
Fast query, apihighlimits limit.
getModuleName()
Get the name of the module being executed by this instance.
This is a base class for all Query modules.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addWhereIDsFld( $table, $field, $ids)
Like addWhereFld for an integer list of IDs.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Query module to enumerate all user blocks.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiQuery $query, $moduleName, BlockActionInfo $blockActionInfo, BlockRestrictionStore $blockRestrictionStore, CommentStore $commentStore)
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main query class.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
A class containing constants representing the names of configuration variables.