9 use DateFormatterFactory
;
10 use EventRelayerGroup
;
14 use IBufferingStatsdDataFactory
;
15 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface
;
16 use MediaWiki\Http\HttpRequestFactory
;
17 use MediaWiki\Permissions\PermissionManager
;
18 use MediaWiki\Preferences\PreferencesFactory
;
19 use MediaWiki\Shell\CommandFactory
;
20 use MediaWiki\Revision\RevisionRenderer
;
21 use MediaWiki\Revision\SlotRoleRegistry
;
22 use MediaWiki\Special\SpecialPageFactory
;
23 use MediaWiki\Storage\BlobStore
;
24 use MediaWiki\Storage\BlobStoreFactory
;
25 use MediaWiki\Storage\NameTableStore
;
26 use MediaWiki\Storage\NameTableStoreFactory
;
27 use MediaWiki\Revision\RevisionFactory
;
28 use MediaWiki\Revision\RevisionLookup
;
29 use MediaWiki\Revision\RevisionStore
;
30 use OldRevisionImporter
;
31 use MediaWiki\Revision\RevisionStoreFactory
;
32 use UploadRevisionImporter
;
33 use Wikimedia\Rdbms\LBFactory
;
35 use Wikimedia\Rdbms\LoadBalancer
;
36 use MediaHandlerFactory
;
37 use MediaWiki\Config\ConfigRepository
;
38 use MediaWiki\Linker\LinkRenderer
;
39 use MediaWiki\Linker\LinkRendererFactory
;
51 use SearchEngineConfig
;
52 use SearchEngineFactory
;
55 use WatchedItemStoreInterface
;
56 use WatchedItemQueryService
;
60 use VirtualRESTServiceClient
;
61 use Wikimedia\Services\SalvageableService
;
62 use Wikimedia\Services\ServiceContainer
;
63 use Wikimedia\Services\NoSuchServiceException
;
64 use MediaWiki\Interwiki\InterwikiLookup
;
66 use MediaWiki\Storage\PageEditStash
;
69 * Service locator for MediaWiki core services.
71 * This program is free software; you can redistribute it and/or modify
72 * it under the terms of the GNU General Public License as published by
73 * the Free Software Foundation; either version 2 of the License, or
74 * (at your option) any later version.
76 * This program is distributed in the hope that it will be useful,
77 * but WITHOUT ANY WARRANTY; without even the implied warranty of
78 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79 * GNU General Public License for more details.
81 * You should have received a copy of the GNU General Public License along
82 * with this program; if not, write to the Free Software Foundation, Inc.,
83 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
84 * http://www.gnu.org/copyleft/gpl.html
92 * MediaWikiServices is the service locator for the application scope of MediaWiki.
93 * Its implemented as a simple configurable DI container.
94 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
95 * the network of service objects that defines MediaWiki's application logic.
96 * It acts as an entry point to MediaWiki's dependency injection mechanism.
98 * Services are defined in the "wiring" array passed to the constructor,
99 * or by calling defineService().
101 * @see docs/injection.txt for an overview of using dependency injection in the
102 * MediaWiki code base.
104 class MediaWikiServices
extends ServiceContainer
{
107 * @var MediaWikiServices|null
109 private static $instance = null;
112 * Returns the global default instance of the top level service locator.
116 * The default instance is initialized using the service instantiator functions
117 * defined in ServiceWiring.php.
119 * @note This should only be called by static functions! The instance returned here
120 * should not be passed around! Objects that need access to a service should have
121 * that service injected into the constructor, never a service locator!
123 * @return MediaWikiServices
125 public static function getInstance() {
126 if ( self
::$instance === null ) {
127 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
128 // but some information from the global scope has to be injected here,
129 // even if it's just a file name or database credentials to load
130 // configuration from.
131 $bootstrapConfig = new GlobalVarConfig();
132 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
135 return self
::$instance;
139 * Replaces the global MediaWikiServices instance.
143 * @note This is for use in PHPUnit tests only!
145 * @throws MWException if called outside of PHPUnit tests.
147 * @param MediaWikiServices $services The new MediaWikiServices object.
149 * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later.
151 public static function forceGlobalInstance( MediaWikiServices
$services ) {
152 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
153 throw new MWException( __METHOD__
. ' must not be used outside unit tests.' );
156 $old = self
::getInstance();
157 self
::$instance = $services;
163 * Creates a new instance of MediaWikiServices and sets it as the global default
164 * instance. getInstance() will return a different MediaWikiServices object
165 * after every call to resetGlobalInstance().
169 * @warning This should not be used during normal operation. It is intended for use
170 * when the configuration has changed significantly since bootstrap time, e.g.
171 * during the installation process or during testing.
173 * @warning Calling resetGlobalInstance() may leave the application in an inconsistent
174 * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to
175 * any of the services managed by MediaWikiServices exist. If any service objects
176 * managed by the old MediaWikiServices instance remain in use, they may INTERFERE
177 * with the operation of the services managed by the new MediaWikiServices.
178 * Operating with a mix of services created by the old and the new
179 * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS!
180 * Any class implementing LAZY LOADING is especially prone to this problem,
181 * since instances would typically retain a reference to a storage layer service.
183 * @see forceGlobalInstance()
184 * @see resetGlobalInstance()
185 * @see resetBetweenTest()
187 * @param Config|null $bootstrapConfig The Config object to be registered as the
188 * 'BootstrapConfig' service. This has to contain at least the information
189 * needed to set up the 'ConfigFactory' service. If not given, the bootstrap
190 * config of the old instance of MediaWikiServices will be re-used. If there
191 * was no previous instance, a new GlobalVarConfig object will be used to
192 * bootstrap the services.
194 * @param string $quick Set this to "quick" to allow expensive resources to be re-used.
195 * See SalvageableService for details.
197 * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in
198 * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN
201 public static function resetGlobalInstance( Config
$bootstrapConfig = null, $quick = '' ) {
202 if ( self
::$instance === null ) {
203 // no global instance yet, nothing to reset
207 self
::failIfResetNotAllowed( __METHOD__
);
209 if ( $bootstrapConfig === null ) {
210 $bootstrapConfig = self
::$instance->getBootstrapConfig();
213 $oldInstance = self
::$instance;
215 self
::$instance = self
::newInstance( $bootstrapConfig, 'load' );
216 self
::$instance->importWiring( $oldInstance, [ 'BootstrapConfig' ] );
218 if ( $quick === 'quick' ) {
219 self
::$instance->salvage( $oldInstance );
221 $oldInstance->destroy();
226 * Salvages the state of any salvageable service instances in $other.
228 * @note $other will have been destroyed when salvage() returns.
230 * @param MediaWikiServices $other
232 private function salvage( self
$other ) {
233 foreach ( $this->getServiceNames() as $name ) {
234 // The service could be new in the new instance and not registered in the
235 // other instance (e.g. an extension that was loaded after the instantiation of
236 // the other instance. Skip this service in this case. See T143974
238 $oldService = $other->peekService( $name );
239 } catch ( NoSuchServiceException
$e ) {
243 if ( $oldService instanceof SalvageableService
) {
244 /** @var SalvageableService $newService */
245 $newService = $this->getService( $name );
246 $newService->salvage( $oldService );
254 * Creates a new MediaWikiServices instance and initializes it according to the
255 * given $bootstrapConfig. In particular, all wiring files defined in the
256 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
258 * @param Config|null $bootstrapConfig The Config object to be registered as the
259 * 'BootstrapConfig' service.
261 * @param string $loadWiring set this to 'load' to load the wiring files specified
262 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
264 * @return MediaWikiServices
265 * @throws MWException
266 * @throws \FatalError
268 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
269 $instance = new self( $bootstrapConfig );
271 // Load the default wiring from the specified files.
272 if ( $loadWiring === 'load' ) {
273 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
274 $instance->loadWiringFiles( $wiringFiles );
277 // Provide a traditional hook point to allow extensions to configure services.
278 Hooks
::run( 'MediaWikiServices', [ $instance ] );
284 * Disables all storage layer services. After calling this, any attempt to access the
285 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
290 * @warning This is intended for extreme situations only and should never be used
291 * while serving normal web requests. Legitimate use cases for this method include
292 * the installation process. Test fixtures may also use this, if the fixture relies
295 * @see resetGlobalInstance()
296 * @see resetChildProcessServices()
298 public static function disableStorageBackend() {
299 // TODO: also disable some Caches, JobQueues, etc
300 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
301 $services = self
::getInstance();
303 foreach ( $destroy as $name ) {
304 $services->disableService( $name );
307 ObjectCache
::clear();
311 * Resets any services that may have become stale after a child process
312 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
313 * to call this method from the parent process.
317 * @note This is intended for use in the context of process forking only!
319 * @see resetGlobalInstance()
320 * @see disableStorageBackend()
322 public static function resetChildProcessServices() {
323 // NOTE: for now, just reset everything. Since we don't know the interdependencies
324 // between services, we can't do this more selectively at this time.
325 self
::resetGlobalInstance();
327 // Child, reseed because there is no bug in PHP:
328 // https://bugs.php.net/bug.php?id=42465
329 mt_srand( getmypid() );
333 * Resets the given service for testing purposes.
337 * @warning This is generally unsafe! Other services may still retain references
338 * to the stale service instance, leading to failures and inconsistencies. Subclasses
339 * may use this method to reset specific services under specific instances, but
340 * it should not be exposed to application logic.
342 * @note With proper dependency injection used throughout the codebase, this method
343 * should not be needed. It is provided to allow tests that pollute global service
344 * instances to clean up.
346 * @param string $name
347 * @param bool $destroy Whether the service instance should be destroyed if it exists.
348 * When set to false, any existing service instance will effectively be detached
349 * from the container.
351 * @throws MWException if called outside of PHPUnit tests.
353 public function resetServiceForTesting( $name, $destroy = true ) {
354 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
355 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
358 $this->resetService( $name, $destroy );
362 * Convenience method that throws an exception unless it is called during a phase in which
363 * resetting of global services is allowed. In general, services should not be reset
364 * individually, since that may introduce inconsistencies.
368 * This method will throw an exception if:
370 * - self::$resetInProgress is false (to allow all services to be reset together
371 * via resetGlobalInstance)
372 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
373 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
375 * This method is intended to be used to safeguard against accidentally resetting
376 * global service instances that are not yet managed by MediaWikiServices. It is
377 * defined here in the MediaWikiServices services class to have a central place
378 * for managing service bootstrapping and resetting.
380 * @param string $method the name of the caller method, as given by __METHOD__.
382 * @throws MWException if called outside bootstrap mode.
384 * @see resetGlobalInstance()
385 * @see forceGlobalInstance()
386 * @see disableStorageBackend()
388 public static function failIfResetNotAllowed( $method ) {
389 if ( !defined( 'MW_PHPUNIT_TEST' )
390 && !defined( 'MW_PARSER_TEST' )
391 && !defined( 'MEDIAWIKI_INSTALL' )
392 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
393 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
395 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
400 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
401 * This has to contain at least the information needed to set up the 'ConfigFactory'
404 public function __construct( Config
$config ) {
405 parent
::__construct();
407 // Register the given Config object as the bootstrap config service.
408 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
413 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
417 * @return ActorMigration
419 public function getActorMigration() {
420 return $this->getService( 'ActorMigration' );
427 public function getBlobStore() {
428 return $this->getService( '_SqlBlobStore' );
433 * @return BlobStoreFactory
435 public function getBlobStoreFactory() {
436 return $this->getService( 'BlobStoreFactory' );
440 * Returns the Config object containing the bootstrap configuration.
441 * Bootstrap configuration would typically include database credentials
442 * and other information that may be needed before the ConfigFactory
443 * service can be instantiated.
445 * @note This should only be used during bootstrapping, in particular
446 * when creating the MainConfig service. Application logic should
447 * use getMainConfig() to get a Config instances.
452 public function getBootstrapConfig() {
453 return $this->getService( 'BootstrapConfig' );
458 * @return NameTableStore
460 public function getChangeTagDefStore() {
461 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
466 * @return CommentStore
468 public function getCommentStore() {
469 return $this->getService( 'CommentStore' );
474 * @return ConfigFactory
476 public function getConfigFactory() {
477 return $this->getService( 'ConfigFactory' );
482 * @return ConfigRepository
484 public function getConfigRepository() {
485 return $this->getService( 'ConfigRepository' );
490 * @return \ConfiguredReadOnlyMode
492 public function getConfiguredReadOnlyMode() {
493 return $this->getService( 'ConfiguredReadOnlyMode' );
500 public function getContentLanguage() {
501 return $this->getService( 'ContentLanguage' );
506 * @return NameTableStore
508 public function getContentModelStore() {
509 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
516 public function getCryptHKDF() {
517 return $this->getService( 'CryptHKDF' );
522 * @return DateFormatterFactory
524 public function getDateFormatterFactory() {
525 return $this->getService( 'DateFormatterFactory' );
530 * @return LoadBalancer The main DB load balancer for the local wiki.
532 public function getDBLoadBalancer() {
533 return $this->getService( 'DBLoadBalancer' );
540 public function getDBLoadBalancerFactory() {
541 return $this->getService( 'DBLoadBalancerFactory' );
546 * @return EventRelayerGroup
548 public function getEventRelayerGroup() {
549 return $this->getService( 'EventRelayerGroup' );
554 * @return \ExternalStoreFactory
556 public function getExternalStoreFactory() {
557 return $this->getService( 'ExternalStoreFactory' );
562 * @return GenderCache
564 public function getGenderCache() {
565 return $this->getService( 'GenderCache' );
570 * @return HttpRequestFactory
572 public function getHttpRequestFactory() {
573 return $this->getService( 'HttpRequestFactory' );
578 * @return InterwikiLookup
580 public function getInterwikiLookup() {
581 return $this->getService( 'InterwikiLookup' );
588 public function getLinkCache() {
589 return $this->getService( 'LinkCache' );
593 * LinkRenderer instance that can be used
594 * if no custom options are needed
597 * @return LinkRenderer
599 public function getLinkRenderer() {
600 return $this->getService( 'LinkRenderer' );
605 * @return LinkRendererFactory
607 public function getLinkRendererFactory() {
608 return $this->getService( 'LinkRendererFactory' );
615 public function getLocalServerObjectCache() {
616 return $this->getService( 'LocalServerObjectCache' );
621 * @return MagicWordFactory
623 public function getMagicWordFactory() {
624 return $this->getService( 'MagicWordFactory' );
628 * Returns the Config object that provides configuration for MediaWiki core.
629 * This may or may not be the same object that is returned by getBootstrapConfig().
634 public function getMainConfig() {
635 return $this->getService( 'MainConfig' );
642 public function getMainObjectStash() {
643 return $this->getService( 'MainObjectStash' );
648 * @return \WANObjectCache
650 public function getMainWANObjectCache() {
651 return $this->getService( 'MainWANObjectCache' );
656 * @return MediaHandlerFactory
658 public function getMediaHandlerFactory() {
659 return $this->getService( 'MediaHandlerFactory' );
664 * @return MimeAnalyzer
666 public function getMimeAnalyzer() {
667 return $this->getService( 'MimeAnalyzer' );
672 * @return NamespaceInfo
674 public function getNamespaceInfo() : NamespaceInfo
{
675 return $this->getService( 'NamespaceInfo' );
680 * @return NameTableStoreFactory
682 public function getNameTableStoreFactory() {
683 return $this->getService( 'NameTableStoreFactory' );
688 * @return OldRevisionImporter
690 public function getOldRevisionImporter() {
691 return $this->getService( 'OldRevisionImporter' );
695 * @return PageEditStash
698 public function getPageEditStash() {
699 return $this->getService( 'PageEditStash' );
706 public function getParser() {
707 return $this->getService( 'Parser' );
712 * @return ParserCache
714 public function getParserCache() {
715 return $this->getService( 'ParserCache' );
720 * @return ParserFactory
722 public function getParserFactory() {
723 return $this->getService( 'ParserFactory' );
728 * @return PasswordFactory
730 public function getPasswordFactory() {
731 return $this->getService( 'PasswordFactory' );
736 * @return StatsdDataFactoryInterface
738 public function getPerDbNameStatsdDataFactory() {
739 return $this->getService( 'PerDbNameStatsdDataFactory' );
744 * @return PermissionManager
746 public function getPermissionManager() {
747 return $this->getService( 'PermissionManager' );
752 * @return PreferencesFactory
754 public function getPreferencesFactory() {
755 return $this->getService( 'PreferencesFactory' );
760 * @return ProxyLookup
762 public function getProxyLookup() {
763 return $this->getService( 'ProxyLookup' );
768 * @return \ReadOnlyMode
770 public function getReadOnlyMode() {
771 return $this->getService( 'ReadOnlyMode' );
776 * @return ResourceLoader
778 public function getResourceLoader() {
779 return $this->getService( 'ResourceLoader' );
784 * @return RevisionFactory
786 public function getRevisionFactory() {
787 return $this->getService( 'RevisionFactory' );
792 * @return RevisionLookup
794 public function getRevisionLookup() {
795 return $this->getService( 'RevisionLookup' );
800 * @return RevisionRenderer
802 public function getRevisionRenderer() {
803 return $this->getService( 'RevisionRenderer' );
808 * @return RevisionStore
810 public function getRevisionStore() {
811 return $this->getService( 'RevisionStore' );
816 * @return RevisionStoreFactory
818 public function getRevisionStoreFactory() {
819 return $this->getService( 'RevisionStoreFactory' );
824 * @return SearchEngine
826 public function newSearchEngine() {
827 // New engine object every time, since they keep state
828 return $this->getService( 'SearchEngineFactory' )->create();
833 * @return SearchEngineConfig
835 public function getSearchEngineConfig() {
836 return $this->getService( 'SearchEngineConfig' );
841 * @return SearchEngineFactory
843 public function getSearchEngineFactory() {
844 return $this->getService( 'SearchEngineFactory' );
849 * @return CommandFactory
851 public function getShellCommandFactory() {
852 return $this->getService( 'ShellCommandFactory' );
859 public function getSiteLookup() {
860 return $this->getService( 'SiteLookup' );
867 public function getSiteStore() {
868 return $this->getService( 'SiteStore' );
873 * @return SkinFactory
875 public function getSkinFactory() {
876 return $this->getService( 'SkinFactory' );
881 * @return SlotRoleRegistry
883 public function getSlotRoleRegistry() {
884 return $this->getService( 'SlotRoleRegistry' );
889 * @return NameTableStore
891 public function getSlotRoleStore() {
892 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
897 * @return SpecialPageFactory
899 public function getSpecialPageFactory() : SpecialPageFactory
{
900 return $this->getService( 'SpecialPageFactory' );
905 * @return IBufferingStatsdDataFactory
907 public function getStatsdDataFactory() {
908 return $this->getService( 'StatsdDataFactory' );
913 * @return TitleFormatter
915 public function getTitleFormatter() {
916 return $this->getService( 'TitleFormatter' );
921 * @return TitleParser
923 public function getTitleParser() {
924 return $this->getService( 'TitleParser' );
929 * @return UploadRevisionImporter
931 public function getUploadRevisionImporter() {
932 return $this->getService( 'UploadRevisionImporter' );
937 * @return VirtualRESTServiceClient
939 public function getVirtualRESTServiceClient() {
940 return $this->getService( 'VirtualRESTServiceClient' );
945 * @return WatchedItemQueryService
947 public function getWatchedItemQueryService() {
948 return $this->getService( 'WatchedItemQueryService' );
953 * @return WatchedItemStoreInterface
955 public function getWatchedItemStore() {
956 return $this->getService( 'WatchedItemStore' );
961 * @return \OldRevisionImporter
963 public function getWikiRevisionOldRevisionImporter() {
964 return $this->getService( 'OldRevisionImporter' );
969 * @return \OldRevisionImporter
971 public function getWikiRevisionOldRevisionImporterNoUpdates() {
972 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
977 * @return \UploadRevisionImporter
979 public function getWikiRevisionUploadImporter() {
980 return $this->getService( 'UploadRevisionImporter' );