mediawiki-extensions-AbuseF.../includes/Consequence/BlockingConsequence.php
Daimona Eaytoy 904d9cddbb Represent Consequences with command objects
The consequence-taking logic is moved away from AbuseFilterRunner, to
dedicated classes. There's now one class per consequence, encapsulating
everything it needs to take the consequence.

Several interfaces allow customizing different types of consequences.
Every "special check" in AbuseFilter was generalized to use these
interfaces, rather than knowing how to handle each consequence.

Adding more consequences from other extensions will also be easier, and
it should happen via a hook (not a global), returning a class that
implements Consequence. The BCConsequence class was temporarily added
for legacy custom consequences.

A ConsequenceFactory class is added to instantiate consequences; this
would possibly benefit from using ObjectFactory, but it doesn't because
it would also reduce readability (although we might do that in the
future).

These classes are still not covered by unit tests, and this is left to
do for later. The new unit tests should mostly replace
AbuseFilterConsequencesTest. @covers tag were added to keep the status
quo (i.e. code that was considered covered while in AbuseFilterRunner
will still be considered covered), although we'll have to adjust them.

Change-Id: Ia1a9a8bbf55ddd875dfd5bbc55fcd612cff568ef
2020-11-25 17:35:36 +00:00

74 lines
1.8 KiB
PHP

<?php
namespace MediaWiki\Extension\AbuseFilter\Consequence;
use MediaWiki\Block\BlockUserFactory;
use MediaWiki\Extension\AbuseFilter\FilterUser;
use User;
/**
* Base class for consequences that block a user
*/
abstract class BlockingConsequence extends Consequence implements HookAborterConsequence {
/** @var BlockUserFactory */
private $blockUserFactory;
/** @var FilterUser */
private $filterUser;
/** @var string Expiry of the block */
protected $expiry;
/**
* @param Parameters $params
* @param string $expiry
* @param BlockUserFactory $blockUserFactory
* @param FilterUser $filterUser
*/
public function __construct(
Parameters $params,
string $expiry,
BlockUserFactory $blockUserFactory,
FilterUser $filterUser
) {
parent::__construct( $params );
$this->expiry = $expiry;
$this->blockUserFactory = $blockUserFactory;
$this->filterUser = $filterUser;
}
/**
* Perform a block by the AbuseFilter system user
* @param string $ruleDesc
* @param int|string $ruleNumber
* @param string $target
* @param string $expiry
* @param bool $isAutoBlock
* @param bool $preventEditOwnUserTalk
*/
protected function doBlockInternal(
string $ruleDesc,
$ruleNumber,
string $target,
string $expiry,
bool $isAutoBlock,
bool $preventEditOwnUserTalk
) : void {
$reason = wfMessage( 'abusefilter-blockreason', $ruleDesc, $ruleNumber )->inContentLanguage()->text();
$this->blockUserFactory->newBlockUser(
$target,
// TODO: Avoid User here (T266409)
User::newFromIdentity( $this->filterUser->getUser() ),
$expiry,
$reason,
[
'isHardBlock' => false,
'isAutoblocking' => $isAutoBlock,
'isCreateAccountBlocked' => true,
'isUserTalkEditBlocked' => $preventEditOwnUserTalk
]
)->placeBlockUnsafe();
}
}