85 $prop = array_fill_keys(
$params[
'prop'],
true );
86 $fld_id = isset( $prop[
'id'] );
87 $fld_user = isset( $prop[
'user'] );
88 $fld_userid = isset( $prop[
'userid'] );
89 $fld_by = isset( $prop[
'by'] );
90 $fld_byid = isset( $prop[
'byid'] );
91 $fld_timestamp = isset( $prop[
'timestamp'] );
92 $fld_expiry = isset( $prop[
'expiry'] );
93 $fld_reason = isset( $prop[
'reason'] );
94 $fld_range = isset( $prop[
'range'] );
95 $fld_flags = isset( $prop[
'flags'] );
96 $fld_restrictions = isset( $prop[
'restrictions'] );
102 $this->
addFields( [
'ipb_auto',
'ipb_id',
'ipb_timestamp',
'ipb_user' ] );
103 $this->
addFieldsIf( [
'ipb_address',
'ipb_user' ], $fld_user || $fld_userid );
104 $bt_range_start =
'ipb_range_start';
105 $bt_range_end =
'ipb_range_end';
106 $bt_user =
'ipb_user';
107 $bt_auto =
'ipb_auto';
108 $bt_address_or_user_name =
'ipb_address';
109 $bl_by_actor =
'ipb_by_actor';
110 $bl_expiry =
'ipb_expiry';
111 $bl_anon_only =
'ipb_anon_only';
112 $bl_create_account =
'ipb_create_account';
113 $bl_enable_autoblock =
'ipb_enable_autoblock';
114 $bl_block_email =
'ipb_block_email';
115 $bl_deleted =
'ipb_deleted';
116 $bl_allow_usertalk =
'ipb_allow_usertalk';
117 $bl_sitewide =
'ipb_sitewide';
118 $bl_reason =
'ipb_reason';
119 $bl_timestamp =
'ipb_timestamp';
121 $blockTable =
'ipblocks';
123 $this->
addTables( [
'block',
'block_target',
'block_target_user' =>
'user' ] );
125 'block_target' => [
'JOIN',
'bt_id=bl_target' ],
126 'block_target_user' => [
'LEFT JOIN',
'user_id=bt_user' ]
128 $this->
addFields( [
'bt_auto',
'bl_id',
'bl_timestamp' ] );
133 'bt_address_or_user_name' =>
'COALESCE(bt_address, bt_user_text)'
135 $fld_user || $fld_userid
137 $bt_range_start =
'bt_range_start';
138 $bt_range_end =
'bt_range_end';
139 $bt_user =
'bt_user';
140 $bt_auto =
'bt_auto';
141 $bt_address_or_user_name =
'bt_address_or_user_name';
142 $bl_by_actor =
'bl_by_actor';
143 $bl_expiry =
'bl_expiry';
144 $bl_anon_only =
'bl_anon_only';
145 $bl_create_account =
'bl_create_account';
146 $bl_enable_autoblock =
'bl_enable_autoblock';
147 $bl_block_email =
'bl_block_email';
148 $bl_deleted =
'bl_deleted';
149 $bl_allow_usertalk =
'bl_allow_usertalk';
150 $bl_sitewide =
'bl_sitewide';
151 $bl_reason =
'bl_reason';
152 $bl_timestamp =
'bl_timestamp';
154 $blockTable =
'block';
157 if ( $fld_by || $fld_byid ) {
159 $this->
addFields( [
'actor_user',
'actor_name' ] );
160 $this->
addJoinConds( [
'actor' => [
'JOIN',
"actor_id=$bl_by_actor" ] ] );
163 $this->
addFieldsIf( [ $bt_range_start, $bt_range_end ], $fld_range );
164 $this->
addFieldsIf( [ $bl_anon_only, $bl_create_account, $bl_enable_autoblock,
165 $bl_block_email, $bl_deleted, $bl_allow_usertalk, $bl_sitewide ],
167 $this->
addFieldsIf( $bl_sitewide, $fld_restrictions );
170 $commentQuery = $this->commentStore->getJoin( $bl_reason );
171 $this->
addTables( $commentQuery[
'tables'] );
172 $this->
addFields( $commentQuery[
'fields'] );
186 if (
$params[
'continue'] !==
null ) {
188 $op = (
$params[
'dir'] ==
'newer' ?
'>=' :
'<=' );
189 $this->
addWhere( $db->buildComparison( $op, [
190 $bl_timestamp => $db->timestamp( $cont[0] ),
204 foreach (
$params[
'users'] as $target ) {
205 if ( IPUtils::isValid( $target ) || IPUtils::isValidRange( $target ) ) {
206 $addresses[] = $target;
208 $userNames[] = $target;
211 if ( $addresses && $userNames ) {
213 $ids = $db->newUnionQueryBuilder()
214 ->add( $db->newSelectQueryBuilder()
216 ->from(
'block_target' )
217 ->where( [
'bt_address' => $addresses ] )
219 ->add( $db->newSelectQueryBuilder()
221 ->from(
'block_target' )
222 ->join(
'user',
null,
'user_id=bt_user' )
223 ->where( [
'user_name' => $userNames ] )
225 ->caller( __METHOD__ )
226 ->fetchFieldValues();
228 $this->
addWhere( [
'bt_id' => $ids ] );
232 } elseif ( $addresses ) {
233 $this->
addWhere( [
'bt_address' => $addresses ] );
234 } elseif ( $userNames ) {
235 $this->
addWhere( [
'block_target_user.user_name' => $userNames ] );
243 if (
$params[
'ip'] !==
null ) {
244 $blockCIDRLimit = $this->
getConfig()->get( MainConfigNames::BlockCIDRLimit );
245 if ( IPUtils::isIPv4(
$params[
'ip'] ) ) {
247 $cidrLimit = $blockCIDRLimit[
'IPv4'];
248 } elseif ( IPUtils::isIPv6(
$params[
'ip'] ) ) {
250 $cidrLimit = $blockCIDRLimit[
'IPv6'];
256 [ $ip, $range ] = IPUtils::parseCIDR(
$params[
'ip'] );
257 if ( $ip !==
false && $range !==
false && $range < $cidrLimit ) {
258 $this->
dieWithError( [
'apierror-cidrtoobroad', $type, $cidrLimit ] );
262 [ $lower, $upper ] = IPUtils::parseRange(
$params[
'ip'] );
264 $this->
addWhere( $this->blockStore->getRangeCond(
265 $lower, $upper, DatabaseBlockStore::SCHEMA_CURRENT ) );
266 $this->
addWhere( [ $bt_auto => 0 ] );
269 if (
$params[
'show'] !==
null ) {
270 $show = array_fill_keys(
$params[
'show'],
true );
273 if ( ( isset( $show[
'account'] ) && isset( $show[
'!account'] ) )
274 || ( isset( $show[
'ip'] ) && isset( $show[
'!ip'] ) )
275 || ( isset( $show[
'range'] ) && isset( $show[
'!range'] ) )
276 || ( isset( $show[
'temp'] ) && isset( $show[
'!temp'] ) )
281 $this->
addWhereIf( [ $bt_user => 0 ], isset( $show[
'!account'] ) );
282 $this->
addWhereIf(
"$bt_user != 0", isset( $show[
'account'] ) );
283 $this->
addWhereIf(
"$bt_user != 0 OR $bt_range_end > $bt_range_start", isset( $show[
'!ip'] ) );
284 $this->
addWhereIf(
"$bt_user = 0 AND $bt_range_end = $bt_range_start", isset( $show[
'ip'] ) );
285 $this->
addWhereIf( [ $bl_expiry => $db->getInfinity() ], isset( $show[
'!temp'] ) );
286 $this->
addWhereIf( $db->expr( $bl_expiry,
'!=', $db->getInfinity() ), isset( $show[
'temp'] ) );
287 $this->
addWhereIf(
"$bt_range_end = $bt_range_start", isset( $show[
'!range'] ) );
288 $this->
addWhereIf(
"$bt_range_end > $bt_range_start", isset( $show[
'range'] ) );
291 if ( !$this->
getAuthority()->isAllowed(
'hideuser' ) ) {
296 $this->hideUserUtils->getExpression( $db,
'block_target.bt_user' )
302 $this->
addWhere( $db->expr( $bl_expiry,
'>', $db->timestamp() ) );
304 $res = $this->
select( __METHOD__ );
307 if ( $fld_restrictions ) {
308 $restrictions = $this->getRestrictionData( $res,
$params[
'limit'] );
312 foreach ( $res as $row ) {
313 if ( ++$count >
$params[
'limit'] ) {
319 ApiResult::META_TYPE =>
'assoc',
322 $block[
'id'] = (int)$row->$bl_id;
324 if ( $fld_user && !$row->$bt_auto ) {
325 $block[
'user'] = $row->$bt_address_or_user_name;
327 if ( $fld_userid && !$row->$bt_auto ) {
328 $block[
'userid'] = (int)$row->$bt_user;
331 $block[
'by'] = $row->actor_name;
334 $block[
'byid'] = (int)$row->actor_user;
336 if ( $fld_timestamp ) {
337 $block[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $row->$bl_timestamp );
340 $block[
'expiry'] = ApiResult::formatExpiry( $row->$bl_expiry );
343 $block[
'reason'] = $this->commentStore->getComment( $bl_reason, $row )->text;
345 if ( $fld_range && !$row->$bt_auto && $row->$bt_range_start !==
null ) {
346 $block[
'rangestart'] = IPUtils::formatHex( $row->$bt_range_start );
347 $block[
'rangeend'] = IPUtils::formatHex( $row->$bt_range_end );
351 $block[
'automatic'] = (bool)$row->$bt_auto;
352 $block[
'anononly'] = (bool)$row->$bl_anon_only;
353 $block[
'nocreate'] = (bool)$row->$bl_create_account;
354 $block[
'autoblock'] = (bool)$row->$bl_enable_autoblock;
355 $block[
'noemail'] = (bool)$row->$bl_block_email;
356 $block[
'hidden'] = (bool)$row->$bl_deleted;
357 $block[
'allowusertalk'] = (bool)$row->$bl_allow_usertalk;
358 $block[
'partial'] = !(bool)$row->$bl_sitewide;
361 if ( $fld_restrictions ) {
362 $block[
'restrictions'] = [];
363 if ( !$row->$bl_sitewide && isset( $restrictions[$row->$bl_id] ) ) {
364 $block[
'restrictions'] = $restrictions[$row->$bl_id];
368 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $block );
374 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'block' );
435 $blockCIDRLimit = $this->
getConfig()->get( MainConfigNames::BlockCIDRLimit );
439 ParamValidator::PARAM_TYPE =>
'timestamp'
442 ParamValidator::PARAM_TYPE =>
'timestamp',
445 ParamValidator::PARAM_TYPE => [
449 ParamValidator::PARAM_DEFAULT =>
'older',
452 'newer' =>
'api-help-paramvalue-direction-newer',
453 'older' =>
'api-help-paramvalue-direction-older',
457 ParamValidator::PARAM_TYPE =>
'integer',
458 ParamValidator::PARAM_ISMULTI => true
461 ParamValidator::PARAM_TYPE =>
'user',
462 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr' ],
463 ParamValidator::PARAM_ISMULTI => true
467 'apihelp-query+blocks-param-ip',
468 $blockCIDRLimit[
'IPv4'],
469 $blockCIDRLimit[
'IPv6'],
473 ParamValidator::PARAM_DEFAULT => 10,
474 ParamValidator::PARAM_TYPE =>
'limit',
475 IntegerDef::PARAM_MIN => 1,
480 ParamValidator::PARAM_DEFAULT =>
'id|user|by|timestamp|expiry|reason|flags',
481 ParamValidator::PARAM_TYPE => [
494 ParamValidator::PARAM_ISMULTI =>
true,
498 ParamValidator::PARAM_TYPE => [
508 ParamValidator::PARAM_ISMULTI => true