61 parent::__construct( $query, $moduleName,
'bk' );
62 $this->blockStore = $blockStore;
63 $this->blockActionInfo = $blockActionInfo;
64 $this->blockRestrictionStore = $blockRestrictionStore;
65 $this->commentStore = $commentStore;
66 $this->hideUserUtils = $hideUserUtils;
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'] );
89 $this->
addTables( [
'block',
'block_target',
'block_target_user' =>
'user' ] );
91 'block_target' => [
'JOIN',
'bt_id=bl_target' ],
92 'block_target_user' => [
'LEFT JOIN',
'user_id=bt_user' ]
94 $this->
addFields( [
'bt_auto',
'bl_id',
'bl_timestamp' ] );
99 'bt_address_or_user_name' =>
'COALESCE(bt_address, bt_user_text)'
101 $fld_user || $fld_userid
104 if ( $fld_by || $fld_byid ) {
106 $this->
addFields( [
'actor_user',
'actor_name' ] );
107 $this->
addJoinConds( [
'actor' => [
'JOIN',
'actor_id=bl_by_actor' ] ] );
110 $this->
addFieldsIf( [
'bt_range_start',
'bt_range_end' ], $fld_range );
111 $this->
addFieldsIf( [
'bl_anon_only',
'bl_create_account',
'bl_enable_autoblock',
112 'bl_block_email',
'bl_deleted',
'bl_allow_usertalk',
'bl_sitewide' ],
114 $this->
addFieldsIf(
'bl_sitewide', $fld_restrictions );
117 $commentQuery = $this->commentStore->getJoin(
'bl_reason' );
118 $this->
addTables( $commentQuery[
'tables'] );
119 $this->
addFields( $commentQuery[
'fields'] );
133 if (
$params[
'continue'] !==
null ) {
135 $op = (
$params[
'dir'] ==
'newer' ?
'>=' :
'<=' );
136 $this->
addWhere( $db->buildComparison( $op, [
137 'bl_timestamp' => $db->timestamp( $cont[0] ),
148 foreach (
$params[
'users'] as $target ) {
149 if ( IPUtils::isValid( $target ) || IPUtils::isValidRange( $target ) ) {
150 $addresses[] = $target;
152 $userNames[] = $target;
155 if ( $addresses && $userNames ) {
157 $ids = $db->newUnionQueryBuilder()
158 ->add( $db->newSelectQueryBuilder()
160 ->from(
'block_target' )
161 ->where( [
'bt_address' => $addresses ] )
163 ->add( $db->newSelectQueryBuilder()
165 ->from(
'block_target' )
166 ->join(
'user',
null,
'user_id=bt_user' )
167 ->where( [
'user_name' => $userNames ] )
169 ->caller( __METHOD__ )
170 ->fetchFieldValues();
172 $this->
addWhere( [
'bt_id' => $ids ] );
176 } elseif ( $addresses ) {
177 $this->
addWhere( [
'bt_address' => $addresses ] );
178 } elseif ( $userNames ) {
179 $this->
addWhere( [
'block_target_user.user_name' => $userNames ] );
186 if (
$params[
'ip'] !==
null ) {
188 if ( IPUtils::isIPv4(
$params[
'ip'] ) ) {
190 $cidrLimit = $blockCIDRLimit[
'IPv4'];
191 } elseif ( IPUtils::isIPv6(
$params[
'ip'] ) ) {
193 $cidrLimit = $blockCIDRLimit[
'IPv6'];
199 [ $ip, $range ] = IPUtils::parseCIDR(
$params[
'ip'] );
200 if ( $ip !==
false && $range !==
false && $range < $cidrLimit ) {
201 $this->
dieWithError( [
'apierror-cidrtoobroad', $type, $cidrLimit ] );
205 [ $lower, $upper ] = IPUtils::parseRange(
$params[
'ip'] );
207 $this->
addWhere( $this->blockStore->getRangeCond( $lower, $upper ) );
208 $this->
addWhere( [
'bt_auto' => 0 ] );
211 if (
$params[
'show'] !==
null ) {
212 $show = array_fill_keys(
$params[
'show'],
true );
215 if ( ( isset( $show[
'account'] ) && isset( $show[
'!account'] ) )
216 || ( isset( $show[
'ip'] ) && isset( $show[
'!ip'] ) )
217 || ( isset( $show[
'range'] ) && isset( $show[
'!range'] ) )
218 || ( isset( $show[
'temp'] ) && isset( $show[
'!temp'] ) )
223 $this->
addWhereIf( [
'bt_user' => 0 ], isset( $show[
'!account'] ) );
224 $this->
addWhereIf( $db->expr(
'bt_user',
'!=', 0 ), isset( $show[
'account'] ) );
226 $db->expr(
'bt_user',
'!=', 0 )->orExpr(
new RawSQLExpression(
'bt_range_end > bt_range_start' ) ),
227 isset( $show[
'!ip'] )
229 $this->
addWhereIf( [
'bt_user' => 0,
'bt_range_end = bt_range_start' ], isset( $show[
'ip'] ) );
230 $this->
addWhereIf( [
'bl_expiry' => $db->getInfinity() ], isset( $show[
'!temp'] ) );
231 $this->
addWhereIf( $db->expr(
'bl_expiry',
'!=', $db->getInfinity() ), isset( $show[
'temp'] ) );
232 $this->
addWhereIf(
'bt_range_end = bt_range_start', isset( $show[
'!range'] ) );
233 $this->
addWhereIf(
'bt_range_end > bt_range_start', isset( $show[
'range'] ) );
236 if ( !$this->
getAuthority()->isAllowed(
'hideuser' ) ) {
238 $this->hideUserUtils->getExpression( $db, $db->tableName(
'block_target' ) .
'.bt_user' )
243 $this->
addWhere( $db->expr(
'bl_expiry',
'>', $db->timestamp() ) );
245 $res = $this->
select( __METHOD__ );
248 if ( $fld_restrictions ) {
249 $restrictions = $this->getRestrictionData( $res,
$params[
'limit'] );
253 foreach ( $res as $row ) {
254 if ( ++$count >
$params[
'limit'] ) {
263 $block[
'id'] = (int)$row->bl_id;
265 if ( $fld_user && !$row->bt_auto ) {
266 $block[
'user'] = $row->bt_address_or_user_name;
268 if ( $fld_userid && !$row->bt_auto ) {
269 $block[
'userid'] = (int)$row->bt_user;
272 $block[
'by'] = $row->actor_name;
275 $block[
'byid'] = (int)$row->actor_user;
277 if ( $fld_timestamp ) {
278 $block[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $row->bl_timestamp );
284 $block[
'reason'] = $this->commentStore->getComment(
'bl_reason', $row )->text;
286 if ( $fld_range && !$row->bt_auto && $row->bt_range_start !==
null ) {
287 $block[
'rangestart'] = IPUtils::formatHex( $row->bt_range_start );
288 $block[
'rangeend'] = IPUtils::formatHex( $row->bt_range_end );
292 $block[
'automatic'] = (bool)$row->bt_auto;
293 $block[
'anononly'] = (bool)$row->bl_anon_only;
294 $block[
'nocreate'] = (bool)$row->bl_create_account;
295 $block[
'autoblock'] = (bool)$row->bl_enable_autoblock;
296 $block[
'noemail'] = (bool)$row->bl_block_email;
297 $block[
'hidden'] = (bool)$row->bl_deleted;
298 $block[
'allowusertalk'] = (bool)$row->bl_allow_usertalk;
299 $block[
'partial'] = !(bool)$row->bl_sitewide;
302 if ( $fld_restrictions ) {
303 $block[
'restrictions'] = [];
304 if ( !$row->bl_sitewide && isset( $restrictions[$row->bl_id] ) ) {
305 $block[
'restrictions'] = $restrictions[$row->bl_id];
309 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $block );
315 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'block' );
326 private function getRestrictionData(
IResultWrapper $result, $limit ) {
329 foreach ( $result as $row ) {
330 if ( ++$count <= $limit && !( $row->ipb_sitewide ?? $row->bl_sitewide ) ) {
331 $partialIds[] = (int)( $row->ipb_id ?? $row->bl_id );
335 $restrictions = $this->blockRestrictionStore->loadByBlockId( $partialIds );
340 'ns' =>
'namespaces',
343 $keys[
'action'] =
'actions';
346 foreach ( $restrictions as $restriction ) {
347 $key = $keys[$restriction->getType()];
348 $id = $restriction->getBlockId();
349 switch ( $restriction->getType() ) {
352 '@phan-var \MediaWiki\Block\Restriction\PageRestriction $restriction';
353 $value = [
'id' => $restriction->getValue() ];
354 if ( $restriction->getTitle() ) {
359 $value = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
362 $value = $restriction->getValue();
365 if ( !isset( $data[$id][$key] ) ) {
366 $data[$id][$key] = [];
369 $data[$id][$key][] = $value;
380 ParamValidator::PARAM_TYPE =>
'timestamp'
383 ParamValidator::PARAM_TYPE =>
'timestamp',
386 ParamValidator::PARAM_TYPE => [
390 ParamValidator::PARAM_DEFAULT =>
'older',
393 'newer' =>
'api-help-paramvalue-direction-newer',
394 'older' =>
'api-help-paramvalue-direction-older',
398 ParamValidator::PARAM_TYPE =>
'integer',
399 ParamValidator::PARAM_ISMULTI => true
402 ParamValidator::PARAM_TYPE =>
'user',
403 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr' ],
404 ParamValidator::PARAM_ISMULTI => true
408 'apihelp-query+blocks-param-ip',
409 $blockCIDRLimit[
'IPv4'],
410 $blockCIDRLimit[
'IPv6'],
414 ParamValidator::PARAM_DEFAULT => 10,
415 ParamValidator::PARAM_TYPE =>
'limit',
416 IntegerDef::PARAM_MIN => 1,
421 ParamValidator::PARAM_DEFAULT =>
'id|user|by|timestamp|expiry|reason|flags',
422 ParamValidator::PARAM_TYPE => [
435 ParamValidator::PARAM_ISMULTI =>
true,
439 ParamValidator::PARAM_TYPE => [
449 ParamValidator::PARAM_ISMULTI => true
459 'action=query&list=blocks'
460 =>
'apihelp-query+blocks-example-simple',
461 'action=query&list=blocks&bkusers=Alice|Bob'
462 =>
'apihelp-query+blocks-example-users',
467 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Blocks';
472class_alias( ApiQueryBlocks::class,
'ApiQueryBlocks' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
A class containing constants representing the names of configuration variables.
const BlockCIDRLimit
Name constant for the BlockCIDRLimit setting, for use with Config::get()
const EnablePartialActionBlocks
Name constant for the EnablePartialActionBlocks setting, for use with Config::get()