9 use DateFormatterFactory
;
10 use EventRelayerGroup
;
14 use IBufferingStatsdDataFactory
;
15 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
16 use MediaWiki\Block\BlockManager
;
17 use MediaWiki\Block\BlockRestrictionStore
;
18 use MediaWiki\Http\HttpRequestFactory
;
19 use MediaWiki\Permissions\PermissionManager
;
20 use MediaWiki\Preferences\PreferencesFactory
;
21 use MediaWiki\Shell\CommandFactory
;
22 use MediaWiki\Revision\RevisionRenderer
;
23 use MediaWiki\Revision\SlotRoleRegistry
;
24 use MediaWiki\Special\SpecialPageFactory
;
25 use MediaWiki\Storage\BlobStore
;
26 use MediaWiki\Storage\BlobStoreFactory
;
27 use MediaWiki\Storage\NameTableStore
;
28 use MediaWiki\Storage\NameTableStoreFactory
;
29 use MediaWiki\Revision\RevisionFactory
;
30 use MediaWiki\Revision\RevisionLookup
;
31 use MediaWiki\Revision\RevisionStore
;
32 use OldRevisionImporter
;
33 use MediaWiki\Revision\RevisionStoreFactory
;
34 use UploadRevisionImporter
;
35 use Wikimedia\Rdbms\LBFactory
;
37 use Wikimedia\Rdbms\LoadBalancer
;
38 use MediaHandlerFactory
;
39 use MediaWiki\Config\ConfigRepository
;
40 use MediaWiki\Linker\LinkRenderer
;
41 use MediaWiki\Linker\LinkRendererFactory
;
54 use SearchEngineConfig
;
55 use SearchEngineFactory
;
58 use WatchedItemStoreInterface
;
59 use WatchedItemQueryService
;
63 use VirtualRESTServiceClient
;
64 use Wikimedia\Services\SalvageableService
;
65 use Wikimedia\Services\ServiceContainer
;
66 use Wikimedia\Services\NoSuchServiceException
;
67 use MediaWiki\Interwiki\InterwikiLookup
;
69 use MediaWiki\Storage\PageEditStash
;
72 * Service locator for MediaWiki core services.
74 * This program is free software; you can redistribute it and/or modify
75 * it under the terms of the GNU General Public License as published by
76 * the Free Software Foundation; either version 2 of the License, or
77 * (at your option) any later version.
79 * This program is distributed in the hope that it will be useful,
80 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82 * GNU General Public License for more details.
84 * You should have received a copy of the GNU General Public License along
85 * with this program; if not, write to the Free Software Foundation, Inc.,
86 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
87 * http://www.gnu.org/copyleft/gpl.html
95 * MediaWikiServices is the service locator for the application scope of MediaWiki.
96 * Its implemented as a simple configurable DI container.
97 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
98 * the network of service objects that defines MediaWiki's application logic.
99 * It acts as an entry point to MediaWiki's dependency injection mechanism.
101 * Services are defined in the "wiring" array passed to the constructor,
102 * or by calling defineService().
104 * @see docs/injection.txt for an overview of using dependency injection in the
105 * MediaWiki code base.
107 class MediaWikiServices
extends ServiceContainer
{
110 * @var MediaWikiServices|null
112 private static $instance = null;
115 * Returns the global default instance of the top level service locator.
119 * The default instance is initialized using the service instantiator functions
120 * defined in ServiceWiring.php.
122 * @note This should only be called by static functions! The instance returned here
123 * should not be passed around! Objects that need access to a service should have
124 * that service injected into the constructor, never a service locator!
126 * @return MediaWikiServices
128 public static function getInstance() {
129 if ( self
::$instance === null ) {
130 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
131 // but some information from the global scope has to be injected here,
132 // even if it's just a file name or database credentials to load
133 // configuration from.
134 $bootstrapConfig = new GlobalVarConfig();
135 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
138 return self
::$instance;
142 * Replaces the global MediaWikiServices instance.
146 * @note This is for use in PHPUnit tests only!
148 * @throws MWException if called outside of PHPUnit tests.
150 * @param MediaWikiServices $services The new MediaWikiServices object.
152 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
154 public static function forceGlobalInstance( MediaWikiServices
$services ) {
155 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
156 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
159 $old = self
::getInstance();
160 self
::$instance = $services;
166 * Creates a new instance of MediaWikiServices and sets it as the global default
167 * instance. getInstance() will return a different MediaWikiServices object
168 * after every call to resetGlobalInstance().
172 * @warning This should not be used during normal operation. It is intended for use
173 * when the configuration has changed significantly since bootstrap time, e.g.
174 * during the installation process or during testing.
176 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
177 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
178 * any of the services managed by MediaWikiServices exist. If any service objects
179 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
180 * with the operation of the services managed by the new MediaWikiServices.
181 * Operating with a mix of services created by the old and the new
182 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
183 * Any class implementing LAZY LOADING is especially prone to this problem,
184 * since instances would typically retain a reference to a storage layer service.
186 * @see forceGlobalInstance()
187 * @see resetGlobalInstance()
188 * @see resetBetweenTest()
190 * @param Config|null $bootstrapConfig The Config object to be registered as the
191 * 'BootstrapConfig' service. This has to contain at least the information
192 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
193 * config of the old instance of MediaWikiServices will be re-used. If there
194 * was no previous instance, a new GlobalVarConfig object will be used to
195 * bootstrap the services.
197 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
198 * See SalvageableService for details.
200 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
201 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
204 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
205 if ( self
::$instance === null ) {
206 // no global instance yet, nothing to reset
210 self
::failIfResetNotAllowed( __METHOD__
);
212 if ( $bootstrapConfig === null ) {
213 $bootstrapConfig = self
::$instance->getBootstrapConfig();
216 $oldInstance = self
::$instance;
218 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
219 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
221 if ( $quick === 'quick' ) {
222 self
::$instance->salvage( $oldInstance );
224 $oldInstance->destroy();
229 * Salvages the state of any salvageable service instances in $other.
231 * @note $other will have been destroyed when salvage() returns.
233 * @param MediaWikiServices $other
235 private function salvage( self
$other ) {
236 foreach ( $this->getServiceNames() as $name ) {
237 // The service could be new in the new instance and not registered in the
238 // other instance (e.g. an extension that was loaded after the instantiation of
239 // the other instance. Skip this service in this case. See T143974
241 $oldService = $other->peekService( $name );
242 } catch ( NoSuchServiceException
$e ) {
246 if ( $oldService instanceof SalvageableService
) {
247 /** @var SalvageableService $newService */
248 $newService = $this->getService( $name );
249 $newService->salvage( $oldService );
257 * Creates a new MediaWikiServices instance and initializes it according to the
258 * given $bootstrapConfig. In particular, all wiring files defined in the
259 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
261 * @param Config|null $bootstrapConfig The Config object to be registered as the
262 * 'BootstrapConfig' service.
264 * @param string $loadWiring set this to 'load' to load the wiring files specified
265 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
267 * @return MediaWikiServices
268 * @throws MWException
269 * @throws \FatalError
271 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
272 $instance = new self( $bootstrapConfig );
274 // Load the default wiring from the specified files.
275 if ( $loadWiring === 'load' ) {
276 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
277 $instance->loadWiringFiles( $wiringFiles );
280 // Provide a traditional hook point to allow extensions to configure services.
281 Hooks
::run( 'MediaWikiServices', [ $instance ] );
287 * Disables all storage layer services. After calling this, any attempt to access the
288 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
293 * @warning This is intended for extreme situations only and should never be used
294 * while serving normal web requests. Legitimate use cases for this method include
295 * the installation process. Test fixtures may also use this, if the fixture relies
298 * @see resetGlobalInstance()
299 * @see resetChildProcessServices()
301 public static function disableStorageBackend() {
302 // TODO: also disable some Caches, JobQueues, etc
303 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
304 $services = self
::getInstance();
306 foreach ( $destroy as $name ) {
307 $services->disableService( $name );
310 ObjectCache
::clear();
314 * Resets any services that may have become stale after a child process
315 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
316 * to call this method from the parent process.
320 * @note This is intended for use in the context of process forking only!
322 * @see resetGlobalInstance()
323 * @see disableStorageBackend()
325 public static function resetChildProcessServices() {
326 // NOTE: for now, just reset everything. Since we don't know the interdependencies
327 // between services, we can't do this more selectively at this time.
328 self
::resetGlobalInstance();
330 // Child, reseed because there is no bug in PHP:
331 // https://bugs.php.net/bug.php?id=42465
332 mt_srand( getmypid() );
336 * Resets the given service for testing purposes.
340 * @warning This is generally unsafe! Other services may still retain references
341 * to the stale service instance, leading to failures and inconsistencies. Subclasses
342 * may use this method to reset specific services under specific instances, but
343 * it should not be exposed to application logic.
345 * @note With proper dependency injection used throughout the codebase, this method
346 * should not be needed. It is provided to allow tests that pollute global service
347 * instances to clean up.
349 * @param string $name
350 * @param bool $destroy Whether the service instance should be destroyed if it exists.
351 * When set to false, any existing service instance will effectively be detached
352 * from the container.
354 * @throws MWException if called outside of PHPUnit tests.
356 public function resetServiceForTesting( $name, $destroy = true ) {
357 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
358 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
361 $this->resetService( $name, $destroy );
365 * Convenience method that throws an exception unless it is called during a phase in which
366 * resetting of global services is allowed. In general, services should not be reset
367 * individually, since that may introduce inconsistencies.
371 * This method will throw an exception if:
373 * - self::$resetInProgress is false (to allow all services to be reset together
374 * via resetGlobalInstance)
375 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
376 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
378 * This method is intended to be used to safeguard against accidentally resetting
379 * global service instances that are not yet managed by MediaWikiServices. It is
380 * defined here in the MediaWikiServices services class to have a central place
381 * for managing service bootstrapping and resetting.
383 * @param string $method the name of the caller method, as given by __METHOD__.
385 * @throws MWException if called outside bootstrap mode.
387 * @see resetGlobalInstance()
388 * @see forceGlobalInstance()
389 * @see disableStorageBackend()
391 public static function failIfResetNotAllowed( $method ) {
392 if ( !defined( 'MW_PHPUNIT_TEST' )
393 && !defined( 'MW_PARSER_TEST' )
394 && !defined( 'MEDIAWIKI_INSTALL' )
395 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
396 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
398 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
403 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
404 * This has to contain at least the information needed to set up the 'ConfigFactory'
407 public function __construct( Config
$config ) {
408 parent
::__construct();
410 // Register the given Config object as the bootstrap config service.
411 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
416 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
420 * @return ActorMigration
422 public function getActorMigration() {
423 return $this->getService( 'ActorMigration' );
430 public function getBlobStore() {
431 return $this->getService( '_SqlBlobStore' );
436 * @return BlobStoreFactory
438 public function getBlobStoreFactory() {
439 return $this->getService( 'BlobStoreFactory' );
444 * @return BlockManager
446 public function getBlockManager() : BlockManager
{
447 return $this->getService( 'BlockManager' );
452 * @return BlockRestrictionStore
454 public function getBlockRestrictionStore() : BlockRestrictionStore
{
455 return $this->getService( 'BlockRestrictionStore' );
459 * Returns the Config object containing the bootstrap configuration.
460 * Bootstrap configuration would typically include database credentials
461 * and other information that may be needed before the ConfigFactory
462 * service can be instantiated.
464 * @note This should only be used during bootstrapping, in particular
465 * when creating the MainConfig service. Application logic should
466 * use getMainConfig() to get a Config instances.
471 public function getBootstrapConfig() {
472 return $this->getService( 'BootstrapConfig' );
477 * @return NameTableStore
479 public function getChangeTagDefStore() {
480 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
485 * @return CommentStore
487 public function getCommentStore() {
488 return $this->getService( 'CommentStore' );
493 * @return ConfigFactory
495 public function getConfigFactory() {
496 return $this->getService( 'ConfigFactory' );
501 * @return ConfigRepository
503 public function getConfigRepository() {
504 return $this->getService( 'ConfigRepository' );
509 * @return \ConfiguredReadOnlyMode
511 public function getConfiguredReadOnlyMode() {
512 return $this->getService( 'ConfiguredReadOnlyMode' );
519 public function getContentLanguage() {
520 return $this->getService( 'ContentLanguage' );
525 * @return NameTableStore
527 public function getContentModelStore() {
528 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
535 public function getCryptHKDF() {
536 return $this->getService( 'CryptHKDF' );
541 * @return DateFormatterFactory
543 public function getDateFormatterFactory() {
544 return $this->getService( 'DateFormatterFactory' );
549 * @return LoadBalancer The main DB load balancer for the local wiki.
551 public function getDBLoadBalancer() {
552 return $this->getService( 'DBLoadBalancer' );
559 public function getDBLoadBalancerFactory() {
560 return $this->getService( 'DBLoadBalancerFactory' );
565 * @return EventRelayerGroup
567 public function getEventRelayerGroup() {
568 return $this->getService( 'EventRelayerGroup' );
573 * @return \ExternalStoreFactory
575 public function getExternalStoreFactory() {
576 return $this->getService( 'ExternalStoreFactory' );
581 * @return GenderCache
583 public function getGenderCache() {
584 return $this->getService( 'GenderCache' );
589 * @return HttpRequestFactory
591 public function getHttpRequestFactory() {
592 return $this->getService( 'HttpRequestFactory' );
597 * @return InterwikiLookup
599 public function getInterwikiLookup() {
600 return $this->getService( 'InterwikiLookup' );
607 public function getLinkCache() {
608 return $this->getService( 'LinkCache' );
612 * LinkRenderer instance that can be used
613 * if no custom options are needed
616 * @return LinkRenderer
618 public function getLinkRenderer() {
619 return $this->getService( 'LinkRenderer' );
624 * @return LinkRendererFactory
626 public function getLinkRendererFactory() {
627 return $this->getService( 'LinkRendererFactory' );
634 public function getLocalServerObjectCache() {
635 return $this->getService( 'LocalServerObjectCache' );
640 * @return MagicWordFactory
642 public function getMagicWordFactory() {
643 return $this->getService( 'MagicWordFactory' );
647 * Returns the Config object that provides configuration for MediaWiki core.
648 * This may or may not be the same object that is returned by getBootstrapConfig().
653 public function getMainConfig() {
654 return $this->getService( 'MainConfig' );
661 public function getMainObjectStash() {
662 return $this->getService( 'MainObjectStash' );
667 * @return \WANObjectCache
669 public function getMainWANObjectCache() {
670 return $this->getService( 'MainWANObjectCache' );
675 * @return MediaHandlerFactory
677 public function getMediaHandlerFactory() {
678 return $this->getService( 'MediaHandlerFactory' );
683 * @return MimeAnalyzer
685 public function getMimeAnalyzer() {
686 return $this->getService( 'MimeAnalyzer' );
691 * @return NamespaceInfo
693 public function getNamespaceInfo() : NamespaceInfo
{
694 return $this->getService( 'NamespaceInfo' );
699 * @return NameTableStoreFactory
701 public function getNameTableStoreFactory() {
702 return $this->getService( 'NameTableStoreFactory' );
707 * @return OldRevisionImporter
709 public function getOldRevisionImporter() {
710 return $this->getService( 'OldRevisionImporter' );
714 * @return PageEditStash
717 public function getPageEditStash() {
718 return $this->getService( 'PageEditStash' );
725 public function getParser() {
726 return $this->getService( 'Parser' );
731 * @return ParserCache
733 public function getParserCache() {
734 return $this->getService( 'ParserCache' );
739 * @return ParserFactory
741 public function getParserFactory() {
742 return $this->getService( 'ParserFactory' );
747 * @return PasswordFactory
749 public function getPasswordFactory() {
750 return $this->getService( 'PasswordFactory' );
755 * @return StatsdDataFactoryInterface
757 public function getPerDbNameStatsdDataFactory() {
758 return $this->getService( 'PerDbNameStatsdDataFactory' );
763 * @return PermissionManager
765 public function getPermissionManager() {
766 return $this->getService( 'PermissionManager' );
771 * @return PreferencesFactory
773 public function getPreferencesFactory() {
774 return $this->getService( 'PreferencesFactory' );
779 * @return ProxyLookup
781 public function getProxyLookup() {
782 return $this->getService( 'ProxyLookup' );
787 * @return \ReadOnlyMode
789 public function getReadOnlyMode() {
790 return $this->getService( 'ReadOnlyMode' );
797 public function getRepoGroup() : RepoGroup
{
798 return $this->getService( 'RepoGroup' );
803 * @return ResourceLoader
805 public function getResourceLoader() {
806 return $this->getService( 'ResourceLoader' );
811 * @return RevisionFactory
813 public function getRevisionFactory() {
814 return $this->getService( 'RevisionFactory' );
819 * @return RevisionLookup
821 public function getRevisionLookup() {
822 return $this->getService( 'RevisionLookup' );
827 * @return RevisionRenderer
829 public function getRevisionRenderer() {
830 return $this->getService( 'RevisionRenderer' );
835 * @return RevisionStore
837 public function getRevisionStore() {
838 return $this->getService( 'RevisionStore' );
843 * @return RevisionStoreFactory
845 public function getRevisionStoreFactory() {
846 return $this->getService( 'RevisionStoreFactory' );
851 * @return SearchEngine
853 public function newSearchEngine() {
854 // New engine object every time, since they keep state
855 return $this->getService( 'SearchEngineFactory' )->create();
860 * @return SearchEngineConfig
862 public function getSearchEngineConfig() {
863 return $this->getService( 'SearchEngineConfig' );
868 * @return SearchEngineFactory
870 public function getSearchEngineFactory() {
871 return $this->getService( 'SearchEngineFactory' );
876 * @return CommandFactory
878 public function getShellCommandFactory() {
879 return $this->getService( 'ShellCommandFactory' );
886 public function getSiteLookup() {
887 return $this->getService( 'SiteLookup' );
894 public function getSiteStore() {
895 return $this->getService( 'SiteStore' );
900 * @return SkinFactory
902 public function getSkinFactory() {
903 return $this->getService( 'SkinFactory' );
908 * @return SlotRoleRegistry
910 public function getSlotRoleRegistry() {
911 return $this->getService( 'SlotRoleRegistry' );
916 * @return NameTableStore
918 public function getSlotRoleStore() {
919 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
924 * @return SpecialPageFactory
926 public function getSpecialPageFactory() : SpecialPageFactory
{
927 return $this->getService( 'SpecialPageFactory' );
932 * @return IBufferingStatsdDataFactory
934 public function getStatsdDataFactory() {
935 return $this->getService( 'StatsdDataFactory' );
940 * @return TitleFormatter
942 public function getTitleFormatter() {
943 return $this->getService( 'TitleFormatter' );
948 * @return TitleParser
950 public function getTitleParser() {
951 return $this->getService( 'TitleParser' );
956 * @return UploadRevisionImporter
958 public function getUploadRevisionImporter() {
959 return $this->getService( 'UploadRevisionImporter' );
964 * @return VirtualRESTServiceClient
966 public function getVirtualRESTServiceClient() {
967 return $this->getService( 'VirtualRESTServiceClient' );
972 * @return WatchedItemQueryService
974 public function getWatchedItemQueryService() {
975 return $this->getService( 'WatchedItemQueryService' );
980 * @return WatchedItemStoreInterface
982 public function getWatchedItemStore() {
983 return $this->getService( 'WatchedItemStore' );
988 * @return \OldRevisionImporter
990 public function getWikiRevisionOldRevisionImporter() {
991 return $this->getService( 'OldRevisionImporter' );
996 * @return \OldRevisionImporter
998 public function getWikiRevisionOldRevisionImporterNoUpdates() {
999 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1004 * @return \UploadRevisionImporter
1006 public function getWikiRevisionUploadImporter() {
1007 return $this->getService( 'UploadRevisionImporter' );