MediaWiki  1.30.0
MediaWikiServices.php
Go to the documentation of this file.
1 <?php
2 namespace MediaWiki;
3 
4 use Config;
11 use Hooks;
26 use Parser;
41 
78 class MediaWikiServices extends ServiceContainer {
79 
83  private static $instance = null;
84 
99  public static function getInstance() {
100  if ( self::$instance === null ) {
101  // NOTE: constructing GlobalVarConfig here is not particularly pretty,
102  // but some information from the global scope has to be injected here,
103  // even if it's just a file name or database credentials to load
104  // configuration from.
105  $bootstrapConfig = new GlobalVarConfig();
106  self::$instance = self::newInstance( $bootstrapConfig, 'load' );
107  }
108 
109  return self::$instance;
110  }
111 
125  public static function forceGlobalInstance( MediaWikiServices $services ) {
126  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
127  throw new MWException( __METHOD__ . ' must not be used outside unit tests.' );
128  }
129 
130  $old = self::getInstance();
131  self::$instance = $services;
132 
133  return $old;
134  }
135 
175  public static function resetGlobalInstance( Config $bootstrapConfig = null, $quick = '' ) {
176  if ( self::$instance === null ) {
177  // no global instance yet, nothing to reset
178  return;
179  }
180 
181  self::failIfResetNotAllowed( __METHOD__ );
182 
183  if ( $bootstrapConfig === null ) {
184  $bootstrapConfig = self::$instance->getBootstrapConfig();
185  }
186 
187  $oldInstance = self::$instance;
188 
189  self::$instance = self::newInstance( $bootstrapConfig, 'load' );
190  self::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
191 
192  if ( $quick === 'quick' ) {
193  self::$instance->salvage( $oldInstance );
194  } else {
195  $oldInstance->destroy();
196  }
197  }
198 
206  private function salvage( self $other ) {
207  foreach ( $this->getServiceNames() as $name ) {
208  // The service could be new in the new instance and not registered in the
209  // other instance (e.g. an extension that was loaded after the instantiation of
210  // the other instance. Skip this service in this case. See T143974
211  try {
212  $oldService = $other->peekService( $name );
213  } catch ( NoSuchServiceException $e ) {
214  continue;
215  }
216 
217  if ( $oldService instanceof SalvageableService ) {
219  $newService = $this->getService( $name );
220  $newService->salvage( $oldService );
221  }
222  }
223 
224  $other->destroy();
225  }
226 
242  private static function newInstance( Config $bootstrapConfig, $loadWiring = '' ) {
243  $instance = new self( $bootstrapConfig );
244 
245  // Load the default wiring from the specified files.
246  if ( $loadWiring === 'load' ) {
247  $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
248  $instance->loadWiringFiles( $wiringFiles );
249  }
250 
251  // Provide a traditional hook point to allow extensions to configure services.
252  Hooks::run( 'MediaWikiServices', [ $instance ] );
253 
254  return $instance;
255  }
256 
272  public static function disableStorageBackend() {
273  // TODO: also disable some Caches, JobQueues, etc
274  $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
275  $services = self::getInstance();
276 
277  foreach ( $destroy as $name ) {
278  $services->disableService( $name );
279  }
280 
282  }
283 
296  public static function resetChildProcessServices() {
297  // NOTE: for now, just reset everything. Since we don't know the interdependencies
298  // between services, we can't do this more selectively at this time.
299  self::resetGlobalInstance();
300 
301  // Child, reseed because there is no bug in PHP:
302  // https://bugs.php.net/bug.php?id=42465
303  mt_srand( getmypid() );
304  }
305 
327  public function resetServiceForTesting( $name, $destroy = true ) {
328  if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
329  throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
330  }
331 
332  $this->resetService( $name, $destroy );
333  }
334 
362  public static function failIfResetNotAllowed( $method ) {
363  if ( !defined( 'MW_PHPUNIT_TEST' )
364  && !defined( 'MW_PARSER_TEST' )
365  && !defined( 'MEDIAWIKI_INSTALL' )
366  && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
367  && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
368  ) {
369  throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
370  }
371  }
372 
378  public function __construct( Config $config ) {
379  parent::__construct();
380 
381  // Register the given Config object as the bootstrap config service.
382  $this->defineService( 'BootstrapConfig', function () use ( $config ) {
383  return $config;
384  } );
385  }
386 
387  // CONVENIENCE GETTERS ////////////////////////////////////////////////////
388 
402  public function getBootstrapConfig() {
403  return $this->getService( 'BootstrapConfig' );
404  }
405 
410  public function getConfigFactory() {
411  return $this->getService( 'ConfigFactory' );
412  }
413 
421  public function getMainConfig() {
422  return $this->getService( 'MainConfig' );
423  }
424 
429  public function getSiteLookup() {
430  return $this->getService( 'SiteLookup' );
431  }
432 
437  public function getSiteStore() {
438  return $this->getService( 'SiteStore' );
439  }
440 
445  public function getInterwikiLookup() {
446  return $this->getService( 'InterwikiLookup' );
447  }
448 
453  public function getStatsdDataFactory() {
454  return $this->getService( 'StatsdDataFactory' );
455  }
456 
461  public function getEventRelayerGroup() {
462  return $this->getService( 'EventRelayerGroup' );
463  }
464 
469  public function newSearchEngine() {
470  // New engine object every time, since they keep state
471  return $this->getService( 'SearchEngineFactory' )->create();
472  }
473 
478  public function getSearchEngineFactory() {
479  return $this->getService( 'SearchEngineFactory' );
480  }
481 
486  public function getSearchEngineConfig() {
487  return $this->getService( 'SearchEngineConfig' );
488  }
489 
494  public function getSkinFactory() {
495  return $this->getService( 'SkinFactory' );
496  }
497 
502  public function getDBLoadBalancerFactory() {
503  return $this->getService( 'DBLoadBalancerFactory' );
504  }
505 
510  public function getDBLoadBalancer() {
511  return $this->getService( 'DBLoadBalancer' );
512  }
513 
518  public function getWatchedItemStore() {
519  return $this->getService( 'WatchedItemStore' );
520  }
521 
526  public function getWatchedItemQueryService() {
527  return $this->getService( 'WatchedItemQueryService' );
528  }
529 
534  public function getCryptRand() {
535  return $this->getService( 'CryptRand' );
536  }
537 
542  public function getCryptHKDF() {
543  return $this->getService( 'CryptHKDF' );
544  }
545 
550  public function getMediaHandlerFactory() {
551  return $this->getService( 'MediaHandlerFactory' );
552  }
553 
558  public function getMimeAnalyzer() {
559  return $this->getService( 'MimeAnalyzer' );
560  }
561 
566  public function getProxyLookup() {
567  return $this->getService( 'ProxyLookup' );
568  }
569 
574  public function getParser() {
575  return $this->getService( 'Parser' );
576  }
577 
582  public function getParserCache() {
583  return $this->getService( 'ParserCache' );
584  }
585 
590  public function getGenderCache() {
591  return $this->getService( 'GenderCache' );
592  }
593 
598  public function getLinkCache() {
599  return $this->getService( 'LinkCache' );
600  }
601 
606  public function getLinkRendererFactory() {
607  return $this->getService( 'LinkRendererFactory' );
608  }
609 
617  public function getLinkRenderer() {
618  return $this->getService( 'LinkRenderer' );
619  }
620 
625  public function getTitleFormatter() {
626  return $this->getService( 'TitleFormatter' );
627  }
628 
633  public function getTitleParser() {
634  return $this->getService( 'TitleParser' );
635  }
636 
641  public function getMainObjectStash() {
642  return $this->getService( 'MainObjectStash' );
643  }
644 
649  public function getMainWANObjectCache() {
650  return $this->getService( 'MainWANObjectCache' );
651  }
652 
657  public function getLocalServerObjectCache() {
658  return $this->getService( 'LocalServerObjectCache' );
659  }
660 
665  public function getVirtualRESTServiceClient() {
666  return $this->getService( 'VirtualRESTServiceClient' );
667  }
668 
673  public function getConfiguredReadOnlyMode() {
674  return $this->getService( 'ConfiguredReadOnlyMode' );
675  }
676 
681  public function getReadOnlyMode() {
682  return $this->getService( 'ReadOnlyMode' );
683  }
684 
689  public function getShellCommandFactory() {
690  return $this->getService( 'ShellCommandFactory' );
691  }
692 
694  // NOTE: When adding a service getter here, don't forget to add a test
695  // case for it in MediaWikiServicesTest::provideGetters() and in
696  // MediaWikiServicesTest::provideGetService()!
698 
699 }
EventRelayerGroup
Factory class for spawning EventRelayer objects using configuration.
Definition: EventRelayerGroup.php:28
ObjectCache\clear
static clear()
Clear all the cached instances.
Definition: ObjectCache.php:400
ObjectCache
Functions to get cache objects.
Definition: ObjectCache.php:80
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:34
MediaWiki\MediaWikiServices\getShellCommandFactory
getShellCommandFactory()
Definition: MediaWikiServices.php:689
MediaWiki\MediaWikiServices\getInterwikiLookup
getInterwikiLookup()
Definition: MediaWikiServices.php:445
MediaWiki\MediaWikiServices\getSearchEngineConfig
getSearchEngineConfig()
Definition: MediaWikiServices.php:486
MediaWiki\MediaWikiServices\getSiteLookup
getSiteLookup()
Definition: MediaWikiServices.php:429
MediaWiki\MediaWikiServices\getDBLoadBalancer
getDBLoadBalancer()
Definition: MediaWikiServices.php:510
MediaWiki\Services\ServiceContainer
ServiceContainer provides a generic service to manage named services using lazy instantiation based o...
Definition: ServiceContainer.php:46
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:78
MimeAnalyzer
Implements functions related to MIME types such as detection and mapping to file extension.
Definition: MimeAnalyzer.php:30
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:42
MediaWiki\MediaWikiServices\getBootstrapConfig
getBootstrapConfig()
Returns the Config object containing the bootstrap configuration.
Definition: MediaWikiServices.php:402
MediaWiki\MediaWikiServices\getCryptRand
getCryptRand()
Definition: MediaWikiServices.php:534
use
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
Definition: MIT-LICENSE.txt:10
GenderCache
Caches user genders when needed to use correct namespace aliases.
Definition: GenderCache.php:31
MediaWiki\Services\NoSuchServiceException
Exception thrown when the requested service is not known.
Definition: NoSuchServiceException.php:33
MediaWiki\Linker\LinkRendererFactory
Factory to create LinkRender objects.
Definition: LinkRendererFactory.php:32
SearchEngineFactory
Factory class for SearchEngine.
Definition: SearchEngineFactory.php:9
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
MediaWiki\MediaWikiServices\getGenderCache
getGenderCache()
Definition: MediaWikiServices.php:590
MediaWiki\MediaWikiServices\getEventRelayerGroup
getEventRelayerGroup()
Definition: MediaWikiServices.php:461
SiteLookup
Definition: SiteLookup.php:28
php
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
Definition: injection.txt:35
MediaHandlerFactory
Class to construct MediaHandler objects.
Definition: MediaHandlerFactory.php:29
MediaWiki\MediaWikiServices\getProxyLookup
getProxyLookup()
Definition: MediaWikiServices.php:566
MediaWiki\MediaWikiServices\getSkinFactory
getSkinFactory()
Definition: MediaWikiServices.php:494
SiteStore
Definition: SiteStore.php:29
MediaWiki\MediaWikiServices\failIfResetNotAllowed
static failIfResetNotAllowed( $method)
Convenience method that throws an exception unless it is called during a phase in which resetting of ...
Definition: MediaWikiServices.php:362
MWException
MediaWiki exception.
Definition: MWException.php:26
MediaWiki\MediaWikiServices\resetServiceForTesting
resetServiceForTesting( $name, $destroy=true)
Resets the given service for testing purposes.
Definition: MediaWikiServices.php:327
MediaWiki\MediaWikiServices\getCryptHKDF
getCryptHKDF()
Definition: MediaWikiServices.php:542
MediaWiki\MediaWikiServices\resetGlobalInstance
static resetGlobalInstance(Config $bootstrapConfig=null, $quick='')
Creates a new instance of MediaWikiServices and sets it as the global default instance.
Definition: MediaWikiServices.php:175
MediaWiki\MediaWikiServices\getWatchedItemStore
getWatchedItemStore()
Definition: MediaWikiServices.php:518
MediaWiki\MediaWikiServices\getMediaHandlerFactory
getMediaHandlerFactory()
Definition: MediaWikiServices.php:550
CryptHKDF
Definition: CryptHKDF.php:33
MediaWiki\MediaWikiServices\getWatchedItemQueryService
getWatchedItemQueryService()
Definition: MediaWikiServices.php:526
MediaWiki\MediaWikiServices\getTitleParser
getTitleParser()
Definition: MediaWikiServices.php:633
WatchedItemQueryService
Definition: WatchedItemQueryService.php:18
ProxyLookup
Definition: ProxyLookup.php:27
VirtualRESTServiceClient
Virtual HTTP service client loosely styled after a Virtual File System.
Definition: VirtualRESTServiceClient.php:45
MediaWiki\Interwiki\InterwikiLookup
Service interface for looking up Interwiki records.
Definition: InterwikiLookup.php:31
MediaWiki
A helper class for throttling authentication attempts.
MediaWiki\MediaWikiServices\newSearchEngine
newSearchEngine()
Definition: MediaWikiServices.php:469
MediaWiki\MediaWikiServices\getMainConfig
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
Definition: MediaWikiServices.php:421
MediaWiki\MediaWikiServices\getSearchEngineFactory
getSearchEngineFactory()
Definition: MediaWikiServices.php:478
GlobalVarConfig
Accesses configuration settings from $GLOBALS.
Definition: GlobalVarConfig.php:28
TitleParser
A title parser service for MediaWiki.
Definition: TitleParser.php:34
MediaWiki\MediaWikiServices\disableStorageBackend
static disableStorageBackend()
Disables all storage layer services.
Definition: MediaWikiServices.php:272
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title 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
Definition: hooks.txt:2198
MediaWiki\MediaWikiServices\getDBLoadBalancerFactory
getDBLoadBalancerFactory()
Definition: MediaWikiServices.php:502
MediaWiki\MediaWikiServices\newInstance
static newInstance(Config $bootstrapConfig, $loadWiring='')
Creates a new MediaWikiServices instance and initializes it according to the given $bootstrapConfig.
Definition: MediaWikiServices.php:242
MediaWiki\MediaWikiServices\getTitleFormatter
getTitleFormatter()
Definition: MediaWikiServices.php:625
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
MediaWiki\MediaWikiServices\getParserCache
getParserCache()
Definition: MediaWikiServices.php:582
MediaWiki\MediaWikiServices\salvage
salvage(self $other)
Salvages the state of any salvageable service instances in $other.
Definition: MediaWikiServices.php:206
MediaWiki\MediaWikiServices\__construct
__construct(Config $config)
Definition: MediaWikiServices.php:378
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:99
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2141
MediaWiki\MediaWikiServices\getVirtualRESTServiceClient
getVirtualRESTServiceClient()
Definition: MediaWikiServices.php:665
SkinFactory
Factory class to create Skin objects.
Definition: SkinFactory.php:31
MediaWiki\MediaWikiServices\getConfigFactory
getConfigFactory()
Definition: MediaWikiServices.php:410
MediaWiki\Shell\CommandFactory
Factory facilitating dependency injection for Command.
Definition: CommandFactory.php:31
MediaWiki\Services\SalvageableService
SalvageableService defines an interface for services that are able to salvage state from a previous i...
Definition: SalvageableService.php:35
SearchEngine
Contain a class for special pages.
Definition: SearchEngine.php:34
WatchedItemStore
Storage layer class for WatchedItems.
Definition: WatchedItemStore.php:23
MediaWiki\MediaWikiServices\getMainWANObjectCache
getMainWANObjectCache()
Definition: MediaWikiServices.php:649
MediaWiki\MediaWikiServices\getMimeAnalyzer
getMimeAnalyzer()
Definition: MediaWikiServices.php:558
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:10
MediaWiki\MediaWikiServices\getSiteStore
getSiteStore()
Definition: MediaWikiServices.php:437
MediaWiki\MediaWikiServices\getLocalServerObjectCache
getLocalServerObjectCache()
Definition: MediaWikiServices.php:657
MediaWiki\MediaWikiServices\getConfiguredReadOnlyMode
getConfiguredReadOnlyMode()
Definition: MediaWikiServices.php:673
MediaWiki\MediaWikiServices\getParser
getParser()
Definition: MediaWikiServices.php:574
MediaWiki\MediaWikiServices\resetChildProcessServices
static resetChildProcessServices()
Resets any services that may have become stale after a child process returns from after pcntl_fork().
Definition: MediaWikiServices.php:296
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:35
Wikimedia\Rdbms\LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:38
as
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
Definition: distributors.txt:9
ParserCache
Definition: ParserCache.php:30
ConfigFactory
Factory class to create Config objects.
Definition: ConfigFactory.php:31
SearchEngineConfig
Configuration handling class for SearchEngine.
Definition: SearchEngineConfig.php:9
MediaWiki\MediaWikiServices\getLinkRendererFactory
getLinkRendererFactory()
Definition: MediaWikiServices.php:606
MediaWiki\MediaWikiServices\getLinkRenderer
getLinkRenderer()
LinkRenderer instance that can be used if no custom options are needed.
Definition: MediaWikiServices.php:617
MediaWiki\MediaWikiServices\getMainObjectStash
getMainObjectStash()
Definition: MediaWikiServices.php:641
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
MediaWiki\MediaWikiServices\getLinkCache
getLinkCache()
Definition: MediaWikiServices.php:598
CryptRand
Definition: CryptRand.php:28
MediaWiki\MediaWikiServices\getReadOnlyMode
getReadOnlyMode()
Definition: MediaWikiServices.php:681
Hooks
Hooks class.
Definition: Hooks.php:34
MediaWiki\MediaWikiServices\forceGlobalInstance
static forceGlobalInstance(MediaWikiServices $services)
Replaces the global MediaWikiServices instance.
Definition: MediaWikiServices.php:125
MediaWiki\MediaWikiServices\getStatsdDataFactory
getStatsdDataFactory()
Definition: MediaWikiServices.php:453