Debian Codegraph @

includes/editpage/Constraint/SpamRegexConstraint.php

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