diff --git a/extension.json b/extension.json index d0655b88d..1ace67b24 100644 --- a/extension.json +++ b/extension.json @@ -429,7 +429,8 @@ "main": { "class": "MediaWiki\\Extension\\MultimediaViewer\\Hooks", "services": [ - "UserOptionsLookup" + "UserOptionsLookup", + "SpecialPageFactory" ] } }, diff --git a/includes/Hooks.php b/includes/Hooks.php index 08ca161ff..a20f4f44c 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -34,7 +34,7 @@ use MediaWiki\MediaWikiServices; use MediaWiki\Page\Hook\CategoryPageViewHook; use MediaWiki\Preferences\Hook\GetPreferencesHook; use MediaWiki\ResourceLoader\Hook\ResourceLoaderGetConfigVarsHook; -use MediaWiki\Title\Title; +use MediaWiki\SpecialPage\SpecialPageFactory; use MediaWiki\User\Hook\UserGetDefaultOptionsHook; use MediaWiki\User\UserOptionsLookup; use OutputPage; @@ -67,12 +67,18 @@ class Hooks implements * @var UserOptionsLookup */ private $userOptionsLookup; + private SpecialPageFactory $specialPageFactory; /** * @param UserOptionsLookup $userOptionsLookup + * @param SpecialPageFactory $specialPageFactory */ - public function __construct( UserOptionsLookup $userOptionsLookup ) { + public function __construct( + UserOptionsLookup $userOptionsLookup, + SpecialPageFactory $specialPageFactory + ) { $this->userOptionsLookup = $userOptionsLookup; + $this->specialPageFactory = $specialPageFactory; } /** @@ -134,16 +140,11 @@ class Hooks implements $pageIsFlowPage = ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) && // CONTENT_MODEL_FLOW_BOARD $out->getTitle()->getContentModel() === 'flow-board'; - $fileRelatedSpecialPages = [ 'NewFiles', 'ListFiles', 'MostLinkedFiles', - 'MostGloballyLinkedFiles', 'UncategorizedFiles', 'UnusedFiles', 'Search' ]; - $fileRelatedSpecialPagesLocalNames = array_map( - static function ( $name ) { - return Title::newFromText( $name, NS_SPECIAL )->fixSpecialName()->getText(); - }, - $fileRelatedSpecialPages - ); + $fileRelatedSpecialPages = [ 'Newimages', 'Listfiles', 'Mostimages', + 'MostGloballyLinkedFiles', 'Uncategorizedimages', 'Unusedimages', 'Search' ]; $pageIsFileRelatedSpecialPage = $out->getTitle()->inNamespace( NS_SPECIAL ) - && in_array( $out->getTitle()->fixSpecialName()->getText(), $fileRelatedSpecialPagesLocalNames ); + && in_array( $this->specialPageFactory->resolveAlias( $out->getTitle()->getDBkey() )[0], + $fileRelatedSpecialPages ); if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage || $pageIsFlowPage ) { self::getModules( $out ); diff --git a/tests/phpunit/HooksTest.php b/tests/phpunit/HooksTest.php new file mode 100644 index 000000000..4abc53fd2 --- /dev/null +++ b/tests/phpunit/HooksTest.php @@ -0,0 +1,43 @@ +getServiceContainer()->getUserOptionsLookup(), + $this->getServiceContainer()->getSpecialPageFactory() + ); + } + + public static function provideOnBeforePageDisplay() { + return [ + 'no files' => [ 'Main Page', 0, false ], + 'with files' => [ 'Main Page', 1, true ], + 'special with files' => [ 'Special:ListFiles', 0, true ], + 'special no files' => [ 'Special:Watchlist', 0, false ], + ]; + } + + /** + * @dataProvider provideOnBeforePageDisplay + */ + public function testOnBeforePageDisplay( $pagename, $fileCount, $modulesExpected ) { + $skin = new SkinTemplate(); + $output = $this->createMock( OutputPage::class ); + $output->method( 'getTitle' )->willReturn( Title::newFromText( $pagename ) ); + $output->method( 'getFileSearchOptions' )->willReturn( array_fill( 0, $fileCount, null ) ); + $output->expects( $this->exactly( $modulesExpected ? 1 : 0 ) )->method( 'addModules' ); + $this->newHooksInstance()->onBeforePageDisplay( $output, $skin ); + } +}