EditPage: Move $sectionHeadingToCheck handling to SpamRegexConstraint

Cleaner and simpler if the constraint takes care of determining
how to handle the section title

Bug: T157658
Change-Id: I45de1aca77fe1ddcd3d94b6ca964328d9911b440
This commit is contained in:
DannyS712 2020-11-11 00:02:42 +00:00
parent d5e294bde9
commit 1f05a9799b
4 changed files with 25 additions and 15 deletions

View file

@ -2055,21 +2055,14 @@ ERROR;
);
// SpamRegexConstraint: ensure that the summary and text don't match the spam regex
if ( $this->section == 'new' ) {
// $wgSpamRegex is enforced on this new heading/summary because, unlike
// regular summaries, it is added to the actual wikitext.
// sectiontitle is only set if the API is used with `sectiontitle`, otherwise
// the summary is used which comes from the API `summary` parameter or the
// "Add Topic" user interface
$sectionHeadingToCheck = ( $this->sectiontitle !== '' ? $this->sectiontitle : $this->summary );
} else {
// No section heading to check
$sectionHeadingToCheck = '';
}
// FIXME $this->section is documented to always be a string, but it can be null
// since importFormData does not provide a default when getting the section from
// WebRequest, and the default default is null.
$constraintRunner->addConstraint(
$constraintFactory->newSpamRegexConstraint(
$this->summary,
$sectionHeadingToCheck,
$this->section === null ? '' : $this->section,
$this->sectiontitle,
$this->textbox1,
$this->context->getRequest()->getIP(),
$this->mTitle
@ -2197,7 +2190,6 @@ ERROR;
$result['sectionanchor'] = '';
if ( $this->section == 'new' ) {
// @phan-suppress-next-line PhanSuspiciousValueComparison
if ( $this->sectiontitle !== '' ) {
// Insert the section title above the content.
$content = $content->addSectionHeader( $this->sectiontitle );
@ -2260,7 +2252,6 @@ ERROR;
}
// If sectiontitle is set, use it, otherwise use the summary as the section title.
// @phan-suppress-next-line PhanSuspiciousValueComparison
if ( $this->sectiontitle !== '' ) {
$sectionTitle = $this->sectiontitle;
} else {

View file

@ -210,6 +210,7 @@ class EditConstraintFactory {
/**
* @param string $summary
* @param string $section
* @param string $sectionHeading
* @param string $text
* @param string $reqIP
@ -218,6 +219,7 @@ class EditConstraintFactory {
*/
public function newSpamRegexConstraint(
string $summary,
string $section,
string $sectionHeading,
string $text,
string $reqIP,
@ -227,6 +229,7 @@ class EditConstraintFactory {
$this->loggerFactory->getLogger( 'SpamRegex' ),
$this->spamRegexChecker,
$summary,
$section,
$sectionHeading,
$text,
$reqIP,

View file

@ -62,6 +62,7 @@ class SpamRegexConstraint implements IEditConstraint {
* @param LoggerInterface $logger for logging hits
* @param SpamChecker $spamChecker
* @param string $summary
* @param string $section
* @param string $sectionHeading
* @param string $text
* @param string $reqIP for logging hits
@ -71,15 +72,28 @@ class SpamRegexConstraint implements IEditConstraint {
LoggerInterface $logger,
SpamChecker $spamChecker,
string $summary,
string $section,
string $sectionHeading,
string $text,
string $reqIP,
Title $title
) {
if ( $section == 'new' ) {
// $wgSpamRegex is enforced on this new heading/summary because, unlike
// regular summaries, it is added to the actual wikitext.
// sectiontitle is only set if the API is used with `sectiontitle`, otherwise
// the summary is used which comes from the API `summary` parameter or the
// "Add Topic" user interface
$sectionHeadingToCheck = ( $sectionHeading !== '' ? $sectionHeading : $summary );
} else {
// No section heading to check
$sectionHeadingToCheck = '';
}
$this->logger = $logger;
$this->spamChecker = $spamChecker;
$this->summary = $summary;
$this->sectionHeading = $sectionHeading;
$this->sectionHeading = $sectionHeadingToCheck;
$this->text = $text;
$this->reqIP = $reqIP;
$this->title = $title;

View file

@ -62,6 +62,7 @@ class SpamRegexConstraintTest extends MediaWikiUnitTestCase {
$logger,
$spamChecker,
$summary,
'new',
$sectionHeading,
$text,
'Request-IP',
@ -94,6 +95,7 @@ class SpamRegexConstraintTest extends MediaWikiUnitTestCase {
$logger,
$spamChecker,
$summary,
'new',
$sectionHeading,
$text,
'Request-IP',