9 use DateFormatterFactory
;
10 use EventRelayerGroup
;
14 use IBufferingStatsdDataFactory
;
15 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
16 use MediaWiki\Block\BlockRestrictionStore
;
17 use MediaWiki\Http\HttpRequestFactory
;
18 use MediaWiki\Permissions\PermissionManager
;
19 use MediaWiki\Preferences\PreferencesFactory
;
20 use MediaWiki\Shell\CommandFactory
;
21 use MediaWiki\Revision\RevisionRenderer
;
22 use MediaWiki\Revision\SlotRoleRegistry
;
23 use MediaWiki\Special\SpecialPageFactory
;
24 use MediaWiki\Storage\BlobStore
;
25 use MediaWiki\Storage\BlobStoreFactory
;
26 use MediaWiki\Storage\NameTableStore
;
27 use MediaWiki\Storage\NameTableStoreFactory
;
28 use MediaWiki\Revision\RevisionFactory
;
29 use MediaWiki\Revision\RevisionLookup
;
30 use MediaWiki\Revision\RevisionStore
;
31 use OldRevisionImporter
;
32 use MediaWiki\Revision\RevisionStoreFactory
;
33 use UploadRevisionImporter
;
34 use Wikimedia\Rdbms\LBFactory
;
36 use Wikimedia\Rdbms\LoadBalancer
;
37 use MediaHandlerFactory
;
38 use MediaWiki\Config\ConfigRepository
;
39 use MediaWiki\Linker\LinkRenderer
;
40 use MediaWiki\Linker\LinkRendererFactory
;
52 use SearchEngineConfig
;
53 use SearchEngineFactory
;
56 use WatchedItemStoreInterface
;
57 use WatchedItemQueryService
;
61 use VirtualRESTServiceClient
;
62 use Wikimedia\Services\SalvageableService
;
63 use Wikimedia\Services\ServiceContainer
;
64 use Wikimedia\Services\NoSuchServiceException
;
65 use MediaWiki\Interwiki\InterwikiLookup
;
67 use MediaWiki\Storage\PageEditStash
;
70 * Service locator for MediaWiki core services.
72 * This program is free software; you can redistribute it and/or modify
73 * it under the terms of the GNU General Public License as published by
74 * the Free Software Foundation; either version 2 of the License, or
75 * (at your option) any later version.
77 * This program is distributed in the hope that it will be useful,
78 * but WITHOUT ANY WARRANTY; without even the implied warranty of
79 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80 * GNU General Public License for more details.
82 * You should have received a copy of the GNU General Public License along
83 * with this program; if not, write to the Free Software Foundation, Inc.,
84 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
85 * http://www.gnu.org/copyleft/gpl.html
93 * MediaWikiServices is the service locator for the application scope of MediaWiki.
94 * Its implemented as a simple configurable DI container.
95 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
96 * the network of service objects that defines MediaWiki's application logic.
97 * It acts as an entry point to MediaWiki's dependency injection mechanism.
99 * Services are defined in the "wiring" array passed to the constructor,
100 * or by calling defineService().
102 * @see docs/injection.txt for an overview of using dependency injection in the
103 * MediaWiki code base.
105 class MediaWikiServices
extends ServiceContainer
{
108 * @var MediaWikiServices|null
110 private static $instance = null;
113 * Returns the global default instance of the top level service locator.
117 * The default instance is initialized using the service instantiator functions
118 * defined in ServiceWiring.php.
120 * @note This should only be called by static functions! The instance returned here
121 * should not be passed around! Objects that need access to a service should have
122 * that service injected into the constructor, never a service locator!
124 * @return MediaWikiServices
126 public static function getInstance() {
127 if ( self
::$instance === null ) {
128 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
129 // but some information from the global scope has to be injected here,
130 // even if it's just a file name or database credentials to load
131 // configuration from.
132 $bootstrapConfig = new GlobalVarConfig();
133 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
136 return self
::$instance;
140 * Replaces the global MediaWikiServices instance.
144 * @note This is for use in PHPUnit tests only!
146 * @throws MWException if called outside of PHPUnit tests.
148 * @param MediaWikiServices $services The new MediaWikiServices object.
150 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
152 public static function forceGlobalInstance( MediaWikiServices
$services ) {
153 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
154 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
157 $old = self
::getInstance();
158 self
::$instance = $services;
164 * Creates a new instance of MediaWikiServices and sets it as the global default
165 * instance. getInstance() will return a different MediaWikiServices object
166 * after every call to resetGlobalInstance().
170 * @warning This should not be used during normal operation. It is intended for use
171 * when the configuration has changed significantly since bootstrap time, e.g.
172 * during the installation process or during testing.
174 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
175 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
176 * any of the services managed by MediaWikiServices exist. If any service objects
177 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
178 * with the operation of the services managed by the new MediaWikiServices.
179 * Operating with a mix of services created by the old and the new
180 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
181 * Any class implementing LAZY LOADING is especially prone to this problem,
182 * since instances would typically retain a reference to a storage layer service.
184 * @see forceGlobalInstance()
185 * @see resetGlobalInstance()
186 * @see resetBetweenTest()
188 * @param Config|null $bootstrapConfig The Config object to be registered as the
189 * 'BootstrapConfig' service. This has to contain at least the information
190 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
191 * config of the old instance of MediaWikiServices will be re-used. If there
192 * was no previous instance, a new GlobalVarConfig object will be used to
193 * bootstrap the services.
195 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
196 * See SalvageableService for details.
198 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
199 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
202 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
203 if ( self
::$instance === null ) {
204 // no global instance yet, nothing to reset
208 self
::failIfResetNotAllowed( __METHOD__
);
210 if ( $bootstrapConfig === null ) {
211 $bootstrapConfig = self
::$instance->getBootstrapConfig();
214 $oldInstance = self
::$instance;
216 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
217 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
219 if ( $quick === 'quick' ) {
220 self
::$instance->salvage( $oldInstance );
222 $oldInstance->destroy();
227 * Salvages the state of any salvageable service instances in $other.
229 * @note $other will have been destroyed when salvage() returns.
231 * @param MediaWikiServices $other
233 private function salvage( self
$other ) {
234 foreach ( $this->getServiceNames() as $name ) {
235 // The service could be new in the new instance and not registered in the
236 // other instance (e.g. an extension that was loaded after the instantiation of
237 // the other instance. Skip this service in this case. See T143974
239 $oldService = $other->peekService( $name );
240 } catch ( NoSuchServiceException
$e ) {
244 if ( $oldService instanceof SalvageableService
) {
245 /** @var SalvageableService $newService */
246 $newService = $this->getService( $name );
247 $newService->salvage( $oldService );
255 * Creates a new MediaWikiServices instance and initializes it according to the
256 * given $bootstrapConfig. In particular, all wiring files defined in the
257 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
259 * @param Config|null $bootstrapConfig The Config object to be registered as the
260 * 'BootstrapConfig' service.
262 * @param string $loadWiring set this to 'load' to load the wiring files specified
263 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
265 * @return MediaWikiServices
266 * @throws MWException
267 * @throws \FatalError
269 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
270 $instance = new self( $bootstrapConfig );
272 // Load the default wiring from the specified files.
273 if ( $loadWiring === 'load' ) {
274 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
275 $instance->loadWiringFiles( $wiringFiles );
278 // Provide a traditional hook point to allow extensions to configure services.
279 Hooks
::run( 'MediaWikiServices', [ $instance ] );
285 * Disables all storage layer services. After calling this, any attempt to access the
286 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
291 * @warning This is intended for extreme situations only and should never be used
292 * while serving normal web requests. Legitimate use cases for this method include
293 * the installation process. Test fixtures may also use this, if the fixture relies
296 * @see resetGlobalInstance()
297 * @see resetChildProcessServices()
299 public static function disableStorageBackend() {
300 // TODO: also disable some Caches, JobQueues, etc
301 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
302 $services = self
::getInstance();
304 foreach ( $destroy as $name ) {
305 $services->disableService( $name );
308 ObjectCache
::clear();
312 * Resets any services that may have become stale after a child process
313 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
314 * to call this method from the parent process.
318 * @note This is intended for use in the context of process forking only!
320 * @see resetGlobalInstance()
321 * @see disableStorageBackend()
323 public static function resetChildProcessServices() {
324 // NOTE: for now, just reset everything. Since we don't know the interdependencies
325 // between services, we can't do this more selectively at this time.
326 self
::resetGlobalInstance();
328 // Child, reseed because there is no bug in PHP:
329 // https://bugs.php.net/bug.php?id=42465
330 mt_srand( getmypid() );
334 * Resets the given service for testing purposes.
338 * @warning This is generally unsafe! Other services may still retain references
339 * to the stale service instance, leading to failures and inconsistencies. Subclasses
340 * may use this method to reset specific services under specific instances, but
341 * it should not be exposed to application logic.
343 * @note With proper dependency injection used throughout the codebase, this method
344 * should not be needed. It is provided to allow tests that pollute global service
345 * instances to clean up.
347 * @param string $name
348 * @param bool $destroy Whether the service instance should be destroyed if it exists.
349 * When set to false, any existing service instance will effectively be detached
350 * from the container.
352 * @throws MWException if called outside of PHPUnit tests.
354 public function resetServiceForTesting( $name, $destroy = true ) {
355 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
356 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
359 $this->resetService( $name, $destroy );
363 * Convenience method that throws an exception unless it is called during a phase in which
364 * resetting of global services is allowed. In general, services should not be reset
365 * individually, since that may introduce inconsistencies.
369 * This method will throw an exception if:
371 * - self::$resetInProgress is false (to allow all services to be reset together
372 * via resetGlobalInstance)
373 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
374 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
376 * This method is intended to be used to safeguard against accidentally resetting
377 * global service instances that are not yet managed by MediaWikiServices. It is
378 * defined here in the MediaWikiServices services class to have a central place
379 * for managing service bootstrapping and resetting.
381 * @param string $method the name of the caller method, as given by __METHOD__.
383 * @throws MWException if called outside bootstrap mode.
385 * @see resetGlobalInstance()
386 * @see forceGlobalInstance()
387 * @see disableStorageBackend()
389 public static function failIfResetNotAllowed( $method ) {
390 if ( !defined( 'MW_PHPUNIT_TEST' )
391 && !defined( 'MW_PARSER_TEST' )
392 && !defined( 'MEDIAWIKI_INSTALL' )
393 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
394 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
396 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
401 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
402 * This has to contain at least the information needed to set up the 'ConfigFactory'
405 public function __construct( Config
$config ) {
406 parent
::__construct();
408 // Register the given Config object as the bootstrap config service.
409 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
414 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
418 * @return ActorMigration
420 public function getActorMigration() {
421 return $this->getService( 'ActorMigration' );
428 public function getBlobStore() {
429 return $this->getService( '_SqlBlobStore' );
434 * @return BlobStoreFactory
436 public function getBlobStoreFactory() {
437 return $this->getService( 'BlobStoreFactory' );
442 * @return BlockRestrictionStore
444 public function getBlockRestrictionStore() : BlockRestrictionStore
{
445 return $this->getService( 'BlockRestrictionStore' );
449 * Returns the Config object containing the bootstrap configuration.
450 * Bootstrap configuration would typically include database credentials
451 * and other information that may be needed before the ConfigFactory
452 * service can be instantiated.
454 * @note This should only be used during bootstrapping, in particular
455 * when creating the MainConfig service. Application logic should
456 * use getMainConfig() to get a Config instances.
461 public function getBootstrapConfig() {
462 return $this->getService( 'BootstrapConfig' );
467 * @return NameTableStore
469 public function getChangeTagDefStore() {
470 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
475 * @return CommentStore
477 public function getCommentStore() {
478 return $this->getService( 'CommentStore' );
483 * @return ConfigFactory
485 public function getConfigFactory() {
486 return $this->getService( 'ConfigFactory' );
491 * @return ConfigRepository
493 public function getConfigRepository() {
494 return $this->getService( 'ConfigRepository' );
499 * @return \ConfiguredReadOnlyMode
501 public function getConfiguredReadOnlyMode() {
502 return $this->getService( 'ConfiguredReadOnlyMode' );
509 public function getContentLanguage() {
510 return $this->getService( 'ContentLanguage' );
515 * @return NameTableStore
517 public function getContentModelStore() {
518 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
525 public function getCryptHKDF() {
526 return $this->getService( 'CryptHKDF' );
531 * @return DateFormatterFactory
533 public function getDateFormatterFactory() {
534 return $this->getService( 'DateFormatterFactory' );
539 * @return LoadBalancer The main DB load balancer for the local wiki.
541 public function getDBLoadBalancer() {
542 return $this->getService( 'DBLoadBalancer' );
549 public function getDBLoadBalancerFactory() {
550 return $this->getService( 'DBLoadBalancerFactory' );
555 * @return EventRelayerGroup
557 public function getEventRelayerGroup() {
558 return $this->getService( 'EventRelayerGroup' );
563 * @return \ExternalStoreFactory
565 public function getExternalStoreFactory() {
566 return $this->getService( 'ExternalStoreFactory' );
571 * @return GenderCache
573 public function getGenderCache() {
574 return $this->getService( 'GenderCache' );
579 * @return HttpRequestFactory
581 public function getHttpRequestFactory() {
582 return $this->getService( 'HttpRequestFactory' );
587 * @return InterwikiLookup
589 public function getInterwikiLookup() {
590 return $this->getService( 'InterwikiLookup' );
597 public function getLinkCache() {
598 return $this->getService( 'LinkCache' );
602 * LinkRenderer instance that can be used
603 * if no custom options are needed
606 * @return LinkRenderer
608 public function getLinkRenderer() {
609 return $this->getService( 'LinkRenderer' );
614 * @return LinkRendererFactory
616 public function getLinkRendererFactory() {
617 return $this->getService( 'LinkRendererFactory' );
624 public function getLocalServerObjectCache() {
625 return $this->getService( 'LocalServerObjectCache' );
630 * @return MagicWordFactory
632 public function getMagicWordFactory() {
633 return $this->getService( 'MagicWordFactory' );
637 * Returns the Config object that provides configuration for MediaWiki core.
638 * This may or may not be the same object that is returned by getBootstrapConfig().
643 public function getMainConfig() {
644 return $this->getService( 'MainConfig' );
651 public function getMainObjectStash() {
652 return $this->getService( 'MainObjectStash' );
657 * @return \WANObjectCache
659 public function getMainWANObjectCache() {
660 return $this->getService( 'MainWANObjectCache' );
665 * @return MediaHandlerFactory
667 public function getMediaHandlerFactory() {
668 return $this->getService( 'MediaHandlerFactory' );
673 * @return MimeAnalyzer
675 public function getMimeAnalyzer() {
676 return $this->getService( 'MimeAnalyzer' );
681 * @return NamespaceInfo
683 public function getNamespaceInfo() : NamespaceInfo
{
684 return $this->getService( 'NamespaceInfo' );
689 * @return NameTableStoreFactory
691 public function getNameTableStoreFactory() {
692 return $this->getService( 'NameTableStoreFactory' );
697 * @return OldRevisionImporter
699 public function getOldRevisionImporter() {
700 return $this->getService( 'OldRevisionImporter' );
704 * @return PageEditStash
707 public function getPageEditStash() {
708 return $this->getService( 'PageEditStash' );
715 public function getParser() {
716 return $this->getService( 'Parser' );
721 * @return ParserCache
723 public function getParserCache() {
724 return $this->getService( 'ParserCache' );
729 * @return ParserFactory
731 public function getParserFactory() {
732 return $this->getService( 'ParserFactory' );
737 * @return PasswordFactory
739 public function getPasswordFactory() {
740 return $this->getService( 'PasswordFactory' );
745 * @return StatsdDataFactoryInterface
747 public function getPerDbNameStatsdDataFactory() {
748 return $this->getService( 'PerDbNameStatsdDataFactory' );
753 * @return PermissionManager
755 public function getPermissionManager() {
756 return $this->getService( 'PermissionManager' );
761 * @return PreferencesFactory
763 public function getPreferencesFactory() {
764 return $this->getService( 'PreferencesFactory' );
769 * @return ProxyLookup
771 public function getProxyLookup() {
772 return $this->getService( 'ProxyLookup' );
777 * @return \ReadOnlyMode
779 public function getReadOnlyMode() {
780 return $this->getService( 'ReadOnlyMode' );
785 * @return ResourceLoader
787 public function getResourceLoader() {
788 return $this->getService( 'ResourceLoader' );
793 * @return RevisionFactory
795 public function getRevisionFactory() {
796 return $this->getService( 'RevisionFactory' );
801 * @return RevisionLookup
803 public function getRevisionLookup() {
804 return $this->getService( 'RevisionLookup' );
809 * @return RevisionRenderer
811 public function getRevisionRenderer() {
812 return $this->getService( 'RevisionRenderer' );
817 * @return RevisionStore
819 public function getRevisionStore() {
820 return $this->getService( 'RevisionStore' );
825 * @return RevisionStoreFactory
827 public function getRevisionStoreFactory() {
828 return $this->getService( 'RevisionStoreFactory' );
833 * @return SearchEngine
835 public function newSearchEngine() {
836 // New engine object every time, since they keep state
837 return $this->getService( 'SearchEngineFactory' )->create();
842 * @return SearchEngineConfig
844 public function getSearchEngineConfig() {
845 return $this->getService( 'SearchEngineConfig' );
850 * @return SearchEngineFactory
852 public function getSearchEngineFactory() {
853 return $this->getService( 'SearchEngineFactory' );
858 * @return CommandFactory
860 public function getShellCommandFactory() {
861 return $this->getService( 'ShellCommandFactory' );
868 public function getSiteLookup() {
869 return $this->getService( 'SiteLookup' );
876 public function getSiteStore() {
877 return $this->getService( 'SiteStore' );
882 * @return SkinFactory
884 public function getSkinFactory() {
885 return $this->getService( 'SkinFactory' );
890 * @return SlotRoleRegistry
892 public function getSlotRoleRegistry() {
893 return $this->getService( 'SlotRoleRegistry' );
898 * @return NameTableStore
900 public function getSlotRoleStore() {
901 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
906 * @return SpecialPageFactory
908 public function getSpecialPageFactory() : SpecialPageFactory
{
909 return $this->getService( 'SpecialPageFactory' );
914 * @return IBufferingStatsdDataFactory
916 public function getStatsdDataFactory() {
917 return $this->getService( 'StatsdDataFactory' );
922 * @return TitleFormatter
924 public function getTitleFormatter() {
925 return $this->getService( 'TitleFormatter' );
930 * @return TitleParser
932 public function getTitleParser() {
933 return $this->getService( 'TitleParser' );
938 * @return UploadRevisionImporter
940 public function getUploadRevisionImporter() {
941 return $this->getService( 'UploadRevisionImporter' );
946 * @return VirtualRESTServiceClient
948 public function getVirtualRESTServiceClient() {
949 return $this->getService( 'VirtualRESTServiceClient' );
954 * @return WatchedItemQueryService
956 public function getWatchedItemQueryService() {
957 return $this->getService( 'WatchedItemQueryService' );
962 * @return WatchedItemStoreInterface
964 public function getWatchedItemStore() {
965 return $this->getService( 'WatchedItemStore' );
970 * @return \OldRevisionImporter
972 public function getWikiRevisionOldRevisionImporter() {
973 return $this->getService( 'OldRevisionImporter' );
978 * @return \OldRevisionImporter
980 public function getWikiRevisionOldRevisionImporterNoUpdates() {
981 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
986 * @return \UploadRevisionImporter
988 public function getWikiRevisionUploadImporter() {
989 return $this->getService( 'UploadRevisionImporter' );