MediaWiki  1.29.2
MediaWikiServices.php
Go to the documentation of this file.
1 <?php
2 namespace MediaWiki;
3 
4 use Config;
11 use Hooks;
14 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
25 use Parser;
39 
76 class MediaWikiServices extends ServiceContainer {
77 
81  private static $instance = null;
82 
97  public static function getInstance() {
98  if ( self::$instance === null ) {
99  // NOTE: constructing GlobalVarConfig here is not particularly pretty,
100  // but some information from the global scope has to be injected here,
101  // even if it's just a file name or database credentials to load
102  // configuration from.
103  $bootstrapConfig = new GlobalVarConfig();
104  self::$instance = self::newInstance( $bootstrapConfig, 'load' );
105  }
106 
107  return self::$instance;
108  }
109 
123  public static function forceGlobalInstance( MediaWikiServices $services ) {
124  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
125  throw new MWException( __METHOD__ . ' must not be used outside unit tests.' );
126  }
127 
128  $old = self::getInstance();
129  self::$instance = $services;
130 
131  return $old;
132  }
133 
173  public static function resetGlobalInstance( Config $bootstrapConfig = null, $quick = '' ) {
174  if ( self::$instance === null ) {
175  // no global instance yet, nothing to reset
176  return;
177  }
178 
179  self::failIfResetNotAllowed( __METHOD__ );
180 
181  if ( $bootstrapConfig === null ) {
182  $bootstrapConfig = self::$instance->getBootstrapConfig();
183  }
184 
185  $oldInstance = self::$instance;
186 
187  self::$instance = self::newInstance( $bootstrapConfig, 'load' );
188  self::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
189 
190  if ( $quick === 'quick' ) {
191  self::$instance->salvage( $oldInstance );
192  } else {
193  $oldInstance->destroy();
194  }
195  }
196 
204  private function salvage( self $other ) {
205  foreach ( $this->getServiceNames() as $name ) {
206  // The service could be new in the new instance and not registered in the
207  // other instance (e.g. an extension that was loaded after the instantiation of
208  // the other instance. Skip this service in this case. See T143974
209  try {
210  $oldService = $other->peekService( $name );
211  } catch ( NoSuchServiceException $e ) {
212  continue;
213  }
214 
215  if ( $oldService instanceof SalvageableService ) {
217  $newService = $this->getService( $name );
218  $newService->salvage( $oldService );
219  }
220  }
221 
222  $other->destroy();
223  }
224 
240  private static function newInstance( Config $bootstrapConfig, $loadWiring = '' ) {
241  $instance = new self( $bootstrapConfig );
242 
243  // Load the default wiring from the specified files.
244  if ( $loadWiring === 'load' ) {
245  $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
246  $instance->loadWiringFiles( $wiringFiles );
247  }
248 
249  // Provide a traditional hook point to allow extensions to configure services.
250  Hooks::run( 'MediaWikiServices', [ $instance ] );
251 
252  return $instance;
253  }
254 
270  public static function disableStorageBackend() {
271  // TODO: also disable some Caches, JobQueues, etc
272  $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
273  $services = self::getInstance();
274 
275  foreach ( $destroy as $name ) {
276  $services->disableService( $name );
277  }
278 
280  }
281 
294  public static function resetChildProcessServices() {
295  // NOTE: for now, just reset everything. Since we don't know the interdependencies
296  // between services, we can't do this more selectively at this time.
297  self::resetGlobalInstance();
298 
299  // Child, reseed because there is no bug in PHP:
300  // https://bugs.php.net/bug.php?id=42465
301  mt_srand( getmypid() );
302  }
303 
325  public function resetServiceForTesting( $name, $destroy = true ) {
326  if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
327  throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
328  }
329 
330  $this->resetService( $name, $destroy );
331  }
332 
360  public static function failIfResetNotAllowed( $method ) {
361  if ( !defined( 'MW_PHPUNIT_TEST' )
362  && !defined( 'MW_PARSER_TEST' )
363  && !defined( 'MEDIAWIKI_INSTALL' )
364  && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
365  && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
366  ) {
367  throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
368  }
369  }
370 
376  public function __construct( Config $config ) {
377  parent::__construct();
378 
379  // Register the given Config object as the bootstrap config service.
380  $this->defineService( 'BootstrapConfig', function() use ( $config ) {
381  return $config;
382  } );
383  }
384 
385  // CONVENIENCE GETTERS ////////////////////////////////////////////////////
386 
400  public function getBootstrapConfig() {
401  return $this->getService( 'BootstrapConfig' );
402  }
403 
408  public function getConfigFactory() {
409  return $this->getService( 'ConfigFactory' );
410  }
411 
419  public function getMainConfig() {
420  return $this->getService( 'MainConfig' );
421  }
422 
427  public function getSiteLookup() {
428  return $this->getService( 'SiteLookup' );
429  }
430 
435  public function getSiteStore() {
436  return $this->getService( 'SiteStore' );
437  }
438 
443  public function getInterwikiLookup() {
444  return $this->getService( 'InterwikiLookup' );
445  }
446 
451  public function getStatsdDataFactory() {
452  return $this->getService( 'StatsdDataFactory' );
453  }
454 
459  public function getEventRelayerGroup() {
460  return $this->getService( 'EventRelayerGroup' );
461  }
462 
467  public function newSearchEngine() {
468  // New engine object every time, since they keep state
469  return $this->getService( 'SearchEngineFactory' )->create();
470  }
471 
476  public function getSearchEngineFactory() {
477  return $this->getService( 'SearchEngineFactory' );
478  }
479 
484  public function getSearchEngineConfig() {
485  return $this->getService( 'SearchEngineConfig' );
486  }
487 
492  public function getSkinFactory() {
493  return $this->getService( 'SkinFactory' );
494  }
495 
500  public function getDBLoadBalancerFactory() {
501  return $this->getService( 'DBLoadBalancerFactory' );
502  }
503 
508  public function getDBLoadBalancer() {
509  return $this->getService( 'DBLoadBalancer' );
510  }
511 
516  public function getWatchedItemStore() {
517  return $this->getService( 'WatchedItemStore' );
518  }
519 
524  public function getWatchedItemQueryService() {
525  return $this->getService( 'WatchedItemQueryService' );
526  }
527 
532  public function getCryptRand() {
533  return $this->getService( 'CryptRand' );
534  }
535 
540  public function getCryptHKDF() {
541  return $this->getService( 'CryptHKDF' );
542  }
543 
548  public function getMediaHandlerFactory() {
549  return $this->getService( 'MediaHandlerFactory' );
550  }
551 
556  public function getMimeAnalyzer() {
557  return $this->getService( 'MimeAnalyzer' );
558  }
559 
564  public function getProxyLookup() {
565  return $this->getService( 'ProxyLookup' );
566  }
567 
572  public function getParser() {
573  return $this->getService( 'Parser' );
574  }
575 
580  public function getGenderCache() {
581  return $this->getService( 'GenderCache' );
582  }
583 
588  public function getLinkCache() {
589  return $this->getService( 'LinkCache' );
590  }
591 
596  public function getLinkRendererFactory() {
597  return $this->getService( 'LinkRendererFactory' );
598  }
599 
607  public function getLinkRenderer() {
608  return $this->getService( 'LinkRenderer' );
609  }
610 
615  public function getTitleFormatter() {
616  return $this->getService( 'TitleFormatter' );
617  }
618 
623  public function getTitleParser() {
624  return $this->getService( 'TitleParser' );
625  }
626 
631  public function getMainObjectStash() {
632  return $this->getService( 'MainObjectStash' );
633  }
634 
639  public function getMainWANObjectCache() {
640  return $this->getService( 'MainWANObjectCache' );
641  }
642 
647  public function getLocalServerObjectCache() {
648  return $this->getService( 'LocalServerObjectCache' );
649  }
650 
655  public function getVirtualRESTServiceClient() {
656  return $this->getService( 'VirtualRESTServiceClient' );
657  }
658 
663  public function getConfiguredReadOnlyMode() {
664  return $this->getService( 'ConfiguredReadOnlyMode' );
665  }
666 
671  public function getReadOnlyMode() {
672  return $this->getService( 'ReadOnlyMode' );
673  }
674 
676  // NOTE: When adding a service getter here, don't forget to add a test
677  // case for it in MediaWikiServicesTest::provideGetters() and in
678  // MediaWikiServicesTest::provideGetService()!
680 
681 }
EventRelayerGroup
Factory class for spawning EventRelayer objects using configuration.
Definition: EventRelayerGroup.php:10
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\getInterwikiLookup
getInterwikiLookup()
Definition: MediaWikiServices.php:443
MediaWiki\MediaWikiServices\getSearchEngineConfig
getSearchEngineConfig()
Definition: MediaWikiServices.php:484
MediaWiki\MediaWikiServices\getSiteLookup
getSiteLookup()
Definition: MediaWikiServices.php:427
MediaWiki\MediaWikiServices\getDBLoadBalancer
getDBLoadBalancer()
Definition: MediaWikiServices.php:508
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:76
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:400
MediaWiki\MediaWikiServices\getCryptRand
getCryptRand()
Definition: MediaWikiServices.php:532
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:304
MediaWiki\MediaWikiServices\getGenderCache
getGenderCache()
Definition: MediaWikiServices.php:580
MediaWiki\MediaWikiServices\getEventRelayerGroup
getEventRelayerGroup()
Definition: MediaWikiServices.php:459
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:564
MediaWiki\MediaWikiServices\getSkinFactory
getSkinFactory()
Definition: MediaWikiServices.php:492
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:360
MWException
MediaWiki exception.
Definition: MWException.php:26
MediaWiki\MediaWikiServices\resetServiceForTesting
resetServiceForTesting( $name, $destroy=true)
Resets the given service for testing purposes.
Definition: MediaWikiServices.php:325
MediaWiki\MediaWikiServices\getCryptHKDF
getCryptHKDF()
Definition: MediaWikiServices.php:540
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:173
MediaWiki\MediaWikiServices\getWatchedItemStore
getWatchedItemStore()
Definition: MediaWikiServices.php:516
MediaWiki\MediaWikiServices\getMediaHandlerFactory
getMediaHandlerFactory()
Definition: MediaWikiServices.php:548
CryptHKDF
Definition: CryptHKDF.php:33
MediaWiki\MediaWikiServices\getWatchedItemQueryService
getWatchedItemQueryService()
Definition: MediaWikiServices.php:524
MediaWiki\MediaWikiServices\getTitleParser
getTitleParser()
Definition: MediaWikiServices.php:623
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:46
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:467
MediaWiki\MediaWikiServices\getMainConfig
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
Definition: MediaWikiServices.php:419
MediaWiki\MediaWikiServices\getSearchEngineFactory
getSearchEngineFactory()
Definition: MediaWikiServices.php:476
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:270
$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:2179
MediaWiki\MediaWikiServices\getDBLoadBalancerFactory
getDBLoadBalancerFactory()
Definition: MediaWikiServices.php:500
MediaWiki\MediaWikiServices\newInstance
static newInstance(Config $bootstrapConfig, $loadWiring='')
Creates a new MediaWikiServices instance and initializes it according to the given $bootstrapConfig.
Definition: MediaWikiServices.php:240
MediaWiki\MediaWikiServices\getTitleFormatter
getTitleFormatter()
Definition: MediaWikiServices.php:615
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
MediaWiki\MediaWikiServices\salvage
salvage(self $other)
Salvages the state of any salvageable service instances in $other.
Definition: MediaWikiServices.php:204
MediaWiki\MediaWikiServices\__construct
__construct(Config $config)
Definition: MediaWikiServices.php:376
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:97
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
MediaWiki\MediaWikiServices\getVirtualRESTServiceClient
getVirtualRESTServiceClient()
Definition: MediaWikiServices.php:655
SkinFactory
Factory class to create Skin objects.
Definition: SkinFactory.php:31
MediaWiki\MediaWikiServices\getConfigFactory
getConfigFactory()
Definition: MediaWikiServices.php:408
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:639
MediaWiki\MediaWikiServices\getMimeAnalyzer
getMimeAnalyzer()
Definition: MediaWikiServices.php:556
MediaWiki\MediaWikiServices\getSiteStore
getSiteStore()
Definition: MediaWikiServices.php:435
MediaWiki\MediaWikiServices\getLocalServerObjectCache
getLocalServerObjectCache()
Definition: MediaWikiServices.php:647
MediaWiki\MediaWikiServices\getConfiguredReadOnlyMode
getConfiguredReadOnlyMode()
Definition: MediaWikiServices.php:663
MediaWiki\MediaWikiServices\getParser
getParser()
Definition: MediaWikiServices.php:572
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:294
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
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:596
MediaWiki\MediaWikiServices\getLinkRenderer
getLinkRenderer()
LinkRenderer instance that can be used if no custom options are needed.
Definition: MediaWikiServices.php:607
MediaWiki\MediaWikiServices\getMainObjectStash
getMainObjectStash()
Definition: MediaWikiServices.php:631
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
MediaWiki\MediaWikiServices\getLinkCache
getLinkCache()
Definition: MediaWikiServices.php:588
CryptRand
Definition: CryptRand.php:28
MediaWiki\MediaWikiServices\getReadOnlyMode
getReadOnlyMode()
Definition: MediaWikiServices.php:671
Hooks
Hooks class.
Definition: Hooks.php:34
MediaWiki\MediaWikiServices\forceGlobalInstance
static forceGlobalInstance(MediaWikiServices $services)
Replaces the global MediaWikiServices instance.
Definition: MediaWikiServices.php:123
MediaWiki\MediaWikiServices\getStatsdDataFactory
getStatsdDataFactory()
Definition: MediaWikiServices.php:451