From: Kaldari Date: Thu, 11 Feb 2016 04:57:03 +0000 (-0600) Subject: resourceloader: Follow redirects for JavaScript/CSS in WikiModule X-Git-Tag: 1.31.0-rc.0~3730^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/?a=commitdiff_plain;h=771e85be6c6012728c37352ea62f0028387ae5a4;p=lhc%2Fweb%2Fwiklou.git resourceloader: Follow redirects for JavaScript/CSS in WikiModule Bug: T108653 Change-Id: I9a321fc5728f3bf9df950c060c92e4148cf3ef93 --- diff --git a/includes/resourceloader/ResourceLoaderWikiModule.php b/includes/resourceloader/ResourceLoaderWikiModule.php index b0d060b788..f6f14b37d4 100644 --- a/includes/resourceloader/ResourceLoaderWikiModule.php +++ b/includes/resourceloader/ResourceLoaderWikiModule.php @@ -149,6 +149,15 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule { return null; } + // If the page is a redirect, follow the redirect. + if ( $title->isRedirect() ) { + $content = $this->getContentObj( $title ); + $title = $content ? $content->getUltimateRedirectTarget() : null; + if ( !$title ) { + return null; + } + } + $handler = ContentHandler::getForTitle( $title ); if ( $handler->isSupportedFormat( CONTENT_FORMAT_CSS ) ) { $format = CONTENT_FORMAT_CSS; @@ -158,6 +167,19 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule { return null; } + $content = $this->getContentObj( $title ); + if ( !$content ) { + return null; + } + + return $content->serialize( $format ); + } + + /** + * @param Title $title + * @return Content|null + */ + protected function getContentObj( Title $title ) { $revision = Revision::newKnownCurrent( wfGetDB( DB_REPLICA ), $title->getArticleID(), $title->getLatestRevID() ); if ( !$revision ) { @@ -165,13 +187,11 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule { } $revision->setTitle( $title ); $content = $revision->getContent( Revision::RAW ); - if ( !$content ) { wfDebugLog( 'resourceloader', __METHOD__ . ': failed to load content of JS/CSS page!' ); return null; } - - return $content->serialize( $format ); + return $content; } /** diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php index a332528ffb..4048ffe6d4 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php @@ -1,5 +1,7 @@ assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' ); } + + /** + * @covers ResourceLoaderWikiModule::getContent + */ + public function testGetContentForRedirects() { + // Set up context and module object + $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader ); + $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' ) + ->setMethods( [ 'getPages', 'getContentObj' ] ) + ->getMock(); + $module->expects( $this->any() ) + ->method( 'getPages' ) + ->will( $this->returnValue( [ + 'MediaWiki:Redirect.js' => [ 'type' => 'script' ] + ] ) ); + $module->expects( $this->any() ) + ->method( 'getContentObj' ) + ->will( $this->returnCallback( function ( Title $title ) { + if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) { + $handler = new JavaScriptContentHandler(); + return $handler->makeRedirectContent( + Title::makeTitle( NS_MEDIAWIKI, 'Target.js' ) + ); + } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) { + return new JavaScriptContent( 'target;' ); + } else { + return null; + } + } ) ); + + // Mock away Title's db queries with LinkCache + MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj( + 1, // id + new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ), + 1, // len + 1 // redirect + ); + + $this->assertEquals( + "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n", + $module->getScript( $context ), + 'Redirect resolved by getContent' + ); + } } class TestResourceLoaderWikiModule extends ResourceLoaderWikiModule {