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\FileBackend\FSFile\TempFSFileFactory
;
20 use MediaWiki\Http\HttpRequestFactory
;
21 use MediaWiki\Page\MovePageFactory
;
22 use MediaWiki\Permissions\PermissionManager
;
23 use MediaWiki\Preferences\PreferencesFactory
;
24 use MediaWiki\Revision\RevisionRenderer
;
25 use MediaWiki\Revision\SlotRoleRegistry
;
26 use MediaWiki\Shell\CommandFactory
;
27 use MediaWiki\Special\SpecialPageFactory
;
28 use MediaWiki\Storage\BlobStore
;
29 use MediaWiki\Storage\BlobStoreFactory
;
30 use MediaWiki\Storage\NameTableStore
;
31 use MediaWiki\Storage\NameTableStoreFactory
;
32 use MediaWiki\Revision\RevisionFactory
;
33 use MediaWiki\Revision\RevisionLookup
;
34 use MediaWiki\Revision\RevisionStore
;
35 use OldRevisionImporter
;
36 use MediaWiki\Revision\RevisionStoreFactory
;
37 use UploadRevisionImporter
;
38 use Wikimedia\Rdbms\ILoadBalancer
;
40 use MediaHandlerFactory
;
41 use MediaWiki\Config\ConfigRepository
;
42 use MediaWiki\Linker\LinkRenderer
;
43 use MediaWiki\Linker\LinkRendererFactory
;
57 use SearchEngineConfig
;
58 use SearchEngineFactory
;
61 use WatchedItemStoreInterface
;
62 use WatchedItemQueryService
;
66 use VirtualRESTServiceClient
;
67 use Wikimedia\Rdbms\LBFactory
;
68 use Wikimedia\Services\SalvageableService
;
69 use Wikimedia\Services\ServiceContainer
;
70 use Wikimedia\Services\NoSuchServiceException
;
71 use MediaWiki\Interwiki\InterwikiLookup
;
73 use MediaWiki\Storage\PageEditStash
;
76 * Service locator for MediaWiki core services.
78 * This program is free software; you can redistribute it and/or modify
79 * it under the terms of the GNU General Public License as published by
80 * the Free Software Foundation; either version 2 of the License, or
81 * (at your option) any later version.
83 * This program is distributed in the hope that it will be useful,
84 * but WITHOUT ANY WARRANTY; without even the implied warranty of
85 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86 * GNU General Public License for more details.
88 * You should have received a copy of the GNU General Public License along
89 * with this program; if not, write to the Free Software Foundation, Inc.,
90 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
91 * http://www.gnu.org/copyleft/gpl.html
99 * MediaWikiServices is the service locator for the application scope of MediaWiki.
100 * Its implemented as a simple configurable DI container.
101 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
102 * the network of service objects that defines MediaWiki's application logic.
103 * It acts as an entry point to MediaWiki's dependency injection mechanism.
105 * Services are defined in the "wiring" array passed to the constructor,
106 * or by calling defineService().
108 * @see docs/injection.txt for an overview of using dependency injection in the
109 * MediaWiki code base.
111 class MediaWikiServices
extends ServiceContainer
{
114 * @var MediaWikiServices|null
116 private static $instance = null;
119 * Returns the global default instance of the top level service locator.
123 * The default instance is initialized using the service instantiator functions
124 * defined in ServiceWiring.php.
126 * @note This should only be called by static functions! The instance returned here
127 * should not be passed around! Objects that need access to a service should have
128 * that service injected into the constructor, never a service locator!
130 * @return MediaWikiServices
132 public static function getInstance() {
133 if ( self
::$instance === null ) {
134 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
135 // but some information from the global scope has to be injected here,
136 // even if it's just a file name or database credentials to load
137 // configuration from.
138 $bootstrapConfig = new GlobalVarConfig();
139 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
142 return self
::$instance;
146 * Replaces the global MediaWikiServices instance.
150 * @note This is for use in PHPUnit tests only!
152 * @throws MWException if called outside of PHPUnit tests.
154 * @param MediaWikiServices $services The new MediaWikiServices object.
156 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
158 public static function forceGlobalInstance( MediaWikiServices
$services ) {
159 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
160 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
163 $old = self
::getInstance();
164 self
::$instance = $services;
170 * Creates a new instance of MediaWikiServices and sets it as the global default
171 * instance. getInstance() will return a different MediaWikiServices object
172 * after every call to resetGlobalInstance().
176 * @warning This should not be used during normal operation. It is intended for use
177 * when the configuration has changed significantly since bootstrap time, e.g.
178 * during the installation process or during testing.
180 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
181 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
182 * any of the services managed by MediaWikiServices exist. If any service objects
183 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
184 * with the operation of the services managed by the new MediaWikiServices.
185 * Operating with a mix of services created by the old and the new
186 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
187 * Any class implementing LAZY LOADING is especially prone to this problem,
188 * since instances would typically retain a reference to a storage layer service.
190 * @see forceGlobalInstance()
191 * @see resetGlobalInstance()
192 * @see resetBetweenTest()
194 * @param Config|null $bootstrapConfig The Config object to be registered as the
195 * 'BootstrapConfig' service. This has to contain at least the information
196 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
197 * config of the old instance of MediaWikiServices will be re-used. If there
198 * was no previous instance, a new GlobalVarConfig object will be used to
199 * bootstrap the services.
201 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
202 * See SalvageableService for details.
204 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
205 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
208 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
209 if ( self
::$instance === null ) {
210 // no global instance yet, nothing to reset
214 self
::failIfResetNotAllowed( __METHOD__
);
216 if ( $bootstrapConfig === null ) {
217 $bootstrapConfig = self
::$instance->getBootstrapConfig();
220 $oldInstance = self
::$instance;
222 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
223 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
225 if ( $quick === 'quick' ) {
226 self
::$instance->salvage( $oldInstance );
228 $oldInstance->destroy();
232 /** @noinspection PhpDocSignatureInspection */
235 * Salvages the state of any salvageable service instances in $other.
237 * @note $other will have been destroyed when salvage() returns.
239 * @param MediaWikiServices $other
241 private function salvage( self
$other ) {
242 foreach ( $this->getServiceNames() as $name ) {
243 // The service could be new in the new instance and not registered in the
244 // other instance (e.g. an extension that was loaded after the instantiation of
245 // the other instance. Skip this service in this case. See T143974
247 $oldService = $other->peekService( $name );
248 } catch ( NoSuchServiceException
$e ) {
252 if ( $oldService instanceof SalvageableService
) {
253 /** @var SalvageableService $newService */
254 $newService = $this->getService( $name );
255 $newService->salvage( $oldService );
263 * Creates a new MediaWikiServices instance and initializes it according to the
264 * given $bootstrapConfig. In particular, all wiring files defined in the
265 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
267 * @param Config|null $bootstrapConfig The Config object to be registered as the
268 * 'BootstrapConfig' service.
270 * @param string $loadWiring set this to 'load' to load the wiring files specified
271 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
273 * @return MediaWikiServices
274 * @throws MWException
275 * @throws \FatalError
277 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
278 $instance = new self( $bootstrapConfig );
280 // Load the default wiring from the specified files.
281 if ( $loadWiring === 'load' ) {
282 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
283 $instance->loadWiringFiles( $wiringFiles );
286 // Provide a traditional hook point to allow extensions to configure services.
287 Hooks
::run( 'MediaWikiServices', [ $instance ] );
293 * Disables all storage layer services. After calling this, any attempt to access the
294 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
299 * @warning This is intended for extreme situations only and should never be used
300 * while serving normal web requests. Legitimate use cases for this method include
301 * the installation process. Test fixtures may also use this, if the fixture relies
304 * @see resetGlobalInstance()
305 * @see resetChildProcessServices()
307 public static function disableStorageBackend() {
308 // TODO: also disable some Caches, JobQueues, etc
309 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
310 $services = self
::getInstance();
312 foreach ( $destroy as $name ) {
313 $services->disableService( $name );
316 ObjectCache
::clear();
320 * Resets any services that may have become stale after a child process
321 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
322 * to call this method from the parent process.
326 * @note This is intended for use in the context of process forking only!
328 * @see resetGlobalInstance()
329 * @see disableStorageBackend()
331 public static function resetChildProcessServices() {
332 // NOTE: for now, just reset everything. Since we don't know the interdependencies
333 // between services, we can't do this more selectively at this time.
334 self
::resetGlobalInstance();
336 // Child, reseed because there is no bug in PHP:
337 // https://bugs.php.net/bug.php?id=42465
338 mt_srand( getmypid() );
342 * Resets the given service for testing purposes.
346 * @warning This is generally unsafe! Other services may still retain references
347 * to the stale service instance, leading to failures and inconsistencies. Subclasses
348 * may use this method to reset specific services under specific instances, but
349 * it should not be exposed to application logic.
351 * @note With proper dependency injection used throughout the codebase, this method
352 * should not be needed. It is provided to allow tests that pollute global service
353 * instances to clean up.
355 * @param string $name
356 * @param bool $destroy Whether the service instance should be destroyed if it exists.
357 * When set to false, any existing service instance will effectively be detached
358 * from the container.
360 * @throws MWException if called outside of PHPUnit tests.
362 public function resetServiceForTesting( $name, $destroy = true ) {
363 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
364 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
367 $this->resetService( $name, $destroy );
371 * Convenience method that throws an exception unless it is called during a phase in which
372 * resetting of global services is allowed. In general, services should not be reset
373 * individually, since that may introduce inconsistencies.
377 * This method will throw an exception if:
379 * - self::$resetInProgress is false (to allow all services to be reset together
380 * via resetGlobalInstance)
381 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
382 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
384 * This method is intended to be used to safeguard against accidentally resetting
385 * global service instances that are not yet managed by MediaWikiServices. It is
386 * defined here in the MediaWikiServices services class to have a central place
387 * for managing service bootstrapping and resetting.
389 * @param string $method the name of the caller method, as given by __METHOD__.
391 * @throws MWException if called outside bootstrap mode.
393 * @see resetGlobalInstance()
394 * @see forceGlobalInstance()
395 * @see disableStorageBackend()
397 public static function failIfResetNotAllowed( $method ) {
398 if ( !defined( 'MW_PHPUNIT_TEST' )
399 && !defined( 'MW_PARSER_TEST' )
400 && !defined( 'MEDIAWIKI_INSTALL' )
401 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
402 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
404 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
409 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
410 * This has to contain at least the information needed to set up the 'ConfigFactory'
413 public function __construct( Config
$config ) {
414 parent
::__construct();
416 // Register the given Config object as the bootstrap config service.
417 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
422 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
426 * @return ActorMigration
428 public function getActorMigration() {
429 return $this->getService( 'ActorMigration' );
434 * @return BadFileLookup
436 public function getBadFileLookup() : BadFileLookup
{
437 return $this->getService( 'BadFileLookup' );
444 public function getBlobStore() {
445 return $this->getService( '_SqlBlobStore' );
450 * @return BlobStoreFactory
452 public function getBlobStoreFactory() {
453 return $this->getService( 'BlobStoreFactory' );
458 * @return BlockManager
460 public function getBlockManager() : BlockManager
{
461 return $this->getService( 'BlockManager' );
466 * @return BlockRestrictionStore
468 public function getBlockRestrictionStore() : BlockRestrictionStore
{
469 return $this->getService( 'BlockRestrictionStore' );
473 * Returns the Config object containing the bootstrap configuration.
474 * Bootstrap configuration would typically include database credentials
475 * and other information that may be needed before the ConfigFactory
476 * service can be instantiated.
478 * @note This should only be used during bootstrapping, in particular
479 * when creating the MainConfig service. Application logic should
480 * use getMainConfig() to get a Config instances.
485 public function getBootstrapConfig() {
486 return $this->getService( 'BootstrapConfig' );
491 * @return NameTableStore
493 public function getChangeTagDefStore() {
494 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
499 * @return CommentStore
501 public function getCommentStore() {
502 return $this->getService( 'CommentStore' );
507 * @return ConfigFactory
509 public function getConfigFactory() {
510 return $this->getService( 'ConfigFactory' );
515 * @return ConfigRepository
517 public function getConfigRepository() {
518 return $this->getService( 'ConfigRepository' );
523 * @return \ConfiguredReadOnlyMode
525 public function getConfiguredReadOnlyMode() {
526 return $this->getService( 'ConfiguredReadOnlyMode' );
533 public function getContentLanguage() {
534 return $this->getService( 'ContentLanguage' );
539 * @return NameTableStore
541 public function getContentModelStore() {
542 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
549 public function getCryptHKDF() {
550 return $this->getService( 'CryptHKDF' );
555 * @return DateFormatterFactory
557 public function getDateFormatterFactory() {
558 return $this->getService( 'DateFormatterFactory' );
563 * @return ILoadBalancer The main DB load balancer for the local wiki.
565 public function getDBLoadBalancer() {
566 return $this->getService( 'DBLoadBalancer' );
573 public function getDBLoadBalancerFactory() {
574 return $this->getService( 'DBLoadBalancerFactory' );
579 * @return EventRelayerGroup
581 public function getEventRelayerGroup() {
582 return $this->getService( 'EventRelayerGroup' );
587 * @return \ExternalStoreAccess
589 public function getExternalStoreAccess() {
590 return $this->getService( 'ExternalStoreAccess' );
595 * @return \ExternalStoreFactory
597 public function getExternalStoreFactory() {
598 return $this->getService( 'ExternalStoreFactory' );
603 * @return GenderCache
605 public function getGenderCache() {
606 return $this->getService( 'GenderCache' );
611 * @return HttpRequestFactory
613 public function getHttpRequestFactory() {
614 return $this->getService( 'HttpRequestFactory' );
619 * @return InterwikiLookup
621 public function getInterwikiLookup() {
622 return $this->getService( 'InterwikiLookup' );
629 public function getLinkCache() {
630 return $this->getService( 'LinkCache' );
634 * LinkRenderer instance that can be used
635 * if no custom options are needed
638 * @return LinkRenderer
640 public function getLinkRenderer() {
641 return $this->getService( 'LinkRenderer' );
646 * @return LinkRendererFactory
648 public function getLinkRendererFactory() {
649 return $this->getService( 'LinkRendererFactory' );
656 public function getLocalServerObjectCache() {
657 return $this->getService( 'LocalServerObjectCache' );
662 * @return MagicWordFactory
664 public function getMagicWordFactory() {
665 return $this->getService( 'MagicWordFactory' );
669 * Returns the Config object that provides configuration for MediaWiki core.
670 * This may or may not be the same object that is returned by getBootstrapConfig().
675 public function getMainConfig() {
676 return $this->getService( 'MainConfig' );
683 public function getMainObjectStash() {
684 return $this->getService( 'MainObjectStash' );
689 * @return \WANObjectCache
691 public function getMainWANObjectCache() {
692 return $this->getService( 'MainWANObjectCache' );
697 * @return MediaHandlerFactory
699 public function getMediaHandlerFactory() {
700 return $this->getService( 'MediaHandlerFactory' );
705 * @return MessageCache
707 public function getMessageCache() : MessageCache
{
708 return $this->getService( 'MessageCache' );
713 * @return MimeAnalyzer
715 public function getMimeAnalyzer() {
716 return $this->getService( 'MimeAnalyzer' );
721 * @return MovePageFactory
723 public function getMovePageFactory() : MovePageFactory
{
724 return $this->getService( 'MovePageFactory' );
729 * @return NamespaceInfo
731 public function getNamespaceInfo() : NamespaceInfo
{
732 return $this->getService( 'NamespaceInfo' );
737 * @return NameTableStoreFactory
739 public function getNameTableStoreFactory() {
740 return $this->getService( 'NameTableStoreFactory' );
745 * @return OldRevisionImporter
747 public function getOldRevisionImporter() {
748 return $this->getService( 'OldRevisionImporter' );
752 * @return PageEditStash
755 public function getPageEditStash() {
756 return $this->getService( 'PageEditStash' );
763 public function getParser() {
764 return $this->getService( 'Parser' );
769 * @return ParserCache
771 public function getParserCache() {
772 return $this->getService( 'ParserCache' );
777 * @return ParserFactory
779 public function getParserFactory() {
780 return $this->getService( 'ParserFactory' );
785 * @return PasswordFactory
787 public function getPasswordFactory() {
788 return $this->getService( 'PasswordFactory' );
793 * @return StatsdDataFactoryInterface
795 public function getPerDbNameStatsdDataFactory() {
796 return $this->getService( 'PerDbNameStatsdDataFactory' );
801 * @return PermissionManager
803 public function getPermissionManager() {
804 return $this->getService( 'PermissionManager' );
809 * @return PreferencesFactory
811 public function getPreferencesFactory() {
812 return $this->getService( 'PreferencesFactory' );
817 * @return ProxyLookup
819 public function getProxyLookup() {
820 return $this->getService( 'ProxyLookup' );
825 * @return \ReadOnlyMode
827 public function getReadOnlyMode() {
828 return $this->getService( 'ReadOnlyMode' );
835 public function getRepoGroup() : RepoGroup
{
836 return $this->getService( 'RepoGroup' );
841 * @return ResourceLoader
843 public function getResourceLoader() {
844 return $this->getService( 'ResourceLoader' );
849 * @return RevisionFactory
851 public function getRevisionFactory() {
852 return $this->getService( 'RevisionFactory' );
857 * @return RevisionLookup
859 public function getRevisionLookup() {
860 return $this->getService( 'RevisionLookup' );
865 * @return RevisionRenderer
867 public function getRevisionRenderer() {
868 return $this->getService( 'RevisionRenderer' );
873 * @return RevisionStore
875 public function getRevisionStore() {
876 return $this->getService( 'RevisionStore' );
881 * @return RevisionStoreFactory
883 public function getRevisionStoreFactory() {
884 return $this->getService( 'RevisionStoreFactory' );
889 * @return SearchEngine
891 public function newSearchEngine() {
892 // New engine object every time, since they keep state
893 return $this->getService( 'SearchEngineFactory' )->create();
898 * @return SearchEngineConfig
900 public function getSearchEngineConfig() {
901 return $this->getService( 'SearchEngineConfig' );
906 * @return SearchEngineFactory
908 public function getSearchEngineFactory() {
909 return $this->getService( 'SearchEngineFactory' );
914 * @return CommandFactory
916 public function getShellCommandFactory() {
917 return $this->getService( 'ShellCommandFactory' );
924 public function getSiteLookup() {
925 return $this->getService( 'SiteLookup' );
932 public function getSiteStore() {
933 return $this->getService( 'SiteStore' );
938 * @return SkinFactory
940 public function getSkinFactory() {
941 return $this->getService( 'SkinFactory' );
946 * @return SlotRoleRegistry
948 public function getSlotRoleRegistry() {
949 return $this->getService( 'SlotRoleRegistry' );
954 * @return NameTableStore
956 public function getSlotRoleStore() {
957 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
962 * @return SpecialPageFactory
964 public function getSpecialPageFactory() : SpecialPageFactory
{
965 return $this->getService( 'SpecialPageFactory' );
970 * @return IBufferingStatsdDataFactory
972 public function getStatsdDataFactory() {
973 return $this->getService( 'StatsdDataFactory' );
978 * @return TempFSFileFactory
980 public function getTempFSFileFactory() : TempFSFileFactory
{
981 return $this->getService( 'TempFSFileFactory' );
986 * @return TitleFormatter
988 public function getTitleFormatter() {
989 return $this->getService( 'TitleFormatter' );
994 * @return TitleParser
996 public function getTitleParser() {
997 return $this->getService( 'TitleParser' );
1002 * @return UploadRevisionImporter
1004 public function getUploadRevisionImporter() {
1005 return $this->getService( 'UploadRevisionImporter' );
1010 * @return VirtualRESTServiceClient
1012 public function getVirtualRESTServiceClient() {
1013 return $this->getService( 'VirtualRESTServiceClient' );
1018 * @return WatchedItemQueryService
1020 public function getWatchedItemQueryService() {
1021 return $this->getService( 'WatchedItemQueryService' );
1026 * @return WatchedItemStoreInterface
1028 public function getWatchedItemStore() {
1029 return $this->getService( 'WatchedItemStore' );
1034 * @return \OldRevisionImporter
1036 public function getWikiRevisionOldRevisionImporter() {
1037 return $this->getService( 'OldRevisionImporter' );
1042 * @return \OldRevisionImporter
1044 public function getWikiRevisionOldRevisionImporterNoUpdates() {
1045 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1050 * @return \UploadRevisionImporter
1052 public function getWikiRevisionUploadImporter() {
1053 return $this->getService( 'UploadRevisionImporter' );