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\LBFactory
;
38 use Wikimedia\Rdbms\LoadBalancer
;
39 use MediaHandlerFactory
;
40 use MediaWiki\Config\ConfigRepository
;
41 use MediaWiki\Linker\LinkRenderer
;
42 use MediaWiki\Linker\LinkRendererFactory
;
55 use SearchEngineConfig
;
56 use SearchEngineFactory
;
59 use WatchedItemStoreInterface
;
60 use WatchedItemQueryService
;
64 use VirtualRESTServiceClient
;
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
;
73 * Service locator for MediaWiki core services.
75 * This program is free software; you can redistribute it and/or modify
76 * it under the terms of the GNU General Public License as published by
77 * the Free Software Foundation; either version 2 of the License, or
78 * (at your option) any later version.
80 * This program is distributed in the hope that it will be useful,
81 * but WITHOUT ANY WARRANTY; without even the implied warranty of
82 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
83 * GNU General Public License for more details.
85 * You should have received a copy of the GNU General Public License along
86 * with this program; if not, write to the Free Software Foundation, Inc.,
87 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
88 * http://www.gnu.org/copyleft/gpl.html
96 * MediaWikiServices is the service locator for the application scope of MediaWiki.
97 * Its implemented as a simple configurable DI container.
98 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
99 * the network of service objects that defines MediaWiki's application logic.
100 * It acts as an entry point to MediaWiki's dependency injection mechanism.
102 * Services are defined in the "wiring" array passed to the constructor,
103 * or by calling defineService().
105 * @see docs/injection.txt for an overview of using dependency injection in the
106 * MediaWiki code base.
108 class MediaWikiServices
extends ServiceContainer
{
111 * @var MediaWikiServices|null
113 private static $instance = null;
116 * Returns the global default instance of the top level service locator.
120 * The default instance is initialized using the service instantiator functions
121 * defined in ServiceWiring.php.
123 * @note This should only be called by static functions! The instance returned here
124 * should not be passed around! Objects that need access to a service should have
125 * that service injected into the constructor, never a service locator!
127 * @return MediaWikiServices
129 public static function getInstance() {
130 if ( self
::$instance === null ) {
131 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
132 // but some information from the global scope has to be injected here,
133 // even if it's just a file name or database credentials to load
134 // configuration from.
135 $bootstrapConfig = new GlobalVarConfig();
136 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
139 return self
::$instance;
143 * Replaces the global MediaWikiServices instance.
147 * @note This is for use in PHPUnit tests only!
149 * @throws MWException if called outside of PHPUnit tests.
151 * @param MediaWikiServices $services The new MediaWikiServices object.
153 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
155 public static function forceGlobalInstance( MediaWikiServices
$services ) {
156 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
157 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
160 $old = self
::getInstance();
161 self
::$instance = $services;
167 * Creates a new instance of MediaWikiServices and sets it as the global default
168 * instance. getInstance() will return a different MediaWikiServices object
169 * after every call to resetGlobalInstance().
173 * @warning This should not be used during normal operation. It is intended for use
174 * when the configuration has changed significantly since bootstrap time, e.g.
175 * during the installation process or during testing.
177 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
178 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
179 * any of the services managed by MediaWikiServices exist. If any service objects
180 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
181 * with the operation of the services managed by the new MediaWikiServices.
182 * Operating with a mix of services created by the old and the new
183 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
184 * Any class implementing LAZY LOADING is especially prone to this problem,
185 * since instances would typically retain a reference to a storage layer service.
187 * @see forceGlobalInstance()
188 * @see resetGlobalInstance()
189 * @see resetBetweenTest()
191 * @param Config|null $bootstrapConfig The Config object to be registered as the
192 * 'BootstrapConfig' service. This has to contain at least the information
193 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
194 * config of the old instance of MediaWikiServices will be re-used. If there
195 * was no previous instance, a new GlobalVarConfig object will be used to
196 * bootstrap the services.
198 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
199 * See SalvageableService for details.
201 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
202 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
205 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
206 if ( self
::$instance === null ) {
207 // no global instance yet, nothing to reset
211 self
::failIfResetNotAllowed( __METHOD__
);
213 if ( $bootstrapConfig === null ) {
214 $bootstrapConfig = self
::$instance->getBootstrapConfig();
217 $oldInstance = self
::$instance;
219 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
220 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
222 if ( $quick === 'quick' ) {
223 self
::$instance->salvage( $oldInstance );
225 $oldInstance->destroy();
229 /** @noinspection PhpDocSignatureInspection */
232 * Salvages the state of any salvageable service instances in $other.
234 * @note $other will have been destroyed when salvage() returns.
236 * @param MediaWikiServices $other
238 private function salvage( self
$other ) {
239 foreach ( $this->getServiceNames() as $name ) {
240 // The service could be new in the new instance and not registered in the
241 // other instance (e.g. an extension that was loaded after the instantiation of
242 // the other instance. Skip this service in this case. See T143974
244 $oldService = $other->peekService( $name );
245 } catch ( NoSuchServiceException
$e ) {
249 if ( $oldService instanceof SalvageableService
) {
250 /** @var SalvageableService $newService */
251 $newService = $this->getService( $name );
252 $newService->salvage( $oldService );
260 * Creates a new MediaWikiServices instance and initializes it according to the
261 * given $bootstrapConfig. In particular, all wiring files defined in the
262 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
264 * @param Config|null $bootstrapConfig The Config object to be registered as the
265 * 'BootstrapConfig' service.
267 * @param string $loadWiring set this to 'load' to load the wiring files specified
268 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
270 * @return MediaWikiServices
271 * @throws MWException
272 * @throws \FatalError
274 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
275 $instance = new self( $bootstrapConfig );
277 // Load the default wiring from the specified files.
278 if ( $loadWiring === 'load' ) {
279 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
280 $instance->loadWiringFiles( $wiringFiles );
283 // Provide a traditional hook point to allow extensions to configure services.
284 Hooks
::run( 'MediaWikiServices', [ $instance ] );
290 * Disables all storage layer services. After calling this, any attempt to access the
291 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
296 * @warning This is intended for extreme situations only and should never be used
297 * while serving normal web requests. Legitimate use cases for this method include
298 * the installation process. Test fixtures may also use this, if the fixture relies
301 * @see resetGlobalInstance()
302 * @see resetChildProcessServices()
304 public static function disableStorageBackend() {
305 // TODO: also disable some Caches, JobQueues, etc
306 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
307 $services = self
::getInstance();
309 foreach ( $destroy as $name ) {
310 $services->disableService( $name );
313 ObjectCache
::clear();
317 * Resets any services that may have become stale after a child process
318 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
319 * to call this method from the parent process.
323 * @note This is intended for use in the context of process forking only!
325 * @see resetGlobalInstance()
326 * @see disableStorageBackend()
328 public static function resetChildProcessServices() {
329 // NOTE: for now, just reset everything. Since we don't know the interdependencies
330 // between services, we can't do this more selectively at this time.
331 self
::resetGlobalInstance();
333 // Child, reseed because there is no bug in PHP:
334 // https://bugs.php.net/bug.php?id=42465
335 mt_srand( getmypid() );
339 * Resets the given service for testing purposes.
343 * @warning This is generally unsafe! Other services may still retain references
344 * to the stale service instance, leading to failures and inconsistencies. Subclasses
345 * may use this method to reset specific services under specific instances, but
346 * it should not be exposed to application logic.
348 * @note With proper dependency injection used throughout the codebase, this method
349 * should not be needed. It is provided to allow tests that pollute global service
350 * instances to clean up.
352 * @param string $name
353 * @param bool $destroy Whether the service instance should be destroyed if it exists.
354 * When set to false, any existing service instance will effectively be detached
355 * from the container.
357 * @throws MWException if called outside of PHPUnit tests.
359 public function resetServiceForTesting( $name, $destroy = true ) {
360 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
361 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
364 $this->resetService( $name, $destroy );
368 * Convenience method that throws an exception unless it is called during a phase in which
369 * resetting of global services is allowed. In general, services should not be reset
370 * individually, since that may introduce inconsistencies.
374 * This method will throw an exception if:
376 * - self::$resetInProgress is false (to allow all services to be reset together
377 * via resetGlobalInstance)
378 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
379 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
381 * This method is intended to be used to safeguard against accidentally resetting
382 * global service instances that are not yet managed by MediaWikiServices. It is
383 * defined here in the MediaWikiServices services class to have a central place
384 * for managing service bootstrapping and resetting.
386 * @param string $method the name of the caller method, as given by __METHOD__.
388 * @throws MWException if called outside bootstrap mode.
390 * @see resetGlobalInstance()
391 * @see forceGlobalInstance()
392 * @see disableStorageBackend()
394 public static function failIfResetNotAllowed( $method ) {
395 if ( !defined( 'MW_PHPUNIT_TEST' )
396 && !defined( 'MW_PARSER_TEST' )
397 && !defined( 'MEDIAWIKI_INSTALL' )
398 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
399 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
401 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
406 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
407 * This has to contain at least the information needed to set up the 'ConfigFactory'
410 public function __construct( Config
$config ) {
411 parent
::__construct();
413 // Register the given Config object as the bootstrap config service.
414 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
419 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
423 * @return ActorMigration
425 public function getActorMigration() {
426 return $this->getService( 'ActorMigration' );
433 public function getBlobStore() {
434 return $this->getService( '_SqlBlobStore' );
439 * @return BlobStoreFactory
441 public function getBlobStoreFactory() {
442 return $this->getService( 'BlobStoreFactory' );
447 * @return BlockManager
449 public function getBlockManager() : BlockManager
{
450 return $this->getService( 'BlockManager' );
455 * @return BlockRestrictionStore
457 public function getBlockRestrictionStore() : BlockRestrictionStore
{
458 return $this->getService( 'BlockRestrictionStore' );
462 * Returns the Config object containing the bootstrap configuration.
463 * Bootstrap configuration would typically include database credentials
464 * and other information that may be needed before the ConfigFactory
465 * service can be instantiated.
467 * @note This should only be used during bootstrapping, in particular
468 * when creating the MainConfig service. Application logic should
469 * use getMainConfig() to get a Config instances.
474 public function getBootstrapConfig() {
475 return $this->getService( 'BootstrapConfig' );
480 * @return NameTableStore
482 public function getChangeTagDefStore() {
483 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
488 * @return CommentStore
490 public function getCommentStore() {
491 return $this->getService( 'CommentStore' );
496 * @return ConfigFactory
498 public function getConfigFactory() {
499 return $this->getService( 'ConfigFactory' );
504 * @return ConfigRepository
506 public function getConfigRepository() {
507 return $this->getService( 'ConfigRepository' );
512 * @return \ConfiguredReadOnlyMode
514 public function getConfiguredReadOnlyMode() {
515 return $this->getService( 'ConfiguredReadOnlyMode' );
522 public function getContentLanguage() {
523 return $this->getService( 'ContentLanguage' );
528 * @return NameTableStore
530 public function getContentModelStore() {
531 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
538 public function getCryptHKDF() {
539 return $this->getService( 'CryptHKDF' );
544 * @return DateFormatterFactory
546 public function getDateFormatterFactory() {
547 return $this->getService( 'DateFormatterFactory' );
552 * @return LoadBalancer The main DB load balancer for the local wiki.
554 public function getDBLoadBalancer() {
555 return $this->getService( 'DBLoadBalancer' );
562 public function getDBLoadBalancerFactory() {
563 return $this->getService( 'DBLoadBalancerFactory' );
568 * @return EventRelayerGroup
570 public function getEventRelayerGroup() {
571 return $this->getService( 'EventRelayerGroup' );
576 * @return \ExternalStoreFactory
578 public function getExternalStoreFactory() {
579 return $this->getService( 'ExternalStoreFactory' );
584 * @return GenderCache
586 public function getGenderCache() {
587 return $this->getService( 'GenderCache' );
592 * @return HttpRequestFactory
594 public function getHttpRequestFactory() {
595 return $this->getService( 'HttpRequestFactory' );
600 * @return InterwikiLookup
602 public function getInterwikiLookup() {
603 return $this->getService( 'InterwikiLookup' );
610 public function getLinkCache() {
611 return $this->getService( 'LinkCache' );
615 * LinkRenderer instance that can be used
616 * if no custom options are needed
619 * @return LinkRenderer
621 public function getLinkRenderer() {
622 return $this->getService( 'LinkRenderer' );
627 * @return LinkRendererFactory
629 public function getLinkRendererFactory() {
630 return $this->getService( 'LinkRendererFactory' );
637 public function getLocalServerObjectCache() {
638 return $this->getService( 'LocalServerObjectCache' );
643 * @return MagicWordFactory
645 public function getMagicWordFactory() {
646 return $this->getService( 'MagicWordFactory' );
650 * Returns the Config object that provides configuration for MediaWiki core.
651 * This may or may not be the same object that is returned by getBootstrapConfig().
656 public function getMainConfig() {
657 return $this->getService( 'MainConfig' );
664 public function getMainObjectStash() {
665 return $this->getService( 'MainObjectStash' );
670 * @return \WANObjectCache
672 public function getMainWANObjectCache() {
673 return $this->getService( 'MainWANObjectCache' );
678 * @return MediaHandlerFactory
680 public function getMediaHandlerFactory() {
681 return $this->getService( 'MediaHandlerFactory' );
686 * @return MimeAnalyzer
688 public function getMimeAnalyzer() {
689 return $this->getService( 'MimeAnalyzer' );
694 * @return NamespaceInfo
696 public function getNamespaceInfo() : NamespaceInfo
{
697 return $this->getService( 'NamespaceInfo' );
702 * @return NameTableStoreFactory
704 public function getNameTableStoreFactory() {
705 return $this->getService( 'NameTableStoreFactory' );
710 * @return OldRevisionImporter
712 public function getOldRevisionImporter() {
713 return $this->getService( 'OldRevisionImporter' );
717 * @return PageEditStash
720 public function getPageEditStash() {
721 return $this->getService( 'PageEditStash' );
728 public function getParser() {
729 return $this->getService( 'Parser' );
734 * @return ParserCache
736 public function getParserCache() {
737 return $this->getService( 'ParserCache' );
742 * @return ParserFactory
744 public function getParserFactory() {
745 return $this->getService( 'ParserFactory' );
750 * @return PasswordFactory
752 public function getPasswordFactory() {
753 return $this->getService( 'PasswordFactory' );
758 * @return StatsdDataFactoryInterface
760 public function getPerDbNameStatsdDataFactory() {
761 return $this->getService( 'PerDbNameStatsdDataFactory' );
766 * @return PermissionManager
768 public function getPermissionManager() {
769 return $this->getService( 'PermissionManager' );
774 * @return PreferencesFactory
776 public function getPreferencesFactory() {
777 return $this->getService( 'PreferencesFactory' );
782 * @return ProxyLookup
784 public function getProxyLookup() {
785 return $this->getService( 'ProxyLookup' );
790 * @return \ReadOnlyMode
792 public function getReadOnlyMode() {
793 return $this->getService( 'ReadOnlyMode' );
800 public function getRepoGroup() : RepoGroup
{
801 return $this->getService( 'RepoGroup' );
806 * @return ResourceLoader
808 public function getResourceLoader() {
809 return $this->getService( 'ResourceLoader' );
814 * @return RevisionFactory
816 public function getRevisionFactory() {
817 return $this->getService( 'RevisionFactory' );
822 * @return RevisionLookup
824 public function getRevisionLookup() {
825 return $this->getService( 'RevisionLookup' );
830 * @return RevisionRenderer
832 public function getRevisionRenderer() {
833 return $this->getService( 'RevisionRenderer' );
838 * @return RevisionStore
840 public function getRevisionStore() {
841 return $this->getService( 'RevisionStore' );
846 * @return RevisionStoreFactory
848 public function getRevisionStoreFactory() {
849 return $this->getService( 'RevisionStoreFactory' );
854 * @return SearchEngine
856 public function newSearchEngine() {
857 // New engine object every time, since they keep state
858 return $this->getService( 'SearchEngineFactory' )->create();
863 * @return SearchEngineConfig
865 public function getSearchEngineConfig() {
866 return $this->getService( 'SearchEngineConfig' );
871 * @return SearchEngineFactory
873 public function getSearchEngineFactory() {
874 return $this->getService( 'SearchEngineFactory' );
879 * @return CommandFactory
881 public function getShellCommandFactory() {
882 return $this->getService( 'ShellCommandFactory' );
889 public function getSiteLookup() {
890 return $this->getService( 'SiteLookup' );
897 public function getSiteStore() {
898 return $this->getService( 'SiteStore' );
903 * @return SkinFactory
905 public function getSkinFactory() {
906 return $this->getService( 'SkinFactory' );
911 * @return SlotRoleRegistry
913 public function getSlotRoleRegistry() {
914 return $this->getService( 'SlotRoleRegistry' );
919 * @return NameTableStore
921 public function getSlotRoleStore() {
922 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
927 * @return SpecialPageFactory
929 public function getSpecialPageFactory() : SpecialPageFactory
{
930 return $this->getService( 'SpecialPageFactory' );
935 * @return IBufferingStatsdDataFactory
937 public function getStatsdDataFactory() {
938 return $this->getService( 'StatsdDataFactory' );
943 * @return TitleFormatter
945 public function getTitleFormatter() {
946 return $this->getService( 'TitleFormatter' );
951 * @return TitleParser
953 public function getTitleParser() {
954 return $this->getService( 'TitleParser' );
959 * @return UploadRevisionImporter
961 public function getUploadRevisionImporter() {
962 return $this->getService( 'UploadRevisionImporter' );
967 * @return VirtualRESTServiceClient
969 public function getVirtualRESTServiceClient() {
970 return $this->getService( 'VirtualRESTServiceClient' );
975 * @return WatchedItemQueryService
977 public function getWatchedItemQueryService() {
978 return $this->getService( 'WatchedItemQueryService' );
983 * @return WatchedItemStoreInterface
985 public function getWatchedItemStore() {
986 return $this->getService( 'WatchedItemStore' );
991 * @return \OldRevisionImporter
993 public function getWikiRevisionOldRevisionImporter() {
994 return $this->getService( 'OldRevisionImporter' );
999 * @return \OldRevisionImporter
1001 public function getWikiRevisionOldRevisionImporterNoUpdates() {
1002 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1007 * @return \UploadRevisionImporter
1009 public function getWikiRevisionUploadImporter() {
1010 return $this->getService( 'UploadRevisionImporter' );