29 use Wikimedia\IPUtils;
59 parent::__construct( $query, $moduleName,
'bk' );
60 $this->blockActionInfo = $blockActionInfo;
61 $this->blockRestrictionStore = $blockRestrictionStore;
62 $this->commentStore = $commentStore;
70 $prop = array_fill_keys( $params[
'prop'],
true );
71 $fld_id = isset( $prop[
'id'] );
72 $fld_user = isset( $prop[
'user'] );
73 $fld_userid = isset( $prop[
'userid'] );
74 $fld_by = isset( $prop[
'by'] );
75 $fld_byid = isset( $prop[
'byid'] );
76 $fld_timestamp = isset( $prop[
'timestamp'] );
77 $fld_expiry = isset( $prop[
'expiry'] );
78 $fld_reason = isset( $prop[
'reason'] );
79 $fld_range = isset( $prop[
'range'] );
80 $fld_flags = isset( $prop[
'flags'] );
81 $fld_restrictions = isset( $prop[
'restrictions'] );
86 $this->
addFields( [
'ipb_auto',
'ipb_id',
'ipb_timestamp' ] );
88 $this->
addFieldsIf( [
'ipb_address',
'ipb_user' ], $fld_user || $fld_userid );
89 if ( $fld_by || $fld_byid ) {
91 $this->
addFields( [
'actor_user',
'actor_name' ] );
92 $this->
addJoinConds( [
'actor' => [
'JOIN',
'actor_id=ipb_by_actor' ] ] );
95 $this->
addFieldsIf( [
'ipb_range_start',
'ipb_range_end' ], $fld_range );
96 $this->
addFieldsIf( [
'ipb_anon_only',
'ipb_create_account',
'ipb_enable_autoblock',
97 'ipb_block_email',
'ipb_deleted',
'ipb_allow_usertalk',
'ipb_sitewide' ],
99 $this->
addFieldsIf(
'ipb_sitewide', $fld_restrictions );
102 $commentQuery = $this->commentStore->getJoin(
'ipb_reason' );
103 $this->
addTables( $commentQuery[
'tables'] );
104 $this->
addFields( $commentQuery[
'fields'] );
108 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
116 $this->
addWhereRange(
'ipb_id', $params[
'dir'],
null,
null );
118 if ( $params[
'continue'] !==
null ) {
120 $op = ( $params[
'dir'] ==
'newer' ?
'>=' :
'<=' );
121 $this->
addWhere( $db->buildComparison( $op, [
122 'ipb_timestamp' => $db->timestamp( $cont[0] ),
123 'ipb_id' => $cont[1],
127 if ( $params[
'ids'] ) {
130 if ( $params[
'users'] ) {
131 $this->
addWhereFld(
'ipb_address', $params[
'users'] );
134 if ( $params[
'ip'] !==
null ) {
135 $blockCIDRLimit = $this->
getConfig()->get( MainConfigNames::BlockCIDRLimit );
136 if ( IPUtils::isIPv4( $params[
'ip'] ) ) {
138 $cidrLimit = $blockCIDRLimit[
'IPv4'];
140 } elseif ( IPUtils::isIPv6( $params[
'ip'] ) ) {
142 $cidrLimit = $blockCIDRLimit[
'IPv6'];
149 [ $ip, $range ] = IPUtils::parseCIDR( $params[
'ip'] );
150 if ( $ip !==
false && $range !==
false && $range < $cidrLimit ) {
151 $this->
dieWithError( [
'apierror-cidrtoobroad', $type, $cidrLimit ] );
155 [ $lower, $upper ] = IPUtils::parseRange( $params[
'ip'] );
158 $prefix = substr( $lower, 0, $prefixLen + (
int)floor( $cidrLimit / 4 ) );
160 $lower = $db->addQuotes( $lower );
161 $upper = $db->addQuotes( $upper );
164 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
165 'ipb_range_start <= ' . $lower,
166 'ipb_range_end >= ' . $upper,
171 if ( $params[
'show'] !==
null ) {
172 $show = array_fill_keys( $params[
'show'],
true );
175 if ( ( isset( $show[
'account'] ) && isset( $show[
'!account'] ) )
176 || ( isset( $show[
'ip'] ) && isset( $show[
'!ip'] ) )
177 || ( isset( $show[
'range'] ) && isset( $show[
'!range'] ) )
178 || ( isset( $show[
'temp'] ) && isset( $show[
'!temp'] ) )
183 $this->
addWhereIf( [
'ipb_user' => 0 ], isset( $show[
'!account'] ) );
184 $this->
addWhereIf(
'ipb_user != 0', isset( $show[
'account'] ) );
185 $this->
addWhereIf(
'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show[
'!ip'] ) );
186 $this->
addWhereIf(
'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show[
'ip'] ) );
187 $this->
addWhereIf( [
'ipb_expiry' => $db->getInfinity() ], isset( $show[
'!temp'] ) );
189 $db->addQuotes( $db->getInfinity() ), isset( $show[
'temp'] ) );
190 $this->
addWhereIf(
'ipb_range_end = ipb_range_start', isset( $show[
'!range'] ) );
191 $this->
addWhereIf(
'ipb_range_end > ipb_range_start', isset( $show[
'range'] ) );
194 if ( !$this->
getAuthority()->isAllowed(
'hideuser' ) ) {
199 $this->
addWhere(
'ipb_expiry > ' . $db->addQuotes( $db->timestamp() ) );
201 $res = $this->
select( __METHOD__ );
204 if ( $fld_restrictions ) {
205 $restrictions = $this->getRestrictionData( $res, $params[
'limit'] );
209 foreach ( $res as $row ) {
210 if ( ++$count > $params[
'limit'] ) {
219 $block[
'id'] = (int)$row->ipb_id;
221 if ( $fld_user && !$row->ipb_auto ) {
222 $block[
'user'] = $row->ipb_address;
224 if ( $fld_userid && !$row->ipb_auto ) {
225 $block[
'userid'] = (int)$row->ipb_user;
228 $block[
'by'] = $row->actor_name;
231 $block[
'byid'] = (int)$row->actor_user;
233 if ( $fld_timestamp ) {
234 $block[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
240 $block[
'reason'] = $this->commentStore->getComment(
'ipb_reason', $row )->text;
242 if ( $fld_range && !$row->ipb_auto ) {
243 $block[
'rangestart'] = IPUtils::formatHex( $row->ipb_range_start );
244 $block[
'rangeend'] = IPUtils::formatHex( $row->ipb_range_end );
248 $block[
'automatic'] = (bool)$row->ipb_auto;
249 $block[
'anononly'] = (
bool)$row->ipb_anon_only;
250 $block[
'nocreate'] = (bool)$row->ipb_create_account;
251 $block[
'autoblock'] = (
bool)$row->ipb_enable_autoblock;
252 $block[
'noemail'] = (bool)$row->ipb_block_email;
253 $block[
'hidden'] = (
bool)$row->ipb_deleted;
254 $block[
'allowusertalk'] = (bool)$row->ipb_allow_usertalk;
255 $block[
'partial'] = !(
bool)$row->ipb_sitewide;
258 if ( $fld_restrictions ) {
259 $block[
'restrictions'] = [];
260 if ( !$row->ipb_sitewide && isset( $restrictions[$row->ipb_id] ) ) {
261 $block[
'restrictions'] = $restrictions[$row->ipb_id];
265 $fit = $result->addValue( [
'query', $this->
getModuleName() ],
null, $block );
271 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'block' );
282 private function getRestrictionData(
IResultWrapper $result, $limit ) {
285 foreach ( $result as $row ) {
286 if ( ++$count <= $limit && !$row->ipb_sitewide ) {
287 $partialIds[] = (int)$row->ipb_id;
291 $restrictions = $this->blockRestrictionStore->loadByBlockId( $partialIds );
296 'ns' =>
'namespaces',
298 if ( $this->
getConfig()->get( MainConfigNames::EnablePartialActionBlocks ) ) {
299 $keys[
'action'] =
'actions';
302 foreach ( $restrictions as $restriction ) {
303 $key = $keys[$restriction->getType()];
304 $id = $restriction->getBlockId();
305 switch ( $restriction->getType() ) {
308 '@phan-var \MediaWiki\Block\Restriction\PageRestriction $restriction';
309 $value = [
'id' => $restriction->getValue() ];
310 if ( $restriction->getTitle() ) {
315 $value = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
318 $value = $restriction->getValue();
321 if ( !isset( $data[$id][$key] ) ) {
322 $data[$id][$key] = [];
325 $data[$id][$key][] = $value;
332 $blockCIDRLimit = $this->
getConfig()->get( MainConfigNames::BlockCIDRLimit );
336 ParamValidator::PARAM_TYPE =>
'timestamp'
339 ParamValidator::PARAM_TYPE =>
'timestamp',
342 ParamValidator::PARAM_TYPE => [
346 ParamValidator::PARAM_DEFAULT =>
'older',
349 'newer' =>
'api-help-paramvalue-direction-newer',
350 'older' =>
'api-help-paramvalue-direction-older',
354 ParamValidator::PARAM_TYPE =>
'integer',
355 ParamValidator::PARAM_ISMULTI =>
true
358 ParamValidator::PARAM_TYPE =>
'user',
359 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'cidr' ],
360 ParamValidator::PARAM_ISMULTI =>
true
364 'apihelp-query+blocks-param-ip',
365 $blockCIDRLimit[
'IPv4'],
366 $blockCIDRLimit[
'IPv6'],
370 ParamValidator::PARAM_DEFAULT => 10,
371 ParamValidator::PARAM_TYPE =>
'limit',
372 IntegerDef::PARAM_MIN => 1,
377 ParamValidator::PARAM_DEFAULT =>
'id|user|by|timestamp|expiry|reason|flags',
378 ParamValidator::PARAM_TYPE => [
391 ParamValidator::PARAM_ISMULTI =>
true,
395 ParamValidator::PARAM_TYPE => [
405 ParamValidator::PARAM_ISMULTI =>
true
415 'action=query&list=blocks'
416 =>
'apihelp-query+blocks-example-simple',
417 'action=query&list=blocks&bkusers=Alice|Bob'
418 =>
'apihelp-query+blocks-example-users',
423 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.
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, BlockActionInfo $blockActionInfo, BlockRestrictionStore $blockRestrictionStore, CommentStore $commentStore)
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main query class.
const META_TYPE
Key for the 'type' metadata item.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
A class containing constants representing the names of configuration variables.