Debian Codegraph @

includes/editpage/SpamChecker.php

raw symbols SCIP
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 }