ServiceWiring.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. <?php
  2. /**
  3. * Default wiring for MediaWiki services.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. *
  22. * This file is loaded by MediaWiki\MediaWikiServices::getInstance() during the
  23. * bootstrapping of the dependency injection framework.
  24. *
  25. * This file returns an array that associates service name with instantiator functions
  26. * that create the default instances for the services used by MediaWiki core.
  27. * For every service that MediaWiki core requires, an instantiator must be defined in
  28. * this file.
  29. *
  30. * Note that, ideally, all information used to instantiate service objects should come
  31. * from configuration. Information derived from the current request is acceptable, but
  32. * only where there is no feasible alternative. It is preferred that such information
  33. * (like the client IP, the acting user's identity, requested title, etc) be passed to
  34. * the service object's methods as parameters. This makes the flow of information more
  35. * obvious, and makes it easier to understand the behavior of services.
  36. *
  37. * @note As of version 1.27, MediaWiki is only beginning to use dependency injection.
  38. * The services defined here do not yet fully represent all services used by core,
  39. * much of the code still relies on global state for this accessing services.
  40. *
  41. * @since 1.27
  42. *
  43. * @see docs/injection.txt for an overview of using dependency injection in the
  44. * MediaWiki code base.
  45. */
  46. use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
  47. use MediaWiki\Auth\AuthManager;
  48. use MediaWiki\BadFileLookup;
  49. use MediaWiki\Block\BlockManager;
  50. use MediaWiki\Block\BlockRestrictionStore;
  51. use MediaWiki\Config\ConfigRepository;
  52. use MediaWiki\Config\ServiceOptions;
  53. use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
  54. use MediaWiki\FileBackend\LockManager\LockManagerGroupFactory;
  55. use MediaWiki\Http\HttpRequestFactory;
  56. use MediaWiki\Interwiki\ClassicInterwikiLookup;
  57. use MediaWiki\Interwiki\InterwikiLookup;
  58. use MediaWiki\Languages\LanguageNameUtils;
  59. use MediaWiki\Linker\LinkRenderer;
  60. use MediaWiki\Linker\LinkRendererFactory;
  61. use MediaWiki\Logger\LoggerFactory;
  62. use MediaWiki\MediaWikiServices;
  63. use Wikimedia\Message\IMessageFormatterFactory;
  64. use MediaWiki\Message\MessageFormatterFactory;
  65. use MediaWiki\Page\MovePageFactory;
  66. use MediaWiki\Permissions\PermissionManager;
  67. use MediaWiki\Preferences\PreferencesFactory;
  68. use MediaWiki\Preferences\DefaultPreferencesFactory;
  69. use MediaWiki\Revision\MainSlotRoleHandler;
  70. use MediaWiki\Revision\RevisionFactory;
  71. use MediaWiki\Revision\RevisionLookup;
  72. use MediaWiki\Revision\SlotRoleRegistry;
  73. use MediaWiki\Revision\RevisionRenderer;
  74. use MediaWiki\Revision\RevisionStore;
  75. use MediaWiki\Revision\RevisionStoreFactory;
  76. use MediaWiki\Shell\CommandFactory;
  77. use MediaWiki\Special\SpecialPageFactory;
  78. use MediaWiki\Storage\BlobStore;
  79. use MediaWiki\Storage\BlobStoreFactory;
  80. use MediaWiki\Storage\NameTableStoreFactory;
  81. use MediaWiki\Storage\SqlBlobStore;
  82. use MediaWiki\Storage\PageEditStash;
  83. use Wikimedia\ObjectFactory;
  84. return [
  85. 'ActorMigration' => function ( MediaWikiServices $services ) : ActorMigration {
  86. return new ActorMigration( SCHEMA_COMPAT_NEW );
  87. },
  88. 'BadFileLookup' => function ( MediaWikiServices $services ) : BadFileLookup {
  89. return new BadFileLookup(
  90. function () {
  91. return wfMessage( 'bad_image_list' )->inContentLanguage()->plain();
  92. },
  93. $services->getLocalServerObjectCache(),
  94. $services->getRepoGroup(),
  95. $services->getTitleParser()
  96. );
  97. },
  98. 'BlobStore' => function ( MediaWikiServices $services ) : BlobStore {
  99. return $services->getService( '_SqlBlobStore' );
  100. },
  101. 'BlobStoreFactory' => function ( MediaWikiServices $services ) : BlobStoreFactory {
  102. return new BlobStoreFactory(
  103. $services->getDBLoadBalancerFactory(),
  104. $services->getExternalStoreAccess(),
  105. $services->getMainWANObjectCache(),
  106. new ServiceOptions( BlobStoreFactory::CONSTRUCTOR_OPTIONS,
  107. $services->getMainConfig() )
  108. );
  109. },
  110. 'BlockManager' => function ( MediaWikiServices $services ) : BlockManager {
  111. return new BlockManager(
  112. new ServiceOptions(
  113. BlockManager::CONSTRUCTOR_OPTIONS, $services->getMainConfig()
  114. ),
  115. $services->getPermissionManager(),
  116. LoggerFactory::getInstance( 'BlockManager' )
  117. );
  118. },
  119. 'BlockRestrictionStore' => function ( MediaWikiServices $services ) : BlockRestrictionStore {
  120. return new BlockRestrictionStore(
  121. $services->getDBLoadBalancer()
  122. );
  123. },
  124. 'CommentStore' => function ( MediaWikiServices $services ) : CommentStore {
  125. return new CommentStore(
  126. $services->getContentLanguage(),
  127. MIGRATION_NEW
  128. );
  129. },
  130. 'ConfigFactory' => function ( MediaWikiServices $services ) : ConfigFactory {
  131. // Use the bootstrap config to initialize the ConfigFactory.
  132. $registry = $services->getBootstrapConfig()->get( 'ConfigRegistry' );
  133. $factory = new ConfigFactory();
  134. foreach ( $registry as $name => $callback ) {
  135. $factory->register( $name, $callback );
  136. }
  137. return $factory;
  138. },
  139. 'ConfigRepository' => function ( MediaWikiServices $services ) : ConfigRepository {
  140. return new ConfigRepository( $services->getConfigFactory() );
  141. },
  142. 'ConfiguredReadOnlyMode' => function ( MediaWikiServices $services ) : ConfiguredReadOnlyMode {
  143. $config = $services->getMainConfig();
  144. return new ConfiguredReadOnlyMode(
  145. $config->get( 'ReadOnly' ),
  146. $config->get( 'ReadOnlyFile' )
  147. );
  148. },
  149. 'ContentLanguage' => function ( MediaWikiServices $services ) : Language {
  150. return Language::factory( $services->getMainConfig()->get( 'LanguageCode' ) );
  151. },
  152. 'CryptHKDF' => function ( MediaWikiServices $services ) : CryptHKDF {
  153. $config = $services->getMainConfig();
  154. $secret = $config->get( 'HKDFSecret' ) ?: $config->get( 'SecretKey' );
  155. if ( !$secret ) {
  156. throw new RuntimeException( "Cannot use MWCryptHKDF without a secret." );
  157. }
  158. // In HKDF, the context can be known to the attacker, but this will
  159. // keep simultaneous runs from producing the same output.
  160. $context = [ microtime(), getmypid(), gethostname() ];
  161. // Setup salt cache. Use APC, or fallback to the main cache if it isn't setup
  162. $cache = $services->getLocalServerObjectCache();
  163. if ( $cache instanceof EmptyBagOStuff ) {
  164. $cache = ObjectCache::getLocalClusterInstance();
  165. }
  166. return new CryptHKDF( $secret, $config->get( 'HKDFAlgorithm' ), $cache, $context );
  167. },
  168. 'DateFormatterFactory' => function () : DateFormatterFactory {
  169. return new DateFormatterFactory;
  170. },
  171. 'DBLoadBalancer' => function ( MediaWikiServices $services ) : Wikimedia\Rdbms\ILoadBalancer {
  172. // just return the default LB from the DBLoadBalancerFactory service
  173. return $services->getDBLoadBalancerFactory()->getMainLB();
  174. },
  175. 'DBLoadBalancerFactory' =>
  176. function ( MediaWikiServices $services ) : Wikimedia\Rdbms\LBFactory {
  177. $mainConfig = $services->getMainConfig();
  178. $lbConf = MWLBFactory::applyDefaultConfig(
  179. $mainConfig->get( 'LBFactoryConf' ),
  180. new ServiceOptions( MWLBFactory::APPLY_DEFAULT_CONFIG_OPTIONS, $mainConfig ),
  181. $services->getConfiguredReadOnlyMode(),
  182. $services->getLocalServerObjectCache(),
  183. $services->getMainObjectStash(),
  184. $services->getMainWANObjectCache()
  185. );
  186. $class = MWLBFactory::getLBFactoryClass( $lbConf );
  187. return new $class( $lbConf );
  188. },
  189. 'EventRelayerGroup' => function ( MediaWikiServices $services ) : EventRelayerGroup {
  190. return new EventRelayerGroup( $services->getMainConfig()->get( 'EventRelayerConfig' ) );
  191. },
  192. 'ExternalStoreAccess' => function ( MediaWikiServices $services ) : ExternalStoreAccess {
  193. return new ExternalStoreAccess(
  194. $services->getExternalStoreFactory(),
  195. LoggerFactory::getInstance( 'ExternalStore' )
  196. );
  197. },
  198. 'ExternalStoreFactory' => function ( MediaWikiServices $services ) : ExternalStoreFactory {
  199. $config = $services->getMainConfig();
  200. $writeStores = $config->get( 'DefaultExternalStore' );
  201. return new ExternalStoreFactory(
  202. $config->get( 'ExternalStores' ),
  203. ( $writeStores !== false ) ? (array)$writeStores : [],
  204. $services->getDBLoadBalancer()->getLocalDomainID(),
  205. LoggerFactory::getInstance( 'ExternalStore' )
  206. );
  207. },
  208. 'GenderCache' => function ( MediaWikiServices $services ) : GenderCache {
  209. $nsInfo = $services->getNamespaceInfo();
  210. // Database layer may be disabled, so processing without database connection
  211. $dbLoadBalancer = $services->isServiceDisabled( 'DBLoadBalancer' )
  212. ? null
  213. : $services->getDBLoadBalancer();
  214. return new GenderCache( $nsInfo, $dbLoadBalancer );
  215. },
  216. 'HttpRequestFactory' =>
  217. function ( MediaWikiServices $services ) : HttpRequestFactory {
  218. return new HttpRequestFactory();
  219. },
  220. 'InterwikiLookup' => function ( MediaWikiServices $services ) : InterwikiLookup {
  221. $config = $services->getMainConfig();
  222. return new ClassicInterwikiLookup(
  223. $services->getContentLanguage(),
  224. $services->getMainWANObjectCache(),
  225. $config->get( 'InterwikiExpiry' ),
  226. $config->get( 'InterwikiCache' ),
  227. $config->get( 'InterwikiScopes' ),
  228. $config->get( 'InterwikiFallbackSite' )
  229. );
  230. },
  231. 'LanguageNameUtils' => function ( MediaWikiServices $services ) : LanguageNameUtils {
  232. return new LanguageNameUtils( new ServiceOptions(
  233. LanguageNameUtils::CONSTRUCTOR_OPTIONS,
  234. $services->getMainConfig()
  235. ) );
  236. },
  237. 'LinkCache' => function ( MediaWikiServices $services ) : LinkCache {
  238. return new LinkCache(
  239. $services->getTitleFormatter(),
  240. $services->getMainWANObjectCache(),
  241. $services->getNamespaceInfo()
  242. );
  243. },
  244. 'LinkRenderer' => function ( MediaWikiServices $services ) : LinkRenderer {
  245. if ( defined( 'MW_NO_SESSION' ) ) {
  246. return $services->getLinkRendererFactory()->create();
  247. } else {
  248. // Normally information from the current request would not be passed in here;
  249. // this is an exception. (See also the class documentation.)
  250. return $services->getLinkRendererFactory()->createForUser(
  251. RequestContext::getMain()->getUser()
  252. );
  253. }
  254. },
  255. 'LinkRendererFactory' => function ( MediaWikiServices $services ) : LinkRendererFactory {
  256. return new LinkRendererFactory(
  257. $services->getTitleFormatter(),
  258. $services->getLinkCache(),
  259. $services->getNamespaceInfo()
  260. );
  261. },
  262. 'LocalisationCache' => function ( MediaWikiServices $services ) : LocalisationCache {
  263. $conf = $services->getMainConfig()->get( 'LocalisationCacheConf' );
  264. $logger = LoggerFactory::getInstance( 'localisation' );
  265. $store = LocalisationCache::getStoreFromConf(
  266. $conf, $services->getMainConfig()->get( 'CacheDirectory' ) );
  267. $logger->debug( 'LocalisationCache: using store ' . get_class( $store ) );
  268. return new $conf['class'](
  269. new ServiceOptions(
  270. LocalisationCache::CONSTRUCTOR_OPTIONS,
  271. // Two of the options are stored in $wgLocalisationCacheConf
  272. $conf,
  273. // In case someone set that config variable and didn't reset all keys, set defaults.
  274. [
  275. 'forceRecache' => false,
  276. 'manualRecache' => false,
  277. ],
  278. // Some other options come from config itself
  279. $services->getMainConfig()
  280. ),
  281. $store,
  282. $logger,
  283. [ function () use ( $services ) {
  284. // NOTE: Make sure we use the same cache object that is assigned in the
  285. // constructor of the MessageBlobStore class used by ResourceLoader.
  286. // T231866: Avoid circular dependency via ResourceLoader.
  287. MessageBlobStore::clearGlobalCacheEntry( $services->getMainWANObjectCache() );
  288. } ],
  289. $services->getLanguageNameUtils()
  290. );
  291. },
  292. 'LocalServerObjectCache' => function ( MediaWikiServices $services ) : BagOStuff {
  293. $config = $services->getMainConfig();
  294. $cacheId = ObjectCache::detectLocalServerCache();
  295. return ObjectCache::newFromParams( $config->get( 'ObjectCaches' )[$cacheId] );
  296. },
  297. 'LockManagerGroupFactory' => function ( MediaWikiServices $services ) : LockManagerGroupFactory {
  298. return new LockManagerGroupFactory(
  299. WikiMap::getCurrentWikiDbDomain()->getId(),
  300. $services->getMainConfig()->get( 'LockManagers' ),
  301. $services->getDBLoadBalancerFactory()
  302. );
  303. },
  304. 'MagicWordFactory' => function ( MediaWikiServices $services ) : MagicWordFactory {
  305. return new MagicWordFactory( $services->getContentLanguage() );
  306. },
  307. 'MainConfig' => function ( MediaWikiServices $services ) : Config {
  308. // Use the 'main' config from the ConfigFactory service.
  309. return $services->getConfigFactory()->makeConfig( 'main' );
  310. },
  311. 'MainObjectStash' => function ( MediaWikiServices $services ) : BagOStuff {
  312. $mainConfig = $services->getMainConfig();
  313. $id = $mainConfig->get( 'MainStash' );
  314. if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
  315. throw new UnexpectedValueException(
  316. "Cache type \"$id\" is not present in \$wgObjectCaches." );
  317. }
  318. $params = $mainConfig->get( 'ObjectCaches' )[$id];
  319. $logger = $params['logger'] = LoggerFactory::getInstance( $params['loggroup'] ?? 'objectcache' );
  320. $store = ObjectCache::newFromParams( $params );
  321. $logger->debug( 'MainObjectStash using store {class}', [
  322. 'class' => get_class( $store )
  323. ] );
  324. return $store;
  325. },
  326. 'MainWANObjectCache' => function ( MediaWikiServices $services ) : WANObjectCache {
  327. $mainConfig = $services->getMainConfig();
  328. $id = $mainConfig->get( 'MainWANCache' );
  329. if ( !isset( $mainConfig->get( 'WANObjectCaches' )[$id] ) ) {
  330. throw new UnexpectedValueException(
  331. "WAN cache type \"$id\" is not present in \$wgWANObjectCaches." );
  332. }
  333. $params = $mainConfig->get( 'WANObjectCaches' )[$id];
  334. $logger = LoggerFactory::getInstance( $params['loggroup'] ?? 'objectcache' );
  335. $objectCacheId = $params['cacheId'];
  336. if ( !isset( $mainConfig->get( 'ObjectCaches' )[$objectCacheId] ) ) {
  337. throw new UnexpectedValueException(
  338. "Cache type \"$objectCacheId\" is not present in \$wgObjectCaches." );
  339. }
  340. $storeParams = $mainConfig->get( 'ObjectCaches' )[$objectCacheId];
  341. $store = ObjectCache::newFromParams( $storeParams );
  342. $logger->debug( 'MainWANObjectCache using store {class}', [
  343. 'class' => get_class( $store )
  344. ] );
  345. $params['logger'] = $logger;
  346. $params['cache'] = $store;
  347. $params['secret'] = $params['secret'] ?? $mainConfig->get( 'SecretKey' );
  348. if ( !$mainConfig->get( 'CommandLineMode' ) ) {
  349. // Send the statsd data post-send on HTTP requests; avoid in CLI mode (T181385)
  350. $params['stats'] = $services->getStatsdDataFactory();
  351. // Let pre-emptive refreshes happen post-send on HTTP requests
  352. $params['asyncHandler'] = [ DeferredUpdates::class, 'addCallableUpdate' ];
  353. }
  354. $class = $params['class'];
  355. $instance = new $class( $params );
  356. '@phan-var WANObjectCache $instance';
  357. return $instance;
  358. },
  359. 'MediaHandlerFactory' => function ( MediaWikiServices $services ) : MediaHandlerFactory {
  360. return new MediaHandlerFactory(
  361. $services->getMainConfig()->get( 'MediaHandlers' )
  362. );
  363. },
  364. 'MessageCache' => function ( MediaWikiServices $services ) : MessageCache {
  365. $mainConfig = $services->getMainConfig();
  366. $clusterCache = ObjectCache::getInstance( $mainConfig->get( 'MessageCacheType' ) );
  367. $srvCache = $mainConfig->get( 'UseLocalMessageCache' )
  368. ? $services->getLocalServerObjectCache()
  369. : new EmptyBagOStuff();
  370. // TODO: Inject this into MessageCache.
  371. $logger = LoggerFactory::getInstance( 'MessageCache' );
  372. $logger->debug( 'MessageCache using store {class}', [
  373. 'class' => get_class( $clusterCache )
  374. ] );
  375. return new MessageCache(
  376. $services->getMainWANObjectCache(),
  377. $clusterCache,
  378. $srvCache,
  379. $mainConfig->get( 'UseDatabaseMessages' ),
  380. $services->getContentLanguage()
  381. );
  382. },
  383. 'MessageFormatterFactory' =>
  384. function ( MediaWikiServices $services ) : IMessageFormatterFactory {
  385. // @phan-suppress-next-line PhanAccessMethodInternal
  386. return new MessageFormatterFactory();
  387. },
  388. 'MimeAnalyzer' => function ( MediaWikiServices $services ) : MimeAnalyzer {
  389. $logger = LoggerFactory::getInstance( 'Mime' );
  390. $mainConfig = $services->getMainConfig();
  391. $params = [
  392. 'typeFile' => $mainConfig->get( 'MimeTypeFile' ),
  393. 'infoFile' => $mainConfig->get( 'MimeInfoFile' ),
  394. 'xmlTypes' => $mainConfig->get( 'XMLMimeTypes' ),
  395. 'guessCallback' =>
  396. function ( $mimeAnalyzer, &$head, &$tail, $file, &$mime ) use ( $logger ) {
  397. // Also test DjVu
  398. $deja = new DjVuImage( $file );
  399. if ( $deja->isValid() ) {
  400. $logger->info( "Detected $file as image/vnd.djvu\n" );
  401. $mime = 'image/vnd.djvu';
  402. return;
  403. }
  404. // Some strings by reference for performance - assuming well-behaved hooks
  405. Hooks::run(
  406. 'MimeMagicGuessFromContent',
  407. [ $mimeAnalyzer, &$head, &$tail, $file, &$mime ]
  408. );
  409. },
  410. 'extCallback' => function ( $mimeAnalyzer, $ext, &$mime ) {
  411. // Media handling extensions can improve the MIME detected
  412. Hooks::run( 'MimeMagicImproveFromExtension', [ $mimeAnalyzer, $ext, &$mime ] );
  413. },
  414. 'initCallback' => function ( $mimeAnalyzer ) {
  415. // Allow media handling extensions adding MIME-types and MIME-info
  416. Hooks::run( 'MimeMagicInit', [ $mimeAnalyzer ] );
  417. },
  418. 'logger' => $logger
  419. ];
  420. if ( $params['infoFile'] === 'includes/mime.info' ) {
  421. $params['infoFile'] = __DIR__ . "/libs/mime/mime.info";
  422. }
  423. if ( $params['typeFile'] === 'includes/mime.types' ) {
  424. $params['typeFile'] = __DIR__ . "/libs/mime/mime.types";
  425. }
  426. $detectorCmd = $mainConfig->get( 'MimeDetectorCommand' );
  427. if ( $detectorCmd ) {
  428. $factory = $services->getShellCommandFactory();
  429. $params['detectCallback'] = function ( $file ) use ( $detectorCmd, $factory ) {
  430. $result = $factory->create()
  431. // $wgMimeDetectorCommand can contain commands with parameters
  432. ->unsafeParams( $detectorCmd )
  433. ->params( $file )
  434. ->execute();
  435. return $result->getStdout();
  436. };
  437. }
  438. return new MimeAnalyzer( $params );
  439. },
  440. 'MovePageFactory' => function ( MediaWikiServices $services ) : MovePageFactory {
  441. return new MovePageFactory(
  442. new ServiceOptions( MovePageFactory::$constructorOptions, $services->getMainConfig() ),
  443. $services->getDBLoadBalancer(),
  444. $services->getNamespaceInfo(),
  445. $services->getWatchedItemStore(),
  446. $services->getPermissionManager(),
  447. $services->getRepoGroup()
  448. );
  449. },
  450. 'NamespaceInfo' => function ( MediaWikiServices $services ) : NamespaceInfo {
  451. return new NamespaceInfo( new ServiceOptions( NamespaceInfo::$constructorOptions,
  452. $services->getMainConfig() ) );
  453. },
  454. 'NameTableStoreFactory' => function ( MediaWikiServices $services ) : NameTableStoreFactory {
  455. return new NameTableStoreFactory(
  456. $services->getDBLoadBalancerFactory(),
  457. $services->getMainWANObjectCache(),
  458. LoggerFactory::getInstance( 'NameTableSqlStore' )
  459. );
  460. },
  461. 'ObjectFactory' => function ( MediaWikiServices $services ) : ObjectFactory {
  462. return new ObjectFactory( $services );
  463. },
  464. 'OldRevisionImporter' => function ( MediaWikiServices $services ) : OldRevisionImporter {
  465. return new ImportableOldRevisionImporter(
  466. true,
  467. LoggerFactory::getInstance( 'OldRevisionImporter' ),
  468. $services->getDBLoadBalancer()
  469. );
  470. },
  471. 'PageEditStash' => function ( MediaWikiServices $services ) : PageEditStash {
  472. $config = $services->getMainConfig();
  473. return new PageEditStash(
  474. ObjectCache::getLocalClusterInstance(),
  475. $services->getDBLoadBalancer(),
  476. LoggerFactory::getInstance( 'StashEdit' ),
  477. $services->getStatsdDataFactory(),
  478. defined( 'MEDIAWIKI_JOB_RUNNER' ) || $config->get( 'CommandLineMode' )
  479. ? PageEditStash::INITIATOR_JOB_OR_CLI
  480. : PageEditStash::INITIATOR_USER
  481. );
  482. },
  483. 'Parser' => function ( MediaWikiServices $services ) : Parser {
  484. return $services->getParserFactory()->create();
  485. },
  486. 'ParserCache' => function ( MediaWikiServices $services ) : ParserCache {
  487. $config = $services->getMainConfig();
  488. $cache = ObjectCache::getInstance( $config->get( 'ParserCacheType' ) );
  489. wfDebugLog( 'caches', 'parser: ' . get_class( $cache ) );
  490. return new ParserCache(
  491. $cache,
  492. $config->get( 'CacheEpoch' )
  493. );
  494. },
  495. 'ParserFactory' => function ( MediaWikiServices $services ) : ParserFactory {
  496. $options = new ServiceOptions( Parser::$constructorOptions,
  497. // 'class' and 'preprocessorClass'
  498. $services->getMainConfig()->get( 'ParserConf' ),
  499. // Make sure to have defaults in case someone overrode ParserConf with something silly
  500. [ 'class' => Parser::class, 'preprocessorClass' => Preprocessor_Hash::class ],
  501. // Plus a buch of actual config options
  502. $services->getMainConfig()
  503. );
  504. return new ParserFactory(
  505. $options,
  506. $services->getMagicWordFactory(),
  507. $services->getContentLanguage(),
  508. wfUrlProtocols(),
  509. $services->getSpecialPageFactory(),
  510. $services->getLinkRendererFactory(),
  511. $services->getNamespaceInfo(),
  512. LoggerFactory::getInstance( 'Parser' )
  513. );
  514. },
  515. 'PasswordFactory' => function ( MediaWikiServices $services ) : PasswordFactory {
  516. $config = $services->getMainConfig();
  517. return new PasswordFactory(
  518. $config->get( 'PasswordConfig' ),
  519. $config->get( 'PasswordDefault' )
  520. );
  521. },
  522. 'PasswordReset' => function ( MediaWikiServices $services ) : PasswordReset {
  523. $options = new ServiceOptions( PasswordReset::CONSTRUCTOR_OPTIONS, $services->getMainConfig() );
  524. return new PasswordReset(
  525. $options,
  526. AuthManager::singleton(),
  527. $services->getPermissionManager(),
  528. $services->getDBLoadBalancer(),
  529. LoggerFactory::getInstance( 'authentication' )
  530. );
  531. },
  532. 'PerDbNameStatsdDataFactory' =>
  533. function ( MediaWikiServices $services ) : StatsdDataFactoryInterface {
  534. $config = $services->getMainConfig();
  535. $wiki = $config->get( 'DBname' );
  536. return new PrefixingStatsdDataFactoryProxy(
  537. $services->getStatsdDataFactory(),
  538. $wiki
  539. );
  540. },
  541. 'PermissionManager' => function ( MediaWikiServices $services ) : PermissionManager {
  542. return new PermissionManager(
  543. new ServiceOptions(
  544. PermissionManager::CONSTRUCTOR_OPTIONS, $services->getMainConfig()
  545. ),
  546. $services->getSpecialPageFactory(),
  547. $services->getRevisionLookup(),
  548. $services->getNamespaceInfo()
  549. );
  550. },
  551. 'PreferencesFactory' => function ( MediaWikiServices $services ) : PreferencesFactory {
  552. $factory = new DefaultPreferencesFactory(
  553. new ServiceOptions(
  554. DefaultPreferencesFactory::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ),
  555. $services->getContentLanguage(),
  556. AuthManager::singleton(),
  557. $services->getLinkRendererFactory()->create(),
  558. $services->getNamespaceInfo(),
  559. $services->getPermissionManager()
  560. );
  561. $factory->setLogger( LoggerFactory::getInstance( 'preferences' ) );
  562. return $factory;
  563. },
  564. 'ProxyLookup' => function ( MediaWikiServices $services ) : ProxyLookup {
  565. $mainConfig = $services->getMainConfig();
  566. return new ProxyLookup(
  567. $mainConfig->get( 'CdnServers' ),
  568. $mainConfig->get( 'CdnServersNoPurge' )
  569. );
  570. },
  571. 'ReadOnlyMode' => function ( MediaWikiServices $services ) : ReadOnlyMode {
  572. return new ReadOnlyMode(
  573. $services->getConfiguredReadOnlyMode(),
  574. $services->getDBLoadBalancer()
  575. );
  576. },
  577. 'RepoGroup' => function ( MediaWikiServices $services ) : RepoGroup {
  578. $config = $services->getMainConfig();
  579. return new RepoGroup(
  580. $config->get( 'LocalFileRepo' ),
  581. $config->get( 'ForeignFileRepos' ),
  582. $services->getMainWANObjectCache()
  583. );
  584. },
  585. 'ResourceLoader' => function ( MediaWikiServices $services ) : ResourceLoader {
  586. // @todo This should not take a Config object, but it's not so easy to remove because it
  587. // exposes it in a getter, which is actually used.
  588. global $IP;
  589. $config = $services->getMainConfig();
  590. $rl = new ResourceLoader(
  591. $config,
  592. LoggerFactory::getInstance( 'resourceloader' )
  593. );
  594. $rl->addSource( $config->get( 'ResourceLoaderSources' ) );
  595. // Core modules, then extension/skin modules
  596. $rl->register( include "$IP/resources/Resources.php" );
  597. $rl->register( $config->get( 'ResourceModules' ) );
  598. Hooks::run( 'ResourceLoaderRegisterModules', [ &$rl ] );
  599. if ( $config->get( 'EnableJavaScriptTest' ) === true ) {
  600. $rl->registerTestModules();
  601. }
  602. return $rl;
  603. },
  604. 'RevisionFactory' => function ( MediaWikiServices $services ) : RevisionFactory {
  605. return $services->getRevisionStore();
  606. },
  607. 'RevisionLookup' => function ( MediaWikiServices $services ) : RevisionLookup {
  608. return $services->getRevisionStore();
  609. },
  610. 'RevisionRenderer' => function ( MediaWikiServices $services ) : RevisionRenderer {
  611. $renderer = new RevisionRenderer(
  612. $services->getDBLoadBalancer(),
  613. $services->getSlotRoleRegistry()
  614. );
  615. $renderer->setLogger( LoggerFactory::getInstance( 'SaveParse' ) );
  616. return $renderer;
  617. },
  618. 'RevisionStore' => function ( MediaWikiServices $services ) : RevisionStore {
  619. return $services->getRevisionStoreFactory()->getRevisionStore();
  620. },
  621. 'RevisionStoreFactory' => function ( MediaWikiServices $services ) : RevisionStoreFactory {
  622. $config = $services->getMainConfig();
  623. $store = new RevisionStoreFactory(
  624. $services->getDBLoadBalancerFactory(),
  625. $services->getBlobStoreFactory(),
  626. $services->getNameTableStoreFactory(),
  627. $services->getSlotRoleRegistry(),
  628. $services->getMainWANObjectCache(),
  629. $services->getCommentStore(),
  630. $services->getActorMigration(),
  631. $config->get( 'MultiContentRevisionSchemaMigrationStage' ),
  632. LoggerFactory::getInstance( 'RevisionStore' ),
  633. $config->get( 'ContentHandlerUseDB' )
  634. );
  635. return $store;
  636. },
  637. 'SearchEngineConfig' => function ( MediaWikiServices $services ) : SearchEngineConfig {
  638. // @todo This should not take a Config object, but it's not so easy to remove because it
  639. // exposes it in a getter, which is actually used.
  640. return new SearchEngineConfig( $services->getMainConfig(),
  641. $services->getContentLanguage() );
  642. },
  643. 'SearchEngineFactory' => function ( MediaWikiServices $services ) : SearchEngineFactory {
  644. return new SearchEngineFactory( $services->getSearchEngineConfig() );
  645. },
  646. 'ShellCommandFactory' => function ( MediaWikiServices $services ) : CommandFactory {
  647. $config = $services->getMainConfig();
  648. $limits = [
  649. 'time' => $config->get( 'MaxShellTime' ),
  650. 'walltime' => $config->get( 'MaxShellWallClockTime' ),
  651. 'memory' => $config->get( 'MaxShellMemory' ),
  652. 'filesize' => $config->get( 'MaxShellFileSize' ),
  653. ];
  654. $cgroup = $config->get( 'ShellCgroup' );
  655. $restrictionMethod = $config->get( 'ShellRestrictionMethod' );
  656. $factory = new CommandFactory( $limits, $cgroup, $restrictionMethod );
  657. $factory->setLogger( LoggerFactory::getInstance( 'exec' ) );
  658. $factory->logStderr();
  659. return $factory;
  660. },
  661. 'SiteLookup' => function ( MediaWikiServices $services ) : SiteLookup {
  662. // Use SiteStore as the SiteLookup as well. This was originally separated
  663. // to allow for a cacheable read-only interface (using FileBasedSiteLookup),
  664. // but this was never used. SiteStore has caching (see below).
  665. return $services->getSiteStore();
  666. },
  667. 'SiteStore' => function ( MediaWikiServices $services ) : SiteStore {
  668. $rawSiteStore = new DBSiteStore( $services->getDBLoadBalancer() );
  669. $cache = $services->getLocalServerObjectCache();
  670. if ( $cache instanceof EmptyBagOStuff ) {
  671. $cache = ObjectCache::getLocalClusterInstance();
  672. }
  673. return new CachingSiteStore( $rawSiteStore, $cache );
  674. },
  675. 'SkinFactory' => function ( MediaWikiServices $services ) : SkinFactory {
  676. $factory = new SkinFactory();
  677. $names = $services->getMainConfig()->get( 'ValidSkinNames' );
  678. foreach ( $names as $name => $skin ) {
  679. $factory->register( $name, $skin, function () use ( $name, $skin ) {
  680. $class = "Skin$skin";
  681. return new $class( $name );
  682. } );
  683. }
  684. // Register a hidden "fallback" skin
  685. $factory->register( 'fallback', 'Fallback', function () {
  686. return new SkinFallback;
  687. } );
  688. // Register a hidden skin for api output
  689. $factory->register( 'apioutput', 'ApiOutput', function () {
  690. return new SkinApi;
  691. } );
  692. return $factory;
  693. },
  694. 'SlotRoleRegistry' => function ( MediaWikiServices $services ) : SlotRoleRegistry {
  695. $config = $services->getMainConfig();
  696. $registry = new SlotRoleRegistry(
  697. $services->getNameTableStoreFactory()->getSlotRoles()
  698. );
  699. $registry->defineRole( 'main', function () use ( $config ) {
  700. return new MainSlotRoleHandler(
  701. $config->get( 'NamespaceContentModels' )
  702. );
  703. } );
  704. return $registry;
  705. },
  706. 'SpecialPageFactory' => function ( MediaWikiServices $services ) : SpecialPageFactory {
  707. return new SpecialPageFactory(
  708. new ServiceOptions(
  709. SpecialPageFactory::$constructorOptions, $services->getMainConfig() ),
  710. $services->getContentLanguage(),
  711. $services->getObjectFactory()
  712. );
  713. },
  714. 'StatsdDataFactory' => function ( MediaWikiServices $services ) : IBufferingStatsdDataFactory {
  715. return new BufferingStatsdDataFactory(
  716. rtrim( $services->getMainConfig()->get( 'StatsdMetricPrefix' ), '.' )
  717. );
  718. },
  719. 'TempFSFileFactory' => function ( MediaWikiServices $services ) : TempFSFileFactory {
  720. return new TempFSFileFactory( $services->getMainConfig()->get( 'TmpDirectory' ) );
  721. },
  722. 'TitleFormatter' => function ( MediaWikiServices $services ) : TitleFormatter {
  723. return $services->getService( '_MediaWikiTitleCodec' );
  724. },
  725. 'TitleParser' => function ( MediaWikiServices $services ) : TitleParser {
  726. return $services->getService( '_MediaWikiTitleCodec' );
  727. },
  728. 'UploadRevisionImporter' => function ( MediaWikiServices $services ) : UploadRevisionImporter {
  729. return new ImportableUploadRevisionImporter(
  730. $services->getMainConfig()->get( 'EnableUploads' ),
  731. LoggerFactory::getInstance( 'UploadRevisionImporter' )
  732. );
  733. },
  734. 'VirtualRESTServiceClient' =>
  735. function ( MediaWikiServices $services ) : VirtualRESTServiceClient {
  736. $config = $services->getMainConfig()->get( 'VirtualRestConfig' );
  737. $vrsClient = new VirtualRESTServiceClient( new MultiHttpClient( [] ) );
  738. foreach ( $config['paths'] as $prefix => $serviceConfig ) {
  739. $class = $serviceConfig['class'];
  740. // Merge in the global defaults
  741. $constructArg = $serviceConfig['options'] ?? [];
  742. $constructArg += $config['global'];
  743. // Make the VRS service available at the mount point
  744. $vrsClient->mount( $prefix, [ 'class' => $class, 'config' => $constructArg ] );
  745. }
  746. return $vrsClient;
  747. },
  748. 'WatchedItemQueryService' =>
  749. function ( MediaWikiServices $services ) : WatchedItemQueryService {
  750. return new WatchedItemQueryService(
  751. $services->getDBLoadBalancer(),
  752. $services->getCommentStore(),
  753. $services->getActorMigration(),
  754. $services->getWatchedItemStore(),
  755. $services->getPermissionManager()
  756. );
  757. },
  758. 'WatchedItemStore' => function ( MediaWikiServices $services ) : WatchedItemStore {
  759. $store = new WatchedItemStore(
  760. $services->getDBLoadBalancerFactory(),
  761. JobQueueGroup::singleton(),
  762. $services->getMainObjectStash(),
  763. new HashBagOStuff( [ 'maxKeys' => 100 ] ),
  764. $services->getReadOnlyMode(),
  765. $services->getMainConfig()->get( 'UpdateRowsPerQuery' ),
  766. $services->getNamespaceInfo(),
  767. $services->getRevisionLookup()
  768. );
  769. $store->setStatsdDataFactory( $services->getStatsdDataFactory() );
  770. if ( $services->getMainConfig()->get( 'ReadOnlyWatchedItemStore' ) ) {
  771. $store = new NoWriteWatchedItemStore( $store );
  772. }
  773. return $store;
  774. },
  775. 'WikiRevisionOldRevisionImporterNoUpdates' =>
  776. function ( MediaWikiServices $services ) : ImportableOldRevisionImporter {
  777. return new ImportableOldRevisionImporter(
  778. false,
  779. LoggerFactory::getInstance( 'OldRevisionImporter' ),
  780. $services->getDBLoadBalancer()
  781. );
  782. },
  783. '_MediaWikiTitleCodec' => function ( MediaWikiServices $services ) : MediaWikiTitleCodec {
  784. return new MediaWikiTitleCodec(
  785. $services->getContentLanguage(),
  786. $services->getGenderCache(),
  787. $services->getMainConfig()->get( 'LocalInterwikis' ),
  788. $services->getInterwikiLookup(),
  789. $services->getNamespaceInfo()
  790. );
  791. },
  792. '_SqlBlobStore' => function ( MediaWikiServices $services ) : SqlBlobStore {
  793. // @phan-suppress-next-line PhanAccessMethodInternal
  794. return $services->getBlobStoreFactory()->newSqlBlobStore();
  795. },
  796. ///////////////////////////////////////////////////////////////////////////
  797. // NOTE: When adding a service here, don't forget to add a getter function
  798. // in the MediaWikiServices class. The convenience getter should just call
  799. // $this->getService( 'FooBarService' ).
  800. ///////////////////////////////////////////////////////////////////////////
  801. ];