aboutsummaryrefslogtreecommitdiffstats
path: root/includes/editpage/SpamChecker.php
blob: 7214dfa46b3200c6d4c964e91aa1ba6974ef2e0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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 string|false 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 string|false Matching string or false
	 */
	public function checkSummary( string $summary ) {
		return self::checkInternal( $summary, $this->summaryRegex );
	}

	/**
	 * @param string $text
	 * @param array $regexes
	 * @return string|false
	 */
	private static function checkInternal( string $text, array $regexes ) {
		foreach ( $regexes as $regex ) {
			$matches = [];
			if ( preg_match( $regex, $text, $matches ) ) {
				return $matches[0];
			}
		}
		return false;
	}
}