Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
31 / 31 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
SpamRegexConstraint | |
100.00% |
31 / 31 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
checkConstraint | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
getLegacyStatus | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
getMatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\EditPage\Constraint; |
22 | |
23 | use MediaWiki\EditPage\SpamChecker; |
24 | use MediaWiki\Title\Title; |
25 | use Psr\Log\LoggerInterface; |
26 | use StatusValue; |
27 | |
28 | /** |
29 | * Verify summary and text do not match spam regexes |
30 | * |
31 | * @since 1.36 |
32 | * @internal |
33 | * @author DannyS712 |
34 | */ |
35 | class SpamRegexConstraint implements IEditConstraint { |
36 | |
37 | /** @var LoggerInterface */ |
38 | private $logger; |
39 | |
40 | /** @var SpamChecker */ |
41 | private $spamChecker; |
42 | |
43 | /** @var string */ |
44 | private $summary; |
45 | |
46 | /** @var ?string */ |
47 | private $sectionHeading; |
48 | |
49 | /** @var string */ |
50 | private $text; |
51 | |
52 | /** @var string */ |
53 | private $reqIP; |
54 | |
55 | /** @var Title */ |
56 | private $title; |
57 | |
58 | /** @var string */ |
59 | private $match = ''; |
60 | |
61 | /** |
62 | * @param LoggerInterface $logger for logging hits |
63 | * @param SpamChecker $spamChecker |
64 | * @param string $summary |
65 | * @param ?string $sectionHeading |
66 | * @param string $text |
67 | * @param string $reqIP for logging hits |
68 | * @param Title $title for logging hits |
69 | */ |
70 | public function __construct( |
71 | LoggerInterface $logger, |
72 | SpamChecker $spamChecker, |
73 | string $summary, |
74 | ?string $sectionHeading, |
75 | string $text, |
76 | string $reqIP, |
77 | Title $title |
78 | ) { |
79 | $this->logger = $logger; |
80 | $this->spamChecker = $spamChecker; |
81 | $this->summary = $summary; |
82 | $this->sectionHeading = $sectionHeading; |
83 | $this->text = $text; |
84 | $this->reqIP = $reqIP; |
85 | $this->title = $title; |
86 | } |
87 | |
88 | public function checkConstraint(): string { |
89 | $match = $this->spamChecker->checkSummary( $this->summary ); |
90 | if ( $match === false && $this->sectionHeading !== null ) { |
91 | // If the section isn't new, the $this->sectionHeading is null |
92 | $match = $this->spamChecker->checkContent( $this->sectionHeading ); |
93 | } |
94 | if ( $match === false ) { |
95 | $match = $this->spamChecker->checkContent( $this->text ); |
96 | } |
97 | |
98 | if ( $match === false ) { |
99 | return self::CONSTRAINT_PASSED; |
100 | } |
101 | |
102 | $this->match = $match; |
103 | $this->logger->debug( |
104 | '{ip} spam regex hit [[{title}]]: "{match}"', |
105 | [ |
106 | 'ip' => $this->reqIP, |
107 | 'title' => $this->title->getPrefixedDBkey(), |
108 | 'match' => str_replace( "\n", '', $match ) |
109 | ] |
110 | ); |
111 | return self::CONSTRAINT_FAILED; |
112 | } |
113 | |
114 | public function getLegacyStatus(): StatusValue { |
115 | $statusValue = StatusValue::newGood(); |
116 | if ( $this->match !== '' ) { |
117 | $match = str_replace( "\n", '', $this->match ); |
118 | $statusValue->fatal( 'spamprotectionmatch', $match ); |
119 | $statusValue->value = self::AS_SPAM_ERROR; |
120 | } |
121 | return $statusValue; |
122 | } |
123 | |
124 | /** |
125 | * @return string |
126 | */ |
127 | public function getMatch(): string { |
128 | return $this->match; |
129 | } |
130 | |
131 | } |