| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\EditPage\Constraint; |
| 22 |
|
| 23 |
use MediaWiki\EditPage\SpamChecker; |
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use Psr\Log\LoggerInterface; |
| 26 |
use StatusValue; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class SpamRegexConstraint implements IEditConstraint { |
| 36 |
|
| 37 |
private LoggerInterface $logger; |
| 38 |
private SpamChecker $spamChecker; |
| 39 |
private string $summary; |
| 40 |
private ?string $sectionHeading; |
| 41 |
private string $text; |
| 42 |
private string $reqIP; |
| 43 |
private Title $title; |
| 44 |
private string $match = ''; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( |
| 56 |
LoggerInterface $logger, |
| 57 |
SpamChecker $spamChecker, |
| 58 |
string $summary, |
| 59 |
?string $sectionHeading, |
| 60 |
string $text, |
| 61 |
string $reqIP, |
| 62 |
Title $title |
| 63 |
) { |
| 64 |
$this->logger = $logger; |
| 65 |
$this->spamChecker = $spamChecker; |
| 66 |
$this->summary = $summary; |
| 67 |
$this->sectionHeading = $sectionHeading; |
| 68 |
$this->text = $text; |
| 69 |
$this->reqIP = $reqIP; |
| 70 |
$this->title = $title; |
| 71 |
} |
| 72 |
|
| 73 |
public function checkConstraint(): string { |
| 74 |
$match = $this->spamChecker->checkSummary( $this->summary ); |
| 75 |
if ( $match === false && $this->sectionHeading !== null ) { |
| 76 |
|
| 77 |
$match = $this->spamChecker->checkContent( $this->sectionHeading ); |
| 78 |
} |
| 79 |
if ( $match === false ) { |
| 80 |
$match = $this->spamChecker->checkContent( $this->text ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( $match === false ) { |
| 84 |
return self::CONSTRAINT_PASSED; |
| 85 |
} |
| 86 |
|
| 87 |
$this->match = $match; |
| 88 |
$this->logger->debug( |
| 89 |
'{ip} spam regex hit [[{title}]]: "{match}"', |
| 90 |
[ |
| 91 |
'ip' => $this->reqIP, |
| 92 |
'title' => $this->title->getPrefixedDBkey(), |
| 93 |
'match' => str_replace( "\n", '', $match ) |
| 94 |
] |
| 95 |
); |
| 96 |
return self::CONSTRAINT_FAILED; |
| 97 |
} |
| 98 |
|
| 99 |
public function getLegacyStatus(): StatusValue { |
| 100 |
$statusValue = StatusValue::newGood(); |
| 101 |
if ( $this->match !== '' ) { |
| 102 |
$match = str_replace( "\n", '', $this->match ); |
| 103 |
$statusValue->fatal( 'spamprotectionmatch', $match ); |
| 104 |
$statusValue->value = self::AS_SPAM_ERROR; |
| 105 |
} |
| 106 |
return $statusValue; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function getMatch(): string { |
| 113 |
return $this->match; |
| 114 |
} |
| 115 |
|
| 116 |
} |