Improve special page handling in onBeforePageDisplay

Avoid creating of Title object to get the "fixed special name" (which is
the localized name) and compare that against the local name of the
current page, just use the SpecialPageFactory to get the canonical name
(which is the english internal name, not the english visible name) and
compare against the list of canonical allowed pages (adjust the existing
list to the canonical names, that are "old" names in different cases)

Change-Id: Ia6f6574bf6e65c75f8977ff016feda6ecdca3776
This commit is contained in:
Umherirrender 2023-09-23 00:03:01 +02:00
parent ed07de63f7
commit fb1de7786c
3 changed files with 57 additions and 12 deletions

View file

@ -429,7 +429,8 @@
"main": {
"class": "MediaWiki\\Extension\\MultimediaViewer\\Hooks",
"services": [
"UserOptionsLookup"
"UserOptionsLookup",
"SpecialPageFactory"
]
}
},

View file

@ -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 );

View file

@ -0,0 +1,43 @@
<?php
namespace MediaWiki\Extension\MultimediaViewer\Tests;
use MediaWiki\Extension\MultimediaViewer\Hooks;
use MediaWiki\Output\OutputPage;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;
use SkinTemplate;
/**
* @covers \MediaWiki\Extension\MultimediaViewer\Hooks
*/
class HooksTest extends MediaWikiIntegrationTestCase {
public function newHooksInstance() {
return new Hooks(
$this->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 );
}
}