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' );
436 public function getBlobStore() {
437 return $this->getService( '_SqlBlobStore' );
442 * @return BlobStoreFactory
444 public function getBlobStoreFactory() {
445 return $this->getService( 'BlobStoreFactory' );
450 * @return BlockManager
452 public function getBlockManager() : BlockManager
{
453 return $this->getService( 'BlockManager' );
458 * @return BlockRestrictionStore
460 public function getBlockRestrictionStore() : BlockRestrictionStore
{
461 return $this->getService( 'BlockRestrictionStore' );
465 * Returns the Config object containing the bootstrap configuration.
466 * Bootstrap configuration would typically include database credentials
467 * and other information that may be needed before the ConfigFactory
468 * service can be instantiated.
470 * @note This should only be used during bootstrapping, in particular
471 * when creating the MainConfig service. Application logic should
472 * use getMainConfig() to get a Config instances.
477 public function getBootstrapConfig() {
478 return $this->getService( 'BootstrapConfig' );
483 * @return NameTableStore
485 public function getChangeTagDefStore() {
486 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
491 * @return CommentStore
493 public function getCommentStore() {
494 return $this->getService( 'CommentStore' );
499 * @return ConfigFactory
501 public function getConfigFactory() {
502 return $this->getService( 'ConfigFactory' );
507 * @return ConfigRepository
509 public function getConfigRepository() {
510 return $this->getService( 'ConfigRepository' );
515 * @return \ConfiguredReadOnlyMode
517 public function getConfiguredReadOnlyMode() {
518 return $this->getService( 'ConfiguredReadOnlyMode' );
525 public function getContentLanguage() {
526 return $this->getService( 'ContentLanguage' );
531 * @return NameTableStore
533 public function getContentModelStore() {
534 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
541 public function getCryptHKDF() {
542 return $this->getService( 'CryptHKDF' );
547 * @return DateFormatterFactory
549 public function getDateFormatterFactory() {
550 return $this->getService( 'DateFormatterFactory' );
555 * @return ILoadBalancer The main DB load balancer for the local wiki.
557 public function getDBLoadBalancer() {
558 return $this->getService( 'DBLoadBalancer' );
565 public function getDBLoadBalancerFactory() {
566 return $this->getService( 'DBLoadBalancerFactory' );
571 * @return EventRelayerGroup
573 public function getEventRelayerGroup() {
574 return $this->getService( 'EventRelayerGroup' );
579 * @return \ExternalStoreAccess
581 public function getExternalStoreAccess() {
582 return $this->getService( 'ExternalStoreAccess' );
587 * @return \ExternalStoreFactory
589 public function getExternalStoreFactory() {
590 return $this->getService( 'ExternalStoreFactory' );
595 * @return GenderCache
597 public function getGenderCache() {
598 return $this->getService( 'GenderCache' );
603 * @return HttpRequestFactory
605 public function getHttpRequestFactory() {
606 return $this->getService( 'HttpRequestFactory' );
611 * @return InterwikiLookup
613 public function getInterwikiLookup() {
614 return $this->getService( 'InterwikiLookup' );
621 public function getLinkCache() {
622 return $this->getService( 'LinkCache' );
626 * LinkRenderer instance that can be used
627 * if no custom options are needed
630 * @return LinkRenderer
632 public function getLinkRenderer() {
633 return $this->getService( 'LinkRenderer' );
638 * @return LinkRendererFactory
640 public function getLinkRendererFactory() {
641 return $this->getService( 'LinkRendererFactory' );
648 public function getLocalServerObjectCache() {
649 return $this->getService( 'LocalServerObjectCache' );
654 * @return MagicWordFactory
656 public function getMagicWordFactory() {
657 return $this->getService( 'MagicWordFactory' );
661 * Returns the Config object that provides configuration for MediaWiki core.
662 * This may or may not be the same object that is returned by getBootstrapConfig().
667 public function getMainConfig() {
668 return $this->getService( 'MainConfig' );
675 public function getMainObjectStash() {
676 return $this->getService( 'MainObjectStash' );
681 * @return \WANObjectCache
683 public function getMainWANObjectCache() {
684 return $this->getService( 'MainWANObjectCache' );
689 * @return MediaHandlerFactory
691 public function getMediaHandlerFactory() {
692 return $this->getService( 'MediaHandlerFactory' );
697 * @return MessageCache
699 public function getMessageCache() : MessageCache
{
700 return $this->getService( 'MessageCache' );
705 * @return MimeAnalyzer
707 public function getMimeAnalyzer() {
708 return $this->getService( 'MimeAnalyzer' );
713 * @return MovePageFactory
715 public function getMovePageFactory() : MovePageFactory
{
716 return $this->getService( 'MovePageFactory' );
721 * @return NamespaceInfo
723 public function getNamespaceInfo() : NamespaceInfo
{
724 return $this->getService( 'NamespaceInfo' );
729 * @return NameTableStoreFactory
731 public function getNameTableStoreFactory() {
732 return $this->getService( 'NameTableStoreFactory' );
737 * @return OldRevisionImporter
739 public function getOldRevisionImporter() {
740 return $this->getService( 'OldRevisionImporter' );
744 * @return PageEditStash
747 public function getPageEditStash() {
748 return $this->getService( 'PageEditStash' );
755 public function getParser() {
756 return $this->getService( 'Parser' );
761 * @return ParserCache
763 public function getParserCache() {
764 return $this->getService( 'ParserCache' );
769 * @return ParserFactory
771 public function getParserFactory() {
772 return $this->getService( 'ParserFactory' );
777 * @return PasswordFactory
779 public function getPasswordFactory() {
780 return $this->getService( 'PasswordFactory' );
785 * @return StatsdDataFactoryInterface
787 public function getPerDbNameStatsdDataFactory() {
788 return $this->getService( 'PerDbNameStatsdDataFactory' );
793 * @return PermissionManager
795 public function getPermissionManager() {
796 return $this->getService( 'PermissionManager' );
801 * @return PreferencesFactory
803 public function getPreferencesFactory() {
804 return $this->getService( 'PreferencesFactory' );
809 * @return ProxyLookup
811 public function getProxyLookup() {
812 return $this->getService( 'ProxyLookup' );
817 * @return \ReadOnlyMode
819 public function getReadOnlyMode() {
820 return $this->getService( 'ReadOnlyMode' );
827 public function getRepoGroup() : RepoGroup
{
828 return $this->getService( 'RepoGroup' );
833 * @return ResourceLoader
835 public function getResourceLoader() {
836 return $this->getService( 'ResourceLoader' );
841 * @return RevisionFactory
843 public function getRevisionFactory() {
844 return $this->getService( 'RevisionFactory' );
849 * @return RevisionLookup
851 public function getRevisionLookup() {
852 return $this->getService( 'RevisionLookup' );
857 * @return RevisionRenderer
859 public function getRevisionRenderer() {
860 return $this->getService( 'RevisionRenderer' );
865 * @return RevisionStore
867 public function getRevisionStore() {
868 return $this->getService( 'RevisionStore' );
873 * @return RevisionStoreFactory
875 public function getRevisionStoreFactory() {
876 return $this->getService( 'RevisionStoreFactory' );
881 * @return SearchEngine
883 public function newSearchEngine() {
884 // New engine object every time, since they keep state
885 return $this->getService( 'SearchEngineFactory' )->create();
890 * @return SearchEngineConfig
892 public function getSearchEngineConfig() {
893 return $this->getService( 'SearchEngineConfig' );
898 * @return SearchEngineFactory
900 public function getSearchEngineFactory() {
901 return $this->getService( 'SearchEngineFactory' );
906 * @return CommandFactory
908 public function getShellCommandFactory() {
909 return $this->getService( 'ShellCommandFactory' );
916 public function getSiteLookup() {
917 return $this->getService( 'SiteLookup' );
924 public function getSiteStore() {
925 return $this->getService( 'SiteStore' );
930 * @return SkinFactory
932 public function getSkinFactory() {
933 return $this->getService( 'SkinFactory' );
938 * @return SlotRoleRegistry
940 public function getSlotRoleRegistry() {
941 return $this->getService( 'SlotRoleRegistry' );
946 * @return NameTableStore
948 public function getSlotRoleStore() {
949 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
954 * @return SpecialPageFactory
956 public function getSpecialPageFactory() : SpecialPageFactory
{
957 return $this->getService( 'SpecialPageFactory' );
962 * @return IBufferingStatsdDataFactory
964 public function getStatsdDataFactory() {
965 return $this->getService( 'StatsdDataFactory' );
970 * @return TempFSFileFactory
972 public function getTempFSFileFactory() : TempFSFileFactory
{
973 return $this->getService( 'TempFSFileFactory' );
978 * @return TitleFormatter
980 public function getTitleFormatter() {
981 return $this->getService( 'TitleFormatter' );
986 * @return TitleParser
988 public function getTitleParser() {
989 return $this->getService( 'TitleParser' );
994 * @return UploadRevisionImporter
996 public function getUploadRevisionImporter() {
997 return $this->getService( 'UploadRevisionImporter' );
1002 * @return VirtualRESTServiceClient
1004 public function getVirtualRESTServiceClient() {
1005 return $this->getService( 'VirtualRESTServiceClient' );
1010 * @return WatchedItemQueryService
1012 public function getWatchedItemQueryService() {
1013 return $this->getService( 'WatchedItemQueryService' );
1018 * @return WatchedItemStoreInterface
1020 public function getWatchedItemStore() {
1021 return $this->getService( 'WatchedItemStore' );
1026 * @return \OldRevisionImporter
1028 public function getWikiRevisionOldRevisionImporter() {
1029 return $this->getService( 'OldRevisionImporter' );
1034 * @return \OldRevisionImporter
1036 public function getWikiRevisionOldRevisionImporterNoUpdates() {
1037 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1042 * @return \UploadRevisionImporter
1044 public function getWikiRevisionUploadImporter() {
1045 return $this->getService( 'UploadRevisionImporter' );