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\Http\HttpRequestFactory
;
20 use MediaWiki\Page\MovePageFactory
;
21 use MediaWiki\Permissions\PermissionManager
;
22 use MediaWiki\Preferences\PreferencesFactory
;
23 use MediaWiki\Revision\RevisionRenderer
;
24 use MediaWiki\Revision\SlotRoleRegistry
;
25 use MediaWiki\Shell\CommandFactory
;
26 use MediaWiki\Special\SpecialPageFactory
;
27 use MediaWiki\Storage\BlobStore
;
28 use MediaWiki\Storage\BlobStoreFactory
;
29 use MediaWiki\Storage\NameTableStore
;
30 use MediaWiki\Storage\NameTableStoreFactory
;
31 use MediaWiki\Revision\RevisionFactory
;
32 use MediaWiki\Revision\RevisionLookup
;
33 use MediaWiki\Revision\RevisionStore
;
34 use OldRevisionImporter
;
35 use MediaWiki\Revision\RevisionStoreFactory
;
36 use UploadRevisionImporter
;
37 use Wikimedia\Rdbms\ILoadBalancer
;
39 use MediaHandlerFactory
;
40 use MediaWiki\Config\ConfigRepository
;
41 use MediaWiki\Linker\LinkRenderer
;
42 use MediaWiki\Linker\LinkRendererFactory
;
56 use SearchEngineConfig
;
57 use SearchEngineFactory
;
60 use WatchedItemStoreInterface
;
61 use WatchedItemQueryService
;
65 use VirtualRESTServiceClient
;
66 use Wikimedia\Rdbms\LBFactory
;
67 use Wikimedia\Services\SalvageableService
;
68 use Wikimedia\Services\ServiceContainer
;
69 use Wikimedia\Services\NoSuchServiceException
;
70 use MediaWiki\Interwiki\InterwikiLookup
;
72 use MediaWiki\Storage\PageEditStash
;
75 * Service locator for MediaWiki core services.
77 * This program is free software; you can redistribute it and/or modify
78 * it under the terms of the GNU General Public License as published by
79 * the Free Software Foundation; either version 2 of the License, or
80 * (at your option) any later version.
82 * This program is distributed in the hope that it will be useful,
83 * but WITHOUT ANY WARRANTY; without even the implied warranty of
84 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
85 * GNU General Public License for more details.
87 * You should have received a copy of the GNU General Public License along
88 * with this program; if not, write to the Free Software Foundation, Inc.,
89 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
90 * http://www.gnu.org/copyleft/gpl.html
98 * MediaWikiServices is the service locator for the application scope of MediaWiki.
99 * Its implemented as a simple configurable DI container.
100 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
101 * the network of service objects that defines MediaWiki's application logic.
102 * It acts as an entry point to MediaWiki's dependency injection mechanism.
104 * Services are defined in the "wiring" array passed to the constructor,
105 * or by calling defineService().
107 * @see docs/injection.txt for an overview of using dependency injection in the
108 * MediaWiki code base.
110 class MediaWikiServices
extends ServiceContainer
{
113 * @var MediaWikiServices|null
115 private static $instance = null;
118 * Returns the global default instance of the top level service locator.
122 * The default instance is initialized using the service instantiator functions
123 * defined in ServiceWiring.php.
125 * @note This should only be called by static functions! The instance returned here
126 * should not be passed around! Objects that need access to a service should have
127 * that service injected into the constructor, never a service locator!
129 * @return MediaWikiServices
131 public static function getInstance() {
132 if ( self
::$instance === null ) {
133 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
134 // but some information from the global scope has to be injected here,
135 // even if it's just a file name or database credentials to load
136 // configuration from.
137 $bootstrapConfig = new GlobalVarConfig();
138 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
141 return self
::$instance;
145 * Replaces the global MediaWikiServices instance.
149 * @note This is for use in PHPUnit tests only!
151 * @throws MWException if called outside of PHPUnit tests.
153 * @param MediaWikiServices $services The new MediaWikiServices object.
155 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
157 public static function forceGlobalInstance( MediaWikiServices
$services ) {
158 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
159 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
162 $old = self
::getInstance();
163 self
::$instance = $services;
169 * Creates a new instance of MediaWikiServices and sets it as the global default
170 * instance. getInstance() will return a different MediaWikiServices object
171 * after every call to resetGlobalInstance().
175 * @warning This should not be used during normal operation. It is intended for use
176 * when the configuration has changed significantly since bootstrap time, e.g.
177 * during the installation process or during testing.
179 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
180 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
181 * any of the services managed by MediaWikiServices exist. If any service objects
182 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
183 * with the operation of the services managed by the new MediaWikiServices.
184 * Operating with a mix of services created by the old and the new
185 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
186 * Any class implementing LAZY LOADING is especially prone to this problem,
187 * since instances would typically retain a reference to a storage layer service.
189 * @see forceGlobalInstance()
190 * @see resetGlobalInstance()
191 * @see resetBetweenTest()
193 * @param Config|null $bootstrapConfig The Config object to be registered as the
194 * 'BootstrapConfig' service. This has to contain at least the information
195 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
196 * config of the old instance of MediaWikiServices will be re-used. If there
197 * was no previous instance, a new GlobalVarConfig object will be used to
198 * bootstrap the services.
200 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
201 * See SalvageableService for details.
203 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
204 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
207 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
208 if ( self
::$instance === null ) {
209 // no global instance yet, nothing to reset
213 self
::failIfResetNotAllowed( __METHOD__
);
215 if ( $bootstrapConfig === null ) {
216 $bootstrapConfig = self
::$instance->getBootstrapConfig();
219 $oldInstance = self
::$instance;
221 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
222 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
224 if ( $quick === 'quick' ) {
225 self
::$instance->salvage( $oldInstance );
227 $oldInstance->destroy();
231 /** @noinspection PhpDocSignatureInspection */
234 * Salvages the state of any salvageable service instances in $other.
236 * @note $other will have been destroyed when salvage() returns.
238 * @param MediaWikiServices $other
240 private function salvage( self
$other ) {
241 foreach ( $this->getServiceNames() as $name ) {
242 // The service could be new in the new instance and not registered in the
243 // other instance (e.g. an extension that was loaded after the instantiation of
244 // the other instance. Skip this service in this case. See T143974
246 $oldService = $other->peekService( $name );
247 } catch ( NoSuchServiceException
$e ) {
251 if ( $oldService instanceof SalvageableService
) {
252 /** @var SalvageableService $newService */
253 $newService = $this->getService( $name );
254 $newService->salvage( $oldService );
262 * Creates a new MediaWikiServices instance and initializes it according to the
263 * given $bootstrapConfig. In particular, all wiring files defined in the
264 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
266 * @param Config|null $bootstrapConfig The Config object to be registered as the
267 * 'BootstrapConfig' service.
269 * @param string $loadWiring set this to 'load' to load the wiring files specified
270 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
272 * @return MediaWikiServices
273 * @throws MWException
274 * @throws \FatalError
276 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
277 $instance = new self( $bootstrapConfig );
279 // Load the default wiring from the specified files.
280 if ( $loadWiring === 'load' ) {
281 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
282 $instance->loadWiringFiles( $wiringFiles );
285 // Provide a traditional hook point to allow extensions to configure services.
286 Hooks
::run( 'MediaWikiServices', [ $instance ] );
292 * Disables all storage layer services. After calling this, any attempt to access the
293 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
298 * @warning This is intended for extreme situations only and should never be used
299 * while serving normal web requests. Legitimate use cases for this method include
300 * the installation process. Test fixtures may also use this, if the fixture relies
303 * @see resetGlobalInstance()
304 * @see resetChildProcessServices()
306 public static function disableStorageBackend() {
307 // TODO: also disable some Caches, JobQueues, etc
308 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
309 $services = self
::getInstance();
311 foreach ( $destroy as $name ) {
312 $services->disableService( $name );
315 ObjectCache
::clear();
319 * Resets any services that may have become stale after a child process
320 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
321 * to call this method from the parent process.
325 * @note This is intended for use in the context of process forking only!
327 * @see resetGlobalInstance()
328 * @see disableStorageBackend()
330 public static function resetChildProcessServices() {
331 // NOTE: for now, just reset everything. Since we don't know the interdependencies
332 // between services, we can't do this more selectively at this time.
333 self
::resetGlobalInstance();
335 // Child, reseed because there is no bug in PHP:
336 // https://bugs.php.net/bug.php?id=42465
337 mt_srand( getmypid() );
341 * Resets the given service for testing purposes.
345 * @warning This is generally unsafe! Other services may still retain references
346 * to the stale service instance, leading to failures and inconsistencies. Subclasses
347 * may use this method to reset specific services under specific instances, but
348 * it should not be exposed to application logic.
350 * @note With proper dependency injection used throughout the codebase, this method
351 * should not be needed. It is provided to allow tests that pollute global service
352 * instances to clean up.
354 * @param string $name
355 * @param bool $destroy Whether the service instance should be destroyed if it exists.
356 * When set to false, any existing service instance will effectively be detached
357 * from the container.
359 * @throws MWException if called outside of PHPUnit tests.
361 public function resetServiceForTesting( $name, $destroy = true ) {
362 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
363 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
366 $this->resetService( $name, $destroy );
370 * Convenience method that throws an exception unless it is called during a phase in which
371 * resetting of global services is allowed. In general, services should not be reset
372 * individually, since that may introduce inconsistencies.
376 * This method will throw an exception if:
378 * - self::$resetInProgress is false (to allow all services to be reset together
379 * via resetGlobalInstance)
380 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
381 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
383 * This method is intended to be used to safeguard against accidentally resetting
384 * global service instances that are not yet managed by MediaWikiServices. It is
385 * defined here in the MediaWikiServices services class to have a central place
386 * for managing service bootstrapping and resetting.
388 * @param string $method the name of the caller method, as given by __METHOD__.
390 * @throws MWException if called outside bootstrap mode.
392 * @see resetGlobalInstance()
393 * @see forceGlobalInstance()
394 * @see disableStorageBackend()
396 public static function failIfResetNotAllowed( $method ) {
397 if ( !defined( 'MW_PHPUNIT_TEST' )
398 && !defined( 'MW_PARSER_TEST' )
399 && !defined( 'MEDIAWIKI_INSTALL' )
400 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
401 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
403 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
408 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
409 * This has to contain at least the information needed to set up the 'ConfigFactory'
412 public function __construct( Config
$config ) {
413 parent
::__construct();
415 // Register the given Config object as the bootstrap config service.
416 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
421 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
425 * @return ActorMigration
427 public function getActorMigration() {
428 return $this->getService( 'ActorMigration' );
435 public function getBlobStore() {
436 return $this->getService( '_SqlBlobStore' );
441 * @return BlobStoreFactory
443 public function getBlobStoreFactory() {
444 return $this->getService( 'BlobStoreFactory' );
449 * @return BlockManager
451 public function getBlockManager() : BlockManager
{
452 return $this->getService( 'BlockManager' );
457 * @return BlockRestrictionStore
459 public function getBlockRestrictionStore() : BlockRestrictionStore
{
460 return $this->getService( 'BlockRestrictionStore' );
464 * Returns the Config object containing the bootstrap configuration.
465 * Bootstrap configuration would typically include database credentials
466 * and other information that may be needed before the ConfigFactory
467 * service can be instantiated.
469 * @note This should only be used during bootstrapping, in particular
470 * when creating the MainConfig service. Application logic should
471 * use getMainConfig() to get a Config instances.
476 public function getBootstrapConfig() {
477 return $this->getService( 'BootstrapConfig' );
482 * @return NameTableStore
484 public function getChangeTagDefStore() {
485 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
490 * @return CommentStore
492 public function getCommentStore() {
493 return $this->getService( 'CommentStore' );
498 * @return ConfigFactory
500 public function getConfigFactory() {
501 return $this->getService( 'ConfigFactory' );
506 * @return ConfigRepository
508 public function getConfigRepository() {
509 return $this->getService( 'ConfigRepository' );
514 * @return \ConfiguredReadOnlyMode
516 public function getConfiguredReadOnlyMode() {
517 return $this->getService( 'ConfiguredReadOnlyMode' );
524 public function getContentLanguage() {
525 return $this->getService( 'ContentLanguage' );
530 * @return NameTableStore
532 public function getContentModelStore() {
533 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
540 public function getCryptHKDF() {
541 return $this->getService( 'CryptHKDF' );
546 * @return DateFormatterFactory
548 public function getDateFormatterFactory() {
549 return $this->getService( 'DateFormatterFactory' );
554 * @return ILoadBalancer The main DB load balancer for the local wiki.
556 public function getDBLoadBalancer() {
557 return $this->getService( 'DBLoadBalancer' );
564 public function getDBLoadBalancerFactory() {
565 return $this->getService( 'DBLoadBalancerFactory' );
570 * @return EventRelayerGroup
572 public function getEventRelayerGroup() {
573 return $this->getService( 'EventRelayerGroup' );
578 * @return \ExternalStoreAccess
580 public function getExternalStoreAccess() {
581 return $this->getService( 'ExternalStoreAccess' );
586 * @return \ExternalStoreFactory
588 public function getExternalStoreFactory() {
589 return $this->getService( 'ExternalStoreFactory' );
594 * @return GenderCache
596 public function getGenderCache() {
597 return $this->getService( 'GenderCache' );
602 * @return HttpRequestFactory
604 public function getHttpRequestFactory() {
605 return $this->getService( 'HttpRequestFactory' );
610 * @return InterwikiLookup
612 public function getInterwikiLookup() {
613 return $this->getService( 'InterwikiLookup' );
620 public function getLinkCache() {
621 return $this->getService( 'LinkCache' );
625 * LinkRenderer instance that can be used
626 * if no custom options are needed
629 * @return LinkRenderer
631 public function getLinkRenderer() {
632 return $this->getService( 'LinkRenderer' );
637 * @return LinkRendererFactory
639 public function getLinkRendererFactory() {
640 return $this->getService( 'LinkRendererFactory' );
647 public function getLocalServerObjectCache() {
648 return $this->getService( 'LocalServerObjectCache' );
653 * @return MagicWordFactory
655 public function getMagicWordFactory() {
656 return $this->getService( 'MagicWordFactory' );
660 * Returns the Config object that provides configuration for MediaWiki core.
661 * This may or may not be the same object that is returned by getBootstrapConfig().
666 public function getMainConfig() {
667 return $this->getService( 'MainConfig' );
674 public function getMainObjectStash() {
675 return $this->getService( 'MainObjectStash' );
680 * @return \WANObjectCache
682 public function getMainWANObjectCache() {
683 return $this->getService( 'MainWANObjectCache' );
688 * @return MediaHandlerFactory
690 public function getMediaHandlerFactory() {
691 return $this->getService( 'MediaHandlerFactory' );
696 * @return MessageCache
698 public function getMessageCache() : MessageCache
{
699 return $this->getService( 'MessageCache' );
704 * @return MimeAnalyzer
706 public function getMimeAnalyzer() {
707 return $this->getService( 'MimeAnalyzer' );
712 * @return MovePageFactory
714 public function getMovePageFactory() : MovePageFactory
{
715 return $this->getService( 'MovePageFactory' );
720 * @return NamespaceInfo
722 public function getNamespaceInfo() : NamespaceInfo
{
723 return $this->getService( 'NamespaceInfo' );
728 * @return NameTableStoreFactory
730 public function getNameTableStoreFactory() {
731 return $this->getService( 'NameTableStoreFactory' );
736 * @return OldRevisionImporter
738 public function getOldRevisionImporter() {
739 return $this->getService( 'OldRevisionImporter' );
743 * @return PageEditStash
746 public function getPageEditStash() {
747 return $this->getService( 'PageEditStash' );
754 public function getParser() {
755 return $this->getService( 'Parser' );
760 * @return ParserCache
762 public function getParserCache() {
763 return $this->getService( 'ParserCache' );
768 * @return ParserFactory
770 public function getParserFactory() {
771 return $this->getService( 'ParserFactory' );
776 * @return PasswordFactory
778 public function getPasswordFactory() {
779 return $this->getService( 'PasswordFactory' );
784 * @return StatsdDataFactoryInterface
786 public function getPerDbNameStatsdDataFactory() {
787 return $this->getService( 'PerDbNameStatsdDataFactory' );
792 * @return PermissionManager
794 public function getPermissionManager() {
795 return $this->getService( 'PermissionManager' );
800 * @return PreferencesFactory
802 public function getPreferencesFactory() {
803 return $this->getService( 'PreferencesFactory' );
808 * @return ProxyLookup
810 public function getProxyLookup() {
811 return $this->getService( 'ProxyLookup' );
816 * @return \ReadOnlyMode
818 public function getReadOnlyMode() {
819 return $this->getService( 'ReadOnlyMode' );
826 public function getRepoGroup() : RepoGroup
{
827 return $this->getService( 'RepoGroup' );
832 * @return ResourceLoader
834 public function getResourceLoader() {
835 return $this->getService( 'ResourceLoader' );
840 * @return RevisionFactory
842 public function getRevisionFactory() {
843 return $this->getService( 'RevisionFactory' );
848 * @return RevisionLookup
850 public function getRevisionLookup() {
851 return $this->getService( 'RevisionLookup' );
856 * @return RevisionRenderer
858 public function getRevisionRenderer() {
859 return $this->getService( 'RevisionRenderer' );
864 * @return RevisionStore
866 public function getRevisionStore() {
867 return $this->getService( 'RevisionStore' );
872 * @return RevisionStoreFactory
874 public function getRevisionStoreFactory() {
875 return $this->getService( 'RevisionStoreFactory' );
880 * @return SearchEngine
882 public function newSearchEngine() {
883 // New engine object every time, since they keep state
884 return $this->getService( 'SearchEngineFactory' )->create();
889 * @return SearchEngineConfig
891 public function getSearchEngineConfig() {
892 return $this->getService( 'SearchEngineConfig' );
897 * @return SearchEngineFactory
899 public function getSearchEngineFactory() {
900 return $this->getService( 'SearchEngineFactory' );
905 * @return CommandFactory
907 public function getShellCommandFactory() {
908 return $this->getService( 'ShellCommandFactory' );
915 public function getSiteLookup() {
916 return $this->getService( 'SiteLookup' );
923 public function getSiteStore() {
924 return $this->getService( 'SiteStore' );
929 * @return SkinFactory
931 public function getSkinFactory() {
932 return $this->getService( 'SkinFactory' );
937 * @return SlotRoleRegistry
939 public function getSlotRoleRegistry() {
940 return $this->getService( 'SlotRoleRegistry' );
945 * @return NameTableStore
947 public function getSlotRoleStore() {
948 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
953 * @return SpecialPageFactory
955 public function getSpecialPageFactory() : SpecialPageFactory
{
956 return $this->getService( 'SpecialPageFactory' );
961 * @return IBufferingStatsdDataFactory
963 public function getStatsdDataFactory() {
964 return $this->getService( 'StatsdDataFactory' );
969 * @return TitleFormatter
971 public function getTitleFormatter() {
972 return $this->getService( 'TitleFormatter' );
977 * @return TitleParser
979 public function getTitleParser() {
980 return $this->getService( 'TitleParser' );
985 * @return UploadRevisionImporter
987 public function getUploadRevisionImporter() {
988 return $this->getService( 'UploadRevisionImporter' );
993 * @return VirtualRESTServiceClient
995 public function getVirtualRESTServiceClient() {
996 return $this->getService( 'VirtualRESTServiceClient' );
1001 * @return WatchedItemQueryService
1003 public function getWatchedItemQueryService() {
1004 return $this->getService( 'WatchedItemQueryService' );
1009 * @return WatchedItemStoreInterface
1011 public function getWatchedItemStore() {
1012 return $this->getService( 'WatchedItemStore' );
1017 * @return \OldRevisionImporter
1019 public function getWikiRevisionOldRevisionImporter() {
1020 return $this->getService( 'OldRevisionImporter' );
1025 * @return \OldRevisionImporter
1027 public function getWikiRevisionOldRevisionImporterNoUpdates() {
1028 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1033 * @return \UploadRevisionImporter
1035 public function getWikiRevisionUploadImporter() {
1036 return $this->getService( 'UploadRevisionImporter' );