| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Block\BlockActionInfo; |
| 26 |
use MediaWiki\Block\BlockRestrictionStore; |
| 27 |
use MediaWiki\Block\DatabaseBlockStore; |
| 28 |
use MediaWiki\Block\HideUserUtils; |
| 29 |
use MediaWiki\Block\Restriction\PageRestriction; |
| 30 |
use MediaWiki\CommentStore\CommentStore; |
| 31 |
use MediaWiki\MainConfigNames; |
| 32 |
use MediaWiki\ParamValidator\TypeDef\UserDef; |
| 33 |
use Wikimedia\IPUtils; |
| 34 |
use Wikimedia\ParamValidator\ParamValidator; |
| 35 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 36 |
use Wikimedia\Rdbms\IResultWrapper; |
| 37 |
use Wikimedia\Rdbms\RawSQLExpression; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ApiQueryBlocks extends ApiQueryBase { |
| 45 |
|
| 46 |
private DatabaseBlockStore $blockStore; |
| 47 |
private BlockActionInfo $blockActionInfo; |
| 48 |
private BlockRestrictionStore $blockRestrictionStore; |
| 49 |
private CommentStore $commentStore; |
| 50 |
private HideUserUtils $hideUserUtils; |
| 51 |
|
| 52 |
public function __construct( |
| 53 |
ApiQuery $query, |
| 54 |
string $moduleName, |
| 55 |
DatabaseBlockStore $blockStore, |
| 56 |
BlockActionInfo $blockActionInfo, |
| 57 |
BlockRestrictionStore $blockRestrictionStore, |
| 58 |
CommentStore $commentStore, |
| 59 |
HideUserUtils $hideUserUtils |
| 60 |
) { |
| 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; |
| 67 |
} |
| 68 |
|
| 69 |
public function execute() { |
| 70 |
$db = $this->getDB(); |
| 71 |
$params = $this->extractRequestParams(); |
| 72 |
$this->requireMaxOneParameter( $params, 'users', 'ip' ); |
| 73 |
|
| 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'] ); |
| 86 |
|
| 87 |
$result = $this->getResult(); |
| 88 |
|
| 89 |
$this->addTables( [ 'block', 'block_target', 'block_target_user' => 'user' ] ); |
| 90 |
$this->addJoinConds( [ |
| 91 |
'block_target' => [ 'JOIN', 'bt_id=bl_target' ], |
| 92 |
'block_target_user' => [ 'LEFT JOIN', 'user_id=bt_user' ] |
| 93 |
] ); |
| 94 |
$this->addFields( [ 'bt_auto', 'bl_id', 'bl_timestamp' ] ); |
| 95 |
$this->addFieldsIf( |
| 96 |
[ |
| 97 |
'bt_address', |
| 98 |
'bt_user', |
| 99 |
'bt_address_or_user_name' => 'COALESCE(bt_address, bt_user_text)' |
| 100 |
], |
| 101 |
$fld_user || $fld_userid |
| 102 |
); |
| 103 |
|
| 104 |
if ( $fld_by || $fld_byid ) { |
| 105 |
$this->addTables( 'actor' ); |
| 106 |
$this->addFields( [ 'actor_user', 'actor_name' ] ); |
| 107 |
$this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id=bl_by_actor' ] ] ); |
| 108 |
} |
| 109 |
$this->addFieldsIf( 'bl_expiry', $fld_expiry ); |
| 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' ], |
| 113 |
$fld_flags ); |
| 114 |
$this->addFieldsIf( 'bl_sitewide', $fld_restrictions ); |
| 115 |
|
| 116 |
if ( $fld_reason ) { |
| 117 |
$commentQuery = $this->commentStore->getJoin( 'bl_reason' ); |
| 118 |
$this->addTables( $commentQuery['tables'] ); |
| 119 |
$this->addFields( $commentQuery['fields'] ); |
| 120 |
$this->addJoinConds( $commentQuery['joins'] ); |
| 121 |
} |
| 122 |
|
| 123 |
$this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 124 |
$this->addTimestampWhereRange( |
| 125 |
'bl_timestamp', |
| 126 |
$params['dir'], |
| 127 |
$params['start'], |
| 128 |
$params['end'] |
| 129 |
); |
| 130 |
|
| 131 |
$this->addWhereRange( 'bl_id', $params['dir'], null, null ); |
| 132 |
|
| 133 |
if ( $params['continue'] !== null ) { |
| 134 |
$cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] ); |
| 135 |
$op = ( $params['dir'] == 'newer' ? '>=' : '<=' ); |
| 136 |
$this->addWhere( $db->buildComparison( $op, [ |
| 137 |
'bl_timestamp' => $db->timestamp( $cont[0] ), |
| 138 |
'bl_id' => $cont[1], |
| 139 |
] ) ); |
| 140 |
} |
| 141 |
|
| 142 |
if ( $params['ids'] ) { |
| 143 |
$this->addWhereIDsFld( 'block', 'bl_id', $params['ids'] ); |
| 144 |
} |
| 145 |
if ( $params['users'] ) { |
| 146 |
$addresses = []; |
| 147 |
$userNames = []; |
| 148 |
foreach ( $params['users'] as $target ) { |
| 149 |
if ( IPUtils::isValid( $target ) || IPUtils::isValidRange( $target ) ) { |
| 150 |
$addresses[] = $target; |
| 151 |
} else { |
| 152 |
$userNames[] = $target; |
| 153 |
} |
| 154 |
} |
| 155 |
if ( $addresses && $userNames ) { |
| 156 |
|
| 157 |
$ids = $db->newUnionQueryBuilder() |
| 158 |
->add( $db->newSelectQueryBuilder() |
| 159 |
->select( 'bt_id' ) |
| 160 |
->from( 'block_target' ) |
| 161 |
->where( [ 'bt_address' => $addresses ] ) |
| 162 |
) |
| 163 |
->add( $db->newSelectQueryBuilder() |
| 164 |
->select( 'bt_id' ) |
| 165 |
->from( 'block_target' ) |
| 166 |
->join( 'user', null, 'user_id=bt_user' ) |
| 167 |
->where( [ 'user_name' => $userNames ] ) |
| 168 |
) |
| 169 |
->caller( __METHOD__ ) |
| 170 |
->fetchFieldValues(); |
| 171 |
if ( $ids ) { |
| 172 |
$this->addWhere( [ 'bt_id' => $ids ] ); |
| 173 |
} else { |
| 174 |
$this->addWhere( '1=0' ); |
| 175 |
} |
| 176 |
} elseif ( $addresses ) { |
| 177 |
$this->addWhere( [ 'bt_address' => $addresses ] ); |
| 178 |
} elseif ( $userNames ) { |
| 179 |
$this->addWhere( [ 'block_target_user.user_name' => $userNames ] ); |
| 180 |
} else { |
| 181 |
|
| 182 |
$this->addWhere( '1=0' ); |
| 183 |
} |
| 184 |
$this->addWhereFld( 'bt_auto', 0 ); |
| 185 |
} |
| 186 |
if ( $params['ip'] !== null ) { |
| 187 |
$blockCIDRLimit = $this->getConfig()->get( MainConfigNames::BlockCIDRLimit ); |
| 188 |
if ( IPUtils::isIPv4( $params['ip'] ) ) { |
| 189 |
$type = 'IPv4'; |
| 190 |
$cidrLimit = $blockCIDRLimit['IPv4']; |
| 191 |
} elseif ( IPUtils::isIPv6( $params['ip'] ) ) { |
| 192 |
$type = 'IPv6'; |
| 193 |
$cidrLimit = $blockCIDRLimit['IPv6']; |
| 194 |
} else { |
| 195 |
$this->dieWithError( 'apierror-badip', 'param_ip' ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
[ $ip, $range ] = IPUtils::parseCIDR( $params['ip'] ); |
| 200 |
if ( $ip !== false && $range !== false && $range < $cidrLimit ) { |
| 201 |
$this->dieWithError( [ 'apierror-cidrtoobroad', $type, $cidrLimit ] ); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
[ $lower, $upper ] = IPUtils::parseRange( $params['ip'] ); |
| 206 |
|
| 207 |
$this->addWhere( $this->blockStore->getRangeCond( $lower, $upper ) ); |
| 208 |
$this->addWhere( [ 'bt_auto' => 0 ] ); |
| 209 |
} |
| 210 |
|
| 211 |
if ( $params['show'] !== null ) { |
| 212 |
$show = array_fill_keys( $params['show'], true ); |
| 213 |
|
| 214 |
|
| 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'] ) ) |
| 219 |
) { |
| 220 |
$this->dieWithError( 'apierror-show' ); |
| 221 |
} |
| 222 |
|
| 223 |
$this->addWhereIf( [ 'bt_user' => 0 ], isset( $show['!account'] ) ); |
| 224 |
$this->addWhereIf( $db->expr( 'bt_user', '!=', 0 ), isset( $show['account'] ) ); |
| 225 |
$this->addWhereIf( |
| 226 |
$db->expr( 'bt_user', '!=', 0 )->orExpr( new RawSQLExpression( 'bt_range_end > bt_range_start' ) ), |
| 227 |
isset( $show['!ip'] ) |
| 228 |
); |
| 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'] ) ); |
| 234 |
} |
| 235 |
|
| 236 |
if ( !$this->getAuthority()->isAllowed( 'hideuser' ) ) { |
| 237 |
$this->addWhere( [ 'bl_deleted' => 0 ] ); |
| 238 |
$this->addWhere( |
| 239 |
$this->hideUserUtils->getExpression( $db, 'block_target.bt_user' ) |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
$this->addWhere( $db->expr( 'bl_expiry', '>', $db->timestamp() ) ); |
| 245 |
|
| 246 |
$res = $this->select( __METHOD__ ); |
| 247 |
|
| 248 |
$restrictions = []; |
| 249 |
if ( $fld_restrictions ) { |
| 250 |
$restrictions = $this->getRestrictionData( $res, $params['limit'] ); |
| 251 |
} |
| 252 |
|
| 253 |
$count = 0; |
| 254 |
foreach ( $res as $row ) { |
| 255 |
if ( ++$count > $params['limit'] ) { |
| 256 |
|
| 257 |
$this->setContinueEnumParameter( 'continue', "{$row->bl_timestamp}|{$row->bl_id}" ); |
| 258 |
break; |
| 259 |
} |
| 260 |
$block = [ |
| 261 |
ApiResult::META_TYPE => 'assoc', |
| 262 |
]; |
| 263 |
if ( $fld_id ) { |
| 264 |
$block['id'] = (int)$row->bl_id; |
| 265 |
} |
| 266 |
if ( $fld_user && !$row->bt_auto ) { |
| 267 |
$block['user'] = $row->bt_address_or_user_name; |
| 268 |
} |
| 269 |
if ( $fld_userid && !$row->bt_auto ) { |
| 270 |
$block['userid'] = (int)$row->bt_user; |
| 271 |
} |
| 272 |
if ( $fld_by ) { |
| 273 |
$block['by'] = $row->actor_name; |
| 274 |
} |
| 275 |
if ( $fld_byid ) { |
| 276 |
$block['byid'] = (int)$row->actor_user; |
| 277 |
} |
| 278 |
if ( $fld_timestamp ) { |
| 279 |
$block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->bl_timestamp ); |
| 280 |
} |
| 281 |
if ( $fld_expiry ) { |
| 282 |
$block['expiry'] = ApiResult::formatExpiry( $row->bl_expiry ); |
| 283 |
} |
| 284 |
if ( $fld_reason ) { |
| 285 |
$block['reason'] = $this->commentStore->getComment( 'bl_reason', $row )->text; |
| 286 |
} |
| 287 |
if ( $fld_range && !$row->bt_auto && $row->bt_range_start !== null ) { |
| 288 |
$block['rangestart'] = IPUtils::formatHex( $row->bt_range_start ); |
| 289 |
$block['rangeend'] = IPUtils::formatHex( $row->bt_range_end ); |
| 290 |
} |
| 291 |
if ( $fld_flags ) { |
| 292 |
|
| 293 |
$block['automatic'] = (bool)$row->bt_auto; |
| 294 |
$block['anononly'] = (bool)$row->bl_anon_only; |
| 295 |
$block['nocreate'] = (bool)$row->bl_create_account; |
| 296 |
$block['autoblock'] = (bool)$row->bl_enable_autoblock; |
| 297 |
$block['noemail'] = (bool)$row->bl_block_email; |
| 298 |
$block['hidden'] = (bool)$row->bl_deleted; |
| 299 |
$block['allowusertalk'] = (bool)$row->bl_allow_usertalk; |
| 300 |
$block['partial'] = !(bool)$row->bl_sitewide; |
| 301 |
} |
| 302 |
|
| 303 |
if ( $fld_restrictions ) { |
| 304 |
$block['restrictions'] = []; |
| 305 |
if ( !$row->bl_sitewide && isset( $restrictions[$row->bl_id] ) ) { |
| 306 |
$block['restrictions'] = $restrictions[$row->bl_id]; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $block ); |
| 311 |
if ( !$fit ) { |
| 312 |
$this->setContinueEnumParameter( 'continue', "{$row->bl_timestamp}|{$row->bl_id}" ); |
| 313 |
break; |
| 314 |
} |
| 315 |
} |
| 316 |
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'block' ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
private function getRestrictionData( IResultWrapper $result, $limit ) { |
| 328 |
$partialIds = []; |
| 329 |
$count = 0; |
| 330 |
foreach ( $result as $row ) { |
| 331 |
if ( ++$count <= $limit && !( $row->ipb_sitewide ?? $row->bl_sitewide ) ) { |
| 332 |
$partialIds[] = (int)( $row->ipb_id ?? $row->bl_id ); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
$restrictions = $this->blockRestrictionStore->loadByBlockId( $partialIds ); |
| 337 |
|
| 338 |
$data = []; |
| 339 |
$keys = [ |
| 340 |
'page' => 'pages', |
| 341 |
'ns' => 'namespaces', |
| 342 |
]; |
| 343 |
if ( $this->getConfig()->get( MainConfigNames::EnablePartialActionBlocks ) ) { |
| 344 |
$keys['action'] = 'actions'; |
| 345 |
} |
| 346 |
|
| 347 |
foreach ( $restrictions as $restriction ) { |
| 348 |
$key = $keys[$restriction->getType()]; |
| 349 |
$id = $restriction->getBlockId(); |
| 350 |
switch ( $restriction->getType() ) { |
| 351 |
case 'page': |
| 352 |
|
| 353 |
'@phan-var \MediaWiki\Block\Restriction\PageRestriction $restriction'; |
| 354 |
$value = [ 'id' => $restriction->getValue() ]; |
| 355 |
if ( $restriction->getTitle() ) { |
| 356 |
self::addTitleInfo( $value, $restriction->getTitle() ); |
| 357 |
} |
| 358 |
break; |
| 359 |
case 'action': |
| 360 |
$value = $this->blockActionInfo->getActionFromId( $restriction->getValue() ); |
| 361 |
break; |
| 362 |
default: |
| 363 |
$value = $restriction->getValue(); |
| 364 |
} |
| 365 |
|
| 366 |
if ( !isset( $data[$id][$key] ) ) { |
| 367 |
$data[$id][$key] = []; |
| 368 |
ApiResult::setIndexedTagName( $data[$id][$key], $restriction->getType() ); |
| 369 |
} |
| 370 |
$data[$id][$key][] = $value; |
| 371 |
} |
| 372 |
|
| 373 |
return $data; |
| 374 |
} |
| 375 |
|
| 376 |
public function getAllowedParams() { |
| 377 |
$blockCIDRLimit = $this->getConfig()->get( MainConfigNames::BlockCIDRLimit ); |
| 378 |
|
| 379 |
return [ |
| 380 |
'start' => [ |
| 381 |
ParamValidator::PARAM_TYPE => 'timestamp' |
| 382 |
], |
| 383 |
'end' => [ |
| 384 |
ParamValidator::PARAM_TYPE => 'timestamp', |
| 385 |
], |
| 386 |
'dir' => [ |
| 387 |
ParamValidator::PARAM_TYPE => [ |
| 388 |
'newer', |
| 389 |
'older' |
| 390 |
], |
| 391 |
ParamValidator::PARAM_DEFAULT => 'older', |
| 392 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-direction', |
| 393 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [ |
| 394 |
'newer' => 'api-help-paramvalue-direction-newer', |
| 395 |
'older' => 'api-help-paramvalue-direction-older', |
| 396 |
], |
| 397 |
], |
| 398 |
'ids' => [ |
| 399 |
ParamValidator::PARAM_TYPE => 'integer', |
| 400 |
ParamValidator::PARAM_ISMULTI => true |
| 401 |
], |
| 402 |
'users' => [ |
| 403 |
ParamValidator::PARAM_TYPE => 'user', |
| 404 |
UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'cidr' ], |
| 405 |
ParamValidator::PARAM_ISMULTI => true |
| 406 |
], |
| 407 |
'ip' => [ |
| 408 |
ApiBase::PARAM_HELP_MSG => [ |
| 409 |
'apihelp-query+blocks-param-ip', |
| 410 |
$blockCIDRLimit['IPv4'], |
| 411 |
$blockCIDRLimit['IPv6'], |
| 412 |
], |
| 413 |
], |
| 414 |
'limit' => [ |
| 415 |
ParamValidator::PARAM_DEFAULT => 10, |
| 416 |
ParamValidator::PARAM_TYPE => 'limit', |
| 417 |
IntegerDef::PARAM_MIN => 1, |
| 418 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 419 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 420 |
], |
| 421 |
'prop' => [ |
| 422 |
ParamValidator::PARAM_DEFAULT => 'id|user|by|timestamp|expiry|reason|flags', |
| 423 |
ParamValidator::PARAM_TYPE => [ |
| 424 |
'id', |
| 425 |
'user', |
| 426 |
'userid', |
| 427 |
'by', |
| 428 |
'byid', |
| 429 |
'timestamp', |
| 430 |
'expiry', |
| 431 |
'reason', |
| 432 |
'range', |
| 433 |
'flags', |
| 434 |
'restrictions', |
| 435 |
], |
| 436 |
ParamValidator::PARAM_ISMULTI => true, |
| 437 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 438 |
], |
| 439 |
'show' => [ |
| 440 |
ParamValidator::PARAM_TYPE => [ |
| 441 |
'account', |
| 442 |
'!account', |
| 443 |
'temp', |
| 444 |
'!temp', |
| 445 |
'ip', |
| 446 |
'!ip', |
| 447 |
'range', |
| 448 |
'!range', |
| 449 |
], |
| 450 |
ParamValidator::PARAM_ISMULTI => true |
| 451 |
], |
| 452 |
'continue' => [ |
| 453 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 454 |
], |
| 455 |
]; |
| 456 |
} |
| 457 |
|
| 458 |
protected function getExamplesMessages() { |
| 459 |
return [ |
| 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', |
| 464 |
]; |
| 465 |
} |
| 466 |
|
| 467 |
public function getHelpUrls() { |
| 468 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Blocks'; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
class_alias( ApiQueryBlocks::class, 'ApiQueryBlocks' ); |