Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SpamRegex | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| validate | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| enabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\SpamFilter; |
| 4 | |
| 5 | use Flow\Model\AbstractRevision; |
| 6 | use MediaWiki\Context\IContextSource; |
| 7 | use MediaWiki\Status\Status; |
| 8 | use MediaWiki\Title\Title; |
| 9 | |
| 10 | class SpamRegex implements SpamFilter { |
| 11 | /** |
| 12 | * @param IContextSource $context |
| 13 | * @param AbstractRevision $newRevision |
| 14 | * @param AbstractRevision|null $oldRevision |
| 15 | * @param Title $title |
| 16 | * @param Title $ownerTitle |
| 17 | * @return Status |
| 18 | */ |
| 19 | public function validate( |
| 20 | IContextSource $context, |
| 21 | AbstractRevision $newRevision, |
| 22 | ?AbstractRevision $oldRevision, |
| 23 | Title $title, |
| 24 | Title $ownerTitle |
| 25 | ) { |
| 26 | global $wgSpamRegex; |
| 27 | |
| 28 | /* |
| 29 | * This should not cause an extra conversion; SpamRegex checks will be |
| 30 | * performed upon submitting new content. Content is always either |
| 31 | * submitted in (topic-title-)wikitext, or submitted in HTML, but |
| 32 | * immediately converted to wikitext and then treated as such. It will only |
| 33 | * be transformed once it's being saved to DB. |
| 34 | */ |
| 35 | $text = $newRevision->getContentInWikitext(); |
| 36 | |
| 37 | // back compat, $wgSpamRegex may be a single string or an array of regexes |
| 38 | $regexes = (array)$wgSpamRegex; |
| 39 | |
| 40 | foreach ( $regexes as $regex ) { |
| 41 | if ( preg_match( $regex, $text, $matches ) ) { |
| 42 | return Status::newFatal( 'spamprotectionmatch', $matches[0] ); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return Status::newGood(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Checks if SpamRegex is enabled. |
| 51 | * |
| 52 | * @return bool |
| 53 | */ |
| 54 | public function enabled() { |
| 55 | global $wgSpamRegex; |
| 56 | return (bool)$wgSpamRegex; |
| 57 | } |
| 58 | } |