| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Consequences\Consequence; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequenceNotPrecheckedException; |
| 6 |
use MediaWiki\Extension\AbuseFilter\Consequences\Parameters; |
| 7 |
use MediaWiki\Extension\AbuseFilter\GlobalNameUtils; |
| 8 |
use MediaWiki\Session\Session; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class Warn extends Consequence implements HookAborterConsequence, ConsequencesDisablerConsequence { |
| 14 |
|
| 15 |
private $session; |
| 16 |
|
| 17 |
private $message; |
| 18 |
|
| 19 |
private $shouldWarn; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( Parameters $parameters, string $message, Session $session ) { |
| 27 |
parent::__construct( $parameters ); |
| 28 |
$this->message = $message; |
| 29 |
$this->session = $session; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function execute(): bool { |
| 37 |
if ( $this->shouldWarn === null ) { |
| 38 |
throw new ConsequenceNotPrecheckedException(); |
| 39 |
} |
| 40 |
$this->setWarn(); |
| 41 |
return $this->shouldWarn; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function shouldDisableOtherConsequences(): bool { |
| 48 |
$this->shouldWarn = $this->shouldBeWarned(); |
| 49 |
return $this->shouldWarn; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function getSort(): int { |
| 56 |
return 5; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function getMessage(): array { |
| 63 |
$filter = $this->parameters->getFilter(); |
| 64 |
return [ |
| 65 |
$this->message, |
| 66 |
$filter->getName(), |
| 67 |
GlobalNameUtils::buildGlobalName( $filter->getID(), $this->parameters->getIsGlobalFilter() ) |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private function shouldBeWarned(): bool { |
| 75 |
|
| 76 |
$this->session->persist(); |
| 77 |
$warnKey = $this->getWarnKey(); |
| 78 |
return ( !isset( $this->session[$warnKey] ) || !$this->session[$warnKey] ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private function setWarn(): void { |
| 85 |
$warnKey = $this->getWarnKey(); |
| 86 |
$this->session[$warnKey] = $this->shouldWarn; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private function getWarnKey(): string { |
| 95 |
$globalFilterName = GlobalNameUtils::buildGlobalName( |
| 96 |
$this->parameters->getFilter()->getID(), |
| 97 |
$this->parameters->getIsGlobalFilter() |
| 98 |
); |
| 99 |
$target = $this->parameters->getTarget(); |
| 100 |
$titleText = $target->getNamespace() . ':' . $target->getText(); |
| 101 |
return 'abusefilter-warned-' . md5( $titleText ) . |
| 102 |
'-' . $globalFilterName . '-' . $this->parameters->getAction(); |
| 103 |
} |
| 104 |
} |