9 use DateFormatterFactory
;
10 use EventRelayerGroup
;
14 use IBufferingStatsdDataFactory
;
15 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
16 use MediaWiki\Block\BlockManager
;
17 use MediaWiki\Block\BlockRestrictionStore
;
18 use MediaWiki\Http\HttpRequestFactory
;
19 use MediaWiki\Permissions\PermissionManager
;
20 use MediaWiki\Preferences\PreferencesFactory
;
21 use MediaWiki\Shell\CommandFactory
;
22 use MediaWiki\Revision\RevisionRenderer
;
23 use MediaWiki\Revision\SlotRoleRegistry
;
24 use MediaWiki\Special\SpecialPageFactory
;
25 use MediaWiki\Storage\BlobStore
;
26 use MediaWiki\Storage\BlobStoreFactory
;
27 use MediaWiki\Storage\NameTableStore
;
28 use MediaWiki\Storage\NameTableStoreFactory
;
29 use MediaWiki\Revision\RevisionFactory
;
30 use MediaWiki\Revision\RevisionLookup
;
31 use MediaWiki\Revision\RevisionStore
;
32 use OldRevisionImporter
;
33 use MediaWiki\Revision\RevisionStoreFactory
;
34 use UploadRevisionImporter
;
35 use Wikimedia\Rdbms\LBFactory
;
37 use Wikimedia\Rdbms\LoadBalancer
;
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\Services\SalvageableService
;
65 use Wikimedia\Services\ServiceContainer
;
66 use Wikimedia\Services\NoSuchServiceException
;
67 use MediaWiki\Interwiki\InterwikiLookup
;
69 use MediaWiki\Storage\PageEditStash
;
72 * Service locator for MediaWiki core services.
74 * This program is free software; you can redistribute it and/or modify
75 * it under the terms of the GNU General Public License as published by
76 * the Free Software Foundation; either version 2 of the License, or
77 * (at your option) any later version.
79 * This program is distributed in the hope that it will be useful,
80 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82 * GNU General Public License for more details.
84 * You should have received a copy of the GNU General Public License along
85 * with this program; if not, write to the Free Software Foundation, Inc.,
86 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
87 * http://www.gnu.org/copyleft/gpl.html
95 * MediaWikiServices is the service locator for the application scope of MediaWiki.
96 * Its implemented as a simple configurable DI container.
97 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
98 * the network of service objects that defines MediaWiki's application logic.
99 * It acts as an entry point to MediaWiki's dependency injection mechanism.
101 * Services are defined in the "wiring" array passed to the constructor,
102 * or by calling defineService().
104 * @see docs/injection.txt for an overview of using dependency injection in the
105 * MediaWiki code base.
107 class MediaWikiServices
extends ServiceContainer
{
110 * @var MediaWikiServices|null
112 private static $instance = null;
115 * Returns the global default instance of the top level service locator.
119 * The default instance is initialized using the service instantiator functions
120 * defined in ServiceWiring.php.
122 * @note This should only be called by static functions! The instance returned here
123 * should not be passed around! Objects that need access to a service should have
124 * that service injected into the constructor, never a service locator!
126 * @return MediaWikiServices
128 public static function getInstance() {
129 if ( self
::$instance === null ) {
130 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
131 // but some information from the global scope has to be injected here,
132 // even if it's just a file name or database credentials to load
133 // configuration from.
134 $bootstrapConfig = new GlobalVarConfig();
135 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
138 return self
::$instance;
142 * Replaces the global MediaWikiServices instance.
146 * @note This is for use in PHPUnit tests only!
148 * @throws MWException if called outside of PHPUnit tests.
150 * @param MediaWikiServices $services The new MediaWikiServices object.
152 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
154 public static function forceGlobalInstance( MediaWikiServices
$services ) {
155 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
156 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
159 $old = self
::getInstance();
160 self
::$instance = $services;
166 * Creates a new instance of MediaWikiServices and sets it as the global default
167 * instance. getInstance() will return a different MediaWikiServices object
168 * after every call to resetGlobalInstance().
172 * @warning This should not be used during normal operation. It is intended for use
173 * when the configuration has changed significantly since bootstrap time, e.g.
174 * during the installation process or during testing.
176 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
177 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
178 * any of the services managed by MediaWikiServices exist. If any service objects
179 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
180 * with the operation of the services managed by the new MediaWikiServices.
181 * Operating with a mix of services created by the old and the new
182 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
183 * Any class implementing LAZY LOADING is especially prone to this problem,
184 * since instances would typically retain a reference to a storage layer service.
186 * @see forceGlobalInstance()
187 * @see resetGlobalInstance()
188 * @see resetBetweenTest()
190 * @param Config|null $bootstrapConfig The Config object to be registered as the
191 * 'BootstrapConfig' service. This has to contain at least the information
192 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
193 * config of the old instance of MediaWikiServices will be re-used. If there
194 * was no previous instance, a new GlobalVarConfig object will be used to
195 * bootstrap the services.
197 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
198 * See SalvageableService for details.
200 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
201 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
204 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
205 if ( self
::$instance === null ) {
206 // no global instance yet, nothing to reset
210 self
::failIfResetNotAllowed( __METHOD__
);
212 if ( $bootstrapConfig === null ) {
213 $bootstrapConfig = self
::$instance->getBootstrapConfig();
216 $oldInstance = self
::$instance;
218 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
219 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
221 if ( $quick === 'quick' ) {
222 self
::$instance->salvage( $oldInstance );
224 $oldInstance->destroy();
228 /** @noinspection PhpDocSignatureInspection */
231 * Salvages the state of any salvageable service instances in $other.
233 * @note $other will have been destroyed when salvage() returns.
235 * @param MediaWikiServices $other
237 private function salvage( self
$other ) {
238 foreach ( $this->getServiceNames() as $name ) {
239 // The service could be new in the new instance and not registered in the
240 // other instance (e.g. an extension that was loaded after the instantiation of
241 // the other instance. Skip this service in this case. See T143974
243 $oldService = $other->peekService( $name );
244 } catch ( NoSuchServiceException
$e ) {
248 if ( $oldService instanceof SalvageableService
) {
249 /** @var SalvageableService $newService */
250 $newService = $this->getService( $name );
251 $newService->salvage( $oldService );
259 * Creates a new MediaWikiServices instance and initializes it according to the
260 * given $bootstrapConfig. In particular, all wiring files defined in the
261 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
263 * @param Config|null $bootstrapConfig The Config object to be registered as the
264 * 'BootstrapConfig' service.
266 * @param string $loadWiring set this to 'load' to load the wiring files specified
267 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
269 * @return MediaWikiServices
270 * @throws MWException
271 * @throws \FatalError
273 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
274 $instance = new self( $bootstrapConfig );
276 // Load the default wiring from the specified files.
277 if ( $loadWiring === 'load' ) {
278 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
279 $instance->loadWiringFiles( $wiringFiles );
282 // Provide a traditional hook point to allow extensions to configure services.
283 Hooks
::run( 'MediaWikiServices', [ $instance ] );
289 * Disables all storage layer services. After calling this, any attempt to access the
290 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
295 * @warning This is intended for extreme situations only and should never be used
296 * while serving normal web requests. Legitimate use cases for this method include
297 * the installation process. Test fixtures may also use this, if the fixture relies
300 * @see resetGlobalInstance()
301 * @see resetChildProcessServices()
303 public static function disableStorageBackend() {
304 // TODO: also disable some Caches, JobQueues, etc
305 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
306 $services = self
::getInstance();
308 foreach ( $destroy as $name ) {
309 $services->disableService( $name );
312 ObjectCache
::clear();
316 * Resets any services that may have become stale after a child process
317 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
318 * to call this method from the parent process.
322 * @note This is intended for use in the context of process forking only!
324 * @see resetGlobalInstance()
325 * @see disableStorageBackend()
327 public static function resetChildProcessServices() {
328 // NOTE: for now, just reset everything. Since we don't know the interdependencies
329 // between services, we can't do this more selectively at this time.
330 self
::resetGlobalInstance();
332 // Child, reseed because there is no bug in PHP:
333 // https://bugs.php.net/bug.php?id=42465
334 mt_srand( getmypid() );
338 * Resets the given service for testing purposes.
342 * @warning This is generally unsafe! Other services may still retain references
343 * to the stale service instance, leading to failures and inconsistencies. Subclasses
344 * may use this method to reset specific services under specific instances, but
345 * it should not be exposed to application logic.
347 * @note With proper dependency injection used throughout the codebase, this method
348 * should not be needed. It is provided to allow tests that pollute global service
349 * instances to clean up.
351 * @param string $name
352 * @param bool $destroy Whether the service instance should be destroyed if it exists.
353 * When set to false, any existing service instance will effectively be detached
354 * from the container.
356 * @throws MWException if called outside of PHPUnit tests.
358 public function resetServiceForTesting( $name, $destroy = true ) {
359 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
360 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
363 $this->resetService( $name, $destroy );
367 * Convenience method that throws an exception unless it is called during a phase in which
368 * resetting of global services is allowed. In general, services should not be reset
369 * individually, since that may introduce inconsistencies.
373 * This method will throw an exception if:
375 * - self::$resetInProgress is false (to allow all services to be reset together
376 * via resetGlobalInstance)
377 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
378 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
380 * This method is intended to be used to safeguard against accidentally resetting
381 * global service instances that are not yet managed by MediaWikiServices. It is
382 * defined here in the MediaWikiServices services class to have a central place
383 * for managing service bootstrapping and resetting.
385 * @param string $method the name of the caller method, as given by __METHOD__.
387 * @throws MWException if called outside bootstrap mode.
389 * @see resetGlobalInstance()
390 * @see forceGlobalInstance()
391 * @see disableStorageBackend()
393 public static function failIfResetNotAllowed( $method ) {
394 if ( !defined( 'MW_PHPUNIT_TEST' )
395 && !defined( 'MW_PARSER_TEST' )
396 && !defined( 'MEDIAWIKI_INSTALL' )
397 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
398 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
400 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
405 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
406 * This has to contain at least the information needed to set up the 'ConfigFactory'
409 public function __construct( Config
$config ) {
410 parent
::__construct();
412 // Register the given Config object as the bootstrap config service.
413 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
418 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
422 * @return ActorMigration
424 public function getActorMigration() {
425 return $this->getService( 'ActorMigration' );
432 public function getBlobStore() {
433 return $this->getService( '_SqlBlobStore' );
438 * @return BlobStoreFactory
440 public function getBlobStoreFactory() {
441 return $this->getService( 'BlobStoreFactory' );
446 * @return BlockManager
448 public function getBlockManager() : BlockManager
{
449 return $this->getService( 'BlockManager' );
454 * @return BlockRestrictionStore
456 public function getBlockRestrictionStore() : BlockRestrictionStore
{
457 return $this->getService( 'BlockRestrictionStore' );
461 * Returns the Config object containing the bootstrap configuration.
462 * Bootstrap configuration would typically include database credentials
463 * and other information that may be needed before the ConfigFactory
464 * service can be instantiated.
466 * @note This should only be used during bootstrapping, in particular
467 * when creating the MainConfig service. Application logic should
468 * use getMainConfig() to get a Config instances.
473 public function getBootstrapConfig() {
474 return $this->getService( 'BootstrapConfig' );
479 * @return NameTableStore
481 public function getChangeTagDefStore() {
482 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
487 * @return CommentStore
489 public function getCommentStore() {
490 return $this->getService( 'CommentStore' );
495 * @return ConfigFactory
497 public function getConfigFactory() {
498 return $this->getService( 'ConfigFactory' );
503 * @return ConfigRepository
505 public function getConfigRepository() {
506 return $this->getService( 'ConfigRepository' );
511 * @return \ConfiguredReadOnlyMode
513 public function getConfiguredReadOnlyMode() {
514 return $this->getService( 'ConfiguredReadOnlyMode' );
521 public function getContentLanguage() {
522 return $this->getService( 'ContentLanguage' );
527 * @return NameTableStore
529 public function getContentModelStore() {
530 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
537 public function getCryptHKDF() {
538 return $this->getService( 'CryptHKDF' );
543 * @return DateFormatterFactory
545 public function getDateFormatterFactory() {
546 return $this->getService( 'DateFormatterFactory' );
551 * @return LoadBalancer The main DB load balancer for the local wiki.
553 public function getDBLoadBalancer() {
554 return $this->getService( 'DBLoadBalancer' );
561 public function getDBLoadBalancerFactory() {
562 return $this->getService( 'DBLoadBalancerFactory' );
567 * @return EventRelayerGroup
569 public function getEventRelayerGroup() {
570 return $this->getService( 'EventRelayerGroup' );
575 * @return \ExternalStoreFactory
577 public function getExternalStoreFactory() {
578 return $this->getService( 'ExternalStoreFactory' );
583 * @return GenderCache
585 public function getGenderCache() {
586 return $this->getService( 'GenderCache' );
591 * @return HttpRequestFactory
593 public function getHttpRequestFactory() {
594 return $this->getService( 'HttpRequestFactory' );
599 * @return InterwikiLookup
601 public function getInterwikiLookup() {
602 return $this->getService( 'InterwikiLookup' );
609 public function getLinkCache() {
610 return $this->getService( 'LinkCache' );
614 * LinkRenderer instance that can be used
615 * if no custom options are needed
618 * @return LinkRenderer
620 public function getLinkRenderer() {
621 return $this->getService( 'LinkRenderer' );
626 * @return LinkRendererFactory
628 public function getLinkRendererFactory() {
629 return $this->getService( 'LinkRendererFactory' );
636 public function getLocalServerObjectCache() {
637 return $this->getService( 'LocalServerObjectCache' );
642 * @return MagicWordFactory
644 public function getMagicWordFactory() {
645 return $this->getService( 'MagicWordFactory' );
649 * Returns the Config object that provides configuration for MediaWiki core.
650 * This may or may not be the same object that is returned by getBootstrapConfig().
655 public function getMainConfig() {
656 return $this->getService( 'MainConfig' );
663 public function getMainObjectStash() {
664 return $this->getService( 'MainObjectStash' );
669 * @return \WANObjectCache
671 public function getMainWANObjectCache() {
672 return $this->getService( 'MainWANObjectCache' );
677 * @return MediaHandlerFactory
679 public function getMediaHandlerFactory() {
680 return $this->getService( 'MediaHandlerFactory' );
685 * @return MimeAnalyzer
687 public function getMimeAnalyzer() {
688 return $this->getService( 'MimeAnalyzer' );
693 * @return NamespaceInfo
695 public function getNamespaceInfo() : NamespaceInfo
{
696 return $this->getService( 'NamespaceInfo' );
701 * @return NameTableStoreFactory
703 public function getNameTableStoreFactory() {
704 return $this->getService( 'NameTableStoreFactory' );
709 * @return OldRevisionImporter
711 public function getOldRevisionImporter() {
712 return $this->getService( 'OldRevisionImporter' );
716 * @return PageEditStash
719 public function getPageEditStash() {
720 return $this->getService( 'PageEditStash' );
727 public function getParser() {
728 return $this->getService( 'Parser' );
733 * @return ParserCache
735 public function getParserCache() {
736 return $this->getService( 'ParserCache' );
741 * @return ParserFactory
743 public function getParserFactory() {
744 return $this->getService( 'ParserFactory' );
749 * @return PasswordFactory
751 public function getPasswordFactory() {
752 return $this->getService( 'PasswordFactory' );
757 * @return StatsdDataFactoryInterface
759 public function getPerDbNameStatsdDataFactory() {
760 return $this->getService( 'PerDbNameStatsdDataFactory' );
765 * @return PermissionManager
767 public function getPermissionManager() {
768 return $this->getService( 'PermissionManager' );
773 * @return PreferencesFactory
775 public function getPreferencesFactory() {
776 return $this->getService( 'PreferencesFactory' );
781 * @return ProxyLookup
783 public function getProxyLookup() {
784 return $this->getService( 'ProxyLookup' );
789 * @return \ReadOnlyMode
791 public function getReadOnlyMode() {
792 return $this->getService( 'ReadOnlyMode' );
799 public function getRepoGroup() : RepoGroup
{
800 return $this->getService( 'RepoGroup' );
805 * @return ResourceLoader
807 public function getResourceLoader() {
808 return $this->getService( 'ResourceLoader' );
813 * @return RevisionFactory
815 public function getRevisionFactory() {
816 return $this->getService( 'RevisionFactory' );
821 * @return RevisionLookup
823 public function getRevisionLookup() {
824 return $this->getService( 'RevisionLookup' );
829 * @return RevisionRenderer
831 public function getRevisionRenderer() {
832 return $this->getService( 'RevisionRenderer' );
837 * @return RevisionStore
839 public function getRevisionStore() {
840 return $this->getService( 'RevisionStore' );
845 * @return RevisionStoreFactory
847 public function getRevisionStoreFactory() {
848 return $this->getService( 'RevisionStoreFactory' );
853 * @return SearchEngine
855 public function newSearchEngine() {
856 // New engine object every time, since they keep state
857 return $this->getService( 'SearchEngineFactory' )->create();
862 * @return SearchEngineConfig
864 public function getSearchEngineConfig() {
865 return $this->getService( 'SearchEngineConfig' );
870 * @return SearchEngineFactory
872 public function getSearchEngineFactory() {
873 return $this->getService( 'SearchEngineFactory' );
878 * @return CommandFactory
880 public function getShellCommandFactory() {
881 return $this->getService( 'ShellCommandFactory' );
888 public function getSiteLookup() {
889 return $this->getService( 'SiteLookup' );
896 public function getSiteStore() {
897 return $this->getService( 'SiteStore' );
902 * @return SkinFactory
904 public function getSkinFactory() {
905 return $this->getService( 'SkinFactory' );
910 * @return SlotRoleRegistry
912 public function getSlotRoleRegistry() {
913 return $this->getService( 'SlotRoleRegistry' );
918 * @return NameTableStore
920 public function getSlotRoleStore() {
921 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
926 * @return SpecialPageFactory
928 public function getSpecialPageFactory() : SpecialPageFactory
{
929 return $this->getService( 'SpecialPageFactory' );
934 * @return IBufferingStatsdDataFactory
936 public function getStatsdDataFactory() {
937 return $this->getService( 'StatsdDataFactory' );
942 * @return TitleFormatter
944 public function getTitleFormatter() {
945 return $this->getService( 'TitleFormatter' );
950 * @return TitleParser
952 public function getTitleParser() {
953 return $this->getService( 'TitleParser' );
958 * @return UploadRevisionImporter
960 public function getUploadRevisionImporter() {
961 return $this->getService( 'UploadRevisionImporter' );
966 * @return VirtualRESTServiceClient
968 public function getVirtualRESTServiceClient() {
969 return $this->getService( 'VirtualRESTServiceClient' );
974 * @return WatchedItemQueryService
976 public function getWatchedItemQueryService() {
977 return $this->getService( 'WatchedItemQueryService' );
982 * @return WatchedItemStoreInterface
984 public function getWatchedItemStore() {
985 return $this->getService( 'WatchedItemStore' );
990 * @return \OldRevisionImporter
992 public function getWikiRevisionOldRevisionImporter() {
993 return $this->getService( 'OldRevisionImporter' );
998 * @return \OldRevisionImporter
1000 public function getWikiRevisionOldRevisionImporterNoUpdates() {
1001 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1006 * @return \UploadRevisionImporter
1008 public function getWikiRevisionUploadImporter() {
1009 return $this->getService( 'UploadRevisionImporter' );