68 parent::__construct( $query, $moduleName,
'bk' );
69 $this->blockStore = $blockStore;
70 $this->blockActionInfo = $blockActionInfo;
71 $this->blockRestrictionStore = $blockRestrictionStore;
72 $this->commentStore = $commentStore;
73 $this->hideUserUtils = $hideUserUtils;
81 $prop = array_fill_keys(
$params[
'prop'],
true );
82 $fld_id = isset( $prop[
'id'] );
83 $fld_user = isset( $prop[
'user'] );
84 $fld_userid = isset( $prop[
'userid'] );
85 $fld_by = isset( $prop[
'by'] );
86 $fld_byid = isset( $prop[
'byid'] );
87 $fld_timestamp = isset( $prop[
'timestamp'] );
88 $fld_expiry = isset( $prop[
'expiry'] );
89 $fld_reason = isset( $prop[
'reason'] );
90 $fld_range = isset( $prop[
'range'] );
91 $fld_flags = isset( $prop[
'flags'] );
92 $fld_restrictions = isset( $prop[
'restrictions'] );
96 $this->
addTables( [
'block',
'block_target',
'block_target_user' =>
'user' ] );
98 'block_target' => [
'JOIN',
'bt_id=bl_target' ],
99 'block_target_user' => [
'LEFT JOIN',
'user_id=bt_user' ]
101 $this->
addFields( [
'bt_auto',
'bl_id',
'bl_timestamp' ] );
106 'bt_address_or_user_name' =>
'COALESCE(bt_address, bt_user_text)'
108 $fld_user || $fld_userid
111 if ( $fld_by || $fld_byid ) {
113 $this->
addFields( [
'actor_user',
'actor_name' ] );
114 $this->
addJoinConds( [
'actor' => [
'JOIN',
'actor_id=bl_by_actor' ] ] );
117 $this->
addFieldsIf( [
'bt_range_start',
'bt_range_end' ], $fld_range );
118 $this->
addFieldsIf( [
'bl_anon_only',
'bl_create_account',
'bl_enable_autoblock',
119 'bl_block_email',
'bl_deleted',
'bl_allow_usertalk',
'bl_sitewide' ],
121 $this->
addFieldsIf(
'bl_sitewide', $fld_restrictions );
124 $commentQuery = $this->commentStore->getJoin(
'bl_reason' );
125 $this->
addTables( $commentQuery[
'tables'] );
126 $this->
addFields( $commentQuery[
'fields'] );
140 if (
$params[
'continue'] !==
null ) {
142 $op = (
$params[
'dir'] ==
'newer' ?
'>=' :
'<=' );
143 $this->
addWhere( $db->buildComparison( $op, [
144 'bl_timestamp' => $db->timestamp( $cont[0] ),
155 foreach (
$params[
'users'] as $target ) {
156 if ( IPUtils::isValid( $target ) || IPUtils::isValidRange( $target ) ) {
157 $addresses[] = $target;
159 $userNames[] = $target;
162 if ( $addresses && $userNames ) {
164 $ids = $db->newUnionQueryBuilder()
165 ->add( $db->newSelectQueryBuilder()
167 ->from(
'block_target' )
168 ->where( [
'bt_address' => $addresses ] )
170 ->add( $db->newSelectQueryBuilder()
172 ->from(
'block_target' )
173 ->join(
'user',
null,
'user_id=bt_user' )
174 ->where( [
'user_name' => $userNames ] )
176 ->caller( __METHOD__ )
177 ->fetchFieldValues();
179 $this->
addWhere( [
'bt_id' => $ids ] );
183 } elseif ( $addresses ) {
184 $this->
addWhere( [
'bt_address' => $addresses ] );
185 } elseif ( $userNames ) {
186 $this->
addWhere( [
'block_target_user.user_name' => $userNames ] );
193 if (
$params[
'ip'] !==
null ) {
194 $blockCIDRLimit = $this->
getConfig()->get( MainConfigNames::BlockCIDRLimit );
195 if ( IPUtils::isIPv4(
$params[
'ip'] ) ) {
197 $cidrLimit = $blockCIDRLimit[
'IPv4'];
198 } elseif ( IPUtils::isIPv6(
$params[
'ip'] ) ) {
200 $cidrLimit = $blockCIDRLimit[
'IPv6'];
206 [ $ip, $range ] = IPUtils::parseCIDR(
$params[
'ip'] );
207 if ( $ip !==
false && $range !==
false && $range < $cidrLimit ) {
208 $this->
dieWithError( [
'apierror-cidrtoobroad', $type, $cidrLimit ] );
212 [ $lower, $upper ] = IPUtils::parseRange(
$params[
'ip'] );
214 $this->
addWhere( $this->blockStore->getRangeCond( $lower, $upper ) );
215 $this->
addWhere( [
'bt_auto' => 0 ] );
218 if (
$params[
'show'] !==
null ) {
219 $show = array_fill_keys(
$params[
'show'],
true );
222 if ( ( isset( $show[
'account'] ) && isset( $show[
'!account'] ) )
223 || ( isset( $show[
'ip'] ) && isset( $show[
'!ip'] ) )
224 || ( isset( $show[
'range'] ) && isset( $show[
'!range'] ) )
225 || ( isset( $show[
'temp'] ) && isset( $show[
'!temp'] ) )
230 $this->
addWhereIf( [
'bt_user' => 0 ], isset( $show[
'!account'] ) );
231 $this->
addWhereIf( $db->expr(
'bt_user',
'!=', 0 ), isset( $show[
'account'] ) );
233 $db->expr(
'bt_user',
'!=', 0 )->orExpr(
new RawSQLExpression(
'bt_range_end > bt_range_start' ) ),
234 isset( $show[
'!ip'] )
236 $this->
addWhereIf( [
'bt_user' => 0,
'bt_range_end = bt_range_start' ], isset( $show[
'ip'] ) );
237 $this->
addWhereIf( [
'bl_expiry' => $db->getInfinity() ], isset( $show[
'!temp'] ) );
238 $this->
addWhereIf( $db->expr(
'bl_expiry',
'!=', $db->getInfinity() ), isset( $show[
'temp'] ) );
239 $this->
addWhereIf(
'bt_range_end = bt_range_start', isset( $show[
'!range'] ) );
240 $this->
addWhereIf(
'bt_range_end > bt_range_start', isset( $show[
'range'] ) );
243 if ( !$this->
getAuthority()->isAllowed(
'hideuser' ) ) {
245 $this->hideUserUtils->getExpression( $db, $db->tableName(
'block_target' ) .
'.bt_user' )
250 $this->
addWhere( $db->expr(
'bl_expiry',
'>', $db->timestamp() ) );
252 $res = $this->
select( __METHOD__ );
255 if ( $fld_restrictions ) {
256 $restrictions = $this->getRestrictionData( $res,
$params[
'limit'] );
260 foreach ( $res as $row ) {
261 if ( ++$count >
$params[
'limit'] ) {
267 ApiResult::META_TYPE =>
'assoc',
270 $block[
'id'] = (int)$row->bl_id;
272 if ( $fld_user && !$row->bt_auto ) {
273 $block[
'user'] = $row->bt_address_or_user_name;
275 if ( $fld_userid && !$row->bt_auto ) {
276 $block[
'userid'] = (int)$row->bt_user;
279 $block[
'by'] = $row->actor_name;
282 $block[
'byid'] = (int)$row->actor_user;
284 if ( $fld_timestamp ) {
285 $block[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $row->bl_timestamp );
288 $block[
'expiry'] = ApiResult::formatExpiry( $row->bl_expiry );
291 $block[
'reason'] = $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',
349 if ( $this->
getConfig()->get( MainConfigNames::EnablePartialActionBlocks ) ) {
350 $keys[
'action'] =
'actions';
353 foreach ( $restrictions as $restriction ) {
354 $key = $keys[$restriction->getType()];
355 $id = $restriction->getBlockId();
356 switch ( $restriction->getType() ) {
359 '@phan-var \MediaWiki\Block\Restriction\PageRestriction $restriction';
360 $value = [
'id' => $restriction->getValue() ];
361 if ( $restriction->getTitle() ) {
366 $value = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
369 $value = $restriction->getValue();
372 if ( !isset( $data[$id][$key] ) ) {
373 $data[$id][$key] = [];
376 $data[$id][$key][] = $value;
383 $blockCIDRLimit = $this->
getConfig()->get( MainConfigNames::BlockCIDRLimit );
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 => [
442 ParamValidator::PARAM_ISMULTI =>
true,
446 ParamValidator::PARAM_TYPE => [
456 ParamValidator::PARAM_ISMULTI => true
466 'action=query&list=blocks'
467 =>
'apihelp-query+blocks-example-simple',
468 'action=query&list=blocks&bkusers=Alice|Bob'
469 =>
'apihelp-query+blocks-example-users',
474 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Blocks';
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
const LIMIT_BIG1
Fast query, standard limit.
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
const LIMIT_BIG2
Fast query, apihighlimits limit.
getModuleName()
Get the name of the module being executed by this instance.
This is a base class for all Query modules.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addWhereIDsFld( $table, $field, $ids)
Like addWhereFld for an integer list of IDs.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Query module to enumerate all user blocks.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiQuery $query, $moduleName, DatabaseBlockStore $blockStore, BlockActionInfo $blockActionInfo, BlockRestrictionStore $blockRestrictionStore, CommentStore $commentStore, HideUserUtils $hideUserUtils)
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main query class.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
A class containing constants representing the names of configuration variables.