Merge "mw.rcfilters.ui.SaveFiltersPopupButtonWidget: Remove pointless option"
[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 As of version 1.27, MediaWiki is only beginning to use dependency injection.
31 * The services defined here do not yet fully represent all services used by core,
32 * much of the code still relies on global state for this accessing services.
33 *
34 * @since 1.27
35 *
36 * @see docs/injection.txt for an overview of using dependency injection in the
37 * MediaWiki code base.
38 */
39
40 use MediaWiki\Interwiki\ClassicInterwikiLookup;
41 use MediaWiki\Linker\LinkRendererFactory;
42 use MediaWiki\Logger\LoggerFactory;
43 use MediaWiki\MediaWikiServices;
44 use MediaWiki\Shell\CommandFactory;
45 use MediaWiki\Storage\BlobStoreFactory;
46 use MediaWiki\Storage\RevisionStore;
47 use MediaWiki\Storage\SqlBlobStore;
48
49 return [
50 'DBLoadBalancerFactory' => function ( MediaWikiServices $services ) {
51 $mainConfig = $services->getMainConfig();
52
53 $lbConf = MWLBFactory::applyDefaultConfig(
54 $mainConfig->get( 'LBFactoryConf' ),
55 $mainConfig,
56 $services->getConfiguredReadOnlyMode()
57 );
58 $class = MWLBFactory::getLBFactoryClass( $lbConf );
59
60 return new $class( $lbConf );
61 },
62
63 'DBLoadBalancer' => function ( MediaWikiServices $services ) {
64 // just return the default LB from the DBLoadBalancerFactory service
65 return $services->getDBLoadBalancerFactory()->getMainLB();
66 },
67
68 'SiteStore' => function ( MediaWikiServices $services ) {
69 $rawSiteStore = new DBSiteStore( $services->getDBLoadBalancer() );
70
71 // TODO: replace wfGetCache with a CacheFactory service.
72 // TODO: replace wfIsHHVM with a capabilities service.
73 $cache = wfGetCache( wfIsHHVM() ? CACHE_ACCEL : CACHE_ANYTHING );
74
75 return new CachingSiteStore( $rawSiteStore, $cache );
76 },
77
78 'SiteLookup' => function ( MediaWikiServices $services ) {
79 $cacheFile = $services->getMainConfig()->get( 'SitesCacheFile' );
80
81 if ( $cacheFile !== false ) {
82 return new FileBasedSiteLookup( $cacheFile );
83 } else {
84 // Use the default SiteStore as the SiteLookup implementation for now
85 return $services->getSiteStore();
86 }
87 },
88
89 'ConfigFactory' => function ( MediaWikiServices $services ) {
90 // Use the bootstrap config to initialize the ConfigFactory.
91 $registry = $services->getBootstrapConfig()->get( 'ConfigRegistry' );
92 $factory = new ConfigFactory();
93
94 foreach ( $registry as $name => $callback ) {
95 $factory->register( $name, $callback );
96 }
97 return $factory;
98 },
99
100 'MainConfig' => function ( MediaWikiServices $services ) {
101 // Use the 'main' config from the ConfigFactory service.
102 return $services->getConfigFactory()->makeConfig( 'main' );
103 },
104
105 'InterwikiLookup' => function ( MediaWikiServices $services ) {
106 global $wgContLang; // TODO: manage $wgContLang as a service
107 $config = $services->getMainConfig();
108 return new ClassicInterwikiLookup(
109 $wgContLang,
110 $services->getMainWANObjectCache(),
111 $config->get( 'InterwikiExpiry' ),
112 $config->get( 'InterwikiCache' ),
113 $config->get( 'InterwikiScopes' ),
114 $config->get( 'InterwikiFallbackSite' )
115 );
116 },
117
118 'StatsdDataFactory' => function ( MediaWikiServices $services ) {
119 return new BufferingStatsdDataFactory(
120 rtrim( $services->getMainConfig()->get( 'StatsdMetricPrefix' ), '.' )
121 );
122 },
123
124 'EventRelayerGroup' => function ( MediaWikiServices $services ) {
125 return new EventRelayerGroup( $services->getMainConfig()->get( 'EventRelayerConfig' ) );
126 },
127
128 'SearchEngineFactory' => function ( MediaWikiServices $services ) {
129 return new SearchEngineFactory( $services->getSearchEngineConfig() );
130 },
131
132 'SearchEngineConfig' => function ( MediaWikiServices $services ) {
133 global $wgContLang;
134 return new SearchEngineConfig( $services->getMainConfig(), $wgContLang );
135 },
136
137 'SkinFactory' => function ( MediaWikiServices $services ) {
138 $factory = new SkinFactory();
139
140 $names = $services->getMainConfig()->get( 'ValidSkinNames' );
141
142 foreach ( $names as $name => $skin ) {
143 $factory->register( $name, $skin, function () use ( $name, $skin ) {
144 $class = "Skin$skin";
145 return new $class( $name );
146 } );
147 }
148 // Register a hidden "fallback" skin
149 $factory->register( 'fallback', 'Fallback', function () {
150 return new SkinFallback;
151 } );
152 // Register a hidden skin for api output
153 $factory->register( 'apioutput', 'ApiOutput', function () {
154 return new SkinApi;
155 } );
156
157 return $factory;
158 },
159
160 'WatchedItemStore' => function ( MediaWikiServices $services ) {
161 $store = new WatchedItemStore(
162 $services->getDBLoadBalancer(),
163 new HashBagOStuff( [ 'maxKeys' => 100 ] ),
164 $services->getReadOnlyMode()
165 );
166 $store->setStatsdDataFactory( $services->getStatsdDataFactory() );
167 return $store;
168 },
169
170 'WatchedItemQueryService' => function ( MediaWikiServices $services ) {
171 return new WatchedItemQueryService( $services->getDBLoadBalancer() );
172 },
173
174 'CryptRand' => function ( MediaWikiServices $services ) {
175 $secretKey = $services->getMainConfig()->get( 'SecretKey' );
176 return new CryptRand(
177 [
178 // To try vary the system information of the state a bit more
179 // by including the system's hostname into the state
180 'wfHostname',
181 // It's mostly worthless but throw the wiki's id into the data
182 // for a little more variance
183 'wfWikiID',
184 // If we have a secret key set then throw it into the state as well
185 function () use ( $secretKey ) {
186 return $secretKey ?: '';
187 }
188 ],
189 // The config file is likely the most often edited file we know should
190 // be around so include its stat info into the state.
191 // The constant with its location will almost always be defined, as
192 // WebStart.php defines MW_CONFIG_FILE to $IP/LocalSettings.php unless
193 // being configured with MW_CONFIG_CALLBACK (e.g. the installer).
194 defined( 'MW_CONFIG_FILE' ) ? [ MW_CONFIG_FILE ] : [],
195 LoggerFactory::getInstance( 'CryptRand' )
196 );
197 },
198
199 'CryptHKDF' => function ( MediaWikiServices $services ) {
200 $config = $services->getMainConfig();
201
202 $secret = $config->get( 'HKDFSecret' ) ?: $config->get( 'SecretKey' );
203 if ( !$secret ) {
204 throw new RuntimeException( "Cannot use MWCryptHKDF without a secret." );
205 }
206
207 // In HKDF, the context can be known to the attacker, but this will
208 // keep simultaneous runs from producing the same output.
209 $context = [ microtime(), getmypid(), gethostname() ];
210
211 // Setup salt cache. Use APC, or fallback to the main cache if it isn't setup
212 $cache = $services->getLocalServerObjectCache();
213 if ( $cache instanceof EmptyBagOStuff ) {
214 $cache = ObjectCache::getLocalClusterInstance();
215 }
216
217 return new CryptHKDF( $secret, $config->get( 'HKDFAlgorithm' ),
218 $cache, $context, $services->getCryptRand()
219 );
220 },
221
222 'MediaHandlerFactory' => function ( MediaWikiServices $services ) {
223 return new MediaHandlerFactory(
224 $services->getMainConfig()->get( 'MediaHandlers' )
225 );
226 },
227
228 'MimeAnalyzer' => function ( MediaWikiServices $services ) {
229 $logger = LoggerFactory::getInstance( 'Mime' );
230 $mainConfig = $services->getMainConfig();
231 $params = [
232 'typeFile' => $mainConfig->get( 'MimeTypeFile' ),
233 'infoFile' => $mainConfig->get( 'MimeInfoFile' ),
234 'xmlTypes' => $mainConfig->get( 'XMLMimeTypes' ),
235 'guessCallback' =>
236 function ( $mimeAnalyzer, &$head, &$tail, $file, &$mime ) use ( $logger ) {
237 // Also test DjVu
238 $deja = new DjVuImage( $file );
239 if ( $deja->isValid() ) {
240 $logger->info( __METHOD__ . ": detected $file as image/vnd.djvu\n" );
241 $mime = 'image/vnd.djvu';
242
243 return;
244 }
245 // Some strings by reference for performance - assuming well-behaved hooks
246 Hooks::run(
247 'MimeMagicGuessFromContent',
248 [ $mimeAnalyzer, &$head, &$tail, $file, &$mime ]
249 );
250 },
251 'extCallback' => function ( $mimeAnalyzer, $ext, &$mime ) {
252 // Media handling extensions can improve the MIME detected
253 Hooks::run( 'MimeMagicImproveFromExtension', [ $mimeAnalyzer, $ext, &$mime ] );
254 },
255 'initCallback' => function ( $mimeAnalyzer ) {
256 // Allow media handling extensions adding MIME-types and MIME-info
257 Hooks::run( 'MimeMagicInit', [ $mimeAnalyzer ] );
258 },
259 'logger' => $logger
260 ];
261
262 if ( $params['infoFile'] === 'includes/mime.info' ) {
263 $params['infoFile'] = __DIR__ . "/libs/mime/mime.info";
264 }
265
266 if ( $params['typeFile'] === 'includes/mime.types' ) {
267 $params['typeFile'] = __DIR__ . "/libs/mime/mime.types";
268 }
269
270 $detectorCmd = $mainConfig->get( 'MimeDetectorCommand' );
271 if ( $detectorCmd ) {
272 $params['detectCallback'] = function ( $file ) use ( $detectorCmd ) {
273 return wfShellExec( "$detectorCmd " . wfEscapeShellArg( $file ) );
274 };
275 }
276
277 // XXX: MimeMagic::singleton currently requires this service to return an instance of MimeMagic
278 return new MimeMagic( $params );
279 },
280
281 'ProxyLookup' => function ( MediaWikiServices $services ) {
282 $mainConfig = $services->getMainConfig();
283 return new ProxyLookup(
284 $mainConfig->get( 'SquidServers' ),
285 $mainConfig->get( 'SquidServersNoPurge' )
286 );
287 },
288
289 'Parser' => function ( MediaWikiServices $services ) {
290 $conf = $services->getMainConfig()->get( 'ParserConf' );
291 return ObjectFactory::constructClassInstance( $conf['class'], [ $conf ] );
292 },
293
294 'ParserCache' => function ( MediaWikiServices $services ) {
295 $config = $services->getMainConfig();
296 $cache = ObjectCache::getInstance( $config->get( 'ParserCacheType' ) );
297 wfDebugLog( 'caches', 'parser: ' . get_class( $cache ) );
298
299 return new ParserCache(
300 $cache,
301 $config->get( 'CacheEpoch' )
302 );
303 },
304
305 'LinkCache' => function ( MediaWikiServices $services ) {
306 return new LinkCache(
307 $services->getTitleFormatter(),
308 $services->getMainWANObjectCache()
309 );
310 },
311
312 'LinkRendererFactory' => function ( MediaWikiServices $services ) {
313 return new LinkRendererFactory(
314 $services->getTitleFormatter(),
315 $services->getLinkCache()
316 );
317 },
318
319 'LinkRenderer' => function ( MediaWikiServices $services ) {
320 global $wgUser;
321
322 if ( defined( 'MW_NO_SESSION' ) ) {
323 return $services->getLinkRendererFactory()->create();
324 } else {
325 return $services->getLinkRendererFactory()->createForUser( $wgUser );
326 }
327 },
328
329 'GenderCache' => function ( MediaWikiServices $services ) {
330 return new GenderCache();
331 },
332
333 '_MediaWikiTitleCodec' => function ( MediaWikiServices $services ) {
334 global $wgContLang;
335
336 return new MediaWikiTitleCodec(
337 $wgContLang,
338 $services->getGenderCache(),
339 $services->getMainConfig()->get( 'LocalInterwikis' )
340 );
341 },
342
343 'TitleFormatter' => function ( MediaWikiServices $services ) {
344 return $services->getService( '_MediaWikiTitleCodec' );
345 },
346
347 'TitleParser' => function ( MediaWikiServices $services ) {
348 return $services->getService( '_MediaWikiTitleCodec' );
349 },
350
351 'MainObjectStash' => function ( MediaWikiServices $services ) {
352 $mainConfig = $services->getMainConfig();
353
354 $id = $mainConfig->get( 'MainStash' );
355 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
356 throw new UnexpectedValueException(
357 "Cache type \"$id\" is not present in \$wgObjectCaches." );
358 }
359
360 return \ObjectCache::newFromParams( $mainConfig->get( 'ObjectCaches' )[$id] );
361 },
362
363 'MainWANObjectCache' => function ( MediaWikiServices $services ) {
364 $mainConfig = $services->getMainConfig();
365
366 $id = $mainConfig->get( 'MainWANCache' );
367 if ( !isset( $mainConfig->get( 'WANObjectCaches' )[$id] ) ) {
368 throw new UnexpectedValueException(
369 "WAN cache type \"$id\" is not present in \$wgWANObjectCaches." );
370 }
371
372 $params = $mainConfig->get( 'WANObjectCaches' )[$id];
373 $objectCacheId = $params['cacheId'];
374 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$objectCacheId] ) ) {
375 throw new UnexpectedValueException(
376 "Cache type \"$objectCacheId\" is not present in \$wgObjectCaches." );
377 }
378 $params['store'] = $mainConfig->get( 'ObjectCaches' )[$objectCacheId];
379
380 return \ObjectCache::newWANCacheFromParams( $params );
381 },
382
383 'LocalServerObjectCache' => function ( MediaWikiServices $services ) {
384 $mainConfig = $services->getMainConfig();
385
386 if ( function_exists( 'apc_fetch' ) ) {
387 $id = 'apc';
388 } elseif ( function_exists( 'apcu_fetch' ) ) {
389 $id = 'apcu';
390 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
391 $id = 'xcache';
392 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
393 $id = 'wincache';
394 } else {
395 $id = CACHE_NONE;
396 }
397
398 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
399 throw new UnexpectedValueException(
400 "Cache type \"$id\" is not present in \$wgObjectCaches." );
401 }
402
403 return \ObjectCache::newFromParams( $mainConfig->get( 'ObjectCaches' )[$id] );
404 },
405
406 'VirtualRESTServiceClient' => function ( MediaWikiServices $services ) {
407 $config = $services->getMainConfig()->get( 'VirtualRestConfig' );
408
409 $vrsClient = new VirtualRESTServiceClient( new MultiHttpClient( [] ) );
410 foreach ( $config['paths'] as $prefix => $serviceConfig ) {
411 $class = $serviceConfig['class'];
412 // Merge in the global defaults
413 $constructArg = isset( $serviceConfig['options'] )
414 ? $serviceConfig['options']
415 : [];
416 $constructArg += $config['global'];
417 // Make the VRS service available at the mount point
418 $vrsClient->mount( $prefix, [ 'class' => $class, 'config' => $constructArg ] );
419 }
420
421 return $vrsClient;
422 },
423
424 'ConfiguredReadOnlyMode' => function ( MediaWikiServices $services ) {
425 return new ConfiguredReadOnlyMode( $services->getMainConfig() );
426 },
427
428 'ReadOnlyMode' => function ( MediaWikiServices $services ) {
429 return new ReadOnlyMode(
430 $services->getConfiguredReadOnlyMode(),
431 $services->getDBLoadBalancer()
432 );
433 },
434
435 'ShellCommandFactory' => function ( MediaWikiServices $services ) {
436 $config = $services->getMainConfig();
437
438 $limits = [
439 'time' => $config->get( 'MaxShellTime' ),
440 'walltime' => $config->get( 'MaxShellWallClockTime' ),
441 'memory' => $config->get( 'MaxShellMemory' ),
442 'filesize' => $config->get( 'MaxShellFileSize' ),
443 ];
444 $cgroup = $config->get( 'ShellCgroup' );
445 $restrictionMethod = $config->get( 'ShellRestrictionMethod' );
446
447 $factory = new CommandFactory( $limits, $cgroup, $restrictionMethod );
448 $factory->setLogger( LoggerFactory::getInstance( 'exec' ) );
449 $factory->logStderr();
450
451 return $factory;
452 },
453
454 'ExternalStoreFactory' => function ( MediaWikiServices $services ) {
455 $config = $services->getMainConfig();
456
457 return new ExternalStoreFactory(
458 $config->get( 'ExternalStores' )
459 );
460 },
461
462 'RevisionStore' => function ( MediaWikiServices $services ) {
463 /** @var SqlBlobStore $blobStore */
464 $blobStore = $services->getService( '_SqlBlobStore' );
465
466 $store = new RevisionStore(
467 $services->getDBLoadBalancer(),
468 $blobStore,
469 $services->getMainWANObjectCache()
470 );
471
472 $config = $services->getMainConfig();
473 $store->setContentHandlerUseDB( $config->get( 'ContentHandlerUseDB' ) );
474
475 return $store;
476 },
477
478 'BlobStoreFactory' => function ( MediaWikiServices $services ) {
479 global $wgContLang;
480 return new BlobStoreFactory(
481 $services->getDBLoadBalancer(),
482 $services->getMainWANObjectCache(),
483 $services->getMainConfig(),
484 $wgContLang
485 );
486 },
487
488 'BlobStore' => function ( MediaWikiServices $services ) {
489 return $services->getService( '_SqlBlobStore' );
490 },
491
492 '_SqlBlobStore' => function ( MediaWikiServices $services ) {
493 return $services->getBlobStoreFactory()->newSqlBlobStore();
494 },
495
496 ///////////////////////////////////////////////////////////////////////////
497 // NOTE: When adding a service here, don't forget to add a getter function
498 // in the MediaWikiServices class. The convenience getter should just call
499 // $this->getService( 'FooBarService' ).
500 ///////////////////////////////////////////////////////////////////////////
501
502 ];