MediaWiki  master
SpamRegexConstraint.php
Go to the documentation of this file.
1 <?php
22 
24 use Psr\Log\LoggerInterface;
25 use StatusValue;
26 use Title;
27 
36 
38  private $logger;
39 
41  private $spamChecker;
42 
44  private $summary;
45 
47  private $sectionHeading;
48 
50  private $text;
51 
53  private $reqIP;
54 
56  private $title;
57 
59  private $match = '';
60 
71  public function __construct(
72  LoggerInterface $logger,
74  string $summary,
75  string $section,
76  string $sectionHeading,
77  string $text,
78  string $reqIP,
80  ) {
81  if ( $section == 'new' ) {
82  // $wgSpamRegex is enforced on this new heading/summary because, unlike
83  // regular summaries, it is added to the actual wikitext.
84  // sectiontitle is only set if the API is used with `sectiontitle`, otherwise
85  // the summary is used which comes from the API `summary` parameter or the
86  // "Add Topic" user interface
87  $sectionHeadingToCheck = ( $sectionHeading !== '' ? $sectionHeading : $summary );
88  } else {
89  // No section heading to check
90  $sectionHeadingToCheck = '';
91  }
92 
93  $this->logger = $logger;
94  $this->spamChecker = $spamChecker;
95  $this->summary = $summary;
96  $this->sectionHeading = $sectionHeadingToCheck;
97  $this->text = $text;
98  $this->reqIP = $reqIP;
99  $this->title = $title;
100  }
101 
102  public function checkConstraint(): string {
103  $match = $this->spamChecker->checkSummary( $this->summary );
104  if ( $match === false ) {
105  // $wgSpamRegex is enforced on this new heading/summary because, unlike
106  // regular summaries, it is added to the actual wikitext.
107  // EditPage has already determined, based on if this is the API with `sectiontitle`,
108  // or action=edit, or the API with `summary`, what will be the section title.
109  // If the section isn't new, the $this->sectionHeading is an empty string
110  $match = $this->spamChecker->checkContent( $this->sectionHeading );
111  }
112  if ( $match === false ) {
113  $match = $this->spamChecker->checkContent( $this->text );
114  }
115 
116  if ( $match === false ) {
117  return self::CONSTRAINT_PASSED;
118  }
119 
120  $this->match = $match;
121  $this->logger->debug(
122  '{ip} spam regex hit [[{title}]]: "{match}"',
123  [
124  'ip' => $this->reqIP,
125  'title' => $this->title->getPrefixedDBkey(),
126  'match' => str_replace( "\n", '', $match )
127  ]
128  );
129  return self::CONSTRAINT_FAILED;
130  }
131 
132  public function getLegacyStatus(): StatusValue {
133  $statusValue = StatusValue::newGood();
134  if ( $this->match !== '' ) {
135  $match = str_replace( "\n", '', $this->match );
136  $statusValue->fatal( 'spamprotectionmatch', $match );
137  $statusValue->value = self::AS_SPAM_ERROR;
138  }
139  return $statusValue;
140  }
141 
145  public function getMatch(): string {
146  return $this->match;
147  }
148 
149 }
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:43
MediaWiki\EditPage\Constraint\SpamRegexConstraint\getLegacyStatus
getLegacyStatus()
Get the legacy status for failure (or success)
Definition: SpamRegexConstraint.php:132
MediaWiki\EditPage\Constraint\SpamRegexConstraint\$text
string $text
Definition: SpamRegexConstraint.php:50
MediaWiki\EditPage\IEditObject\AS_SPAM_ERROR
const AS_SPAM_ERROR
Status: summary contained spam according to one of the regexes in $wgSummarySpamRegex.
Definition: IEditObject.php:89
MediaWiki\EditPage\Constraint\SpamRegexConstraint\$reqIP
string $reqIP
Definition: SpamRegexConstraint.php:53
MediaWiki\EditPage\Constraint\SpamRegexConstraint\$sectionHeading
string $sectionHeading
Definition: SpamRegexConstraint.php:47
MediaWiki\EditPage\Constraint\SpamRegexConstraint
Verify summary and text do not match spam regexes.
Definition: SpamRegexConstraint.php:35
MediaWiki\EditPage\Constraint\SpamRegexConstraint\$logger
LoggerInterface $logger
Definition: SpamRegexConstraint.php:38
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
MediaWiki\EditPage\Constraint\SpamRegexConstraint\$spamChecker
SpamChecker $spamChecker
Definition: SpamRegexConstraint.php:41
MediaWiki\EditPage\SpamChecker
Service to check if text (either content or a summary) qualifies as spam.
Definition: SpamChecker.php:14
MediaWiki\EditPage\Constraint\SpamRegexConstraint\checkConstraint
checkConstraint()
Definition: SpamRegexConstraint.php:102
Title
Represents a title within MediaWiki.
Definition: Title.php:47
MediaWiki\EditPage\Constraint\SpamRegexConstraint\$title
Title $title
Definition: SpamRegexConstraint.php:56
MediaWiki\EditPage\Constraint
Definition: AccidentalRecreationConstraint.php:21
MediaWiki\EditPage\Constraint\SpamRegexConstraint\$match
string $match
Definition: SpamRegexConstraint.php:59
MediaWiki\EditPage\Constraint\SpamRegexConstraint\__construct
__construct(LoggerInterface $logger, SpamChecker $spamChecker, string $summary, string $section, string $sectionHeading, string $text, string $reqIP, Title $title)
Definition: SpamRegexConstraint.php:71
MediaWiki\EditPage\Constraint\SpamRegexConstraint\getMatch
getMatch()
Definition: SpamRegexConstraint.php:145
MediaWiki\EditPage\Constraint\IEditConstraint
Interface for all constraints that can prevent edits.
Definition: IEditConstraint.php:33
MediaWiki\EditPage\Constraint\SpamRegexConstraint\$summary
string $summary
Definition: SpamRegexConstraint.php:44