diff --git a/includes/EditPage.php b/includes/EditPage.php index 79af9d4f970..fcd7d130799 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -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 { diff --git a/includes/editpage/Constraint/EditConstraintFactory.php b/includes/editpage/Constraint/EditConstraintFactory.php index e11f42ccd23..26b29c6a808 100644 --- a/includes/editpage/Constraint/EditConstraintFactory.php +++ b/includes/editpage/Constraint/EditConstraintFactory.php @@ -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, diff --git a/includes/editpage/Constraint/SpamRegexConstraint.php b/includes/editpage/Constraint/SpamRegexConstraint.php index 3043a4383f7..f2467ec40dc 100644 --- a/includes/editpage/Constraint/SpamRegexConstraint.php +++ b/includes/editpage/Constraint/SpamRegexConstraint.php @@ -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; diff --git a/tests/phpunit/unit/includes/editpage/Constraint/SpamRegexConstraintTest.php b/tests/phpunit/unit/includes/editpage/Constraint/SpamRegexConstraintTest.php index 2a1d66908bc..589f64b35f0 100644 --- a/tests/phpunit/unit/includes/editpage/Constraint/SpamRegexConstraintTest.php +++ b/tests/phpunit/unit/includes/editpage/Constraint/SpamRegexConstraintTest.php @@ -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',