23 $testConfig->set(
'ServiceWiringFiles', $globalConfig->get(
'ServiceWiringFiles' ) );
24 $testConfig->set(
'ConfigRegistry', $globalConfig->get(
'ConfigRegistry' ) );
33 if ( $config ===
null ) {
40 $wiringFiles = $config->get(
'ServiceWiringFiles' );
41 $instance->loadWiringFiles( $wiringFiles );
47 $services = MediaWikiServices::getInstance();
48 $this->assertInstanceOf( MediaWikiServices::class,
$services );
53 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
55 $this->assertInstanceOf( MediaWikiServices::class, $oldServices );
56 $this->assertNotSame( $oldServices, $newServices );
58 $theServices = MediaWikiServices::getInstance();
59 $this->assertSame( $theServices, $newServices );
61 MediaWikiServices::forceGlobalInstance( $oldServices );
63 $theServices = MediaWikiServices::getInstance();
64 $this->assertSame( $theServices, $oldServices );
69 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
71 $service1 = $this->createMock( SalvageableService::class );
72 $service1->expects( $this->never() )
73 ->method(
'salvage' );
75 $newServices->defineService(
77 function ()
use ( $service1 ) {
83 $newServices->getService(
'Test' );
85 MediaWikiServices::resetGlobalInstance( $this->
newTestConfig() );
86 $theServices = MediaWikiServices::getInstance();
90 $theServices->getService(
'Test' ),
91 'service definition should survive reset'
94 $this->assertNotSame( $theServices, $newServices );
95 $this->assertNotSame( $theServices, $oldServices );
97 MediaWikiServices::forceGlobalInstance( $oldServices );
102 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
104 $service1 = $this->createMock( SalvageableService::class );
105 $service1->expects( $this->never() )
106 ->method(
'salvage' );
108 $service2 = $this->createMock( SalvageableService::class );
109 $service2->expects( $this->once() )
110 ->method(
'salvage' )
114 $instantiatorReturnValues = [
119 $newServices->defineService(
121 function ()
use ( &$instantiatorReturnValues ) {
122 return array_shift( $instantiatorReturnValues );
127 $newServices->getService(
'Test' );
129 MediaWikiServices::resetGlobalInstance( $this->
newTestConfig(),
'quick' );
130 $theServices = MediaWikiServices::getInstance();
132 $this->assertSame( $service2, $theServices->getService(
'Test' ) );
134 $this->assertNotSame( $theServices, $newServices );
135 $this->assertNotSame( $theServices, $oldServices );
137 MediaWikiServices::forceGlobalInstance( $oldServices );
142 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
144 $lbFactory = $this->getMockBuilder( \
Wikimedia\Rdbms\LBFactorySimple::class )
145 ->disableOriginalConstructor()
148 $newServices->redefineService(
149 'DBLoadBalancerFactory',
150 function ()
use ( $lbFactory ) {
156 $newServices->getService(
'DBLoadBalancerFactory' );
158 MediaWikiServices::disableStorageBackend();
161 MediaWikiServices::getInstance()->getService(
'DBLoadBalancerFactory' );
162 $this->fail(
'DBLoadBalancerFactory should have been disabled' );
166 }
catch ( Throwable $ex ) {
167 $this->fail(
'ServiceDisabledException expected, caught ' . get_class( $ex ) );
170 MediaWikiServices::forceGlobalInstance( $oldServices );
171 $newServices->destroy();
174 $this->assertTrue(
true );
179 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
181 $service1 = $this->createMock( DestructibleService::class );
182 $service1->expects( $this->once() )
183 ->method(
'destroy' );
185 $service2 = $this->createMock( DestructibleService::class );
186 $service2->expects( $this->never() )
187 ->method(
'destroy' );
190 $instantiatorReturnValues = [
195 $newServices->defineService(
197 function ()
use ( &$instantiatorReturnValues ) {
198 return array_shift( $instantiatorReturnValues );
203 $oldTestService = $newServices->getService(
'Test' );
205 MediaWikiServices::resetChildProcessServices();
206 $finalServices = MediaWikiServices::getInstance();
208 $newTestService = $finalServices->getService(
'Test' );
209 $this->assertNotSame( $oldTestService, $newTestService );
211 MediaWikiServices::forceGlobalInstance( $oldServices );
220 function ()
use ( &$serviceCounter ) {
222 $service = $this->createMock(
Wikimedia\Services\DestructibleService::class );
223 $service->expects( $this->once() )->method(
'destroy' );
229 $services->resetServiceForTesting(
'Test' );
230 $this->assertEquals( 0, $serviceCounter,
'No service instance should be created yet.' );
232 $oldInstance =
$services->getService(
'Test' );
233 $this->assertEquals( 1, $serviceCounter,
'A service instance should exit now.' );
236 $services->resetServiceForTesting(
'Test' );
237 $newInstance =
$services->getService(
'Test' );
239 $this->assertNotSame( $oldInstance, $newInstance );
242 $newInstance->destroy();
251 $service = $this->createMock(
Wikimedia\Services\DestructibleService::class );
252 $service->expects( $this->never() )->method(
'destroy' );
257 $oldInstance =
$services->getService(
'Test' );
260 $services->resetServiceForTesting(
'Test',
false );
261 $newInstance =
$services->getService(
'Test' );
263 $this->assertNotSame( $oldInstance, $newInstance );
271 foreach ( $getServiceCases
as $name => $case ) {
272 if (
$name[0] ===
'_' ) {
276 list( $service, $class ) = $case;
277 $getterCases[
$name] = [
280 in_array( $service, $this->deprecatedServices )
291 if ( $isDeprecated ) {
296 $services = MediaWikiServices::getInstance();
298 $this->assertInstanceOf(
$type, $service );
303 $serviceList = require
"$IP/includes/ServiceWiring.php";
305 foreach ( $serviceList
as $name => $callback ) {
306 $fun =
new ReflectionFunction( $callback );
307 if ( !$fun->hasReturnType() ) {
308 throw new MWException(
'All service callbacks must have a return type defined, ' .
309 "none found for $name" );
321 $services = MediaWikiServices::getInstance();
324 $this->assertInstanceOf(
$type, $service );
331 $services = MediaWikiServices::getInstance();
337 $this->assertInternalType(
'object', $service );
344 $allServices = array_keys( require
"$IP/includes/ServiceWiring.php" );
347 array_diff( $allServices, $testedServices ),
348 'The following services have not been added to MediaWikiServicesTest::provideGetService'
353 $methods = (
new ReflectionClass( MediaWikiServices::class ) )
354 ->getMethods( ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC );
356 $names = array_map(
function ( $method ) {
357 return $method->getName();
359 $serviceNames = array_map(
function (
$name ) {
362 $names = array_values( array_filter( $names,
function (
$name )
use ( $serviceNames ) {
363 return in_array(
$name, $serviceNames );
366 $sortedNames = $names;
367 sort( $sortedNames );
369 $this->assertSame( $sortedNames, $names,
370 'Please keep service getters sorted alphabetically' );
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Accesses configuration settings from $GLOBALS.
A Config instance which stores all settings as a member variable.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
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
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
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
Allows to change the fields on the form that will be generated $name
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
Interface for configuration instances.