MediaWiki uses a service container and dependency injection (DI) pattern to manage shared objects. Rather than instantiating dependencies directly or relying on global singletons scattered through the codebase, services are registered centrally and injected where needed.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/wikimedia/mediawiki/llms.txt
Use this file to discover all available pages before exploring further.
What the Service Container Is
The heart of DI in MediaWiki isMediaWikiServices, which acts as the top-level factory (or registry) for services. It represents the tree of service objects that define MediaWiki’s application logic and serves as the entry point to all dependency injection for core.
When MediaWikiServices::getInstance() is first called, it creates an instance and populates it with services defined in includes/ServiceWiring.php, as well as any additional wiring files listed in $wgServiceWiringFiles.
MediaWikiServices extends Wikimedia\Services\ServiceContainer. It has been available since MediaWiki 1.27.Accessing Services
The primary API is typed getter methods onMediaWikiServices:
Common Services
RevisionStore
Loading and storing page revisions. Use
getRevisionStore().PageStore
Loading
PageRecord objects by ID, name, or title. Use getPageStore().DBLoadBalancerFactory
Database connection management across wikis. Use
getDBLoadBalancerFactory().HookContainer
Registering and dispatching hook calls. Use
getHookContainer().UserFactory
Creating
User objects from user identities. Use getUserFactory().TitleFormatter
Formatting
TitleValue objects into display strings. Use getTitleFormatter().PermissionManager
Checking user permissions against pages. Use
getPermissionManager().ParserFactory
Creating
Parser instances for wikitext parsing. Use getParserFactory().Injecting Services into Classes (Preferred Pattern)
Constructor injection is strongly preferred over callinggetInstance() inside business logic:
ServiceWiring.php would look like:
Using ServiceOptions for Configuration
When a service requires multiple configuration values, useServiceOptions with a CONSTRUCTOR_OPTIONS constant:
ServiceWiring.php:
ServiceWiring.php
includes/ServiceWiring.php contains the default instantiator (factory) functions for all core services. It returns a PHP array where each key is a service name and each value is a static closure that receives the MediaWikiServices container and returns a new service instance.
Services must not vary their behavior on global state such as the current web request,
RequestContext, or the current user. This ensures they are safe to use across web requests, APIs, jobs, and maintenance scripts.Creating Services in Extensions
Extensions register additional service wiring files viaServiceWiringFiles in extension.json:
ServiceWiring.php follows the same format as core’s:
MediaWikiServices hook:
Service Reset
Services receive configuration at instantiation time. Changes to global configuration variables after a service is created have no effect on it. To address this,Setup.php calls MediaWikiServices::resetGlobalInstance() once configuration and extension registration is complete.
Services that manage their own singletons (unmanaged legacy services) must not hold references to services managed by MediaWikiServices, because after a reset those references become stale.