Go to the documentation of this file.
3 use Wikimedia\TestingAccessWrapper;
7 protected function setUp() {
11 'wgShowExceptionDetails' =>
true,
21 $resourceLoaderRegisterModulesHook =
false;
24 'ResourceLoaderRegisterModules' => [
26 $resourceLoaderRegisterModulesHook =
true;
31 $unused =
new ResourceLoader();
33 $resourceLoaderRegisterModulesHook,
34 'Hook ResourceLoaderRegisterModules called'
86 $this->setExpectedException(
MWException::class,
'ResourceLoader module info type error' );
95 $logger->expects( $this->once() )
96 ->method(
'warning' );
115 [
'test.foo',
'test.bar' ],
122 ->disableOriginalConstructor()
128 'FileModule object' => [
false,
131 'simple empty' => [
true,
134 'simple scripts' => [
true,
135 [
'scripts' =>
'example.js' ]
137 'simple scripts, raw and targets' => [
true, [
138 'scripts' => [
'a.js',
'b.js' ],
140 'targets' => [
'desktop',
'mobile' ],
142 'FileModule' => [
true,
145 'TestModule' => [
false,
148 'SkinModule (FileModule subclass)' => [
true,
151 'JqueryMsgModule (FileModule subclass)' => [
true, [
153 'scripts' =>
'example.js',
155 'WikiModule' => [
false, [
157 'scripts' => [
'MediaWiki:Example.js' ],
168 $rl->register(
'test', $module );
169 $this->assertSame( $expected, $rl->isFileModule(
'test' ) );
177 $this->assertSame(
false, $rl->isFileModule(
'unknown' ) );
186 $this->assertTrue( $rl->isModuleRegistered(
'test' ) );
187 $this->assertFalse( $rl->isModuleRegistered(
'test.unknown' ) );
195 $this->assertSame(
null, $rl->getModule(
'test' ) );
204 $this->assertInstanceOf(
206 $rl->getModule(
'test' )
214 $factory =
function (
array $info ) {
215 $this->assertArrayHasKey(
'kitten', $info );
220 $rl->register(
'test', [
'factory' => $factory,
'kitten' =>
'little ball of fur' ] );
221 $this->assertInstanceOf(
223 $rl->getModule(
'test' )
232 $rl->register(
'test', [] );
233 $this->assertInstanceOf(
235 $rl->getModule(
'test' ),
236 'Array-style module registrations default to FileModule'
245 $lc = $rl->getLessCompiler( [
'foo' =>
'2px',
'Foo' =>
'#eeeeee' ] );
246 $basePath = dirname( dirname( __DIR__ ) ) .
'/data/less';
247 $lc->SetImportDirs( [
248 "$basePath/common" =>
'',
250 $css = $lc->parseFile(
"$basePath/module/use-import-dir.less" )->getCss();
251 $this->assertStringEqualsFile(
"$basePath/module/styles.css",
$css );
257 'Example from makePackedModulesString doc comment',
258 [
'foo.bar',
'foo.baz',
'bar.baz',
'bar.quux' ],
259 'foo.bar,baz|bar.baz,quux',
262 'Example from expandModuleNames doc comment',
263 [
'jquery.foo',
'jquery.bar',
'jquery.ui.baz',
'jquery.ui.quux' ],
264 'jquery.foo,bar|jquery.ui.baz,quux',
267 'Regression fixed in r87497 (7fee86c38e) with dotless names',
268 [
'foo',
'bar',
'baz' ],
272 'Prefixless modules after a prefixed module',
273 [
'single.module',
'foobar',
'foobaz' ],
274 'single.module|foobar,foobaz',
278 [
'foo',
'foo.baz',
'baz.quux',
'foo.bar' ],
279 'foo|foo.baz,bar|baz.quux',
280 [
'foo',
'foo.baz',
'foo.bar',
'baz.quux' ],
290 $this->assertEquals( $packed, ResourceLoader::makePackedModulesString(
$modules ), $desc );
307 [
'foowiki',
'https://example.org/w/load.php',
'foowiki' ],
308 [
'foowiki', [
'loadScript' =>
'https://example.org/w/load.php' ],
'foowiki' ],
311 'foowiki' =>
'https://example.org/w/load.php',
312 'bazwiki' =>
'https://example.com/w/load.php',
315 [
'foowiki',
'bazwiki' ]
326 $rl =
new ResourceLoader;
327 $rl->addSource(
$name, $info );
328 if ( is_array( $expected ) ) {
330 $this->assertArrayHasKey(
$source, $rl->getSources() );
333 $this->assertArrayHasKey( $expected, $rl->getSources() );
341 $rl =
new ResourceLoader;
342 $this->setExpectedException(
345 $rl->addSource(
'foo',
'https://example.org/w/load.php' );
346 $rl->addSource(
'foo',
'https://example.com/w/load.php' );
353 $rl =
new ResourceLoader;
355 $rl->addSource(
'foo', [
'x' =>
'https://example.org/w/load.php' ] );
361 'title' =>
'Implement scripts, styles and messages',
363 'name' =>
'test.example',
364 'scripts' =>
'mw.example();',
365 'styles' => [
'css' => [
'.mw-example {}' ] ],
366 'messages' => [
'example' =>
'' ],
369 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
380 'title' =>
'Implement scripts',
382 'name' =>
'test.example',
383 'scripts' =>
'mw.example();',
386 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
391 'title' =>
'Implement styles',
393 'name' =>
'test.example',
395 'styles' => [
'css' => [
'.mw-example {}' ] ],
397 'expected' =>
'mw.loader.implement( "test.example", [], {
404 'title' =>
'Implement scripts and messages',
406 'name' =>
'test.example',
407 'scripts' =>
'mw.example();',
408 'messages' => [
'example' =>
'' ],
410 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
417 'title' =>
'Implement scripts and templates',
419 'name' =>
'test.example',
420 'scripts' =>
'mw.example();',
421 'templates' => [
'example.html' =>
'' ],
423 'expected' =>
'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
430 'title' =>
'Implement unwrapped user script',
433 'scripts' =>
'mw.example( 1 );',
436 'expected' =>
'mw.loader.implement( "user", "mw.example( 1 );" );',
449 'styles' => [],
'templates' => [],
'messages' =>
new XmlJsCode(
'{}' )
451 ResourceLoader::clearCache();
457 $rl->makeLoaderImplementScript(
459 ( $case[
'wrap'] && is_string( $case[
'scripts'] ) )
475 $rl->makeLoaderImplementScript(
489 'mw.loader.register( [
495 ResourceLoader::makeLoaderRegisterScript( [
496 [
'test.name',
'1234567' ],
498 'Nested array parameter'
502 'mw.loader.register( [
531 ResourceLoader::makeLoaderRegisterScript( [
532 [
'test.foo',
'100' , [],
null,
null ],
533 [
'test.bar',
'200', [
'test.unknown' ],
null ],
534 [
'test.baz',
'300', [
'test.quux',
'test.foo' ],
null ],
535 [
'test.quux',
'400', [],
null,
null,
'return true;' ],
537 'Compact dependency indexes'
546 'mw.loader.addSource( {
547 "local": "/w/load.php"
549 ResourceLoader::makeLoaderSourcesScript(
'local',
'/w/load.php' )
552 'mw.loader.addSource( {
553 "local": "/w/load.php"
555 ResourceLoader::makeLoaderSourcesScript( [
'local' =>
'/w/load.php' ] )
558 'mw.loader.addSource( {
559 "local": "/w/load.php",
560 "example": "https://example.org/w/load.php"
562 ResourceLoader::makeLoaderSourcesScript( [
563 'local' =>
'/w/load.php',
564 'example' =>
'https://example.org/w/load.php'
568 'mw.loader.addSource( [] );',
569 ResourceLoader::makeLoaderSourcesScript( [] )
576 'loadScript' =>
'//example.org/w/load.php',
577 'apiScript' =>
'//example.org/w/api.php',
580 'loadScript' =>
'//example.com/w/load.php',
581 'apiScript' =>
'//example.com/w/api.php',
591 $rl =
new ResourceLoader();
593 $rl->addSource( $sources );
594 foreach ( [
'examplewiki',
'example2wiki' ]
as $name ) {
595 $this->assertEquals( $rl->getLoadScript(
$name ), $sources[
$name][
'loadScript'] );
599 $rl->getLoadScript(
'thiswasneverreigstered' );
600 $this->assertTrue(
false,
'ResourceLoader::getLoadScript should have thrown an exception' );
602 $this->assertTrue(
true );
608 ->setMethods( [ $getter ] )
610 $mock->method( $getter )->will( $this->throwException(
611 new Exception(
'Ferry not found' )
618 ->setMethods( [
'getScript' ] )
620 $mock->method(
'getScript' )->willReturn( $script );
626 ->setMethods( [
'getStyles' ] )
628 $mock->method(
'getStyles' )->willReturn( [
'' => $styles ] );
638 ->setMethods( [
'outputErrorAndLog' ] )->getMock();
640 'foo' => self::getSimpleModuleMock(),
641 'ferry' => self::getFailFerryMock(),
642 'bar' => self::getSimpleModuleMock(),
648 $rl->getCombinedVersion(
$context, [] ),
653 ResourceLoader::makeHash( self::BLANK_VERSION ),
654 $rl->getCombinedVersion(
$context, [
'foo' ] ),
661 ResourceLoader::makeHash( self::BLANK_VERSION . self::BLANK_VERSION ),
662 $rl->getCombinedVersion(
$context, [
'foo',
'ferry',
'bar' ] ),
663 'compute foo+ferry+bar (T152266)'
673 'expected' =>
"foo()\n" .
'mw.loader.state( {
676 'minified' =>
"foo()\n" .
'mw.loader.state({"foo":"ready"});',
677 'message' =>
'Script without semi-colon',
684 'expected' =>
"foo()\nbar()\n" .
'mw.loader.state( {
688 'minified' =>
"foo()\nbar()\n" .
'mw.loader.state({"foo":"ready","bar":"ready"});',
689 'message' =>
'Two scripts without semi-colon',
693 'foo' =>
"foo()\n// bar();"
695 'expected' =>
"foo()\n// bar();\n" .
'mw.loader.state( {
698 'minified' =>
"foo()\n" .
'mw.loader.state({"foo":"ready"});',
699 'message' =>
'Script with semi-colon in comment (T162719)',
703 foreach ( $testcases
as $i => $case ) {
710 $ret[
"#$i (minified)"] = [
728 $modules = array_map(
function ( $script ) {
736 'modules' => implode(
'|', array_keys(
$modules ) ),
743 $this->assertSame( [], $rl->getErrors(),
'Errors' );
744 $this->assertEquals( $expected,
$response, $message ?:
'Response' );
753 [
'modules' =>
'',
'only' =>
'scripts' ],
758 $this->assertSame( [], $rl->getErrors(),
'Errors' );
759 $this->assertRegExp(
'/^\/\*.+no modules were requested.+\*\/$/ms',
$response );
779 'modules' =>
'foo|ferry|bar',
786 $this->
setLogger(
'exception',
new Psr\Log\NullLogger() );
789 $errors = $rl->getErrors();
791 $this->assertCount( 1, $errors );
792 $this->assertRegExp(
'/Ferry not found/', $errors[0] );
794 "foo();\nbar();\n" .
'mw.loader.state( {
819 'modules' =>
'foo|ferry|bar',
827 $this->
setLogger(
'exception',
new Psr\Log\NullLogger() );
830 $errors = $rl->getErrors();
832 $this->assertCount( 2, $errors );
833 $this->assertRegExp(
'/Ferry not found/', $errors[0] );
834 $this->assertRegExp(
'/Problem.+"ferry":\s*"error"/ms', $errors[1] );
850 'foo' => self::getSimpleModuleMock(
'foo();' ),
851 'ferry' => self::getFailFerryMock(),
852 'bar' => self::getSimpleModuleMock(
'bar();' ),
857 'modules' =>
'startup',
864 [
'foo',
'ferry',
'bar',
'startup' ],
865 $rl->getModuleNames(),
870 $this->
setLogger(
'exception',
new Psr\Log\NullLogger() );
872 $modules = [
'startup' => $rl->getModule(
'startup' ) ];
874 $errors = $rl->getErrors();
876 $this->assertRegExp(
'/Ferry not found/', $errors[0] );
877 $this->assertCount( 1, $errors );
879 '/isCompatible.*window\.RLQ/s',
881 'startup response undisrupted (T152266)'
884 '/register\([^)]+"ferry",\s*""/s',
886 'startup response registers broken module'
889 '/state\([^)]+"ferry":\s*"error"/s',
891 'startup response sets state to error'
904 ->setMethods( [
'getPreloadLinks' ] )->getMock();
905 $module->method(
'getPreloadLinks' )->willReturn( [
906 'https://example.org/script.js' => [
'as' =>
'script' ],
914 [
'modules' =>
'foo',
'only' =>
'scripts' ],
918 $modules = [
'foo' => $rl->getModule(
'foo' ) ];
920 $extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders;
924 'Link: <https://example.org/script.js>;rel=preload;as=script'
938 ->setMethods( [
'getPreloadLinks' ] )->getMock();
939 $foo->method(
'getPreloadLinks' )->willReturn( [
940 'https://example.org/script.js' => [
'as' =>
'script' ],
944 ->setMethods( [
'getPreloadLinks' ] )->getMock();
945 $bar->method(
'getPreloadLinks' )->willReturn( [
946 '/example.png' => [
'as' =>
'image' ],
947 '/example.jpg' => [
'as' =>
'image' ],
951 $rl->register( [
'foo' => $foo,
'bar' => $bar ] );
953 [
'modules' =>
'foo|bar',
'only' =>
'scripts' ],
957 $modules = [
'foo' => $rl->getModule(
'foo' ),
'bar' => $rl->getModule(
'bar' ) ];
959 $extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders;
962 'Link: <https://example.org/script.js>;rel=preload;as=script',
963 'Link: </example.png>;rel=preload;as=image,</example.jpg>;rel=preload;as=image'
976 'tryRespondNotModified',
977 'sendResponseHeaders',
978 'measureResponseTime',
983 $rl->expects( $this->once() )->method(
'measureResponseTime' );
984 $this->expectOutputRegex(
'/no modules were requested/' );
996 'measureResponseTime',
997 'tryRespondNotModified',
998 'sendResponseHeaders',
999 'makeModuleResponse',
1002 $rl->register(
'test', $module );
1004 [
'modules' =>
'test',
'only' =>
null ],
1008 $rl->expects( $this->once() )->method(
'makeModuleResponse' )
1009 ->with(
$context, [
'test' => $module ] )
1010 ->willReturn(
'implement_foo;' );
1011 $this->expectOutputRegex(
'/^implement_foo;/' );
1023 'measureResponseTime',
1024 'preloadModuleInfo',
1025 'getCombinedVersion',
1026 'tryRespondNotModified',
1027 'makeModuleResponse',
1028 'sendResponseHeaders',
1031 $rl->register(
'test', $module );
1034 $this->
setLogger(
'exception',
new Psr\Log\NullLogger() );
1036 $rl->expects( $this->once() )->method(
'preloadModuleInfo' )
1037 ->willThrowException(
new Exception(
'Preload error' ) );
1038 $rl->expects( $this->once() )->method(
'getCombinedVersion' )
1039 ->willThrowException(
new Exception(
'Version error' ) );
1040 $rl->expects( $this->once() )->method(
'makeModuleResponse' )
1041 ->with(
$context, [
'test' => $module ] )
1042 ->willReturn(
'foo;' );
1044 $this->expectOutputRegex(
'/^\/\*.+Preload error.+Version error.+\*\/.*foo;/ms' );
1054 ->setMethods( [
'timing' ] )->getMock();
1055 $this->
setService(
'StatsdDataFactory', $stats );
1057 $stats->expects( $this->once() )->method(
'timing' )
1058 ->with(
'resourceloader.responseTime', $this->
anything() );
1061 $timing->mark(
'requestShutdown' );
1063 $rl->measureResponseTime( $timing );
testIsFileModule( $expected, $module)
provideTestIsFileModule ResourceLoader::isFileModule
testRespondSimple()
ResourceLoader::respond.
testMakePackedModulesString( $desc, $modules, $packed)
providePackedModules ResourceLoader::makePackedModulesString
testConstructRegistrationHook()
Ensure the ResourceLoaderRegisterModules hook is called.
testRegisterInvalidType()
ResourceLoader::register.
testGetModuleClassDefault()
ResourceLoader::getModule.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
getSimpleStyleModuleMock( $styles='')
testRegisterDuplicate()
ResourceLoader::register.
testLessImportDirs()
ResourceLoader::getLessCompiler.
testIsFileModuleUnknown()
ResourceLoader::isFileModule.
testMakeModuleResponseError()
Verify that when building module content in a load.php response, an exception from one module will no...
testRegisterInvalidName()
ResourceLoader::register.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break anything
static expandModuleNames( $modules)
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like ‘[ 'jq...
testMakeLoaderImplementScriptInvalid()
ResourceLoader::makeLoaderImplementScript.
testGetModuleNames()
ResourceLoader::getModuleNames.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext such as when responding to a resource loader request or generating HTML output & $resourceLoader
provideTestIsFileModule()
testGetModuleClass()
ResourceLoader::getModule.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
A wrapper class which causes Xml::encodeJsVar() and Xml::encodeJsCall() to interpret a given string a...
static provideAddSource()
testMakeLoaderSourcesScript()
ResourceLoader::makeLoaderSourcesScript.
An interface to help developers measure the performance of their applications.
testRespondInternalFailures()
ResourceLoader::respond.
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
testAddSourceDupe()
ResourceLoader::addSource.
testMakeLoaderRegisterScript()
ResourceLoader::makeLoaderRegisterScript.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
testGetLoadScript()
ResourceLoader::getLoadScript.
testRegisterValidArray()
ResourceLoader::register ResourceLoader::getModule.
static provideMakeModuleResponseConcat()
Allows to change the fields on the form that will be generated $name
testRegisterEmptyString()
ResourceLoader::register.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
testMakeModuleResponseExtraHeaders()
Integration test for modules sending extra HTTP response headers.
testMakeLoaderImplementScript( $case)
provideLoaderImplement ResourceLoader::makeLoaderImplementScript ResourceLoader::trimArray
testMakeModuleResponseConcat( $scripts, $expected, $debug, $message=null)
Verify how multiple scripts and mw.loader.state() calls are concatenated.
testRegisterValidObject()
ResourceLoader::register ResourceLoader::getModule.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
testRespondEmpty()
ResourceLoader::respond.
testMakeModuleResponseErrorCSS()
Verify that exceptions in PHP for one module will not break others (stylesheet response).
getSimpleModuleMock( $script='')
this hook is for auditing only $response
static doUpdates( $mode='run', $stage=self::ALL)
Do any deferred updates and clear the list.
testExpandModuleNames( $desc, $modules, $packed, $unpacked=null)
providePackedModules ResourceLoaderContext::expandModuleNames
testGetCombinedVersion()
ResourceLoader::getCombinedVersion.
testAddSourceInvalid()
ResourceLoader::addSource.
testMakeModuleResponseExtraHeadersMulti()
ResourceLoaderModule::getHeaders ResourceLoaderModule::buildContent ResourceLoader::makeModuleRespons...
testGetModuleFactory()
ResourceLoader::getModule.
testMeasureResponseTime()
ResourceLoader::measureResponseTime.
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
testIsModuleRegistered()
ResourceLoader::isModuleRegistered.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
getFailFerryMock( $getter='getScript')
static providePackedModules()
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
testGetModuleUnknown()
ResourceLoader::getModule.
testMakeModuleResponseStartupError()
Verify that when building the startup module response, an exception from one module class will not br...
static provideLoaderImplement()
testMakeModuleResponseEmpty()
ResourceLoader::makeModuleResponse.
testAddSource( $name, $info, $expected)
provideAddSource ResourceLoader::addSource ResourceLoader::getSources