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