resourceloader: Instantiate main class via ServiceWiring

It also removes some code duplication which is nice.

This unlocks various future changes, including:

* Making the `$config` parameter mandatory for the ResourceLoader class
  constructor, which currently falls back to global state.
  This should be deprecated and removed.

* Making it possible to instantiate the ResourceLoader class
  without all the default MW modules being registered from
  global state. E.g. move MW module registration from main class
  constructor to ServiceWiring, and remove the 'EmptyResourceLoader'
  class hack from unit tests, and use regular 'new ResourceLoader'
  instead.

* Making ResourceLoader a standalone library (some day),
  e.g. allowing it to be instantiated from a basic PHP script,
  in a way that is still useful and perhaps able to serve
  (most) RL modules without MW itself.

Bug: T32956
Change-Id: I4939f296c705b268e9cf8de635e923a739410470
This commit is contained in:
Timo Tijhof 2019-02-16 23:16:09 +00:00 committed by Aaron Schulz
parent e7790e5793
commit a186dc62fd
11 changed files with 45 additions and 18 deletions

View file

@ -44,6 +44,7 @@ use ParserCache;
use ParserFactory;
use PasswordFactory;
use ProxyLookup;
use ResourceLoader;
use SearchEngine;
use SearchEngineConfig;
use SearchEngineFactory;
@ -744,6 +745,14 @@ class MediaWikiServices extends ServiceContainer {
return $this->getService( 'ReadOnlyMode' );
}
/**
* @since 1.33
* @return ResourceLoader
*/
public function getResourceLoader() {
return $this->getService( 'ResourceLoader' );
}
/**
* @since 1.31
* @return RevisionFactory

View file

@ -21,7 +21,6 @@
*/
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Session\SessionManager;
use Wikimedia\Rdbms\IResultWrapper;
@ -3276,10 +3275,8 @@ class OutputPage extends ContextSource {
*/
public function getResourceLoader() {
if ( is_null( $this->mResourceLoader ) ) {
$this->mResourceLoader = new ResourceLoader(
$this->getConfig(),
LoggerFactory::getInstance( 'resourceloader' )
);
// Lazy-initialise as needed
$this->mResourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
}
return $this->mResourceLoader;
}

View file

@ -413,6 +413,13 @@ return [
);
},
'ResourceLoader' => function ( MediaWikiServices $services ) : ResourceLoader {
return new ResourceLoader(
$services->getMainConfig(),
LoggerFactory::getInstance( 'resourceloader' )
);
},
'RevisionFactory' => function ( MediaWikiServices $services ) : RevisionFactory {
return $services->getRevisionStore();
},

View file

@ -23,6 +23,7 @@
* @author Timo Tijhof
*/
use MediaWiki\MediaWikiServices;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
@ -194,7 +195,7 @@ class MessageBlobStore implements LoggerAwareInterface {
// Lazy-initialise this property because most callers don't need it.
if ( $this->resourceloader === null ) {
$this->logger->warning( __CLASS__ . ' created without a ResourceLoader instance' );
$this->resourceloader = new ResourceLoader();
$this->resourceloader = MediaWikiServices::getInstance()->getResourceLoader();
}
return $this->resourceloader;
}

View file

@ -21,6 +21,8 @@
* @ingroup Deployment
*/
use MediaWiki\MediaWikiServices;
/**
* Output class modelled on OutputPage.
*
@ -147,7 +149,7 @@ class WebInstallerOutput {
'mediawiki.skinning.interface',
];
$resourceLoader = new ResourceLoader();
$resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
if ( file_exists( "$wgStyleDirectory/Vector/skin.json" ) ) {
// Force loading Vector skin if available as a fallback skin

View file

@ -245,6 +245,7 @@ class ResourceLoader implements LoggerAwareInterface {
$this->logger = $logger ?: new NullLogger();
if ( !$config ) {
// TODO: Deprecate and remove.
$this->logger->debug( __METHOD__ . ' was called without providing a Config instance' );
$config = MediaWikiServices::getInstance()->getMainConfig();
}

View file

@ -133,9 +133,20 @@ class ResourceLoaderContext implements MessageLocalizer {
/**
* Return a dummy ResourceLoaderContext object suitable for passing into
* things that don't "really" need a context.
*
* Use cases:
* - Creating html5shiv script tag in OutputPage.
* - FileModule::readStyleFiles (deprecated, to be removed).
* - Unit tests (deprecated, create empty instance directly or use RLTestCase).
*
* @return ResourceLoaderContext
*/
public static function newDummyContext() {
// This currently creates a non-empty instance of ResourceLoader (all modules registered),
// but that's probably not needed. So once that moves into ServiceWiring, this'll
// become more like the EmptyResourceLoader class we have in PHPUnit tests, which
// is what this should've had originally. If this turns out to be untrue, change to:
// `MediaWikiServices::getInstance()->getResourceLoader()` instead.
return new self( new ResourceLoader(
MediaWikiServices::getInstance()->getMainConfig(),
LoggerFactory::getInstance( 'resourceloader' )

View file

@ -22,7 +22,6 @@
* @author Trevor Parscal
*/
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
// This endpoint is supposed to be independent of request cookies and other
@ -40,11 +39,7 @@ if ( !$wgRequest->checkUrlExtension() ) {
// writes when getting database connections for ResourceLoader. (T192611)
MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->disableChronologyProtection();
// Set up ResourceLoader
$resourceLoader = new ResourceLoader(
ConfigFactory::getDefaultInstance()->makeConfig( 'main' ),
LoggerFactory::getInstance( 'resourceloader' )
);
$resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
$context = new ResourceLoaderContext( $resourceLoader, $wgRequest );
// Respond to ResourceLoader request

View file

@ -46,7 +46,7 @@ class CleanupRemovedModules extends Maintenance {
$this->output( "Cleaning up module_deps table...\n" );
$dbw = $this->getDB( DB_MASTER );
$rl = new ResourceLoader( MediaWikiServices::getInstance()->getMainConfig() );
$rl = MediaWikiServices::getInstance()->getResourceLoader();
$moduleNames = $rl->getModuleNames();
$res = $dbw->select( 'module_deps',
[ 'md_module', 'md_skin' ],

View file

@ -1,8 +1,10 @@
<?php
use MediaWiki\MediaWikiServices;
/**
* Sanity checks for making sure registered resources are sane.
*
* @file
* @author Antoine Musso
* @author Niklas Laxström
* @author Santhosh Thottingal
@ -171,8 +173,8 @@ class ResourcesTest extends MediaWikiTestCase {
$org_wgEnableJavaScriptTest = $wgEnableJavaScriptTest;
$wgEnableJavaScriptTest = true;
// Initialize ResourceLoader
$rl = new ResourceLoader();
// Get main ResourceLoader
$rl = MediaWikiServices::getInstance()->getResourceLoader();
$modules = [];

View file

@ -1,5 +1,7 @@
<?php
use MediaWiki\MediaWikiServices;
/**
* @author Sam Smith <samsmith@wikimedia.org>
*/
@ -7,7 +9,7 @@ class LessTestSuite extends PHPUnit_Framework_TestSuite {
public function __construct() {
parent::__construct();
$resourceLoader = new ResourceLoader();
$resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
foreach ( $resourceLoader->getModuleNames() as $name ) {
$module = $resourceLoader->getModule( $name );