diff --git a/RELEASE-NOTES-1.36 b/RELEASE-NOTES-1.36 index fb2c561a376..249bf117710 100644 --- a/RELEASE-NOTES-1.36 +++ b/RELEASE-NOTES-1.36 @@ -623,6 +623,7 @@ because of Phabricator reports. is now allowed. * DatabaseBlock constructor 'byText' option was deprecated in favour of 'by' option, which now accepts UserIdentity. Passing user ID is deprecated. +* Parser::getUser was deprecated. Use Parser::getUserIdentity instead. * … === Other changes in 1.36 === diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index e7202f73b85..8944dc2343a 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -998,7 +998,9 @@ return [ $services->getLanguageConverterFactory(), $services->getHookContainer(), $services->getTidy(), - $services->getMainWANObjectCache() + $services->getMainWANObjectCache(), + $services->getUserOptionsLookup(), + $services->getUserFactory() ); }, diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 46894990c72..4259c6949eb 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -37,6 +37,9 @@ use MediaWiki\Revision\RevisionRecord; use MediaWiki\Revision\SlotRecord; use MediaWiki\SpecialPage\SpecialPageFactory; use MediaWiki\Tidy\TidyDriverBase; +use MediaWiki\User\UserFactory; +use MediaWiki\User\UserIdentity; +use MediaWiki\User\UserOptionsLookup; use Psr\Log\LoggerInterface; use Wikimedia\IPUtils; use Wikimedia\ScopedCallback; @@ -347,6 +350,12 @@ class Parser { /** @var TidyDriverBase */ private $tidy; + /** @var UserOptionsLookup */ + private $userOptionsLookup; + + /** @var UserFactory */ + private $userFactory; + /** * @internal For use by ServiceWiring */ @@ -390,6 +399,8 @@ class Parser { * @param HookContainer $hookContainer * @param TidyDriverBase $tidy * @param WANObjectCache $wanCache + * @param UserOptionsLookup $userOptionsLookup + * @param UserFactory $userFactory */ public function __construct( ServiceOptions $svcOptions, @@ -405,7 +416,9 @@ class Parser { LanguageConverterFactory $languageConverterFactory, HookContainer $hookContainer, TidyDriverBase $tidy, - WANObjectCache $wanCache + WANObjectCache $wanCache, + UserOptionsLookup $userOptionsLookup, + UserFactory $userFactory ) { if ( ParserFactory::$inParserFactory === 0 ) { // Direct construction of Parser was deprecated in 1.34 and @@ -447,6 +460,9 @@ class Parser { ] ); + $this->userOptionsLookup = $userOptionsLookup; + $this->userFactory = $userFactory; + // These steps used to be done in "::firstCallInit()" // (if you're chasing a reference from some old code) CoreParserFunctions::register( $this ); @@ -944,10 +960,14 @@ class Parser { * Set the current user. * Should only be used when doing pre-save transform. * - * @param User|null $user User object or null (to reset) + * @param UserIdentity|null $user user identity or null (to reset) */ - public function setUser( ?User $user ) { - $this->mUser = $user; + public function setUser( ?UserIdentity $user ) { + if ( $user ) { + $this->mUser = $this->userFactory->newFromUserIdentity( $user ); + } else { + $this->mUser = $user; + } } /** @@ -1104,7 +1124,7 @@ class Parser { /** * Get a User object either from $this->mUser, if set, or from the * ParserOptions object otherwise - * + * @deprecated since 1.36. Use ::getUserIdentity instead. * @return User */ public function getUser() { @@ -1114,6 +1134,14 @@ class Parser { return $this->mOptions->getUser(); } + /** + * Get an identity of the user for whom the parse is being made, if set. + * @return UserIdentity + */ + public function getUserIdentity(): UserIdentity { + return $this->getUser(); + } + /** * Get a preprocessor object * @@ -4498,12 +4526,12 @@ class Parser { * * @param string $text The text to transform * @param Title $title The Title object for the current article - * @param User $user The User object describing the current user + * @param UserIdentity $user The User object describing the current user * @param ParserOptions $options Parsing options * @param bool $clearState Whether to clear the parser state first * @return string The altered wiki markup */ - public function preSaveTransform( $text, Title $title, User $user, + public function preSaveTransform( $text, Title $title, UserIdentity $user, ParserOptions $options, $clearState = true ) { if ( $clearState ) { @@ -4536,11 +4564,11 @@ class Parser { * Pre-save transform helper function * * @param string $text - * @param User $user + * @param UserIdentity $user * * @return string */ - private function pstPass2( $text, User $user ) { + private function pstPass2( $text, UserIdentity $user ) { # Note: This is the timestamp saved as hardcoded wikitext to the database, we use # $this->contLang here in order to give everyone the same signature and use the default one # rather than the one selected in each user's preferences. (see also T14815) @@ -4610,22 +4638,22 @@ class Parser { * Do not reuse this parser instance after calling getUserSig(), * as it may have changed. * - * @param User $user + * @param UserIdentity $user * @param string|false $nickname Nickname to use or false to use user's default nickname * @param bool|null $fancySig whether the nicknname is the complete signature * or null to use default value * @return string */ - public function getUserSig( User $user, $nickname = false, $fancySig = null ) { + public function getUserSig( UserIdentity $user, $nickname = false, $fancySig = null ) { $username = $user->getName(); # If not given, retrieve from the user object. if ( $nickname === false ) { - $nickname = $user->getOption( 'nickname' ); + $nickname = $this->userOptionsLookup->getOption( $user, 'nickname' ); } if ( $fancySig === null ) { - $fancySig = $user->getBoolOption( 'fancysig' ); + $fancySig = $this->userOptionsLookup->getBoolOption( $user, 'fancysig' ); } if ( $nickname === null || $nickname === '' ) { @@ -4664,7 +4692,7 @@ class Parser { # If we're still here, make it a link to the user page $userText = wfEscapeWikiText( $username ); $nickText = wfEscapeWikiText( $nickname ); - $msgName = $user->isAnon() ? 'signature-anon' : 'signature'; + $msgName = $user->isRegistered() ? 'signature' : 'signature-anon'; return wfMessage( $msgName, $userText, $nickText )->inContentLanguage() ->title( $this->getTitle() )->text(); diff --git a/includes/parser/ParserFactory.php b/includes/parser/ParserFactory.php index a355f069fc7..79d95e3afe1 100644 --- a/includes/parser/ParserFactory.php +++ b/includes/parser/ParserFactory.php @@ -26,6 +26,8 @@ use MediaWiki\Languages\LanguageConverterFactory; use MediaWiki\Linker\LinkRendererFactory; use MediaWiki\SpecialPage\SpecialPageFactory; use MediaWiki\Tidy\TidyDriverBase; +use MediaWiki\User\UserFactory; +use MediaWiki\User\UserOptionsLookup; use Psr\Log\LoggerInterface; /** @@ -62,6 +64,12 @@ class ParserFactory { /** @var LanguageConverterFactory */ private $languageConverterFactory; + /** @var UserOptionsLookup */ + private $userOptionsLookup; + + /** @var UserFactory */ + private $userFactory; + /** * Track calls to Parser constructor to aid in deprecation of direct * Parser invocation. This is temporary: it will be removed once the @@ -95,6 +103,8 @@ class ParserFactory { * @param HookContainer $hookContainer * @param TidyDriverBase $tidy * @param WANObjectCache $wanCache + * @param UserOptionsLookup $userOptionsLookup + * @param UserFactory $userFactory * @since 1.32 * @internal */ @@ -111,7 +121,9 @@ class ParserFactory { LanguageConverterFactory $languageConverterFactory, HookContainer $hookContainer, TidyDriverBase $tidy, - WANObjectCache $wanCache + WANObjectCache $wanCache, + UserOptionsLookup $userOptionsLookup, + UserFactory $userFactory ) { $svcOptions->assertRequiredOptions( Parser::CONSTRUCTOR_OPTIONS ); @@ -130,6 +142,8 @@ class ParserFactory { $this->hookContainer = $hookContainer; $this->tidy = $tidy; $this->wanCache = $wanCache; + $this->userOptionsLookup = $userOptionsLookup; + $this->userFactory = $userFactory; } /** @@ -155,7 +169,9 @@ class ParserFactory { $this->languageConverterFactory, $this->hookContainer, $this->tidy, - $this->wanCache + $this->wanCache, + $this->userOptionsLookup, + $this->userFactory ); } finally { self::$inParserFactory--; diff --git a/includes/preferences/SignatureValidator.php b/includes/preferences/SignatureValidator.php index 2d174f216ac..4dec8b21db4 100644 --- a/includes/preferences/SignatureValidator.php +++ b/includes/preferences/SignatureValidator.php @@ -22,13 +22,13 @@ namespace MediaWiki\Preferences; use Html; use MediaWiki\MediaWikiServices; +use MediaWiki\User\UserIdentity; use MessageLocalizer; use MultiHttpClient; use ParserOptions; use ParsoidVirtualRESTService; use SpecialPage; use Title; -use User; use VirtualRESTServiceClient; /** @@ -36,14 +36,14 @@ use VirtualRESTServiceClient; */ class SignatureValidator { - /** @var User */ + /** @var UserIdentity */ private $user; /** @var MessageLocalizer|null */ private $localizer; /** @var ParserOptions */ private $popts; - public function __construct( User $user, ?MessageLocalizer $localizer, ParserOptions $popts ) { + public function __construct( UserIdentity $user, ?MessageLocalizer $localizer, ParserOptions $popts ) { $this->user = $user; $this->localizer = $localizer; $this->popts = $popts; diff --git a/tests/phpunit/includes/parser/ParserTest.php b/tests/phpunit/includes/parser/ParserTest.php index d03c93a7d7a..7c0876c03b6 100644 --- a/tests/phpunit/includes/parser/ParserTest.php +++ b/tests/phpunit/includes/parser/ParserTest.php @@ -51,6 +51,8 @@ class ParserTest extends MediaWikiIntegrationTestCase { $this->createMock( MediaWiki\HookContainer\HookContainer::class ), $this->createMock( MediaWiki\Tidy\TidyDriverBase::class ), $this->createMock( WANObjectCache::class ), + $this->createMock( MediaWiki\User\UserOptionsLookup::class ), + $this->createMock( MediaWiki\User\UserFactory::class ) ]; } diff --git a/tests/phpunit/unit/includes/parser/ParserFactoryTest.php b/tests/phpunit/unit/includes/parser/ParserFactoryTest.php index a17c089df8f..2024a42b954 100644 --- a/tests/phpunit/unit/includes/parser/ParserFactoryTest.php +++ b/tests/phpunit/unit/includes/parser/ParserFactoryTest.php @@ -6,6 +6,8 @@ use MediaWiki\Languages\LanguageConverterFactory; use MediaWiki\Linker\LinkRendererFactory; use MediaWiki\SpecialPage\SpecialPageFactory; use MediaWiki\Tidy\TidyDriverBase; +use MediaWiki\User\UserFactory; +use MediaWiki\User\UserOptionsLookup; use Psr\Log\LoggerInterface; use Wikimedia\TestingAccessWrapper; @@ -54,7 +56,9 @@ class ParserFactoryTest extends MediaWikiUnitTestCase { $this->createNoOpMock( LanguageConverterFactory::class ), $this->createHookContainer(), $this->createNoOpMock( TidyDriverBase::class ), - $this->createNoOpMock( WANObjectCache::class ) + $this->createNoOpMock( WANObjectCache::class ), + $this->createNoOpMock( UserOptionsLookup::class ), + $this->createNoOpMock( UserFactory::class ) ); return $factory; }