25use Wikimedia\Timestamp\TimestampFormat as TS;
44 parent::__construct( $query, $moduleName,
'bk' );
52 $prop = array_fill_keys( $params[
'prop'],
true );
53 $fld_id = isset( $prop[
'id'] );
54 $fld_user = isset( $prop[
'user'] );
55 $fld_userid = isset( $prop[
'userid'] );
56 $fld_by = isset( $prop[
'by'] );
57 $fld_byid = isset( $prop[
'byid'] );
58 $fld_timestamp = isset( $prop[
'timestamp'] );
59 $fld_expiry = isset( $prop[
'expiry'] );
60 $fld_reason = isset( $prop[
'reason'] );
61 $fld_parsedreason = isset( $prop[
'parsedreason'] );
62 $fld_range = isset( $prop[
'range'] );
63 $fld_flags = isset( $prop[
'flags'] );
64 $fld_restrictions = isset( $prop[
'restrictions'] );
70 'block_target' =>
'block_target',
71 'block_target_user' =>
'user'
74 'block_target' => [
'JOIN',
'bt_id=bl_target' ],
75 'block_target_user' => [
'LEFT JOIN',
'user_id=bt_user' ]
77 $this->
addFields( [
'bt_auto',
'bl_id',
'bl_timestamp' ] );
82 'bt_address_or_user_name' =>
'COALESCE(bt_address, bt_user_text)'
84 $fld_user || $fld_userid
87 if ( $fld_by || $fld_byid ) {
89 $this->
addFields( [
'actor_user',
'actor_name' ] );
90 $this->
addJoinConds( [
'actor' => [
'JOIN',
'actor_id=bl_by_actor' ] ] );
93 $this->
addFieldsIf( [
'bt_range_start',
'bt_range_end' ], $fld_range );
94 $this->
addFieldsIf( [
'bl_anon_only',
'bl_create_account',
'bl_enable_autoblock',
95 'bl_block_email',
'bl_deleted',
'bl_allow_usertalk',
'bl_sitewide' ],
97 $this->
addFieldsIf(
'bl_sitewide', $fld_restrictions );
99 if ( $fld_reason || $fld_parsedreason ) {
100 $commentQuery = $this->commentStore->getJoin(
'bl_reason' );
101 $this->
addTables( $commentQuery[
'tables'] );
102 $this->
addFields( $commentQuery[
'fields'] );
106 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
116 if ( $params[
'continue'] !==
null ) {
118 $op = ( $params[
'dir'] ==
'newer' ?
'>=' :
'<=' );
119 $this->
addWhere( $db->buildComparison( $op, [
120 'bl_timestamp' => $db->timestamp( $cont[0] ),
125 if ( $params[
'ids'] ) {
128 if ( $params[
'users'] ) {
131 foreach ( $params[
'users'] as $target ) {
132 if ( IPUtils::isValid( $target ) || IPUtils::isValidRange( $target ) ) {
133 $addresses[] = $target;
135 $userNames[] = $target;
138 if ( $addresses && $userNames ) {
140 $ids = $db->newUnionQueryBuilder()
141 ->add( $db->newSelectQueryBuilder()
143 ->from(
'block_target' )
144 ->where( [
'bt_address' => $addresses ] )
146 ->add( $db->newSelectQueryBuilder()
148 ->from(
'block_target' )
149 ->join(
'user',
null,
'user_id=bt_user' )
150 ->where( [
'user_name' => $userNames ] )
152 ->caller( __METHOD__ )
153 ->fetchFieldValues();
155 $this->
addWhere( [
'bt_id' => $ids ] );
159 } elseif ( $addresses ) {
160 $this->
addWhere( [
'bt_address' => $addresses ] );
161 } elseif ( $userNames ) {
162 $this->
addWhere( [
'block_target_user.user_name' => $userNames ] );
169 if ( $params[
'ip'] !==
null ) {
171 if ( IPUtils::isIPv4( $params[
'ip'] ) ) {
173 $cidrLimit = $blockCIDRLimit[
'IPv4'];
174 } elseif ( IPUtils::isIPv6( $params[
'ip'] ) ) {
176 $cidrLimit = $blockCIDRLimit[
'IPv6'];
182 [ $ip, $range ] = IPUtils::parseCIDR( $params[
'ip'] );
183 if ( $ip !==
false && $range !==
false && $range < $cidrLimit ) {
184 $this->
dieWithError( [
'apierror-cidrtoobroad', $type, $cidrLimit ] );
188 [ $lower, $upper ] = IPUtils::parseRange( $params[
'ip'] );
190 $this->
addWhere( $this->blockStore->getRangeCond( $lower, $upper ) );
191 $this->
addWhere( [
'bt_auto' => 0 ] );
194 if ( $params[
'show'] !==
null ) {
195 $show = array_fill_keys( $params[
'show'],
true );
198 if ( ( isset( $show[
'account'] ) && isset( $show[
'!account'] ) )
199 || ( isset( $show[
'ip'] ) && isset( $show[
'!ip'] ) )
200 || ( isset( $show[
'range'] ) && isset( $show[
'!range'] ) )
201 || ( isset( $show[
'temp'] ) && isset( $show[
'!temp'] ) )
206 $this->
addWhereIf( [
'bt_user' => 0 ], isset( $show[
'!account'] ) );
207 $this->
addWhereIf( $db->expr(
'bt_user',
'!=', 0 ), isset( $show[
'account'] ) );
209 $db->expr(
'bt_user',
'!=', 0 )->orExpr(
new RawSQLExpression(
'bt_range_end > bt_range_start' ) ),
210 isset( $show[
'!ip'] )
212 $this->
addWhereIf( [
'bt_user' => 0,
'bt_range_end = bt_range_start' ], isset( $show[
'ip'] ) );
213 $this->
addWhereIf( [
'bl_expiry' => $db->getInfinity() ], isset( $show[
'!temp'] ) );
214 $this->
addWhereIf( $db->expr(
'bl_expiry',
'!=', $db->getInfinity() ), isset( $show[
'temp'] ) );
215 $this->
addWhereIf(
'bt_range_end = bt_range_start', isset( $show[
'!range'] ) );
216 $this->
addWhereIf(
'bt_range_end > bt_range_start', isset( $show[
'range'] ) );
219 if ( !$this->
getAuthority()->isAllowed(
'hideuser' ) ) {
220 $this->
addWhere( [
'bl_deleted' => 0 ] );
222 $this->hideUserUtils->getExpression( $db,
'block_target.bt_user' )
227 $this->
addWhere( $db->expr(
'bl_expiry',
'>', $db->timestamp() ) );
229 $res = $this->
select( __METHOD__ );
232 if ( $fld_restrictions ) {
233 $restrictions = $this->getRestrictionData( $res, $params[
'limit'] );
237 foreach ( $res as $row ) {
238 if ( ++$count > $params[
'limit'] ) {
247 $block[
'id'] = (int)$row->bl_id;
249 if ( $fld_user && !$row->bt_auto ) {
250 $block[
'user'] = $row->bt_address_or_user_name;
252 if ( $fld_userid && !$row->bt_auto ) {
253 $block[
'userid'] = (int)$row->bt_user;
256 $block[
'by'] = $row->actor_name;
259 $block[
'byid'] = (int)$row->actor_user;
261 if ( $fld_timestamp ) {
262 $block[
'timestamp'] =
wfTimestamp( TS::ISO_8601, $row->bl_timestamp );
267 $duration = $this->
msg(
'infiniteblock' )->plain();
269 $duration = $this->
getLanguage()->formatDurationBetweenTimestamps(
274 $block[
'duration-l10n'] = $duration;
277 $block[
'reason'] = $this->commentStore->getComment(
'bl_reason', $row )->text;
279 if ( $fld_parsedreason ) {
280 $block[
'parsedreason'] = $this->commentFormatter->format(
281 $this->commentStore->getComment(
'bl_reason', $row )->text
284 if ( $fld_range && !$row->bt_auto && $row->bt_range_start !==
null ) {
285 $block[
'rangestart'] = IPUtils::formatHex( $row->bt_range_start );
286 $block[
'rangeend'] = IPUtils::formatHex( $row->bt_range_end );
290 $block[
'automatic'] = (bool)$row->bt_auto;
291 $block[
'anononly'] = (bool)$row->bl_anon_only;
292 $block[
'nocreate'] = (bool)$row->bl_create_account;
293 $block[
'autoblock'] = (bool)$row->bl_enable_autoblock;
294 $block[
'noemail'] = (bool)$row->bl_block_email;
295 $block[
'hidden'] = intval( $row->bl_deleted ) === 1;
296 $block[
'block-hidden'] = (bool)$row->bl_deleted;
297 $block[
'allowusertalk'] = (bool)$row->bl_allow_usertalk;
298 $block[
'partial'] = !(bool)$row->bl_sitewide;
301 if ( $fld_restrictions ) {
302 $block[
'restrictions'] = [];
303 if ( !$row->bl_sitewide && isset( $restrictions[$row->bl_id] ) ) {
304 $block[
'restrictions'] = $restrictions[$row->bl_id];
308 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $block );
314 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'block' );
325 private function getRestrictionData(
IResultWrapper $result, $limit ) {
328 foreach ( $result as $row ) {
329 if ( ++$count <= $limit && !( $row->ipb_sitewide ?? $row->bl_sitewide ) ) {
330 $partialIds[] = (int)( $row->ipb_id ?? $row->bl_id );
334 $restrictions = $this->blockRestrictionStore->loadByBlockId( $partialIds );
339 'ns' =>
'namespaces',
340 'action' =>
'actions',
343 foreach ( $restrictions as $restriction ) {
344 $key = $keys[$restriction->getType()];
345 $id = $restriction->getBlockId();
346 switch ( $restriction->getType() ) {
348 $value = [
'id' => $restriction->getValue() ];
350 '@phan-var \MediaWiki\Block\Restriction\PageRestriction $restriction';
351 if ( !$restriction->getTitle() ) {
357 $value = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
360 $value = $restriction->getValue();
363 if ( !isset( $data[$id][$key] ) ) {
364 $data[$id][$key] = [];
367 $data[$id][$key][] = $value;
379 ParamValidator::PARAM_TYPE =>
'timestamp'
382 ParamValidator::PARAM_TYPE =>
'timestamp',
385 ParamValidator::PARAM_TYPE => [
389 ParamValidator::PARAM_DEFAULT =>
'older',
392 'newer' =>
'api-help-paramvalue-direction-newer',
393 'older' =>
'api-help-paramvalue-direction-older',
397 ParamValidator::PARAM_TYPE =>
'integer',
398 ParamValidator::PARAM_ISMULTI => true
401 ParamValidator::PARAM_TYPE =>
'user',
402 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr' ],
403 ParamValidator::PARAM_ISMULTI => true
407 'apihelp-query+blocks-param-ip',
408 $blockCIDRLimit[
'IPv4'],
409 $blockCIDRLimit[
'IPv6'],
413 ParamValidator::PARAM_DEFAULT => 10,
414 ParamValidator::PARAM_TYPE =>
'limit',
415 IntegerDef::PARAM_MIN => 1,
420 ParamValidator::PARAM_DEFAULT =>
'id|user|by|timestamp|expiry|reason|flags',
421 ParamValidator::PARAM_TYPE => [
435 ParamValidator::PARAM_ISMULTI =>
true,
439 ParamValidator::PARAM_TYPE => [
449 ParamValidator::PARAM_ISMULTI => true
460 'action=query&list=blocks'
461 =>
'apihelp-query+blocks-example-simple',
462 'action=query&list=blocks&bkusers=Alice|Bob'
463 =>
'apihelp-query+blocks-example-users',
469 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Blocks';
474class_alias( ApiQueryBlocks::class,
'ApiQueryBlocks' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfIsInfinity( $str)
Determine input string is represents as infinity.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
A class containing constants representing the names of configuration variables.
const BlockCIDRLimit
Name constant for the BlockCIDRLimit setting, for use with Config::get()