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();
228 /** @noinspection PhpDocSignatureInspection */
230 * Salvages the state of any salvageable service instances in $other.
232 * @note $other will have been destroyed when salvage() returns.
234 * @param MediaWikiServices $other
236 private function salvage( self
$other ) {
237 foreach ( $this->getServiceNames() as $name ) {
238 // The service could be new in the new instance and not registered in the
239 // other instance (e.g. an extension that was loaded after the instantiation of
240 // the other instance. Skip this service in this case. See T143974
242 $oldService = $other->peekService( $name );
243 } catch ( NoSuchServiceException
$e ) {
247 if ( $oldService instanceof SalvageableService
) {
248 /** @var SalvageableService $newService */
249 $newService = $this->getService( $name );
250 $newService->salvage( $oldService );
258 * Creates a new MediaWikiServices instance and initializes it according to the
259 * given $bootstrapConfig. In particular, all wiring files defined in the
260 * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called.
262 * @param Config|null $bootstrapConfig The Config object to be registered as the
263 * 'BootstrapConfig' service.
265 * @param string $loadWiring set this to 'load' to load the wiring files specified
266 * in the 'ServiceWiringFiles' setting in $bootstrapConfig.
268 * @return MediaWikiServices
269 * @throws MWException
270 * @throws \FatalError
272 private static function newInstance( Config
$bootstrapConfig, $loadWiring = '' ) {
273 $instance = new self( $bootstrapConfig );
275 // Load the default wiring from the specified files.
276 if ( $loadWiring === 'load' ) {
277 $wiringFiles = $bootstrapConfig->get( 'ServiceWiringFiles' );
278 $instance->loadWiringFiles( $wiringFiles );
281 // Provide a traditional hook point to allow extensions to configure services.
282 Hooks
::run( 'MediaWikiServices', [ $instance ] );
288 * Disables all storage layer services. After calling this, any attempt to access the
289 * storage layer will result in an error. Use resetGlobalInstance() to restore normal
294 * @warning This is intended for extreme situations only and should never be used
295 * while serving normal web requests. Legitimate use cases for this method include
296 * the installation process. Test fixtures may also use this, if the fixture relies
299 * @see resetGlobalInstance()
300 * @see resetChildProcessServices()
302 public static function disableStorageBackend() {
303 // TODO: also disable some Caches, JobQueues, etc
304 $destroy = [ 'DBLoadBalancer', 'DBLoadBalancerFactory' ];
305 $services = self
::getInstance();
307 foreach ( $destroy as $name ) {
308 $services->disableService( $name );
311 ObjectCache
::clear();
315 * Resets any services that may have become stale after a child process
316 * returns from after pcntl_fork(). It's also safe, but generally unnecessary,
317 * to call this method from the parent process.
321 * @note This is intended for use in the context of process forking only!
323 * @see resetGlobalInstance()
324 * @see disableStorageBackend()
326 public static function resetChildProcessServices() {
327 // NOTE: for now, just reset everything. Since we don't know the interdependencies
328 // between services, we can't do this more selectively at this time.
329 self
::resetGlobalInstance();
331 // Child, reseed because there is no bug in PHP:
332 // https://bugs.php.net/bug.php?id=42465
333 mt_srand( getmypid() );
337 * Resets the given service for testing purposes.
341 * @warning This is generally unsafe! Other services may still retain references
342 * to the stale service instance, leading to failures and inconsistencies. Subclasses
343 * may use this method to reset specific services under specific instances, but
344 * it should not be exposed to application logic.
346 * @note With proper dependency injection used throughout the codebase, this method
347 * should not be needed. It is provided to allow tests that pollute global service
348 * instances to clean up.
350 * @param string $name
351 * @param bool $destroy Whether the service instance should be destroyed if it exists.
352 * When set to false, any existing service instance will effectively be detached
353 * from the container.
355 * @throws MWException if called outside of PHPUnit tests.
357 public function resetServiceForTesting( $name, $destroy = true ) {
358 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
359 throw new MWException( 'resetServiceForTesting() must not be used outside unit tests.' );
362 $this->resetService( $name, $destroy );
366 * Convenience method that throws an exception unless it is called during a phase in which
367 * resetting of global services is allowed. In general, services should not be reset
368 * individually, since that may introduce inconsistencies.
372 * This method will throw an exception if:
374 * - self::$resetInProgress is false (to allow all services to be reset together
375 * via resetGlobalInstance)
376 * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation)
377 * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing)
379 * This method is intended to be used to safeguard against accidentally resetting
380 * global service instances that are not yet managed by MediaWikiServices. It is
381 * defined here in the MediaWikiServices services class to have a central place
382 * for managing service bootstrapping and resetting.
384 * @param string $method the name of the caller method, as given by __METHOD__.
386 * @throws MWException if called outside bootstrap mode.
388 * @see resetGlobalInstance()
389 * @see forceGlobalInstance()
390 * @see disableStorageBackend()
392 public static function failIfResetNotAllowed( $method ) {
393 if ( !defined( 'MW_PHPUNIT_TEST' )
394 && !defined( 'MW_PARSER_TEST' )
395 && !defined( 'MEDIAWIKI_INSTALL' )
396 && !defined( 'RUN_MAINTENANCE_IF_MAIN' )
397 && defined( 'MW_SERVICE_BOOTSTRAP_COMPLETE' )
399 throw new MWException( $method . ' may only be called during bootstrapping and unit tests!' );
404 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
405 * This has to contain at least the information needed to set up the 'ConfigFactory'
408 public function __construct( Config
$config ) {
409 parent
::__construct();
411 // Register the given Config object as the bootstrap config service.
412 $this->defineService( 'BootstrapConfig', function () use ( $config ) {
417 // CONVENIENCE GETTERS ////////////////////////////////////////////////////
421 * @return ActorMigration
423 public function getActorMigration() {
424 return $this->getService( 'ActorMigration' );
431 public function getBlobStore() {
432 return $this->getService( '_SqlBlobStore' );
437 * @return BlobStoreFactory
439 public function getBlobStoreFactory() {
440 return $this->getService( 'BlobStoreFactory' );
445 * @return BlockManager
447 public function getBlockManager() : BlockManager
{
448 return $this->getService( 'BlockManager' );
453 * @return BlockRestrictionStore
455 public function getBlockRestrictionStore() : BlockRestrictionStore
{
456 return $this->getService( 'BlockRestrictionStore' );
460 * Returns the Config object containing the bootstrap configuration.
461 * Bootstrap configuration would typically include database credentials
462 * and other information that may be needed before the ConfigFactory
463 * service can be instantiated.
465 * @note This should only be used during bootstrapping, in particular
466 * when creating the MainConfig service. Application logic should
467 * use getMainConfig() to get a Config instances.
472 public function getBootstrapConfig() {
473 return $this->getService( 'BootstrapConfig' );
478 * @return NameTableStore
480 public function getChangeTagDefStore() {
481 return $this->getService( 'NameTableStoreFactory' )->getChangeTagDef();
486 * @return CommentStore
488 public function getCommentStore() {
489 return $this->getService( 'CommentStore' );
494 * @return ConfigFactory
496 public function getConfigFactory() {
497 return $this->getService( 'ConfigFactory' );
502 * @return ConfigRepository
504 public function getConfigRepository() {
505 return $this->getService( 'ConfigRepository' );
510 * @return \ConfiguredReadOnlyMode
512 public function getConfiguredReadOnlyMode() {
513 return $this->getService( 'ConfiguredReadOnlyMode' );
520 public function getContentLanguage() {
521 return $this->getService( 'ContentLanguage' );
526 * @return NameTableStore
528 public function getContentModelStore() {
529 return $this->getService( 'NameTableStoreFactory' )->getContentModels();
536 public function getCryptHKDF() {
537 return $this->getService( 'CryptHKDF' );
542 * @return DateFormatterFactory
544 public function getDateFormatterFactory() {
545 return $this->getService( 'DateFormatterFactory' );
550 * @return LoadBalancer The main DB load balancer for the local wiki.
552 public function getDBLoadBalancer() {
553 return $this->getService( 'DBLoadBalancer' );
560 public function getDBLoadBalancerFactory() {
561 return $this->getService( 'DBLoadBalancerFactory' );
566 * @return EventRelayerGroup
568 public function getEventRelayerGroup() {
569 return $this->getService( 'EventRelayerGroup' );
574 * @return \ExternalStoreFactory
576 public function getExternalStoreFactory() {
577 return $this->getService( 'ExternalStoreFactory' );
582 * @return GenderCache
584 public function getGenderCache() {
585 return $this->getService( 'GenderCache' );
590 * @return HttpRequestFactory
592 public function getHttpRequestFactory() {
593 return $this->getService( 'HttpRequestFactory' );
598 * @return InterwikiLookup
600 public function getInterwikiLookup() {
601 return $this->getService( 'InterwikiLookup' );
608 public function getLinkCache() {
609 return $this->getService( 'LinkCache' );
613 * LinkRenderer instance that can be used
614 * if no custom options are needed
617 * @return LinkRenderer
619 public function getLinkRenderer() {
620 return $this->getService( 'LinkRenderer' );
625 * @return LinkRendererFactory
627 public function getLinkRendererFactory() {
628 return $this->getService( 'LinkRendererFactory' );
635 public function getLocalServerObjectCache() {
636 return $this->getService( 'LocalServerObjectCache' );
641 * @return MagicWordFactory
643 public function getMagicWordFactory() {
644 return $this->getService( 'MagicWordFactory' );
648 * Returns the Config object that provides configuration for MediaWiki core.
649 * This may or may not be the same object that is returned by getBootstrapConfig().
654 public function getMainConfig() {
655 return $this->getService( 'MainConfig' );
662 public function getMainObjectStash() {
663 return $this->getService( 'MainObjectStash' );
668 * @return \WANObjectCache
670 public function getMainWANObjectCache() {
671 return $this->getService( 'MainWANObjectCache' );
676 * @return MediaHandlerFactory
678 public function getMediaHandlerFactory() {
679 return $this->getService( 'MediaHandlerFactory' );
684 * @return MimeAnalyzer
686 public function getMimeAnalyzer() {
687 return $this->getService( 'MimeAnalyzer' );
692 * @return NamespaceInfo
694 public function getNamespaceInfo() : NamespaceInfo
{
695 return $this->getService( 'NamespaceInfo' );
700 * @return NameTableStoreFactory
702 public function getNameTableStoreFactory() {
703 return $this->getService( 'NameTableStoreFactory' );
708 * @return OldRevisionImporter
710 public function getOldRevisionImporter() {
711 return $this->getService( 'OldRevisionImporter' );
715 * @return PageEditStash
718 public function getPageEditStash() {
719 return $this->getService( 'PageEditStash' );
726 public function getParser() {
727 return $this->getService( 'Parser' );
732 * @return ParserCache
734 public function getParserCache() {
735 return $this->getService( 'ParserCache' );
740 * @return ParserFactory
742 public function getParserFactory() {
743 return $this->getService( 'ParserFactory' );
748 * @return PasswordFactory
750 public function getPasswordFactory() {
751 return $this->getService( 'PasswordFactory' );
756 * @return StatsdDataFactoryInterface
758 public function getPerDbNameStatsdDataFactory() {
759 return $this->getService( 'PerDbNameStatsdDataFactory' );
764 * @return PermissionManager
766 public function getPermissionManager() {
767 return $this->getService( 'PermissionManager' );
772 * @return PreferencesFactory
774 public function getPreferencesFactory() {
775 return $this->getService( 'PreferencesFactory' );
780 * @return ProxyLookup
782 public function getProxyLookup() {
783 return $this->getService( 'ProxyLookup' );
788 * @return \ReadOnlyMode
790 public function getReadOnlyMode() {
791 return $this->getService( 'ReadOnlyMode' );
798 public function getRepoGroup() : RepoGroup
{
799 return $this->getService( 'RepoGroup' );
804 * @return ResourceLoader
806 public function getResourceLoader() {
807 return $this->getService( 'ResourceLoader' );
812 * @return RevisionFactory
814 public function getRevisionFactory() {
815 return $this->getService( 'RevisionFactory' );
820 * @return RevisionLookup
822 public function getRevisionLookup() {
823 return $this->getService( 'RevisionLookup' );
828 * @return RevisionRenderer
830 public function getRevisionRenderer() {
831 return $this->getService( 'RevisionRenderer' );
836 * @return RevisionStore
838 public function getRevisionStore() {
839 return $this->getService( 'RevisionStore' );
844 * @return RevisionStoreFactory
846 public function getRevisionStoreFactory() {
847 return $this->getService( 'RevisionStoreFactory' );
852 * @return SearchEngine
854 public function newSearchEngine() {
855 // New engine object every time, since they keep state
856 return $this->getService( 'SearchEngineFactory' )->create();
861 * @return SearchEngineConfig
863 public function getSearchEngineConfig() {
864 return $this->getService( 'SearchEngineConfig' );
869 * @return SearchEngineFactory
871 public function getSearchEngineFactory() {
872 return $this->getService( 'SearchEngineFactory' );
877 * @return CommandFactory
879 public function getShellCommandFactory() {
880 return $this->getService( 'ShellCommandFactory' );
887 public function getSiteLookup() {
888 return $this->getService( 'SiteLookup' );
895 public function getSiteStore() {
896 return $this->getService( 'SiteStore' );
901 * @return SkinFactory
903 public function getSkinFactory() {
904 return $this->getService( 'SkinFactory' );
909 * @return SlotRoleRegistry
911 public function getSlotRoleRegistry() {
912 return $this->getService( 'SlotRoleRegistry' );
917 * @return NameTableStore
919 public function getSlotRoleStore() {
920 return $this->getService( 'NameTableStoreFactory' )->getSlotRoles();
925 * @return SpecialPageFactory
927 public function getSpecialPageFactory() : SpecialPageFactory
{
928 return $this->getService( 'SpecialPageFactory' );
933 * @return IBufferingStatsdDataFactory
935 public function getStatsdDataFactory() {
936 return $this->getService( 'StatsdDataFactory' );
941 * @return TitleFormatter
943 public function getTitleFormatter() {
944 return $this->getService( 'TitleFormatter' );
949 * @return TitleParser
951 public function getTitleParser() {
952 return $this->getService( 'TitleParser' );
957 * @return UploadRevisionImporter
959 public function getUploadRevisionImporter() {
960 return $this->getService( 'UploadRevisionImporter' );
965 * @return VirtualRESTServiceClient
967 public function getVirtualRESTServiceClient() {
968 return $this->getService( 'VirtualRESTServiceClient' );
973 * @return WatchedItemQueryService
975 public function getWatchedItemQueryService() {
976 return $this->getService( 'WatchedItemQueryService' );
981 * @return WatchedItemStoreInterface
983 public function getWatchedItemStore() {
984 return $this->getService( 'WatchedItemStore' );
989 * @return \OldRevisionImporter
991 public function getWikiRevisionOldRevisionImporter() {
992 return $this->getService( 'OldRevisionImporter' );
997 * @return \OldRevisionImporter
999 public function getWikiRevisionOldRevisionImporterNoUpdates() {
1000 return $this->getService( 'WikiRevisionOldRevisionImporterNoUpdates' );
1005 * @return \UploadRevisionImporter
1007 public function getWikiRevisionUploadImporter() {
1008 return $this->getService( 'UploadRevisionImporter' );