25use Wikimedia\Timestamp\TimestampFormat as TS;
51 parent::__construct( $query, $moduleName,
'bk' );
52 $this->blockStore = $blockStore;
53 $this->blockActionInfo = $blockActionInfo;
54 $this->blockRestrictionStore = $blockRestrictionStore;
55 $this->commentStore = $commentStore;
56 $this->hideUserUtils = $hideUserUtils;
57 $this->commentFormatter = $commentFormatter;
65 $prop = array_fill_keys( $params[
'prop'],
true );
66 $fld_id = isset( $prop[
'id'] );
67 $fld_user = isset( $prop[
'user'] );
68 $fld_userid = isset( $prop[
'userid'] );
69 $fld_by = isset( $prop[
'by'] );
70 $fld_byid = isset( $prop[
'byid'] );
71 $fld_timestamp = isset( $prop[
'timestamp'] );
72 $fld_expiry = isset( $prop[
'expiry'] );
73 $fld_reason = isset( $prop[
'reason'] );
74 $fld_parsedreason = isset( $prop[
'parsedreason'] );
75 $fld_range = isset( $prop[
'range'] );
76 $fld_flags = isset( $prop[
'flags'] );
77 $fld_restrictions = isset( $prop[
'restrictions'] );
81 $this->
addTables( [
'block',
'block_target',
'block_target_user' =>
'user' ] );
83 'block_target' => [
'JOIN',
'bt_id=bl_target' ],
84 'block_target_user' => [
'LEFT JOIN',
'user_id=bt_user' ]
86 $this->
addFields( [
'bt_auto',
'bl_id',
'bl_timestamp' ] );
91 'bt_address_or_user_name' =>
'COALESCE(bt_address, bt_user_text)'
93 $fld_user || $fld_userid
96 if ( $fld_by || $fld_byid ) {
98 $this->
addFields( [
'actor_user',
'actor_name' ] );
99 $this->
addJoinConds( [
'actor' => [
'JOIN',
'actor_id=bl_by_actor' ] ] );
102 $this->
addFieldsIf( [
'bt_range_start',
'bt_range_end' ], $fld_range );
103 $this->
addFieldsIf( [
'bl_anon_only',
'bl_create_account',
'bl_enable_autoblock',
104 'bl_block_email',
'bl_deleted',
'bl_allow_usertalk',
'bl_sitewide' ],
106 $this->
addFieldsIf(
'bl_sitewide', $fld_restrictions );
108 if ( $fld_reason || $fld_parsedreason ) {
109 $commentQuery = $this->commentStore->getJoin(
'bl_reason' );
110 $this->
addTables( $commentQuery[
'tables'] );
111 $this->
addFields( $commentQuery[
'fields'] );
115 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
125 if ( $params[
'continue'] !==
null ) {
127 $op = ( $params[
'dir'] ==
'newer' ?
'>=' :
'<=' );
128 $this->
addWhere( $db->buildComparison( $op, [
129 'bl_timestamp' => $db->timestamp( $cont[0] ),
134 if ( $params[
'ids'] ) {
137 if ( $params[
'users'] ) {
140 foreach ( $params[
'users'] as $target ) {
141 if ( IPUtils::isValid( $target ) || IPUtils::isValidRange( $target ) ) {
142 $addresses[] = $target;
144 $userNames[] = $target;
147 if ( $addresses && $userNames ) {
149 $ids = $db->newUnionQueryBuilder()
150 ->add( $db->newSelectQueryBuilder()
152 ->from(
'block_target' )
153 ->where( [
'bt_address' => $addresses ] )
155 ->add( $db->newSelectQueryBuilder()
157 ->from(
'block_target' )
158 ->join(
'user',
null,
'user_id=bt_user' )
159 ->where( [
'user_name' => $userNames ] )
161 ->caller( __METHOD__ )
162 ->fetchFieldValues();
164 $this->
addWhere( [
'bt_id' => $ids ] );
168 } elseif ( $addresses ) {
169 $this->
addWhere( [
'bt_address' => $addresses ] );
170 } elseif ( $userNames ) {
171 $this->
addWhere( [
'block_target_user.user_name' => $userNames ] );
178 if ( $params[
'ip'] !==
null ) {
180 if ( IPUtils::isIPv4( $params[
'ip'] ) ) {
182 $cidrLimit = $blockCIDRLimit[
'IPv4'];
183 } elseif ( IPUtils::isIPv6( $params[
'ip'] ) ) {
185 $cidrLimit = $blockCIDRLimit[
'IPv6'];
191 [ $ip, $range ] = IPUtils::parseCIDR( $params[
'ip'] );
192 if ( $ip !==
false && $range !==
false && $range < $cidrLimit ) {
193 $this->
dieWithError( [
'apierror-cidrtoobroad', $type, $cidrLimit ] );
197 [ $lower, $upper ] = IPUtils::parseRange( $params[
'ip'] );
199 $this->
addWhere( $this->blockStore->getRangeCond( $lower, $upper ) );
200 $this->
addWhere( [
'bt_auto' => 0 ] );
203 if ( $params[
'show'] !==
null ) {
204 $show = array_fill_keys( $params[
'show'],
true );
207 if ( ( isset( $show[
'account'] ) && isset( $show[
'!account'] ) )
208 || ( isset( $show[
'ip'] ) && isset( $show[
'!ip'] ) )
209 || ( isset( $show[
'range'] ) && isset( $show[
'!range'] ) )
210 || ( isset( $show[
'temp'] ) && isset( $show[
'!temp'] ) )
215 $this->
addWhereIf( [
'bt_user' => 0 ], isset( $show[
'!account'] ) );
216 $this->
addWhereIf( $db->expr(
'bt_user',
'!=', 0 ), isset( $show[
'account'] ) );
218 $db->expr(
'bt_user',
'!=', 0 )->orExpr(
new RawSQLExpression(
'bt_range_end > bt_range_start' ) ),
219 isset( $show[
'!ip'] )
221 $this->
addWhereIf( [
'bt_user' => 0,
'bt_range_end = bt_range_start' ], isset( $show[
'ip'] ) );
222 $this->
addWhereIf( [
'bl_expiry' => $db->getInfinity() ], isset( $show[
'!temp'] ) );
223 $this->
addWhereIf( $db->expr(
'bl_expiry',
'!=', $db->getInfinity() ), isset( $show[
'temp'] ) );
224 $this->
addWhereIf(
'bt_range_end = bt_range_start', isset( $show[
'!range'] ) );
225 $this->
addWhereIf(
'bt_range_end > bt_range_start', isset( $show[
'range'] ) );
228 if ( !$this->
getAuthority()->isAllowed(
'hideuser' ) ) {
229 $this->
addWhere( [
'bl_deleted' => 0 ] );
231 $this->hideUserUtils->getExpression( $db,
'block_target.bt_user' )
236 $this->
addWhere( $db->expr(
'bl_expiry',
'>', $db->timestamp() ) );
238 $res = $this->
select( __METHOD__ );
241 if ( $fld_restrictions ) {
242 $restrictions = $this->getRestrictionData( $res, $params[
'limit'] );
246 foreach ( $res as $row ) {
247 if ( ++$count > $params[
'limit'] ) {
256 $block[
'id'] = (int)$row->bl_id;
258 if ( $fld_user && !$row->bt_auto ) {
259 $block[
'user'] = $row->bt_address_or_user_name;
261 if ( $fld_userid && !$row->bt_auto ) {
262 $block[
'userid'] = (int)$row->bt_user;
265 $block[
'by'] = $row->actor_name;
268 $block[
'byid'] = (int)$row->actor_user;
270 if ( $fld_timestamp ) {
271 $block[
'timestamp'] =
wfTimestamp( TS::ISO_8601, $row->bl_timestamp );
276 $duration = $this->
msg(
'infiniteblock' )->plain();
278 $duration = $this->
getLanguage()->formatDurationBetweenTimestamps(
283 $block[
'duration-l10n'] = $duration;
286 $block[
'reason'] = $this->commentStore->getComment(
'bl_reason', $row )->text;
288 if ( $fld_parsedreason ) {
289 $block[
'parsedreason'] = $this->commentFormatter->format(
290 $this->commentStore->getComment(
'bl_reason', $row )->text
293 if ( $fld_range && !$row->bt_auto && $row->bt_range_start !==
null ) {
294 $block[
'rangestart'] = IPUtils::formatHex( $row->bt_range_start );
295 $block[
'rangeend'] = IPUtils::formatHex( $row->bt_range_end );
299 $block[
'automatic'] = (bool)$row->bt_auto;
300 $block[
'anononly'] = (bool)$row->bl_anon_only;
301 $block[
'nocreate'] = (bool)$row->bl_create_account;
302 $block[
'autoblock'] = (bool)$row->bl_enable_autoblock;
303 $block[
'noemail'] = (bool)$row->bl_block_email;
304 $block[
'hidden'] = (bool)$row->bl_deleted;
305 $block[
'allowusertalk'] = (bool)$row->bl_allow_usertalk;
306 $block[
'partial'] = !(bool)$row->bl_sitewide;
309 if ( $fld_restrictions ) {
310 $block[
'restrictions'] = [];
311 if ( !$row->bl_sitewide && isset( $restrictions[$row->bl_id] ) ) {
312 $block[
'restrictions'] = $restrictions[$row->bl_id];
316 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $block );
322 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'block' );
333 private function getRestrictionData(
IResultWrapper $result, $limit ) {
336 foreach ( $result as $row ) {
337 if ( ++$count <= $limit && !( $row->ipb_sitewide ?? $row->bl_sitewide ) ) {
338 $partialIds[] = (int)( $row->ipb_id ?? $row->bl_id );
342 $restrictions = $this->blockRestrictionStore->loadByBlockId( $partialIds );
347 'ns' =>
'namespaces',
348 'action' =>
'actions',
351 foreach ( $restrictions as $restriction ) {
352 $key = $keys[$restriction->getType()];
353 $id = $restriction->getBlockId();
354 switch ( $restriction->getType() ) {
356 $value = [
'id' => $restriction->getValue() ];
358 '@phan-var \MediaWiki\Block\Restriction\PageRestriction $restriction';
359 if ( !$restriction->getTitle() ) {
365 $value = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
368 $value = $restriction->getValue();
371 if ( !isset( $data[$id][$key] ) ) {
372 $data[$id][$key] = [];
375 $data[$id][$key][] = $value;
387 ParamValidator::PARAM_TYPE =>
'timestamp'
390 ParamValidator::PARAM_TYPE =>
'timestamp',
393 ParamValidator::PARAM_TYPE => [
397 ParamValidator::PARAM_DEFAULT =>
'older',
400 'newer' =>
'api-help-paramvalue-direction-newer',
401 'older' =>
'api-help-paramvalue-direction-older',
405 ParamValidator::PARAM_TYPE =>
'integer',
406 ParamValidator::PARAM_ISMULTI => true
409 ParamValidator::PARAM_TYPE =>
'user',
410 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr' ],
411 ParamValidator::PARAM_ISMULTI => true
415 'apihelp-query+blocks-param-ip',
416 $blockCIDRLimit[
'IPv4'],
417 $blockCIDRLimit[
'IPv6'],
421 ParamValidator::PARAM_DEFAULT => 10,
422 ParamValidator::PARAM_TYPE =>
'limit',
423 IntegerDef::PARAM_MIN => 1,
428 ParamValidator::PARAM_DEFAULT =>
'id|user|by|timestamp|expiry|reason|flags',
429 ParamValidator::PARAM_TYPE => [
443 ParamValidator::PARAM_ISMULTI =>
true,
447 ParamValidator::PARAM_TYPE => [
457 ParamValidator::PARAM_ISMULTI => true
468 'action=query&list=blocks'
469 =>
'apihelp-query+blocks-example-simple',
470 'action=query&list=blocks&bkusers=Alice|Bob'
471 =>
'apihelp-query+blocks-example-users',
477 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Blocks';
482class_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()