From 57d1f15d64def378c4be2700557f4093626db3ae Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sat, 23 Mar 2024 19:16:47 +0000 Subject: [PATCH] Inject service 'Minerva.Permissions' into SkinMinerva Change-Id: Ie68691d7030ea622f2bc232fea2df49b70484e19 --- includes/Skins/SkinMinerva.php | 37 +++++++------------------ skin.json | 1 + tests/phpunit/skins/SkinMinervaTest.php | 1 + 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/includes/Skins/SkinMinerva.php b/includes/Skins/SkinMinerva.php index d5ec653d9..36ab3b49e 100644 --- a/includes/Skins/SkinMinerva.php +++ b/includes/Skins/SkinMinerva.php @@ -32,6 +32,7 @@ use MediaWiki\Minerva\Menu\Main\MainMenuDirector; use MediaWiki\Minerva\Menu\PageActions\PageActionsDirector; use MediaWiki\Minerva\Menu\User\UserMenuDirector; use MediaWiki\Minerva\Permissions\IMinervaPagePermissions; +use MediaWiki\Minerva\Permissions\MinervaPagePermissions; use MediaWiki\Minerva\SkinOptions; use MediaWiki\Revision\RevisionLookup; use MediaWiki\SpecialPage\SpecialPage; @@ -63,17 +64,12 @@ class SkinMinerva extends SkinMustache { /** @var array|null */ private $contentNavigationUrls; - /** - * This variable is lazy loaded, please use getPermissions() getter - * @see SkinMinerva::getPermissions() - * @var IMinervaPagePermissions - */ - private $permissions; - private GenderCache $genderCache; private LinkRenderer $linkRenderer; + private IMinervaPagePermissions $permissions; + private SkinOptions $skinOptions; private NamespaceInfo $namespaceInfo; @@ -85,6 +81,7 @@ class SkinMinerva extends SkinMustache { /** * @param GenderCache $genderCache * @param LinkRenderer $linkRenderer + * @param MinervaPagePermissions $permissions * @param SkinOptions $skinOptions * @param NamespaceInfo $namespaceInfo * @param RevisionLookup $revisionLookup @@ -94,6 +91,7 @@ class SkinMinerva extends SkinMustache { public function __construct( GenderCache $genderCache, LinkRenderer $linkRenderer, + MinervaPagePermissions $permissions, SkinOptions $skinOptions, NamespaceInfo $namespaceInfo, RevisionLookup $revisionLookup, @@ -103,6 +101,8 @@ class SkinMinerva extends SkinMustache { parent::__construct( $options ); $this->genderCache = $genderCache; $this->linkRenderer = $linkRenderer; + $this->permissions = $permissions + ->setContext( $this->getContext() ); $this->skinOptions = $skinOptions; $this->namespaceInfo = $namespaceInfo; $this->revisionLookup = $revisionLookup; @@ -492,21 +492,6 @@ class SkinMinerva extends SkinMustache { ] : []; } - /** - * Lazy load the permissions object. We don't want to initialize it as it requires many - * dependencies, sometimes some of those dependencies cannot be fulfilled (like missing Title - * object) - * @return IMinervaPagePermissions - */ - private function getPermissions(): IMinervaPagePermissions { - if ( $this->permissions === null ) { - $this->permissions = MediaWikiServices::getInstance() - ->getService( 'Minerva.Permissions' ) - ->setContext( $this->getContext() ); - } - return $this->permissions; - } - /** * Initalized main menu. Please use getter. * @var MainMenuDirector @@ -975,7 +960,7 @@ class SkinMinerva extends SkinMustache { $talkAtBottom = !$this->skinOptions->get( SkinOptions::TALK_AT_TOP ) || $subjectPage->isMainPage(); if ( !$this->getUserPageHelper()->isUserPage() && - $this->getPermissions()->isTalkAllowed() && $talkAtBottom && + $this->permissions->isTalkAllowed() && $talkAtBottom && // When showing talk at the bottom we restrict this so it is not shown to anons // https://phabricator.wikimedia.org/T54165 // This whole code block can be removed when SkinOptions::TALK_AT_TOP is always true @@ -1006,12 +991,10 @@ class SkinMinerva extends SkinMustache { * @return array */ protected function getJsConfigVars(): array { - $permissions = $this->getPermissions(); - return array_merge( parent::getJsConfigVars(), [ 'wgMinervaPermissions' => [ - 'watchable' => $permissions->isAllowed( IMinervaPagePermissions::WATCHABLE ), - 'watch' => $permissions->isAllowed( IMinervaPagePermissions::WATCH ), + 'watchable' => $this->permissions->isAllowed( IMinervaPagePermissions::WATCHABLE ), + 'watch' => $this->permissions->isAllowed( IMinervaPagePermissions::WATCH ), ], 'wgMinervaFeatures' => $this->skinOptions->getAll(), 'wgMinervaDownloadNamespaces' => $this->getConfig()->get( 'MinervaDownloadNamespaces' ), diff --git a/skin.json b/skin.json index 0ea489a72..1c4ba7dbb 100644 --- a/skin.json +++ b/skin.json @@ -119,6 +119,7 @@ "services": [ "GenderCache", "LinkRenderer", + "Minerva.Permissions", "Minerva.SkinOptions", "NamespaceInfo", "RevisionLookup", diff --git a/tests/phpunit/skins/SkinMinervaTest.php b/tests/phpunit/skins/SkinMinervaTest.php index 13ae29879..e68a0ad21 100644 --- a/tests/phpunit/skins/SkinMinervaTest.php +++ b/tests/phpunit/skins/SkinMinervaTest.php @@ -41,6 +41,7 @@ class SkinMinervaTest extends MediaWikiIntegrationTestCase { return new SkinMinerva( $services->getGenderCache(), $services->getLinkRenderer(), + $services->getService( 'Minerva.Permissions' ), $services->getService( 'Minerva.SkinOptions' ), $services->getNamespaceInfo(), $services->getRevisionLookup(),