MediaWiki REL1_32
MediaWikiServices.php
Go to the documentation of this file.
1<?php
2namespace MediaWiki;
3
6use Config;
8use CryptHKDF;
9use CryptRand;
11use GenderCache;
13use Hooks;
15use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
32use LinkCache;
41use MWException;
42use MimeAnalyzer;
43use ObjectCache;
44use Parser;
45use ParserCache;
48use ProxyLookup;
49use SearchEngine;
52use SiteLookup;
53use SiteStore;
56use SkinFactory;
58use TitleParser;
62
100
104 private static $instance = null;
105
120 public static function getInstance() {
121 if ( self::$instance === null ) {
122 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
123 // but some information from the global scope has to be injected here,
124 // even if it's just a file name or database credentials to load
125 // configuration from.
126 $bootstrapConfig = new GlobalVarConfig();
127 self::$instance = self::newInstance( $bootstrapConfig, 'load' );
128 }
129
130 return self::$instance;
131 }
132
147 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
148 throw new MWException( __METHOD__ . ' must not be used outside unit tests.' );
149 }
150
151 $old = self::getInstance();
152 self::$instance = $services;
153
154 return $old;
155 }
156
196 public static function resetGlobalInstance( Config $bootstrapConfig = null, $quick = '' ) {
197 if ( self::$instance === null ) {
198 // no global instance yet, nothing to reset
199 return;
200 }
201
202 self::failIfResetNotAllowed( __METHOD__ );
203
204 if ( $bootstrapConfig === null ) {
205 $bootstrapConfig = self::$instance->getBootstrapConfig();
206 }
207
208 $oldInstance = self::$instance;
209
210 self::$instance = self::newInstance( $bootstrapConfig, 'load' );
211 self::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
212
213 if ( $quick === 'quick' ) {
214 self::$instance->salvage( $oldInstance );
215 } else {
216 $oldInstance->destroy();
217 }
218 }
219
227 private function salvage( self $other ) {
228 foreach ( $this->getServiceNames() as $name ) {
229 // The service could be new in the new instance and not registered in the
230 // other instance (e.g. an extension that was loaded after the instantiation of
231 // the other instance. Skip this service in this case. See T143974
232 try {
233 $oldService = $other->peekService( $name );
234 } catch ( NoSuchServiceException $e ) {
235 continue;
236 }
237
238 if ( $oldService instanceof SalvageableService ) {
240 $newService = $this->getService( $name );
241 $newService->salvage( $oldService );
242 }
243 }
244
245 $other->destroy();
246 }
247
263 private static function newInstance( Config $bootstrapConfig, $loadWiring = '' ) {
264 $instance = new self( $bootstrapConfig );
265
266 // Load the default wiring from the specified files.
267 if ( $loadWiring === 'load' ) {
268 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
269 $instance->loadWiringFiles( $wiringFiles );
270 }
271
272 // Provide a traditional hook point to allow extensions to configure services.
273 Hooks::run( 'MediaWikiServices', [ $instance ] );
274
275 return $instance;
276 }
277
293 public static function disableStorageBackend() {
294 // TODO: also disable some Caches, JobQueues, etc
295 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
296 $services = self::getInstance();
297
298 foreach ( $destroy as $name ) {
299 $services->disableService( $name );
300 }
301
302 ObjectCache::clear();
303 }
304
317 public static function resetChildProcessServices() {
318 // NOTE: for now, just reset everything. Since we don't know the interdependencies
319 // between services, we can't do this more selectively at this time.
320 self::resetGlobalInstance();
321
322 // Child, reseed because there is no bug in PHP:
323 // https://bugs.php.net/bug.php?id=42465
324 mt_srand( getmypid() );
325 }
326
348 public function resetServiceForTesting( $name, $destroy = true ) {
349 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
350 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
351 }
352
353 $this->resetService( $name, $destroy );
354 }
355
383 public static function failIfResetNotAllowed( $method ) {
384 if ( !defined( 'MW_PHPUNIT_TEST' )
385 && !defined( 'MW_PARSER_TEST' )
386 && !defined( 'MEDIAWIKI_INSTALL' )
387 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
388 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
389 ) {
390 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
391 }
392 }
393
399 public function __construct( Config $config ) {
400 parent::__construct();
401
402 // Register the given Config object as the bootstrap config service.
403 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
404 return $config;
405 } );
406 }
407
408 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
409
414 public function getActorMigration() {
415 return $this->getService( 'ActorMigration' );
416 }
417
422 public function getBlobStore() {
423 return $this->getService( '_SqlBlobStore' );
424 }
425
430 public function getBlobStoreFactory() {
431 return $this->getService( 'BlobStoreFactory' );
432 }
433
447 public function getBootstrapConfig() {
448 return $this->getService( 'BootstrapConfig' );
449 }
450
455 public function getChangeTagDefStore() {
456 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
457 }
458
463 public function getCommentStore() {
464 return $this->getService( 'CommentStore' );
465 }
466
471 public function getConfigFactory() {
472 return $this->getService( 'ConfigFactory' );
473 }
474
479 public function getConfigRepository() {
480 return $this->getService( 'ConfigRepository' );
481 }
482
487 public function getConfiguredReadOnlyMode() {
488 return $this->getService( 'ConfiguredReadOnlyMode' );
489 }
490
495 public function getContentLanguage() {
496 return $this->getService( 'ContentLanguage' );
497 }
498
503 public function getContentModelStore() {
504 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
505 }
506
511 public function getCryptHKDF() {
512 return $this->getService( 'CryptHKDF' );
513 }
514
520 public function getCryptRand() {
521 return $this->getService( 'CryptRand' );
522 }
523
528 public function getDBLoadBalancer() {
529 return $this->getService( 'DBLoadBalancer' );
530 }
531
536 public function getDBLoadBalancerFactory() {
537 return $this->getService( 'DBLoadBalancerFactory' );
538 }
539
544 public function getEventRelayerGroup() {
545 return $this->getService( 'EventRelayerGroup' );
546 }
547
552 public function getExternalStoreFactory() {
553 return $this->getService( 'ExternalStoreFactory' );
554 }
555
560 public function getGenderCache() {
561 return $this->getService( 'GenderCache' );
562 }
563
568 public function getHttpRequestFactory() {
569 return $this->getService( 'HttpRequestFactory' );
570 }
571
576 public function getInterwikiLookup() {
577 return $this->getService( 'InterwikiLookup' );
578 }
579
584 public function getLinkCache() {
585 return $this->getService( 'LinkCache' );
586 }
587
595 public function getLinkRenderer() {
596 return $this->getService( 'LinkRenderer' );
597 }
598
603 public function getLinkRendererFactory() {
604 return $this->getService( 'LinkRendererFactory' );
605 }
606
611 public function getLocalServerObjectCache() {
612 return $this->getService( 'LocalServerObjectCache' );
613 }
614
619 public function getMagicWordFactory() {
620 return $this->getService( 'MagicWordFactory' );
621 }
622
630 public function getMainConfig() {
631 return $this->getService( 'MainConfig' );
632 }
633
638 public function getMainObjectStash() {
639 return $this->getService( 'MainObjectStash' );
640 }
641
646 public function getMainWANObjectCache() {
647 return $this->getService( 'MainWANObjectCache' );
648 }
649
654 public function getMediaHandlerFactory() {
655 return $this->getService( 'MediaHandlerFactory' );
656 }
657
662 public function getMimeAnalyzer() {
663 return $this->getService( 'MimeAnalyzer' );
664 }
665
670 public function getNameTableStoreFactory() {
671 return $this->getService( 'NameTableStoreFactory' );
672 }
673
677 public function getOldRevisionImporter() {
678 return $this->getService( 'OldRevisionImporter' );
679 }
680
685 public function getParser() {
686 return $this->getService( 'Parser' );
687 }
688
693 public function getParserCache() {
694 return $this->getService( 'ParserCache' );
695 }
696
701 public function getParserFactory() {
702 return $this->getService( 'ParserFactory' );
703 }
704
709 public function getPasswordFactory() {
710 return $this->getService( 'PasswordFactory' );
711 }
712
718 return $this->getService( 'PerDbNameStatsdDataFactory' );
719 }
720
725 public function getPreferencesFactory() {
726 return $this->getService( 'PreferencesFactory' );
727 }
728
733 public function getProxyLookup() {
734 return $this->getService( 'ProxyLookup' );
735 }
736
741 public function getReadOnlyMode() {
742 return $this->getService( 'ReadOnlyMode' );
743 }
744
749 public function getRevisionFactory() {
750 return $this->getService( 'RevisionFactory' );
751 }
752
757 public function getRevisionLookup() {
758 return $this->getService( 'RevisionLookup' );
759 }
760
765 public function getRevisionRenderer() {
766 return $this->getService( 'RevisionRenderer' );
767 }
768
773 public function getRevisionStore() {
774 return $this->getService( 'RevisionStore' );
775 }
776
781 public function getRevisionStoreFactory() {
782 return $this->getService( 'RevisionStoreFactory' );
783 }
784
789 public function newSearchEngine() {
790 // New engine object every time, since they keep state
791 return $this->getService( 'SearchEngineFactory' )->create();
792 }
793
798 public function getSearchEngineConfig() {
799 return $this->getService( 'SearchEngineConfig' );
800 }
801
806 public function getSearchEngineFactory() {
807 return $this->getService( 'SearchEngineFactory' );
808 }
809
814 public function getShellCommandFactory() {
815 return $this->getService( 'ShellCommandFactory' );
816 }
817
822 public function getSiteLookup() {
823 return $this->getService( 'SiteLookup' );
824 }
825
830 public function getSiteStore() {
831 return $this->getService( 'SiteStore' );
832 }
833
838 public function getSkinFactory() {
839 return $this->getService( 'SkinFactory' );
840 }
841
846 public function getSlotRoleStore() {
847 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
848 }
849
855 return $this->getService( 'SpecialPageFactory' );
856 }
857
862 public function getStatsdDataFactory() {
863 return $this->getService( 'StatsdDataFactory' );
864 }
865
870 public function getTitleFormatter() {
871 return $this->getService( 'TitleFormatter' );
872 }
873
878 public function getTitleParser() {
879 return $this->getService( 'TitleParser' );
880 }
881
886 public function getUploadRevisionImporter() {
887 return $this->getService( 'UploadRevisionImporter' );
888 }
889
894 public function getVirtualRESTServiceClient() {
895 return $this->getService( 'VirtualRESTServiceClient' );
896 }
897
902 public function getWatchedItemQueryService() {
903 return $this->getService( 'WatchedItemQueryService' );
904 }
905
910 public function getWatchedItemStore() {
911 return $this->getService( 'WatchedItemStore' );
912 }
913
919 return $this->getService( 'OldRevisionImporter' );
920 }
921
927 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
928 }
929
935 return $this->getService( 'UploadRevisionImporter' );
936 }
937
938}
This class handles the logic for the actor table migration.
CommentStore handles storage of comments (edit summaries, log reasons, etc) in the database.
Factory class to create Config objects.
Factory class for spawning EventRelayer objects using configuration.
Caches user genders when needed to use correct namespace aliases.
Accesses configuration settings from $GLOBALS.
Hooks class.
Definition Hooks.php:34
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:34
MediaWiki exception.
A factory that stores information about MagicWords, and creates them on demand with caching.
Class to construct MediaHandler objects.
Object which holds currently registered configuration options.
Factory creating MWHttpRequest objects.
Factory to create LinkRender objects.
Class that generates HTML links for pages.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static disableStorageBackend()
Disables all storage layer services.
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
salvage(self $other)
Salvages the state of any salvageable service instances in $other.
getLinkRenderer()
LinkRenderer instance that can be used if no custom options are needed.
static failIfResetNotAllowed( $method)
Convenience method that throws an exception unless it is called during a phase in which resetting of ...
getBootstrapConfig()
Returns the Config object containing the bootstrap configuration.
static resetChildProcessServices()
Resets any services that may have become stale after a child process returns from after pcntl_fork().
static newInstance(Config $bootstrapConfig, $loadWiring='')
Creates a new MediaWikiServices instance and initializes it according to the given $bootstrapConfig.
static forceGlobalInstance(MediaWikiServices $services)
Replaces the global MediaWikiServices instance.
static resetGlobalInstance(Config $bootstrapConfig=null, $quick='')
Creates a new instance of MediaWikiServices and sets it as the global default instance.
resetServiceForTesting( $name, $destroy=true)
Resets the given service for testing purposes.
static getInstance()
Returns the global default instance of the top level service locator.
The RevisionRenderer service provides access to rendered output for revisions.
Factory service for RevisionStore instances.
Service for looking up page revisions.
Exception thrown when the requested service is not known.
ServiceContainer provides a generic service to manage named services using lazy instantiation based o...
Factory facilitating dependency injection for Command.
Factory for handling the special page list and generating SpecialPage objects.
Service for instantiating BlobStores.
Functions to get cache objects.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:68
Factory class for creating and checking Password objects.
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
Contain a class for special pages.
Factory class to create Skin objects.
Virtual HTTP service client loosely styled after a Virtual File System.
An interface for generating database load balancers.
Definition LBFactory.php:39
Database connection, tracking, load balancing, and transaction manager for a cluster.
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
Definition hooks.txt:2335
returning false will NOT prevent logging $e
Definition hooks.txt:2226
Interface for configuration instances.
Definition Config.php:28
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Service interface for looking up Interwiki records.
A PreferencesFactory is a MediaWiki service that provides the definitions of preferences for a given ...
Service for constructing revision objects.
Service for looking up page revisions.
SalvageableService defines an interface for services that are able to salvage state from a previous i...
Service for loading and storing data blobs.
Definition BlobStore.php:33
A title formatter service for MediaWiki.
A title parser service for MediaWiki.
A helper class for throttling authentication attempts.