wiki.techinc.nl/includes/editpage/SpamChecker.php
DannyS712 96b8be5750 Add a new SpamChecker service
For checks against `$wgSpamRegex` and `$wgSummarySpamRegex`.
EditPage methods will be deprecated in a separate follow-up patch,
as well as making use of the service where needed.

Bug: T251015
Change-Id: I12120b51073c31680713204cfeb5c61c0ca8c52c
2020-05-11 14:52:28 +00:00

65 lines
1.4 KiB
PHP

<?php
namespace MediaWiki\EditPage;
/**
* Service to check if text (either content or a summary) qualifies as spam
*
* Text qualifies as spam if it matches the global $wgSpamRegex
* Summaries qualify as spam if they match the global $wgSummarySpamRegex
*
* @author DannyS712
* @since 1.35
*/
class SpamChecker {
/** @var string[] */
private $spamRegex;
/** @var string[] */
private $summaryRegex;
/**
* @param string[] $spamRegex
* @param string[] $summaryRegex
*/
public function __construct( $spamRegex, $summaryRegex ) {
$this->spamRegex = $spamRegex;
$this->summaryRegex = $summaryRegex;
}
/**
* Check whether content text is considered spam
*
* @param string $text
* @return bool|string Matching string or false
*/
public function checkContent( string $text ) {
return self::checkInternal( $text, $this->spamRegex );
}
/**
* Check whether summary text is considered spam
*
* @param string $summary
* @return bool|string Matching string or false
*/
public function checkSummary( string $summary ) {
return self::checkInternal( $summary, $this->summaryRegex );
}
/**
* @param string $text
* @param array $regexes
* @return bool|string
*/
private static function checkInternal( string $text, array $regexes ) {
foreach ( $regexes as $regex ) {
$matches = [];
if ( preg_match( $regex, $text, $matches ) ) {
return $matches[0];
}
}
return false;
}
}