diff --git a/includes/AbuseFilterHooks.php b/includes/AbuseFilterHooks.php index 41d4271a8..6ddfe8b7c 100644 --- a/includes/AbuseFilterHooks.php +++ b/includes/AbuseFilterHooks.php @@ -166,20 +166,21 @@ class AbuseFilterHooks { $text = AbuseFilter::contentToString( $content ); $title = $context->getTitle(); + $logger = LoggerFactory::getInstance( 'AbuseFilter' ); if ( $title === null ) { // T144265: This *should* never happen. - $logger = LoggerFactory::getInstance( 'AbuseFilter' ); $logger->warning( __METHOD__ . ' received a null title.' ); return Status::newGood(); } - - if ( $title->canExist() && $title->exists() ) { - // Make sure we load the latest text saved in database (bug 31656) - $page = $context->getWikiPage(); - } else { - $page = null; + if ( !$title->canExist() ) { + // This also should be handled in EditPage or whoever is calling the hook. + $logger->warning( __METHOD__ . ' received a Title that cannot exist.' ); + // Note that if the title cannot exist, there's no much point in filtering the edit anyway + return Status::newGood(); } + $page = $context->getWikiPage(); + $vars = new AbuseFilterVariableHolder(); $builder = new RunVariableGenerator( $vars, $user, $title ); $vars = $builder->getEditVars( $content, $text, $summary, $slot, $page ); diff --git a/includes/VariableGenerator/RCVariableGenerator.php b/includes/VariableGenerator/RCVariableGenerator.php index bdb70ddac..cb0b88927 100644 --- a/includes/VariableGenerator/RCVariableGenerator.php +++ b/includes/VariableGenerator/RCVariableGenerator.php @@ -210,7 +210,7 @@ class RCVariableGenerator extends VariableGenerator { $this->vars->setVar( 'old_wikitext', '' ); } - $this->addEditVars( $title ); + $this->addEditVars( $title, \WikiPage::factory( $title ) ); return $this; } diff --git a/includes/VariableGenerator/RunVariableGenerator.php b/includes/VariableGenerator/RunVariableGenerator.php index 1f0deab46..2f873026f 100644 --- a/includes/VariableGenerator/RunVariableGenerator.php +++ b/includes/VariableGenerator/RunVariableGenerator.php @@ -110,7 +110,7 @@ class RunVariableGenerator extends VariableGenerator { } /** - * @param WikiPage|null $page + * @param WikiPage $page * @param string $summary * @param Content $newcontent * @param string $text @@ -120,7 +120,7 @@ class RunVariableGenerator extends VariableGenerator { * @throws MWException */ private function newVariableHolderForEdit( - ?WikiPage $page, + WikiPage $page, string $summary, Content $newcontent, string $text, @@ -153,7 +153,7 @@ class RunVariableGenerator extends VariableGenerator { * @param string $text * @param string $summary * @param string $slot - * @param WikiPage|null $page + * @param WikiPage $page * @return AbuseFilterVariableHolder|null */ public function getEditVars( @@ -161,17 +161,17 @@ class RunVariableGenerator extends VariableGenerator { string $text, string $summary, $slot, - WikiPage $page = null + WikiPage $page ) : ?AbuseFilterVariableHolder { - $oldContent = null; - - if ( $page !== null ) { + if ( $this->title->exists() ) { $filterText = $this->getEditTextForFiltering( $page, $content, $slot ); if ( $filterText === null ) { return null; } list( $oldContent, $oldAfText, $text ) = $filterText; } else { + // Optimization + $oldContent = null; $oldAfText = ''; } @@ -262,8 +262,8 @@ class RunVariableGenerator extends VariableGenerator { // We only have the upload comment and page text when using the UploadVerifyUpload hook if ( $summary !== null && $text !== null ) { // This block is adapted from self::getTextForFiltering() + $page = WikiPage::factory( $this->title ); if ( $this->title->exists() ) { - $page = WikiPage::factory( $this->title ); $revRec = $page->getRevisionRecord(); if ( !$revRec ) { return null; @@ -279,7 +279,6 @@ class RunVariableGenerator extends VariableGenerator { // Page text is ignored for uploads when the page already exists $text = $oldtext; } else { - $page = null; $oldtext = ''; } diff --git a/includes/VariableGenerator/VariableGenerator.php b/includes/VariableGenerator/VariableGenerator.php index 468d5c75f..3ea30b9ff 100644 --- a/includes/VariableGenerator/VariableGenerator.php +++ b/includes/VariableGenerator/VariableGenerator.php @@ -161,16 +161,10 @@ class VariableGenerator { /** * @param Title $title - * @param WikiPage|null $page + * @param WikiPage $page * @return $this For chaining */ - public function addEditVars( Title $title, WikiPage $page = null ) : self { - // NOTE: $page may end up remaining null, e.g. if $title points to a special page. - if ( !$page && $title->canExist() ) { - // TODO: The caller should do this! - $page = WikiPage::factory( $title ); - } - + public function addEditVars( Title $title, WikiPage $page ) : self { $this->vars->setLazyLoadVar( 'edit_diff', 'diff-array', [ 'oldtext-var' => 'old_wikitext', 'newtext-var' => 'new_wikitext' ] ); $this->vars->setLazyLoadVar( 'edit_diff_pst', 'diff-array',