64 parent::__construct( $query, $moduleName,
'bk' );
65 $this->blockStore = $blockStore;
66 $this->blockActionInfo = $blockActionInfo;
67 $this->blockRestrictionStore = $blockRestrictionStore;
68 $this->commentStore = $commentStore;
69 $this->hideUserUtils = $hideUserUtils;
70 $this->commentFormatter = $commentFormatter;
78 $prop = array_fill_keys(
$params[
'prop'],
true );
79 $fld_id = isset( $prop[
'id'] );
80 $fld_user = isset( $prop[
'user'] );
81 $fld_userid = isset( $prop[
'userid'] );
82 $fld_by = isset( $prop[
'by'] );
83 $fld_byid = isset( $prop[
'byid'] );
84 $fld_timestamp = isset( $prop[
'timestamp'] );
85 $fld_expiry = isset( $prop[
'expiry'] );
86 $fld_reason = isset( $prop[
'reason'] );
87 $fld_parsedreason = isset( $prop[
'parsedreason'] );
88 $fld_range = isset( $prop[
'range'] );
89 $fld_flags = isset( $prop[
'flags'] );
90 $fld_restrictions = isset( $prop[
'restrictions'] );
94 $this->
addTables( [
'block',
'block_target',
'block_target_user' =>
'user' ] );
96 'block_target' => [
'JOIN',
'bt_id=bl_target' ],
97 'block_target_user' => [
'LEFT JOIN',
'user_id=bt_user' ]
99 $this->
addFields( [
'bt_auto',
'bl_id',
'bl_timestamp' ] );
104 'bt_address_or_user_name' =>
'COALESCE(bt_address, bt_user_text)'
106 $fld_user || $fld_userid
109 if ( $fld_by || $fld_byid ) {
111 $this->
addFields( [
'actor_user',
'actor_name' ] );
112 $this->
addJoinConds( [
'actor' => [
'JOIN',
'actor_id=bl_by_actor' ] ] );
115 $this->
addFieldsIf( [
'bt_range_start',
'bt_range_end' ], $fld_range );
116 $this->
addFieldsIf( [
'bl_anon_only',
'bl_create_account',
'bl_enable_autoblock',
117 'bl_block_email',
'bl_deleted',
'bl_allow_usertalk',
'bl_sitewide' ],
119 $this->
addFieldsIf(
'bl_sitewide', $fld_restrictions );
121 if ( $fld_reason || $fld_parsedreason ) {
122 $commentQuery = $this->commentStore->getJoin(
'bl_reason' );
123 $this->
addTables( $commentQuery[
'tables'] );
124 $this->
addFields( $commentQuery[
'fields'] );
138 if (
$params[
'continue'] !==
null ) {
140 $op = (
$params[
'dir'] ==
'newer' ?
'>=' :
'<=' );
141 $this->
addWhere( $db->buildComparison( $op, [
142 'bl_timestamp' => $db->timestamp( $cont[0] ),
153 foreach (
$params[
'users'] as $target ) {
154 if ( IPUtils::isValid( $target ) || IPUtils::isValidRange( $target ) ) {
155 $addresses[] = $target;
157 $userNames[] = $target;
160 if ( $addresses && $userNames ) {
162 $ids = $db->newUnionQueryBuilder()
163 ->add( $db->newSelectQueryBuilder()
165 ->from(
'block_target' )
166 ->where( [
'bt_address' => $addresses ] )
168 ->add( $db->newSelectQueryBuilder()
170 ->from(
'block_target' )
171 ->join(
'user',
null,
'user_id=bt_user' )
172 ->where( [
'user_name' => $userNames ] )
174 ->caller( __METHOD__ )
175 ->fetchFieldValues();
177 $this->
addWhere( [
'bt_id' => $ids ] );
181 } elseif ( $addresses ) {
182 $this->
addWhere( [
'bt_address' => $addresses ] );
183 } elseif ( $userNames ) {
184 $this->
addWhere( [
'block_target_user.user_name' => $userNames ] );
191 if (
$params[
'ip'] !==
null ) {
193 if ( IPUtils::isIPv4(
$params[
'ip'] ) ) {
195 $cidrLimit = $blockCIDRLimit[
'IPv4'];
196 } elseif ( IPUtils::isIPv6(
$params[
'ip'] ) ) {
198 $cidrLimit = $blockCIDRLimit[
'IPv6'];
204 [ $ip, $range ] = IPUtils::parseCIDR(
$params[
'ip'] );
205 if ( $ip !==
false && $range !==
false && $range < $cidrLimit ) {
206 $this->
dieWithError( [
'apierror-cidrtoobroad', $type, $cidrLimit ] );
210 [ $lower, $upper ] = IPUtils::parseRange(
$params[
'ip'] );
212 $this->
addWhere( $this->blockStore->getRangeCond( $lower, $upper ) );
213 $this->
addWhere( [
'bt_auto' => 0 ] );
216 if (
$params[
'show'] !==
null ) {
217 $show = array_fill_keys(
$params[
'show'],
true );
220 if ( ( isset( $show[
'account'] ) && isset( $show[
'!account'] ) )
221 || ( isset( $show[
'ip'] ) && isset( $show[
'!ip'] ) )
222 || ( isset( $show[
'range'] ) && isset( $show[
'!range'] ) )
223 || ( isset( $show[
'temp'] ) && isset( $show[
'!temp'] ) )
228 $this->
addWhereIf( [
'bt_user' => 0 ], isset( $show[
'!account'] ) );
229 $this->
addWhereIf( $db->expr(
'bt_user',
'!=', 0 ), isset( $show[
'account'] ) );
231 $db->expr(
'bt_user',
'!=', 0 )->orExpr(
new RawSQLExpression(
'bt_range_end > bt_range_start' ) ),
232 isset( $show[
'!ip'] )
234 $this->
addWhereIf( [
'bt_user' => 0,
'bt_range_end = bt_range_start' ], isset( $show[
'ip'] ) );
235 $this->
addWhereIf( [
'bl_expiry' => $db->getInfinity() ], isset( $show[
'!temp'] ) );
236 $this->
addWhereIf( $db->expr(
'bl_expiry',
'!=', $db->getInfinity() ), isset( $show[
'temp'] ) );
237 $this->
addWhereIf(
'bt_range_end = bt_range_start', isset( $show[
'!range'] ) );
238 $this->
addWhereIf(
'bt_range_end > bt_range_start', isset( $show[
'range'] ) );
241 if ( !$this->
getAuthority()->isAllowed(
'hideuser' ) ) {
243 $this->hideUserUtils->getExpression( $db,
'block_target.bt_user' )
248 $this->
addWhere( $db->expr(
'bl_expiry',
'>', $db->timestamp() ) );
250 $res = $this->
select( __METHOD__ );
253 if ( $fld_restrictions ) {
254 $restrictions = $this->getRestrictionData( $res,
$params[
'limit'] );
258 foreach ( $res as $row ) {
259 if ( ++$count >
$params[
'limit'] ) {
268 $block[
'id'] = (int)$row->bl_id;
270 if ( $fld_user && !$row->bt_auto ) {
271 $block[
'user'] = $row->bt_address_or_user_name;
273 if ( $fld_userid && !$row->bt_auto ) {
274 $block[
'userid'] = (int)$row->bt_user;
277 $block[
'by'] = $row->actor_name;
280 $block[
'byid'] = (int)$row->actor_user;
282 if ( $fld_timestamp ) {
283 $block[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $row->bl_timestamp );
288 $duration = $this->
msg(
'infiniteblock' )->plain();
290 $duration = $this->
getLanguage()->formatDurationBetweenTimestamps(
295 $block[
'duration-l10n'] = $duration;
298 $block[
'reason'] = $this->commentStore->getComment(
'bl_reason', $row )->text;
300 if ( $fld_parsedreason ) {
301 $block[
'parsedreason'] = $this->commentFormatter->format(
302 $this->commentStore->getComment(
'bl_reason', $row )->text
305 if ( $fld_range && !$row->bt_auto && $row->bt_range_start !==
null ) {
306 $block[
'rangestart'] = IPUtils::formatHex( $row->bt_range_start );
307 $block[
'rangeend'] = IPUtils::formatHex( $row->bt_range_end );
311 $block[
'automatic'] = (bool)$row->bt_auto;
312 $block[
'anononly'] = (bool)$row->bl_anon_only;
313 $block[
'nocreate'] = (bool)$row->bl_create_account;
314 $block[
'autoblock'] = (bool)$row->bl_enable_autoblock;
315 $block[
'noemail'] = (bool)$row->bl_block_email;
316 $block[
'hidden'] = (bool)$row->bl_deleted;
317 $block[
'allowusertalk'] = (bool)$row->bl_allow_usertalk;
318 $block[
'partial'] = !(bool)$row->bl_sitewide;
321 if ( $fld_restrictions ) {
322 $block[
'restrictions'] = [];
323 if ( !$row->bl_sitewide && isset( $restrictions[$row->bl_id] ) ) {
324 $block[
'restrictions'] = $restrictions[$row->bl_id];
328 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $block );
334 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'block' );
345 private function getRestrictionData(
IResultWrapper $result, $limit ) {
348 foreach ( $result as $row ) {
349 if ( ++$count <= $limit && !( $row->ipb_sitewide ?? $row->bl_sitewide ) ) {
350 $partialIds[] = (int)( $row->ipb_id ?? $row->bl_id );
354 $restrictions = $this->blockRestrictionStore->loadByBlockId( $partialIds );
359 'ns' =>
'namespaces',
362 $keys[
'action'] =
'actions';
365 foreach ( $restrictions as $restriction ) {
366 $key = $keys[$restriction->getType()];
367 $id = $restriction->getBlockId();
368 switch ( $restriction->getType() ) {
371 '@phan-var \MediaWiki\Block\Restriction\PageRestriction $restriction';
372 $value = [
'id' => $restriction->getValue() ];
373 if ( $restriction->getTitle() ) {
378 $value = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
381 $value = $restriction->getValue();
384 if ( !isset( $data[$id][$key] ) ) {
385 $data[$id][$key] = [];
388 $data[$id][$key][] = $value;
399 ParamValidator::PARAM_TYPE =>
'timestamp'
402 ParamValidator::PARAM_TYPE =>
'timestamp',
405 ParamValidator::PARAM_TYPE => [
409 ParamValidator::PARAM_DEFAULT =>
'older',
412 'newer' =>
'api-help-paramvalue-direction-newer',
413 'older' =>
'api-help-paramvalue-direction-older',
417 ParamValidator::PARAM_TYPE =>
'integer',
418 ParamValidator::PARAM_ISMULTI => true
421 ParamValidator::PARAM_TYPE =>
'user',
422 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr' ],
423 ParamValidator::PARAM_ISMULTI => true
427 'apihelp-query+blocks-param-ip',
428 $blockCIDRLimit[
'IPv4'],
429 $blockCIDRLimit[
'IPv6'],
433 ParamValidator::PARAM_DEFAULT => 10,
434 ParamValidator::PARAM_TYPE =>
'limit',
435 IntegerDef::PARAM_MIN => 1,
440 ParamValidator::PARAM_DEFAULT =>
'id|user|by|timestamp|expiry|reason|flags',
441 ParamValidator::PARAM_TYPE => [
455 ParamValidator::PARAM_ISMULTI =>
true,
459 ParamValidator::PARAM_TYPE => [
469 ParamValidator::PARAM_ISMULTI => true
479 'action=query&list=blocks'
480 =>
'apihelp-query+blocks-example-simple',
481 'action=query&list=blocks&bkusers=Alice|Bob'
482 =>
'apihelp-query+blocks-example-users',
487 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Blocks';
492class_alias( ApiQueryBlocks::class,
'ApiQueryBlocks' );
wfIsInfinity( $str)
Determine input string is represents as infinity.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
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()
const EnablePartialActionBlocks
Name constant for the EnablePartialActionBlocks setting, for use with Config::get()