Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
SpamChecker | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
checkContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
checkSummary | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
checkInternal | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace MediaWiki\EditPage; |
4 | |
5 | /** |
6 | * Service to check if text (either content or a summary) qualifies as spam |
7 | * |
8 | * Text qualifies as spam if it matches the global $wgSpamRegex |
9 | * Summaries qualify as spam if they match the global $wgSummarySpamRegex |
10 | * |
11 | * @author DannyS712 |
12 | * @since 1.35 |
13 | */ |
14 | class SpamChecker { |
15 | |
16 | /** @var string[] */ |
17 | private $spamRegex; |
18 | |
19 | /** @var string[] */ |
20 | private $summaryRegex; |
21 | |
22 | /** |
23 | * @param string[] $spamRegex |
24 | * @param string[] $summaryRegex |
25 | */ |
26 | public function __construct( $spamRegex, $summaryRegex ) { |
27 | $this->spamRegex = $spamRegex; |
28 | $this->summaryRegex = $summaryRegex; |
29 | } |
30 | |
31 | /** |
32 | * Check whether content text is considered spam |
33 | * |
34 | * @param string $text |
35 | * @return string|false Matching string or false |
36 | */ |
37 | public function checkContent( string $text ) { |
38 | return self::checkInternal( $text, $this->spamRegex ); |
39 | } |
40 | |
41 | /** |
42 | * Check whether summary text is considered spam |
43 | * |
44 | * @param string $summary |
45 | * @return string|false Matching string or false |
46 | */ |
47 | public function checkSummary( string $summary ) { |
48 | return self::checkInternal( $summary, $this->summaryRegex ); |
49 | } |
50 | |
51 | /** |
52 | * @param string $text |
53 | * @param array $regexes |
54 | * @return string|false |
55 | */ |
56 | private static function checkInternal( string $text, array $regexes ) { |
57 | foreach ( $regexes as $regex ) { |
58 | $matches = []; |
59 | if ( preg_match( $regex, $text, $matches ) ) { |
60 | return $matches[0]; |
61 | } |
62 | } |
63 | return false; |
64 | } |
65 | } |