Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Block | |
100.00% |
27 / 27 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| revert | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| getMessage | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getExpiry | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\AbuseFilter\Consequences\Consequence; |
| 4 | |
| 5 | use MediaWiki\Block\BlockUserFactory; |
| 6 | use MediaWiki\Block\DatabaseBlockStore; |
| 7 | use MediaWiki\Block\UnblockUserFactory; |
| 8 | use MediaWiki\Extension\AbuseFilter\Consequences\Parameters; |
| 9 | use MediaWiki\Extension\AbuseFilter\FilterUser; |
| 10 | use MediaWiki\Extension\AbuseFilter\GlobalNameUtils; |
| 11 | use MediaWiki\Language\MessageLocalizer; |
| 12 | use MediaWiki\Permissions\Authority; |
| 13 | use Psr\Log\LoggerInterface; |
| 14 | |
| 15 | /** |
| 16 | * Consequence that blocks a single user. |
| 17 | */ |
| 18 | class Block extends BlockingConsequence implements ReversibleConsequence { |
| 19 | |
| 20 | public function __construct( |
| 21 | Parameters $params, |
| 22 | string $expiry, |
| 23 | private readonly bool $preventTalkEdit, |
| 24 | BlockUserFactory $blockUserFactory, |
| 25 | private readonly UnblockUserFactory $unblockUserFactory, |
| 26 | private readonly DatabaseBlockStore $databaseBlockStore, |
| 27 | FilterUser $filterUser, |
| 28 | MessageLocalizer $messageLocalizer, |
| 29 | LoggerInterface $logger |
| 30 | ) { |
| 31 | parent::__construct( $params, $expiry, $blockUserFactory, $filterUser, $messageLocalizer, $logger ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public function execute(): bool { |
| 38 | $status = $this->doBlockInternal( |
| 39 | $this->parameters->getFilter()->getName(), |
| 40 | $this->parameters->getFilter()->getID(), |
| 41 | $this->parameters->getUser()->getName(), |
| 42 | $this->expiry, |
| 43 | true, |
| 44 | $this->preventTalkEdit |
| 45 | ); |
| 46 | // TODO: Should we reblock in case of partial blocks? At that point we could return |
| 47 | // the status of doBlockInternal |
| 48 | return defined( 'MW_PHPUNIT_TEST' ) ? $status->isOK() : true; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @inheritDoc |
| 53 | */ |
| 54 | public function revert( Authority $performer, string $reason ): bool { |
| 55 | $blocks = $this->databaseBlockStore->newListFromTarget( |
| 56 | $this->parameters->getUser()->getName(), null, false, DatabaseBlockStore::AUTO_NONE ); |
| 57 | foreach ( $blocks as $block ) { |
| 58 | if ( $block->getBy() === $this->filterUser->getUserIdentity()->getId() ) { |
| 59 | return $this->unblockUserFactory->newRemoveBlock( |
| 60 | $block, |
| 61 | $performer, |
| 62 | $reason, |
| 63 | )->unblockUnsafe()->isOK(); |
| 64 | } |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @inheritDoc |
| 71 | */ |
| 72 | public function getMessage(): array { |
| 73 | $filter = $this->parameters->getFilter(); |
| 74 | return [ |
| 75 | 'abusefilter-blocked-display', |
| 76 | $filter->getName(), |
| 77 | GlobalNameUtils::buildGlobalName( $filter->getID(), $this->parameters->getIsGlobalFilter() ) |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return string |
| 83 | * @internal |
| 84 | */ |
| 85 | public function getExpiry(): string { |
| 86 | return $this->expiry; |
| 87 | } |
| 88 | } |