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
;
53 use SearchEngineConfig
;
54 use SearchEngineFactory
;
57 use WatchedItemStoreInterface
;
58 use WatchedItemQueryService
;
62 use VirtualRESTServiceClient
;
63 use Wikimedia\Services\SalvageableService
;
64 use Wikimedia\Services\ServiceContainer
;
65 use Wikimedia\Services\NoSuchServiceException
;
66 use MediaWiki\Interwiki\InterwikiLookup
;
68 use MediaWiki\Storage\PageEditStash
;
71 * Service locator for MediaWiki core services.
73 * This program is free software; you can redistribute it and/or modify
74 * it under the terms of the GNU General Public License as published by
75 * the Free Software Foundation; either version 2 of the License, or
76 * (at your option) any later version.
78 * This program is distributed in the hope that it will be useful,
79 * but WITHOUT ANY WARRANTY; without even the implied warranty of
80 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
81 * GNU General Public License for more details.
83 * You should have received a copy of the GNU General Public License along
84 * with this program; if not, write to the Free Software Foundation, Inc.,
85 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
86 * http://www.gnu.org/copyleft/gpl.html
94 * MediaWikiServices is the service locator for the application scope of MediaWiki.
95 * Its implemented as a simple configurable DI container.
96 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
97 * the network of service objects that defines MediaWiki's application logic.
98 * It acts as an entry point to MediaWiki's dependency injection mechanism.
100 * Services are defined in the "wiring" array passed to the constructor,
101 * or by calling defineService().
103 * @see docs/injection.txt for an overview of using dependency injection in the
104 * MediaWiki code base.
106 class MediaWikiServices
extends ServiceContainer
{
109 * @var MediaWikiServices|null
111 private static $instance = null;
114 * Returns the global default instance of the top level service locator.
118 * The default instance is initialized using the service instantiator functions
119 * defined in ServiceWiring.php.
121 * @note This should only be called by static functions! The instance returned here
122 * should not be passed around! Objects that need access to a service should have
123 * that service injected into the constructor, never a service locator!
125 * @return MediaWikiServices
127 public static function getInstance() {
128 if ( self
::$instance === null ) {
129 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
130 // but some information from the global scope has to be injected here,
131 // even if it's just a file name or database credentials to load
132 // configuration from.
133 $bootstrapConfig = new GlobalVarConfig();
134 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
137 return self
::$instance;
141 * Replaces the global MediaWikiServices instance.
145 * @note This is for use in PHPUnit tests only!
147 * @throws MWException if called outside of PHPUnit tests.
149 * @param MediaWikiServices $services The new MediaWikiServices object.
151 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
153 public static function forceGlobalInstance( MediaWikiServices
$services ) {
154 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
155 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
158 $old = self
::getInstance();
159 self
::$instance = $services;
165 * Creates a new instance of MediaWikiServices and sets it as the global default
166 * instance. getInstance() will return a different MediaWikiServices object
167 * after every call to resetGlobalInstance().
171 * @warning This should not be used during normal operation. It is intended for use
172 * when the configuration has changed significantly since bootstrap time, e.g.
173 * during the installation process or during testing.
175 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
176 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
177 * any of the services managed by MediaWikiServices exist. If any service objects
178 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
179 * with the operation of the services managed by the new MediaWikiServices.
180 * Operating with a mix of services created by the old and the new
181 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
182 * Any class implementing LAZY LOADING is especially prone to this problem,
183 * since instances would typically retain a reference to a storage layer service.
185 * @see forceGlobalInstance()
186 * @see resetGlobalInstance()
187 * @see resetBetweenTest()
189 * @param Config|null $bootstrapConfig The Config object to be registered as the
190 * 'BootstrapConfig' service. This has to contain at least the information
191 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
192 * config of the old instance of MediaWikiServices will be re-used. If there
193 * was no previous instance, a new GlobalVarConfig object will be used to
194 * bootstrap the services.
196 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
197 * See SalvageableService for details.
199 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
200 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
203 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
204 if ( self
::$instance === null ) {
205 // no global instance yet, nothing to reset
209 self
::failIfResetNotAllowed( __METHOD__
);
211 if ( $bootstrapConfig === null ) {
212 $bootstrapConfig = self
::$instance->getBootstrapConfig();
215 $oldInstance = self
::$instance;
217 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
218 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
220 if ( $quick === 'quick' ) {
221 self
::$instance->salvage( $oldInstance );
223 $oldInstance->destroy();
228 * Salvages the state of any salvageable service instances in $other.
230 * @note $other will have been destroyed when salvage() returns.
232 * @param MediaWikiServices $other
234 private function salvage( self
$other ) {
235 foreach ( $this->getServiceNames() as $name ) {
236 // The service could be new in the new instance and not registered in the
237 // other instance (e.g. an extension that was loaded after the instantiation of
238 // the other instance. Skip this service in this case. See T143974
240 $oldService = $other->peekService( $name );
241 } catch ( NoSuchServiceException
$e ) {
245 if ( $oldService instanceof SalvageableService
) {
246 /** @var SalvageableService $newService */
247 $newService = $this->getService( $name );
248 $newService->salvage( $oldService );
256 * Creates a new MediaWikiServices instance and initializes it according to the
257 * given $bootstrapConfig. In particular, all wiring files defined in the
258 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
260 * @param Config|null $bootstrapConfig The Config object to be registered as the
261 * 'BootstrapConfig' service.
263 * @param string $loadWiring set this to 'load' to load the wiring files specified
264 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
266 * @return MediaWikiServices
267 * @throws MWException
268 * @throws \FatalError
270 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
271 $instance = new self( $bootstrapConfig );
273 // Load the default wiring from the specified files.
274 if ( $loadWiring === 'load' ) {
275 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
276 $instance->loadWiringFiles( $wiringFiles );
279 // Provide a traditional hook point to allow extensions to configure services.
280 Hooks
::run( 'MediaWikiServices', [ $instance ] );
286 * Disables all storage layer services. After calling this, any attempt to access the
287 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
292 * @warning This is intended for extreme situations only and should never be used
293 * while serving normal web requests. Legitimate use cases for this method include
294 * the installation process. Test fixtures may also use this, if the fixture relies
297 * @see resetGlobalInstance()
298 * @see resetChildProcessServices()
300 public static function disableStorageBackend() {
301 // TODO: also disable some Caches, JobQueues, etc
302 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
303 $services = self
::getInstance();
305 foreach ( $destroy as $name ) {
306 $services->disableService( $name );
309 ObjectCache
::clear();
313 * Resets any services that may have become stale after a child process
314 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
315 * to call this method from the parent process.
319 * @note This is intended for use in the context of process forking only!
321 * @see resetGlobalInstance()
322 * @see disableStorageBackend()
324 public static function resetChildProcessServices() {
325 // NOTE: for now, just reset everything. Since we don't know the interdependencies
326 // between services, we can't do this more selectively at this time.
327 self
::resetGlobalInstance();
329 // Child, reseed because there is no bug in PHP:
330 // https://bugs.php.net/bug.php?id=42465
331 mt_srand( getmypid() );
335 * Resets the given service for testing purposes.
339 * @warning This is generally unsafe! Other services may still retain references
340 * to the stale service instance, leading to failures and inconsistencies. Subclasses
341 * may use this method to reset specific services under specific instances, but
342 * it should not be exposed to application logic.
344 * @note With proper dependency injection used throughout the codebase, this method
345 * should not be needed. It is provided to allow tests that pollute global service
346 * instances to clean up.
348 * @param string $name
349 * @param bool $destroy Whether the service instance should be destroyed if it exists.
350 * When set to false, any existing service instance will effectively be detached
351 * from the container.
353 * @throws MWException if called outside of PHPUnit tests.
355 public function resetServiceForTesting( $name, $destroy = true ) {
356 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
357 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
360 $this->resetService( $name, $destroy );
364 * Convenience method that throws an exception unless it is called during a phase in which
365 * resetting of global services is allowed. In general, services should not be reset
366 * individually, since that may introduce inconsistencies.
370 * This method will throw an exception if:
372 * - self::$resetInProgress is false (to allow all services to be reset together
373 * via resetGlobalInstance)
374 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
375 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
377 * This method is intended to be used to safeguard against accidentally resetting
378 * global service instances that are not yet managed by MediaWikiServices. It is
379 * defined here in the MediaWikiServices services class to have a central place
380 * for managing service bootstrapping and resetting.
382 * @param string $method the name of the caller method, as given by __METHOD__.
384 * @throws MWException if called outside bootstrap mode.
386 * @see resetGlobalInstance()
387 * @see forceGlobalInstance()
388 * @see disableStorageBackend()
390 public static function failIfResetNotAllowed( $method ) {
391 if ( !defined( 'MW_PHPUNIT_TEST' )
392 && !defined( 'MW_PARSER_TEST' )
393 && !defined( 'MEDIAWIKI_INSTALL' )
394 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
395 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
397 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
402 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
403 * This has to contain at least the information needed to set up the 'ConfigFactory'
406 public function __construct( Config
$config ) {
407 parent
::__construct();
409 // Register the given Config object as the bootstrap config service.
410 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
415 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
419 * @return ActorMigration
421 public function getActorMigration() {
422 return $this->getService( 'ActorMigration' );
429 public function getBlobStore() {
430 return $this->getService( '_SqlBlobStore' );
435 * @return BlobStoreFactory
437 public function getBlobStoreFactory() {
438 return $this->getService( 'BlobStoreFactory' );
443 * @return BlockManager
445 public function getBlockManager() : BlockManager
{
446 return $this->getService( 'BlockManager' );
451 * @return BlockRestrictionStore
453 public function getBlockRestrictionStore() : BlockRestrictionStore
{
454 return $this->getService( 'BlockRestrictionStore' );
458 * Returns the Config object containing the bootstrap configuration.
459 * Bootstrap configuration would typically include database credentials
460 * and other information that may be needed before the ConfigFactory
461 * service can be instantiated.
463 * @note This should only be used during bootstrapping, in particular
464 * when creating the MainConfig service. Application logic should
465 * use getMainConfig() to get a Config instances.
470 public function getBootstrapConfig() {
471 return $this->getService( 'BootstrapConfig' );
476 * @return NameTableStore
478 public function getChangeTagDefStore() {
479 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
484 * @return CommentStore
486 public function getCommentStore() {
487 return $this->getService( 'CommentStore' );
492 * @return ConfigFactory
494 public function getConfigFactory() {
495 return $this->getService( 'ConfigFactory' );
500 * @return ConfigRepository
502 public function getConfigRepository() {
503 return $this->getService( 'ConfigRepository' );
508 * @return \ConfiguredReadOnlyMode
510 public function getConfiguredReadOnlyMode() {
511 return $this->getService( 'ConfiguredReadOnlyMode' );
518 public function getContentLanguage() {
519 return $this->getService( 'ContentLanguage' );
524 * @return NameTableStore
526 public function getContentModelStore() {
527 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
534 public function getCryptHKDF() {
535 return $this->getService( 'CryptHKDF' );
540 * @return DateFormatterFactory
542 public function getDateFormatterFactory() {
543 return $this->getService( 'DateFormatterFactory' );
548 * @return LoadBalancer The main DB load balancer for the local wiki.
550 public function getDBLoadBalancer() {
551 return $this->getService( 'DBLoadBalancer' );
558 public function getDBLoadBalancerFactory() {
559 return $this->getService( 'DBLoadBalancerFactory' );
564 * @return EventRelayerGroup
566 public function getEventRelayerGroup() {
567 return $this->getService( 'EventRelayerGroup' );
572 * @return \ExternalStoreFactory
574 public function getExternalStoreFactory() {
575 return $this->getService( 'ExternalStoreFactory' );
580 * @return GenderCache
582 public function getGenderCache() {
583 return $this->getService( 'GenderCache' );
588 * @return HttpRequestFactory
590 public function getHttpRequestFactory() {
591 return $this->getService( 'HttpRequestFactory' );
596 * @return InterwikiLookup
598 public function getInterwikiLookup() {
599 return $this->getService( 'InterwikiLookup' );
606 public function getLinkCache() {
607 return $this->getService( 'LinkCache' );
611 * LinkRenderer instance that can be used
612 * if no custom options are needed
615 * @return LinkRenderer
617 public function getLinkRenderer() {
618 return $this->getService( 'LinkRenderer' );
623 * @return LinkRendererFactory
625 public function getLinkRendererFactory() {
626 return $this->getService( 'LinkRendererFactory' );
633 public function getLocalServerObjectCache() {
634 return $this->getService( 'LocalServerObjectCache' );
639 * @return MagicWordFactory
641 public function getMagicWordFactory() {
642 return $this->getService( 'MagicWordFactory' );
646 * Returns the Config object that provides configuration for MediaWiki core.
647 * This may or may not be the same object that is returned by getBootstrapConfig().
652 public function getMainConfig() {
653 return $this->getService( 'MainConfig' );
660 public function getMainObjectStash() {
661 return $this->getService( 'MainObjectStash' );
666 * @return \WANObjectCache
668 public function getMainWANObjectCache() {
669 return $this->getService( 'MainWANObjectCache' );
674 * @return MediaHandlerFactory
676 public function getMediaHandlerFactory() {
677 return $this->getService( 'MediaHandlerFactory' );
682 * @return MimeAnalyzer
684 public function getMimeAnalyzer() {
685 return $this->getService( 'MimeAnalyzer' );
690 * @return NamespaceInfo
692 public function getNamespaceInfo() : NamespaceInfo
{
693 return $this->getService( 'NamespaceInfo' );
698 * @return NameTableStoreFactory
700 public function getNameTableStoreFactory() {
701 return $this->getService( 'NameTableStoreFactory' );
706 * @return OldRevisionImporter
708 public function getOldRevisionImporter() {
709 return $this->getService( 'OldRevisionImporter' );
713 * @return PageEditStash
716 public function getPageEditStash() {
717 return $this->getService( 'PageEditStash' );
724 public function getParser() {
725 return $this->getService( 'Parser' );
730 * @return ParserCache
732 public function getParserCache() {
733 return $this->getService( 'ParserCache' );
738 * @return ParserFactory
740 public function getParserFactory() {
741 return $this->getService( 'ParserFactory' );
746 * @return PasswordFactory
748 public function getPasswordFactory() {
749 return $this->getService( 'PasswordFactory' );
754 * @return StatsdDataFactoryInterface
756 public function getPerDbNameStatsdDataFactory() {
757 return $this->getService( 'PerDbNameStatsdDataFactory' );
762 * @return PermissionManager
764 public function getPermissionManager() {
765 return $this->getService( 'PermissionManager' );
770 * @return PreferencesFactory
772 public function getPreferencesFactory() {
773 return $this->getService( 'PreferencesFactory' );
778 * @return ProxyLookup
780 public function getProxyLookup() {
781 return $this->getService( 'ProxyLookup' );
786 * @return \ReadOnlyMode
788 public function getReadOnlyMode() {
789 return $this->getService( 'ReadOnlyMode' );
794 * @return ResourceLoader
796 public function getResourceLoader() {
797 return $this->getService( 'ResourceLoader' );
802 * @return RevisionFactory
804 public function getRevisionFactory() {
805 return $this->getService( 'RevisionFactory' );
810 * @return RevisionLookup
812 public function getRevisionLookup() {
813 return $this->getService( 'RevisionLookup' );
818 * @return RevisionRenderer
820 public function getRevisionRenderer() {
821 return $this->getService( 'RevisionRenderer' );
826 * @return RevisionStore
828 public function getRevisionStore() {
829 return $this->getService( 'RevisionStore' );
834 * @return RevisionStoreFactory
836 public function getRevisionStoreFactory() {
837 return $this->getService( 'RevisionStoreFactory' );
842 * @return SearchEngine
844 public function newSearchEngine() {
845 // New engine object every time, since they keep state
846 return $this->getService( 'SearchEngineFactory' )->create();
851 * @return SearchEngineConfig
853 public function getSearchEngineConfig() {
854 return $this->getService( 'SearchEngineConfig' );
859 * @return SearchEngineFactory
861 public function getSearchEngineFactory() {
862 return $this->getService( 'SearchEngineFactory' );
867 * @return CommandFactory
869 public function getShellCommandFactory() {
870 return $this->getService( 'ShellCommandFactory' );
877 public function getSiteLookup() {
878 return $this->getService( 'SiteLookup' );
885 public function getSiteStore() {
886 return $this->getService( 'SiteStore' );
891 * @return SkinFactory
893 public function getSkinFactory() {
894 return $this->getService( 'SkinFactory' );
899 * @return SlotRoleRegistry
901 public function getSlotRoleRegistry() {
902 return $this->getService( 'SlotRoleRegistry' );
907 * @return NameTableStore
909 public function getSlotRoleStore() {
910 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
915 * @return SpecialPageFactory
917 public function getSpecialPageFactory() : SpecialPageFactory
{
918 return $this->getService( 'SpecialPageFactory' );
923 * @return IBufferingStatsdDataFactory
925 public function getStatsdDataFactory() {
926 return $this->getService( 'StatsdDataFactory' );
931 * @return TitleFormatter
933 public function getTitleFormatter() {
934 return $this->getService( 'TitleFormatter' );
939 * @return TitleParser
941 public function getTitleParser() {
942 return $this->getService( 'TitleParser' );
947 * @return UploadRevisionImporter
949 public function getUploadRevisionImporter() {
950 return $this->getService( 'UploadRevisionImporter' );
955 * @return VirtualRESTServiceClient
957 public function getVirtualRESTServiceClient() {
958 return $this->getService( 'VirtualRESTServiceClient' );
963 * @return WatchedItemQueryService
965 public function getWatchedItemQueryService() {
966 return $this->getService( 'WatchedItemQueryService' );
971 * @return WatchedItemStoreInterface
973 public function getWatchedItemStore() {
974 return $this->getService( 'WatchedItemStore' );
979 * @return \OldRevisionImporter
981 public function getWikiRevisionOldRevisionImporter() {
982 return $this->getService( 'OldRevisionImporter' );
987 * @return \OldRevisionImporter
989 public function getWikiRevisionOldRevisionImporterNoUpdates() {
990 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
995 * @return \UploadRevisionImporter
997 public function getWikiRevisionUploadImporter() {
998 return $this->getService( 'UploadRevisionImporter' );