From 5aa1bf33f6b47f74604fd8ce82ed5a80ec3c0557 Mon Sep 17 00:00:00 2001 From: Alexander Vorwerk Date: Fri, 15 Oct 2021 16:12:55 +0200 Subject: [PATCH] Make PageEditStash::checkCache() accept an UserIdentity This allows using the already into PageEditStash injected UserFactory instead of falling back to global state in DerivedPageDataUpdater. Change-Id: I18f198d323c3ca001c6a0de91bc669180cd1d11c --- includes/Storage/DerivedPageDataUpdater.php | 20 ++----------------- includes/Storage/PageEditStash.php | 6 +++--- .../phpunit/includes/api/ApiStashEditTest.php | 5 +++-- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/includes/Storage/DerivedPageDataUpdater.php b/includes/Storage/DerivedPageDataUpdater.php index c2433754c1e..09f281dc673 100644 --- a/includes/Storage/DerivedPageDataUpdater.php +++ b/includes/Storage/DerivedPageDataUpdater.php @@ -371,15 +371,6 @@ class DerivedPageDataUpdater implements IDBAccessObject, LoggerAwareInterface { $this->logger = new NullLogger(); } - /** - * @param UserIdentity $user - * - * @return User - */ - private static function toLegacyUser( UserIdentity $user ) { - return User::newFromIdentity( $user ); - } - public function setLogger( LoggerInterface $logger ) { $this->logger = $logger; } @@ -834,14 +825,12 @@ class DerivedPageDataUpdater implements IDBAccessObject, LoggerAwareInterface { // The edit may have already been prepared via api.php?action=stashedit $stashedEdit = false; - $legacyUser = self::toLegacyUser( $user ); - // TODO: MCR: allow output for all slots to be stashed. if ( $useStash && $slotsUpdate->isModifiedSlot( SlotRecord::MAIN ) ) { $stashedEdit = $this->pageEditStash->checkCache( $title, $slotsUpdate->getModifiedSlot( SlotRecord::MAIN )->getContent(), - $legacyUser + $user ); } @@ -1581,18 +1570,13 @@ class DerivedPageDataUpdater implements IDBAccessObject, LoggerAwareInterface { DeferredUpdates::addUpdate( new SearchUpdate( $id, $title, $mainSlot->getContent() ) ); } - $legacyUser = self::toLegacyUser( $this->user ); - // If this is another user's talk page, update newtalk. // Don't do this if $options['changed'] = false (null-edits) nor if // it's a minor edit and the user making the edit doesn't generate notifications for those. // TODO: the permission check should be performed by the callers, see T276181. if ( $this->options['changed'] && $title->getNamespace() === NS_USER_TALK - - // TODO User::getTitleKey is just a string manipulation of the user name, - // duplicate it here and use $this->user (a UserIdentity) instead - && $shortTitle != $legacyUser->getTitleKey() + && $title->getText() != $this->user->getName() && !( $this->revision->isMinor() && $this->permissionManager ->userHasRight( $this->user, 'nominornewtalk' ) ) diff --git a/includes/Storage/PageEditStash.php b/includes/Storage/PageEditStash.php index b1a3fdc2b40..295578dabc5 100644 --- a/includes/Storage/PageEditStash.php +++ b/includes/Storage/PageEditStash.php @@ -35,7 +35,6 @@ use MediaWiki\User\UserIdentity; use ParserOutput; use Psr\Log\LoggerInterface; use stdClass; -use User; use Wikimedia\Rdbms\ILoadBalancer; use Wikimedia\ScopedCallback; use WikiPage; @@ -213,10 +212,11 @@ class PageEditStash { * * @param PageIdentity $page * @param Content $content - * @param User $user to get parser options from + * @param UserIdentity $useridentity to get parser options from * @return stdClass|bool Returns edit stash object or false on cache miss */ - public function checkCache( PageIdentity $page, Content $content, User $user ) { + public function checkCache( PageIdentity $page, Content $content, UserIdentity $useridentity ) { + $user = $this->userFactory->newFromUserIdentity( $useridentity ); if ( // The context is not an HTTP POST request !$user->getRequest()->wasPosted() || diff --git a/tests/phpunit/includes/api/ApiStashEditTest.php b/tests/phpunit/includes/api/ApiStashEditTest.php index 3f336ce696d..b1c8da55437 100644 --- a/tests/phpunit/includes/api/ApiStashEditTest.php +++ b/tests/phpunit/includes/api/ApiStashEditTest.php @@ -2,6 +2,7 @@ use MediaWiki\MediaWikiServices; use MediaWiki\Storage\PageEditStash; +use MediaWiki\User\UserIdentity; use Psr\Log\NullLogger; use Wikimedia\TestingAccessWrapper; @@ -304,11 +305,11 @@ class ApiStashEditTest extends ApiTestCase { * Shortcut for calling PageStashEdit::checkCache() without * having to create Titles and Contents in every test. * - * @param User $user + * @param UserIdentity $user * @param string $text The text of the article * @return stdClass|bool Return value of PageStashEdit::checkCache(), false if not in cache */ - protected function doCheckCache( User $user, $text = 'Content' ) { + protected function doCheckCache( UserIdentity $user, $text = 'Content' ) { return MediaWikiServices::getInstance()->getPageEditStash()->checkCache( Title::newFromText( __CLASS__ ), new WikitextContent( $text ),