10 use DateFormatterFactory
;
11 use EventRelayerGroup
;
15 use IBufferingStatsdDataFactory
;
16 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
17 use LocalisationCache
;
18 use MediaWiki\Block\BlockManager
;
19 use MediaWiki\Block\BlockRestrictionStore
;
20 use MediaWiki\FileBackend\FSFile\TempFSFileFactory
;
21 use MediaWiki\Http\HttpRequestFactory
;
22 use MediaWiki\Languages\LanguageNameUtils
;
23 use MediaWiki\Page\MovePageFactory
;
24 use MediaWiki\Permissions\PermissionManager
;
25 use MediaWiki\Preferences\PreferencesFactory
;
26 use MediaWiki\Revision\RevisionRenderer
;
27 use MediaWiki\Revision\SlotRoleRegistry
;
28 use MediaWiki\Shell\CommandFactory
;
29 use MediaWiki\Special\SpecialPageFactory
;
30 use MediaWiki\Storage\BlobStore
;
31 use MediaWiki\Storage\BlobStoreFactory
;
32 use MediaWiki\Storage\NameTableStore
;
33 use MediaWiki\Storage\NameTableStoreFactory
;
34 use MediaWiki\Revision\RevisionFactory
;
35 use MediaWiki\Revision\RevisionLookup
;
36 use MediaWiki\Revision\RevisionStore
;
37 use OldRevisionImporter
;
38 use MediaWiki\Revision\RevisionStoreFactory
;
39 use UploadRevisionImporter
;
40 use Wikimedia\Rdbms\ILoadBalancer
;
42 use MediaHandlerFactory
;
43 use MediaWiki\Config\ConfigRepository
;
44 use MediaWiki\Linker\LinkRenderer
;
45 use MediaWiki\Linker\LinkRendererFactory
;
59 use SearchEngineConfig
;
60 use SearchEngineFactory
;
63 use WatchedItemStoreInterface
;
64 use WatchedItemQueryService
;
68 use VirtualRESTServiceClient
;
69 use Wikimedia\ObjectFactory
;
70 use Wikimedia\Rdbms\LBFactory
;
71 use Wikimedia\Services\SalvageableService
;
72 use Wikimedia\Services\ServiceContainer
;
73 use Wikimedia\Services\NoSuchServiceException
;
74 use MediaWiki\Interwiki\InterwikiLookup
;
76 use MediaWiki\Storage\PageEditStash
;
79 * Service locator for MediaWiki core services.
81 * This program is free software; you can redistribute it and/or modify
82 * it under the terms of the GNU General Public License as published by
83 * the Free Software Foundation; either version 2 of the License, or
84 * (at your option) any later version.
86 * This program is distributed in the hope that it will be useful,
87 * but WITHOUT ANY WARRANTY; without even the implied warranty of
88 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
89 * GNU General Public License for more details.
91 * You should have received a copy of the GNU General Public License along
92 * with this program; if not, write to the Free Software Foundation, Inc.,
93 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
94 * http://www.gnu.org/copyleft/gpl.html
102 * MediaWikiServices is the service locator for the application scope of MediaWiki.
103 * Its implemented as a simple configurable DI container.
104 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
105 * the network of service objects that defines MediaWiki's application logic.
106 * It acts as an entry point to MediaWiki's dependency injection mechanism.
108 * Services are defined in the "wiring" array passed to the constructor,
109 * or by calling defineService().
111 * @see docs/injection.txt for an overview of using dependency injection in the
112 * MediaWiki code base.
114 class MediaWikiServices
extends ServiceContainer
{
117 * @var MediaWikiServices|null
119 private static $instance = null;
122 * Returns the global default instance of the top level service locator.
126 * The default instance is initialized using the service instantiator functions
127 * defined in ServiceWiring.php.
129 * @note This should only be called by static functions! The instance returned here
130 * should not be passed around! Objects that need access to a service should have
131 * that service injected into the constructor, never a service locator!
133 * @return MediaWikiServices
135 public static function getInstance() {
136 if ( self
::$instance === null ) {
137 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
138 // but some information from the global scope has to be injected here,
139 // even if it's just a file name or database credentials to load
140 // configuration from.
141 $bootstrapConfig = new GlobalVarConfig();
142 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
145 return self
::$instance;
149 * Replaces the global MediaWikiServices instance.
153 * @note This is for use in PHPUnit tests only!
155 * @throws MWException if called outside of PHPUnit tests.
157 * @param MediaWikiServices $services The new MediaWikiServices object.
159 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
161 public static function forceGlobalInstance( MediaWikiServices
$services ) {
162 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
163 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
166 $old = self
::getInstance();
167 self
::$instance = $services;
173 * Creates a new instance of MediaWikiServices and sets it as the global default
174 * instance. getInstance() will return a different MediaWikiServices object
175 * after every call to resetGlobalInstance().
179 * @warning This should not be used during normal operation. It is intended for use
180 * when the configuration has changed significantly since bootstrap time, e.g.
181 * during the installation process or during testing.
183 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
184 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
185 * any of the services managed by MediaWikiServices exist. If any service objects
186 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
187 * with the operation of the services managed by the new MediaWikiServices.
188 * Operating with a mix of services created by the old and the new
189 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
190 * Any class implementing LAZY LOADING is especially prone to this problem,
191 * since instances would typically retain a reference to a storage layer service.
193 * @see forceGlobalInstance()
194 * @see resetGlobalInstance()
195 * @see resetBetweenTest()
197 * @param Config|null $bootstrapConfig The Config object to be registered as the
198 * 'BootstrapConfig' service. This has to contain at least the information
199 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
200 * config of the old instance of MediaWikiServices will be re-used. If there
201 * was no previous instance, a new GlobalVarConfig object will be used to
202 * bootstrap the services.
204 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
205 * See SalvageableService for details.
207 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
208 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
211 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
212 if ( self
::$instance === null ) {
213 // no global instance yet, nothing to reset
217 self
::failIfResetNotAllowed( __METHOD__
);
219 if ( $bootstrapConfig === null ) {
220 $bootstrapConfig = self
::$instance->getBootstrapConfig();
223 $oldInstance = self
::$instance;
225 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
226 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
228 if ( $quick === 'quick' ) {
229 self
::$instance->salvage( $oldInstance );
231 $oldInstance->destroy();
235 /** @noinspection PhpDocSignatureInspection */
238 * Salvages the state of any salvageable service instances in $other.
240 * @note $other will have been destroyed when salvage() returns.
242 * @param MediaWikiServices $other
244 private function salvage( self
$other ) {
245 foreach ( $this->getServiceNames() as $name ) {
246 // The service could be new in the new instance and not registered in the
247 // other instance (e.g. an extension that was loaded after the instantiation of
248 // the other instance. Skip this service in this case. See T143974
250 $oldService = $other->peekService( $name );
251 } catch ( NoSuchServiceException
$e ) {
255 if ( $oldService instanceof SalvageableService
) {
256 /** @var SalvageableService $newService */
257 $newService = $this->getService( $name );
258 $newService->salvage( $oldService );
266 * Creates a new MediaWikiServices instance and initializes it according to the
267 * given $bootstrapConfig. In particular, all wiring files defined in the
268 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
270 * @param Config|null $bootstrapConfig The Config object to be registered as the
271 * 'BootstrapConfig' service.
273 * @param string $loadWiring set this to 'load' to load the wiring files specified
274 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
276 * @return MediaWikiServices
277 * @throws MWException
278 * @throws \FatalError
280 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
281 $instance = new self( $bootstrapConfig );
283 // Load the default wiring from the specified files.
284 if ( $loadWiring === 'load' ) {
285 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
286 $instance->loadWiringFiles( $wiringFiles );
289 // Provide a traditional hook point to allow extensions to configure services.
290 Hooks
::run( 'MediaWikiServices', [ $instance ] );
296 * Disables all storage layer services. After calling this, any attempt to access the
297 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
302 * @warning This is intended for extreme situations only and should never be used
303 * while serving normal web requests. Legitimate use cases for this method include
304 * the installation process. Test fixtures may also use this, if the fixture relies
307 * @see resetGlobalInstance()
308 * @see resetChildProcessServices()
310 public static function disableStorageBackend() {
311 // TODO: also disable some Caches, JobQueues, etc
312 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
313 $services = self
::getInstance();
315 foreach ( $destroy as $name ) {
316 $services->disableService( $name );
319 ObjectCache
::clear();
323 * Resets any services that may have become stale after a child process
324 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
325 * to call this method from the parent process.
329 * @note This is intended for use in the context of process forking only!
331 * @see resetGlobalInstance()
332 * @see disableStorageBackend()
334 public static function resetChildProcessServices() {
335 // NOTE: for now, just reset everything. Since we don't know the interdependencies
336 // between services, we can't do this more selectively at this time.
337 self
::resetGlobalInstance();
339 // Child, reseed because there is no bug in PHP:
340 // https://bugs.php.net/bug.php?id=42465
341 mt_srand( getmypid() );
345 * Resets the given service for testing purposes.
349 * @warning This is generally unsafe! Other services may still retain references
350 * to the stale service instance, leading to failures and inconsistencies. Subclasses
351 * may use this method to reset specific services under specific instances, but
352 * it should not be exposed to application logic.
354 * @note With proper dependency injection used throughout the codebase, this method
355 * should not be needed. It is provided to allow tests that pollute global service
356 * instances to clean up.
358 * @param string $name
359 * @param bool $destroy Whether the service instance should be destroyed if it exists.
360 * When set to false, any existing service instance will effectively be detached
361 * from the container.
363 * @throws MWException if called outside of PHPUnit tests.
365 public function resetServiceForTesting( $name, $destroy = true ) {
366 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
367 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
370 $this->resetService( $name, $destroy );
374 * Convenience method that throws an exception unless it is called during a phase in which
375 * resetting of global services is allowed. In general, services should not be reset
376 * individually, since that may introduce inconsistencies.
380 * This method will throw an exception if:
382 * - self::$resetInProgress is false (to allow all services to be reset together
383 * via resetGlobalInstance)
384 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
385 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
387 * This method is intended to be used to safeguard against accidentally resetting
388 * global service instances that are not yet managed by MediaWikiServices. It is
389 * defined here in the MediaWikiServices services class to have a central place
390 * for managing service bootstrapping and resetting.
392 * @param string $method the name of the caller method, as given by __METHOD__.
394 * @throws MWException if called outside bootstrap mode.
396 * @see resetGlobalInstance()
397 * @see forceGlobalInstance()
398 * @see disableStorageBackend()
400 public static function failIfResetNotAllowed( $method ) {
401 if ( !defined( 'MW_PHPUNIT_TEST' )
402 && !defined( 'MW_PARSER_TEST' )
403 && !defined( 'MEDIAWIKI_INSTALL' )
404 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
405 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
407 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
412 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
413 * This has to contain at least the information needed to set up the 'ConfigFactory'
416 public function __construct( Config
$config ) {
417 parent
::__construct();
419 // Register the given Config object as the bootstrap config service.
420 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
425 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
429 * @return ActorMigration
431 public function getActorMigration() {
432 return $this->getService( 'ActorMigration' );
437 * @return BadFileLookup
439 public function getBadFileLookup() : BadFileLookup
{
440 return $this->getService( 'BadFileLookup' );
447 public function getBlobStore() {
448 return $this->getService( '_SqlBlobStore' );
453 * @return BlobStoreFactory
455 public function getBlobStoreFactory() {
456 return $this->getService( 'BlobStoreFactory' );
461 * @return BlockManager
463 public function getBlockManager() : BlockManager
{
464 return $this->getService( 'BlockManager' );
469 * @return BlockRestrictionStore
471 public function getBlockRestrictionStore() : BlockRestrictionStore
{
472 return $this->getService( 'BlockRestrictionStore' );
476 * Returns the Config object containing the bootstrap configuration.
477 * Bootstrap configuration would typically include database credentials
478 * and other information that may be needed before the ConfigFactory
479 * service can be instantiated.
481 * @note This should only be used during bootstrapping, in particular
482 * when creating the MainConfig service. Application logic should
483 * use getMainConfig() to get a Config instances.
488 public function getBootstrapConfig() {
489 return $this->getService( 'BootstrapConfig' );
494 * @return NameTableStore
496 public function getChangeTagDefStore() {
497 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
502 * @return CommentStore
504 public function getCommentStore() {
505 return $this->getService( 'CommentStore' );
510 * @return ConfigFactory
512 public function getConfigFactory() {
513 return $this->getService( 'ConfigFactory' );
518 * @return ConfigRepository
520 public function getConfigRepository() {
521 return $this->getService( 'ConfigRepository' );
526 * @return \ConfiguredReadOnlyMode
528 public function getConfiguredReadOnlyMode() {
529 return $this->getService( 'ConfiguredReadOnlyMode' );
536 public function getContentLanguage() {
537 return $this->getService( 'ContentLanguage' );
542 * @return NameTableStore
544 public function getContentModelStore() {
545 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
552 public function getCryptHKDF() {
553 return $this->getService( 'CryptHKDF' );
558 * @return DateFormatterFactory
560 public function getDateFormatterFactory() {
561 return $this->getService( 'DateFormatterFactory' );
566 * @return ILoadBalancer The main DB load balancer for the local wiki.
568 public function getDBLoadBalancer() {
569 return $this->getService( 'DBLoadBalancer' );
576 public function getDBLoadBalancerFactory() {
577 return $this->getService( 'DBLoadBalancerFactory' );
582 * @return EventRelayerGroup
584 public function getEventRelayerGroup() {
585 return $this->getService( 'EventRelayerGroup' );
590 * @return \ExternalStoreAccess
592 public function getExternalStoreAccess() {
593 return $this->getService( 'ExternalStoreAccess' );
598 * @return \ExternalStoreFactory
600 public function getExternalStoreFactory() {
601 return $this->getService( 'ExternalStoreFactory' );
606 * @return GenderCache
608 public function getGenderCache() {
609 return $this->getService( 'GenderCache' );
614 * @return HttpRequestFactory
616 public function getHttpRequestFactory() {
617 return $this->getService( 'HttpRequestFactory' );
622 * @return InterwikiLookup
624 public function getInterwikiLookup() {
625 return $this->getService( 'InterwikiLookup' );
630 * @return LanguageNameUtils
632 public function getLanguageNameUtils() {
633 return $this->getService( 'LanguageNameUtils' );
640 public function getLinkCache() {
641 return $this->getService( 'LinkCache' );
645 * LinkRenderer instance that can be used
646 * if no custom options are needed
649 * @return LinkRenderer
651 public function getLinkRenderer() {
652 return $this->getService( 'LinkRenderer' );
657 * @return LinkRendererFactory
659 public function getLinkRendererFactory() {
660 return $this->getService( 'LinkRendererFactory' );
665 * @return LocalisationCache
667 public function getLocalisationCache() : LocalisationCache
{
668 return $this->getService( 'LocalisationCache' );
675 public function getLocalServerObjectCache() {
676 return $this->getService( 'LocalServerObjectCache' );
681 * @return MagicWordFactory
683 public function getMagicWordFactory() {
684 return $this->getService( 'MagicWordFactory' );
688 * Returns the Config object that provides configuration for MediaWiki core.
689 * This may or may not be the same object that is returned by getBootstrapConfig().
694 public function getMainConfig() {
695 return $this->getService( 'MainConfig' );
702 public function getMainObjectStash() {
703 return $this->getService( 'MainObjectStash' );
708 * @return \WANObjectCache
710 public function getMainWANObjectCache() {
711 return $this->getService( 'MainWANObjectCache' );
716 * @return MediaHandlerFactory
718 public function getMediaHandlerFactory() {
719 return $this->getService( 'MediaHandlerFactory' );
724 * @return MessageCache
726 public function getMessageCache() : MessageCache
{
727 return $this->getService( 'MessageCache' );
732 * @return MimeAnalyzer
734 public function getMimeAnalyzer() {
735 return $this->getService( 'MimeAnalyzer' );
740 * @return MovePageFactory
742 public function getMovePageFactory() : MovePageFactory
{
743 return $this->getService( 'MovePageFactory' );
748 * @return NamespaceInfo
750 public function getNamespaceInfo() : NamespaceInfo
{
751 return $this->getService( 'NamespaceInfo' );
756 * @return NameTableStoreFactory
758 public function getNameTableStoreFactory() {
759 return $this->getService( 'NameTableStoreFactory' );
763 * ObjectFactory is intended for instantiating "handlers" from declarative definitions,
764 * such as Action API modules, special pages, or REST API handlers.
767 * @return ObjectFactory
769 public function getObjectFactory() {
770 return $this->getService( 'ObjectFactory' );
775 * @return OldRevisionImporter
777 public function getOldRevisionImporter() {
778 return $this->getService( 'OldRevisionImporter' );
782 * @return PageEditStash
785 public function getPageEditStash() {
786 return $this->getService( 'PageEditStash' );
793 public function getParser() {
794 return $this->getService( 'Parser' );
799 * @return ParserCache
801 public function getParserCache() {
802 return $this->getService( 'ParserCache' );
807 * @return ParserFactory
809 public function getParserFactory() {
810 return $this->getService( 'ParserFactory' );
815 * @return PasswordFactory
817 public function getPasswordFactory() {
818 return $this->getService( 'PasswordFactory' );
823 * @return StatsdDataFactoryInterface
825 public function getPerDbNameStatsdDataFactory() {
826 return $this->getService( 'PerDbNameStatsdDataFactory' );
831 * @return PermissionManager
833 public function getPermissionManager() {
834 return $this->getService( 'PermissionManager' );
839 * @return PreferencesFactory
841 public function getPreferencesFactory() {
842 return $this->getService( 'PreferencesFactory' );
847 * @return ProxyLookup
849 public function getProxyLookup() {
850 return $this->getService( 'ProxyLookup' );
855 * @return \ReadOnlyMode
857 public function getReadOnlyMode() {
858 return $this->getService( 'ReadOnlyMode' );
865 public function getRepoGroup() : RepoGroup
{
866 return $this->getService( 'RepoGroup' );
871 * @return ResourceLoader
873 public function getResourceLoader() {
874 return $this->getService( 'ResourceLoader' );
879 * @return RevisionFactory
881 public function getRevisionFactory() {
882 return $this->getService( 'RevisionFactory' );
887 * @return RevisionLookup
889 public function getRevisionLookup() {
890 return $this->getService( 'RevisionLookup' );
895 * @return RevisionRenderer
897 public function getRevisionRenderer() {
898 return $this->getService( 'RevisionRenderer' );
903 * @return RevisionStore
905 public function getRevisionStore() {
906 return $this->getService( 'RevisionStore' );
911 * @return RevisionStoreFactory
913 public function getRevisionStoreFactory() {
914 return $this->getService( 'RevisionStoreFactory' );
919 * @return SearchEngine
921 public function newSearchEngine() {
922 // New engine object every time, since they keep state
923 return $this->getService( 'SearchEngineFactory' )->create();
928 * @return SearchEngineConfig
930 public function getSearchEngineConfig() {
931 return $this->getService( 'SearchEngineConfig' );
936 * @return SearchEngineFactory
938 public function getSearchEngineFactory() {
939 return $this->getService( 'SearchEngineFactory' );
944 * @return CommandFactory
946 public function getShellCommandFactory() {
947 return $this->getService( 'ShellCommandFactory' );
954 public function getSiteLookup() {
955 return $this->getService( 'SiteLookup' );
962 public function getSiteStore() {
963 return $this->getService( 'SiteStore' );
968 * @return SkinFactory
970 public function getSkinFactory() {
971 return $this->getService( 'SkinFactory' );
976 * @return SlotRoleRegistry
978 public function getSlotRoleRegistry() {
979 return $this->getService( 'SlotRoleRegistry' );
984 * @return NameTableStore
986 public function getSlotRoleStore() {
987 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
992 * @return SpecialPageFactory
994 public function getSpecialPageFactory() : SpecialPageFactory
{
995 return $this->getService( 'SpecialPageFactory' );
1000 * @return IBufferingStatsdDataFactory
1002 public function getStatsdDataFactory() {
1003 return $this->getService( 'StatsdDataFactory' );
1008 * @return TempFSFileFactory
1010 public function getTempFSFileFactory() : TempFSFileFactory
{
1011 return $this->getService( 'TempFSFileFactory' );
1016 * @return TitleFormatter
1018 public function getTitleFormatter() {
1019 return $this->getService( 'TitleFormatter' );
1024 * @return TitleParser
1026 public function getTitleParser() {
1027 return $this->getService( 'TitleParser' );
1032 * @return UploadRevisionImporter
1034 public function getUploadRevisionImporter() {
1035 return $this->getService( 'UploadRevisionImporter' );
1040 * @return VirtualRESTServiceClient
1042 public function getVirtualRESTServiceClient() {
1043 return $this->getService( 'VirtualRESTServiceClient' );
1048 * @return WatchedItemQueryService
1050 public function getWatchedItemQueryService() {
1051 return $this->getService( 'WatchedItemQueryService' );
1056 * @return WatchedItemStoreInterface
1058 public function getWatchedItemStore() {
1059 return $this->getService( 'WatchedItemStore' );
1064 * @return \OldRevisionImporter
1066 public function getWikiRevisionOldRevisionImporter() {
1067 return $this->getService( 'OldRevisionImporter' );
1072 * @return \OldRevisionImporter
1074 public function getWikiRevisionOldRevisionImporterNoUpdates() {
1075 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1080 * @return \UploadRevisionImporter
1082 public function getWikiRevisionUploadImporter() {
1083 return $this->getService( 'UploadRevisionImporter' );