Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SpamRegexConstraint
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 checkConstraint
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
 getLegacyStatus
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getMatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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
21namespace MediaWiki\EditPage\Constraint;
22
23use MediaWiki\EditPage\SpamChecker;
24use MediaWiki\Title\Title;
25use Psr\Log\LoggerInterface;
26use StatusValue;
27
28/**
29 * Verify summary and text do not match spam regexes
30 *
31 * @since 1.36
32 * @internal
33 * @author DannyS712
34 */
35class SpamRegexConstraint implements IEditConstraint {
36
37    private LoggerInterface $logger;
38    private SpamChecker $spamChecker;
39    private string $summary;
40    private ?string $sectionHeading;
41    private string $text;
42    private string $reqIP;
43    private Title $title;
44    private string $match = '';
45
46    /**
47     * @param LoggerInterface $logger for logging hits
48     * @param SpamChecker $spamChecker
49     * @param string $summary
50     * @param ?string $sectionHeading
51     * @param string $text
52     * @param string $reqIP for logging hits
53     * @param Title $title for logging hits
54     */
55    public function __construct(
56        LoggerInterface $logger,
57        SpamChecker $spamChecker,
58        string $summary,
59        ?string $sectionHeading,
60        string $text,
61        string $reqIP,
62        Title $title
63    ) {
64        $this->logger = $logger;
65        $this->spamChecker = $spamChecker;
66        $this->summary = $summary;
67        $this->sectionHeading = $sectionHeading;
68        $this->text = $text;
69        $this->reqIP = $reqIP;
70        $this->title = $title;
71    }
72
73    public function checkConstraint(): string {
74        $match = $this->spamChecker->checkSummary( $this->summary );
75        if ( $match === false && $this->sectionHeading !== null ) {
76            // If the section isn't new, the $this->sectionHeading is null
77            $match = $this->spamChecker->checkContent( $this->sectionHeading );
78        }
79        if ( $match === false ) {
80            $match = $this->spamChecker->checkContent( $this->text );
81        }
82
83        if ( $match === false ) {
84            return self::CONSTRAINT_PASSED;
85        }
86
87        $this->match = $match;
88        $this->logger->debug(
89            '{ip} spam regex hit [[{title}]]: "{match}"',
90            [
91                'ip' => $this->reqIP,
92                'title' => $this->title->getPrefixedDBkey(),
93                'match' => str_replace( "\n", '', $match )
94            ]
95        );
96        return self::CONSTRAINT_FAILED;
97    }
98
99    public function getLegacyStatus(): StatusValue {
100        $statusValue = StatusValue::newGood();
101        if ( $this->match !== '' ) {
102            $match = str_replace( "\n", '', $this->match );
103            $statusValue->fatal( 'spamprotectionmatch', $match );
104            $statusValue->value = self::AS_SPAM_ERROR;
105        }
106        return $statusValue;
107    }
108
109    /**
110     * @return string
111     */
112    public function getMatch(): string {
113        return $this->match;
114    }
115
116}