10 use DateFormatterFactory
;
11 use EventRelayerGroup
;
15 use IBufferingStatsdDataFactory
;
16 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
17 use MediaWiki\Block\BlockManager
;
18 use MediaWiki\Block\BlockRestrictionStore
;
19 use MediaWiki\Http\HttpRequestFactory
;
20 use MediaWiki\Permissions\PermissionManager
;
21 use MediaWiki\Preferences\PreferencesFactory
;
22 use MediaWiki\Shell\CommandFactory
;
23 use MediaWiki\Revision\RevisionRenderer
;
24 use MediaWiki\Revision\SlotRoleRegistry
;
25 use MediaWiki\Special\SpecialPageFactory
;
26 use MediaWiki\Storage\BlobStore
;
27 use MediaWiki\Storage\BlobStoreFactory
;
28 use MediaWiki\Storage\NameTableStore
;
29 use MediaWiki\Storage\NameTableStoreFactory
;
30 use MediaWiki\Revision\RevisionFactory
;
31 use MediaWiki\Revision\RevisionLookup
;
32 use MediaWiki\Revision\RevisionStore
;
33 use OldRevisionImporter
;
34 use MediaWiki\Revision\RevisionStoreFactory
;
35 use UploadRevisionImporter
;
36 use Wikimedia\Rdbms\ILoadBalancer
;
38 use MediaHandlerFactory
;
39 use MediaWiki\Config\ConfigRepository
;
40 use MediaWiki\Linker\LinkRenderer
;
41 use MediaWiki\Linker\LinkRendererFactory
;
54 use SearchEngineConfig
;
55 use SearchEngineFactory
;
58 use WatchedItemStoreInterface
;
59 use WatchedItemQueryService
;
63 use VirtualRESTServiceClient
;
64 use Wikimedia\Rdbms\LBFactory
;
65 use Wikimedia\Services\SalvageableService
;
66 use Wikimedia\Services\ServiceContainer
;
67 use Wikimedia\Services\NoSuchServiceException
;
68 use MediaWiki\Interwiki\InterwikiLookup
;
70 use MediaWiki\Storage\PageEditStash
;
74 * Service locator for MediaWiki core services.
76 * This program is free software; you can redistribute it and/or modify
77 * it under the terms of the GNU General Public License as published by
78 * the Free Software Foundation; either version 2 of the License, or
79 * (at your option) any later version.
81 * This program is distributed in the hope that it will be useful,
82 * but WITHOUT ANY WARRANTY; without even the implied warranty of
83 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84 * GNU General Public License for more details.
86 * You should have received a copy of the GNU General Public License along
87 * with this program; if not, write to the Free Software Foundation, Inc.,
88 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
89 * http://www.gnu.org/copyleft/gpl.html
97 * MediaWikiServices is the service locator for the application scope of MediaWiki.
98 * Its implemented as a simple configurable DI container.
99 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
100 * the network of service objects that defines MediaWiki's application logic.
101 * It acts as an entry point to MediaWiki's dependency injection mechanism.
103 * Services are defined in the "wiring" array passed to the constructor,
104 * or by calling defineService().
106 * @see docs/injection.txt for an overview of using dependency injection in the
107 * MediaWiki code base.
109 class MediaWikiServices
extends ServiceContainer
{
112 * @var MediaWikiServices|null
114 private static $instance = null;
117 * Returns the global default instance of the top level service locator.
121 * The default instance is initialized using the service instantiator functions
122 * defined in ServiceWiring.php.
124 * @note This should only be called by static functions! The instance returned here
125 * should not be passed around! Objects that need access to a service should have
126 * that service injected into the constructor, never a service locator!
128 * @return MediaWikiServices
130 public static function getInstance() {
131 if ( self
::$instance === null ) {
132 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
133 // but some information from the global scope has to be injected here,
134 // even if it's just a file name or database credentials to load
135 // configuration from.
136 $bootstrapConfig = new GlobalVarConfig();
137 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
140 return self
::$instance;
144 * Replaces the global MediaWikiServices instance.
148 * @note This is for use in PHPUnit tests only!
150 * @throws MWException if called outside of PHPUnit tests.
152 * @param MediaWikiServices $services The new MediaWikiServices object.
154 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
156 public static function forceGlobalInstance( MediaWikiServices
$services ) {
157 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
158 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
161 $old = self
::getInstance();
162 self
::$instance = $services;
168 * Creates a new instance of MediaWikiServices and sets it as the global default
169 * instance. getInstance() will return a different MediaWikiServices object
170 * after every call to resetGlobalInstance().
174 * @warning This should not be used during normal operation. It is intended for use
175 * when the configuration has changed significantly since bootstrap time, e.g.
176 * during the installation process or during testing.
178 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
179 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
180 * any of the services managed by MediaWikiServices exist. If any service objects
181 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
182 * with the operation of the services managed by the new MediaWikiServices.
183 * Operating with a mix of services created by the old and the new
184 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
185 * Any class implementing LAZY LOADING is especially prone to this problem,
186 * since instances would typically retain a reference to a storage layer service.
188 * @see forceGlobalInstance()
189 * @see resetGlobalInstance()
190 * @see resetBetweenTest()
192 * @param Config|null $bootstrapConfig The Config object to be registered as the
193 * 'BootstrapConfig' service. This has to contain at least the information
194 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
195 * config of the old instance of MediaWikiServices will be re-used. If there
196 * was no previous instance, a new GlobalVarConfig object will be used to
197 * bootstrap the services.
199 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
200 * See SalvageableService for details.
202 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
203 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
206 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
207 if ( self
::$instance === null ) {
208 // no global instance yet, nothing to reset
212 self
::failIfResetNotAllowed( __METHOD__
);
214 if ( $bootstrapConfig === null ) {
215 $bootstrapConfig = self
::$instance->getBootstrapConfig();
218 $oldInstance = self
::$instance;
220 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
221 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
223 if ( $quick === 'quick' ) {
224 self
::$instance->salvage( $oldInstance );
226 $oldInstance->destroy();
230 /** @noinspection PhpDocSignatureInspection */
233 * Salvages the state of any salvageable service instances in $other.
235 * @note $other will have been destroyed when salvage() returns.
237 * @param MediaWikiServices $other
239 private function salvage( self
$other ) {
240 foreach ( $this->getServiceNames() as $name ) {
241 // The service could be new in the new instance and not registered in the
242 // other instance (e.g. an extension that was loaded after the instantiation of
243 // the other instance. Skip this service in this case. See T143974
245 $oldService = $other->peekService( $name );
246 } catch ( NoSuchServiceException
$e ) {
250 if ( $oldService instanceof SalvageableService
) {
251 /** @var SalvageableService $newService */
252 $newService = $this->getService( $name );
253 $newService->salvage( $oldService );
261 * Creates a new MediaWikiServices instance and initializes it according to the
262 * given $bootstrapConfig. In particular, all wiring files defined in the
263 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
265 * @param Config|null $bootstrapConfig The Config object to be registered as the
266 * 'BootstrapConfig' service.
268 * @param string $loadWiring set this to 'load' to load the wiring files specified
269 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
271 * @return MediaWikiServices
272 * @throws MWException
273 * @throws \FatalError
275 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
276 $instance = new self( $bootstrapConfig );
278 // Load the default wiring from the specified files.
279 if ( $loadWiring === 'load' ) {
280 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
281 $instance->loadWiringFiles( $wiringFiles );
284 // Provide a traditional hook point to allow extensions to configure services.
285 Hooks
::run( 'MediaWikiServices', [ $instance ] );
291 * Disables all storage layer services. After calling this, any attempt to access the
292 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
297 * @warning This is intended for extreme situations only and should never be used
298 * while serving normal web requests. Legitimate use cases for this method include
299 * the installation process. Test fixtures may also use this, if the fixture relies
302 * @see resetGlobalInstance()
303 * @see resetChildProcessServices()
305 public static function disableStorageBackend() {
306 // TODO: also disable some Caches, JobQueues, etc
307 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
308 $services = self
::getInstance();
310 foreach ( $destroy as $name ) {
311 $services->disableService( $name );
314 ObjectCache
::clear();
318 * Resets any services that may have become stale after a child process
319 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
320 * to call this method from the parent process.
324 * @note This is intended for use in the context of process forking only!
326 * @see resetGlobalInstance()
327 * @see disableStorageBackend()
329 public static function resetChildProcessServices() {
330 // NOTE: for now, just reset everything. Since we don't know the interdependencies
331 // between services, we can't do this more selectively at this time.
332 self
::resetGlobalInstance();
334 // Child, reseed because there is no bug in PHP:
335 // https://bugs.php.net/bug.php?id=42465
336 mt_srand( getmypid() );
340 * Resets the given service for testing purposes.
344 * @warning This is generally unsafe! Other services may still retain references
345 * to the stale service instance, leading to failures and inconsistencies. Subclasses
346 * may use this method to reset specific services under specific instances, but
347 * it should not be exposed to application logic.
349 * @note With proper dependency injection used throughout the codebase, this method
350 * should not be needed. It is provided to allow tests that pollute global service
351 * instances to clean up.
353 * @param string $name
354 * @param bool $destroy Whether the service instance should be destroyed if it exists.
355 * When set to false, any existing service instance will effectively be detached
356 * from the container.
358 * @throws MWException if called outside of PHPUnit tests.
360 public function resetServiceForTesting( $name, $destroy = true ) {
361 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
362 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
365 $this->resetService( $name, $destroy );
369 * Convenience method that throws an exception unless it is called during a phase in which
370 * resetting of global services is allowed. In general, services should not be reset
371 * individually, since that may introduce inconsistencies.
375 * This method will throw an exception if:
377 * - self::$resetInProgress is false (to allow all services to be reset together
378 * via resetGlobalInstance)
379 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
380 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
382 * This method is intended to be used to safeguard against accidentally resetting
383 * global service instances that are not yet managed by MediaWikiServices. It is
384 * defined here in the MediaWikiServices services class to have a central place
385 * for managing service bootstrapping and resetting.
387 * @param string $method the name of the caller method, as given by __METHOD__.
389 * @throws MWException if called outside bootstrap mode.
391 * @see resetGlobalInstance()
392 * @see forceGlobalInstance()
393 * @see disableStorageBackend()
395 public static function failIfResetNotAllowed( $method ) {
396 if ( !defined( 'MW_PHPUNIT_TEST' )
397 && !defined( 'MW_PARSER_TEST' )
398 && !defined( 'MEDIAWIKI_INSTALL' )
399 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
400 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
402 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
407 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
408 * This has to contain at least the information needed to set up the 'ConfigFactory'
411 public function __construct( Config
$config ) {
412 parent
::__construct();
414 // Register the given Config object as the bootstrap config service.
415 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
420 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
424 * @return ActorMigration
426 public function getActorMigration() {
427 return $this->getService( 'ActorMigration' );
434 public function getBlobStore() {
435 return $this->getService( '_SqlBlobStore' );
440 * @return BlobStoreFactory
442 public function getBlobStoreFactory() {
443 return $this->getService( 'BlobStoreFactory' );
448 * @return BlockManager
450 public function getBlockManager() : BlockManager
{
451 return $this->getService( 'BlockManager' );
456 * @return BlockRestrictionStore
458 public function getBlockRestrictionStore() : BlockRestrictionStore
{
459 return $this->getService( 'BlockRestrictionStore' );
463 * Returns the Config object containing the bootstrap configuration.
464 * Bootstrap configuration would typically include database credentials
465 * and other information that may be needed before the ConfigFactory
466 * service can be instantiated.
468 * @note This should only be used during bootstrapping, in particular
469 * when creating the MainConfig service. Application logic should
470 * use getMainConfig() to get a Config instances.
475 public function getBootstrapConfig() {
476 return $this->getService( 'BootstrapConfig' );
481 * @return NameTableStore
483 public function getChangeTagDefStore() {
484 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
489 * @return CommentStore
491 public function getCommentStore() {
492 return $this->getService( 'CommentStore' );
497 * @return ConfigFactory
499 public function getConfigFactory() {
500 return $this->getService( 'ConfigFactory' );
505 * @return ConfigRepository
507 public function getConfigRepository() {
508 return $this->getService( 'ConfigRepository' );
513 * @return \ConfiguredReadOnlyMode
515 public function getConfiguredReadOnlyMode() {
516 return $this->getService( 'ConfiguredReadOnlyMode' );
523 public function getContentLanguage() {
524 return $this->getService( 'ContentLanguage' );
529 * @return NameTableStore
531 public function getContentModelStore() {
532 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
539 public function getCryptHKDF() {
540 return $this->getService( 'CryptHKDF' );
545 * @return DateFormatterFactory
547 public function getDateFormatterFactory() {
548 return $this->getService( 'DateFormatterFactory' );
553 * @return ILoadBalancer The main DB load balancer for the local wiki.
555 public function getDBLoadBalancer() {
556 return $this->getService( 'DBLoadBalancer' );
563 public function getDBLoadBalancerFactory() {
564 return $this->getService( 'DBLoadBalancerFactory' );
569 * @return EventRelayerGroup
571 public function getEventRelayerGroup() {
572 return $this->getService( 'EventRelayerGroup' );
577 * @return \ExternalStoreAccess
579 public function getExternalStoreAccess() {
580 return $this->getService( 'ExternalStoreAccess' );
585 * @return \ExternalStoreFactory
587 public function getExternalStoreFactory() {
588 return $this->getService( 'ExternalStoreFactory' );
593 * @return GenderCache
595 public function getGenderCache() {
596 return $this->getService( 'GenderCache' );
600 * @return HtmlCacheUpdater
603 public function getHtmlCacheUpdater() {
604 return $this->getService( 'HtmlCacheUpdater' );
609 * @return HttpRequestFactory
611 public function getHttpRequestFactory() {
612 return $this->getService( 'HttpRequestFactory' );
617 * @return InterwikiLookup
619 public function getInterwikiLookup() {
620 return $this->getService( 'InterwikiLookup' );
627 public function getLinkCache() {
628 return $this->getService( 'LinkCache' );
632 * LinkRenderer instance that can be used
633 * if no custom options are needed
636 * @return LinkRenderer
638 public function getLinkRenderer() {
639 return $this->getService( 'LinkRenderer' );
644 * @return LinkRendererFactory
646 public function getLinkRendererFactory() {
647 return $this->getService( 'LinkRendererFactory' );
654 public function getLocalServerObjectCache() {
655 return $this->getService( 'LocalServerObjectCache' );
660 * @return MagicWordFactory
662 public function getMagicWordFactory() {
663 return $this->getService( 'MagicWordFactory' );
667 * Returns the Config object that provides configuration for MediaWiki core.
668 * This may or may not be the same object that is returned by getBootstrapConfig().
673 public function getMainConfig() {
674 return $this->getService( 'MainConfig' );
681 public function getMainObjectStash() {
682 return $this->getService( 'MainObjectStash' );
687 * @return \WANObjectCache
689 public function getMainWANObjectCache() {
690 return $this->getService( 'MainWANObjectCache' );
695 * @return MediaHandlerFactory
697 public function getMediaHandlerFactory() {
698 return $this->getService( 'MediaHandlerFactory' );
703 * @return MimeAnalyzer
705 public function getMimeAnalyzer() {
706 return $this->getService( 'MimeAnalyzer' );
711 * @return NamespaceInfo
713 public function getNamespaceInfo() : NamespaceInfo
{
714 return $this->getService( 'NamespaceInfo' );
719 * @return NameTableStoreFactory
721 public function getNameTableStoreFactory() {
722 return $this->getService( 'NameTableStoreFactory' );
727 * @return OldRevisionImporter
729 public function getOldRevisionImporter() {
730 return $this->getService( 'OldRevisionImporter' );
734 * @return PageEditStash
737 public function getPageEditStash() {
738 return $this->getService( 'PageEditStash' );
745 public function getParser() {
746 return $this->getService( 'Parser' );
751 * @return ParserCache
753 public function getParserCache() {
754 return $this->getService( 'ParserCache' );
759 * @return ParserFactory
761 public function getParserFactory() {
762 return $this->getService( 'ParserFactory' );
767 * @return PasswordFactory
769 public function getPasswordFactory() {
770 return $this->getService( 'PasswordFactory' );
775 * @return StatsdDataFactoryInterface
777 public function getPerDbNameStatsdDataFactory() {
778 return $this->getService( 'PerDbNameStatsdDataFactory' );
783 * @return PermissionManager
785 public function getPermissionManager() {
786 return $this->getService( 'PermissionManager' );
791 * @return PreferencesFactory
793 public function getPreferencesFactory() {
794 return $this->getService( 'PreferencesFactory' );
799 * @return ProxyLookup
801 public function getProxyLookup() {
802 return $this->getService( 'ProxyLookup' );
807 * @return \ReadOnlyMode
809 public function getReadOnlyMode() {
810 return $this->getService( 'ReadOnlyMode' );
817 public function getRepoGroup() : RepoGroup
{
818 return $this->getService( 'RepoGroup' );
823 * @return ResourceLoader
825 public function getResourceLoader() {
826 return $this->getService( 'ResourceLoader' );
831 * @return RevisionFactory
833 public function getRevisionFactory() {
834 return $this->getService( 'RevisionFactory' );
839 * @return RevisionLookup
841 public function getRevisionLookup() {
842 return $this->getService( 'RevisionLookup' );
847 * @return RevisionRenderer
849 public function getRevisionRenderer() {
850 return $this->getService( 'RevisionRenderer' );
855 * @return RevisionStore
857 public function getRevisionStore() {
858 return $this->getService( 'RevisionStore' );
863 * @return RevisionStoreFactory
865 public function getRevisionStoreFactory() {
866 return $this->getService( 'RevisionStoreFactory' );
871 * @return SearchEngine
873 public function newSearchEngine() {
874 // New engine object every time, since they keep state
875 return $this->getService( 'SearchEngineFactory' )->create();
880 * @return SearchEngineConfig
882 public function getSearchEngineConfig() {
883 return $this->getService( 'SearchEngineConfig' );
888 * @return SearchEngineFactory
890 public function getSearchEngineFactory() {
891 return $this->getService( 'SearchEngineFactory' );
896 * @return CommandFactory
898 public function getShellCommandFactory() {
899 return $this->getService( 'ShellCommandFactory' );
906 public function getSiteLookup() {
907 return $this->getService( 'SiteLookup' );
914 public function getSiteStore() {
915 return $this->getService( 'SiteStore' );
920 * @return SkinFactory
922 public function getSkinFactory() {
923 return $this->getService( 'SkinFactory' );
928 * @return SlotRoleRegistry
930 public function getSlotRoleRegistry() {
931 return $this->getService( 'SlotRoleRegistry' );
936 * @return NameTableStore
938 public function getSlotRoleStore() {
939 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
944 * @return SpecialPageFactory
946 public function getSpecialPageFactory() : SpecialPageFactory
{
947 return $this->getService( 'SpecialPageFactory' );
952 * @return IBufferingStatsdDataFactory
954 public function getStatsdDataFactory() {
955 return $this->getService( 'StatsdDataFactory' );
960 * @return TitleFormatter
962 public function getTitleFormatter() {
963 return $this->getService( 'TitleFormatter' );
968 * @return TitleParser
970 public function getTitleParser() {
971 return $this->getService( 'TitleParser' );
976 * @return UploadRevisionImporter
978 public function getUploadRevisionImporter() {
979 return $this->getService( 'UploadRevisionImporter' );
984 * @return VirtualRESTServiceClient
986 public function getVirtualRESTServiceClient() {
987 return $this->getService( 'VirtualRESTServiceClient' );
992 * @return WatchedItemQueryService
994 public function getWatchedItemQueryService() {
995 return $this->getService( 'WatchedItemQueryService' );
1000 * @return WatchedItemStoreInterface
1002 public function getWatchedItemStore() {
1003 return $this->getService( 'WatchedItemStore' );
1008 * @return \OldRevisionImporter
1010 public function getWikiRevisionOldRevisionImporter() {
1011 return $this->getService( 'OldRevisionImporter' );
1016 * @return \OldRevisionImporter
1018 public function getWikiRevisionOldRevisionImporterNoUpdates() {
1019 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1024 * @return \UploadRevisionImporter
1026 public function getWikiRevisionUploadImporter() {
1027 return $this->getService( 'UploadRevisionImporter' );