Merge "Don't fallback from uk to ru"
[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
45 return [
46 'DBLoadBalancerFactory' => function( MediaWikiServices $services ) {
47 $mainConfig = $services->getMainConfig();
48
49 $lbConf = MWLBFactory::applyDefaultConfig(
50 $mainConfig->get( 'LBFactoryConf' ),
51 $mainConfig
52 );
53 $class = MWLBFactory::getLBFactoryClass( $lbConf );
54
55 return new $class( $lbConf );
56 },
57
58 'DBLoadBalancer' => function( MediaWikiServices $services ) {
59 // just return the default LB from the DBLoadBalancerFactory service
60 return $services->getDBLoadBalancerFactory()->getMainLB();
61 },
62
63 'SiteStore' => function( MediaWikiServices $services ) {
64 $rawSiteStore = new DBSiteStore( $services->getDBLoadBalancer() );
65
66 // TODO: replace wfGetCache with a CacheFactory service.
67 // TODO: replace wfIsHHVM with a capabilities service.
68 $cache = wfGetCache( wfIsHHVM() ? CACHE_ACCEL : CACHE_ANYTHING );
69
70 return new CachingSiteStore( $rawSiteStore, $cache );
71 },
72
73 'SiteLookup' => function( MediaWikiServices $services ) {
74 // Use the default SiteStore as the SiteLookup implementation for now
75 return $services->getSiteStore();
76 },
77
78 'ConfigFactory' => function( MediaWikiServices $services ) {
79 // Use the bootstrap config to initialize the ConfigFactory.
80 $registry = $services->getBootstrapConfig()->get( 'ConfigRegistry' );
81 $factory = new ConfigFactory();
82
83 foreach ( $registry as $name => $callback ) {
84 $factory->register( $name, $callback );
85 }
86 return $factory;
87 },
88
89 'MainConfig' => function( MediaWikiServices $services ) {
90 // Use the 'main' config from the ConfigFactory service.
91 return $services->getConfigFactory()->makeConfig( 'main' );
92 },
93
94 'InterwikiLookup' => function( MediaWikiServices $services ) {
95 global $wgContLang; // TODO: manage $wgContLang as a service
96 $config = $services->getMainConfig();
97 return new ClassicInterwikiLookup(
98 $wgContLang,
99 $services->getMainWANObjectCache(),
100 $config->get( 'InterwikiExpiry' ),
101 $config->get( 'InterwikiCache' ),
102 $config->get( 'InterwikiScopes' ),
103 $config->get( 'InterwikiFallbackSite' )
104 );
105 },
106
107 'StatsdDataFactory' => function( MediaWikiServices $services ) {
108 return new BufferingStatsdDataFactory(
109 rtrim( $services->getMainConfig()->get( 'StatsdMetricPrefix' ), '.' )
110 );
111 },
112
113 'EventRelayerGroup' => function( MediaWikiServices $services ) {
114 return new EventRelayerGroup( $services->getMainConfig()->get( 'EventRelayerConfig' ) );
115 },
116
117 'SearchEngineFactory' => function( MediaWikiServices $services ) {
118 return new SearchEngineFactory( $services->getSearchEngineConfig() );
119 },
120
121 'SearchEngineConfig' => function( MediaWikiServices $services ) {
122 global $wgContLang;
123 return new SearchEngineConfig( $services->getMainConfig(), $wgContLang );
124 },
125
126 'SkinFactory' => function( MediaWikiServices $services ) {
127 $factory = new SkinFactory();
128
129 $names = $services->getMainConfig()->get( 'ValidSkinNames' );
130
131 foreach ( $names as $name => $skin ) {
132 $factory->register( $name, $skin, function () use ( $name, $skin ) {
133 $class = "Skin$skin";
134 return new $class( $name );
135 } );
136 }
137 // Register a hidden "fallback" skin
138 $factory->register( 'fallback', 'Fallback', function () {
139 return new SkinFallback;
140 } );
141 // Register a hidden skin for api output
142 $factory->register( 'apioutput', 'ApiOutput', function () {
143 return new SkinApi;
144 } );
145
146 return $factory;
147 },
148
149 'WatchedItemStore' => function( MediaWikiServices $services ) {
150 $store = new WatchedItemStore(
151 $services->getDBLoadBalancer(),
152 new HashBagOStuff( [ 'maxKeys' => 100 ] )
153 );
154 $store->setStatsdDataFactory( $services->getStatsdDataFactory() );
155 return $store;
156 },
157
158 'WatchedItemQueryService' => function( MediaWikiServices $services ) {
159 return new WatchedItemQueryService( $services->getDBLoadBalancer() );
160 },
161
162 'CryptRand' => function( MediaWikiServices $services ) {
163 $secretKey = $services->getMainConfig()->get( 'SecretKey' );
164 return new CryptRand(
165 [
166 // To try vary the system information of the state a bit more
167 // by including the system's hostname into the state
168 'wfHostname',
169 // It's mostly worthless but throw the wiki's id into the data
170 // for a little more variance
171 'wfWikiID',
172 // If we have a secret key set then throw it into the state as well
173 function() use ( $secretKey ) {
174 return $secretKey ?: '';
175 }
176 ],
177 // The config file is likely the most often edited file we know should
178 // be around so include its stat info into the state.
179 // The constant with its location will almost always be defined, as
180 // WebStart.php defines MW_CONFIG_FILE to $IP/LocalSettings.php unless
181 // being configured with MW_CONFIG_CALLBACK (e.g. the installer).
182 defined( 'MW_CONFIG_FILE' ) ? [ MW_CONFIG_FILE ] : [],
183 LoggerFactory::getInstance( 'CryptRand' )
184 );
185 },
186
187 'CryptHKDF' => function( MediaWikiServices $services ) {
188 $config = $services->getMainConfig();
189
190 $secret = $config->get( 'HKDFSecret' ) ?: $config->get( 'SecretKey' );
191 if ( !$secret ) {
192 throw new RuntimeException( "Cannot use MWCryptHKDF without a secret." );
193 }
194
195 // In HKDF, the context can be known to the attacker, but this will
196 // keep simultaneous runs from producing the same output.
197 $context = [ microtime(), getmypid(), gethostname() ];
198
199 // Setup salt cache. Use APC, or fallback to the main cache if it isn't setup
200 $cache = $services->getLocalServerObjectCache();
201 if ( $cache instanceof EmptyBagOStuff ) {
202 $cache = ObjectCache::getLocalClusterInstance();
203 }
204
205 return new CryptHKDF( $secret, $config->get( 'HKDFAlgorithm' ),
206 $cache, $context, $services->getCryptRand()
207 );
208 },
209
210 'MediaHandlerFactory' => function( MediaWikiServices $services ) {
211 return new MediaHandlerFactory(
212 $services->getMainConfig()->get( 'MediaHandlers' )
213 );
214 },
215
216 'MimeAnalyzer' => function( MediaWikiServices $services ) {
217 $logger = LoggerFactory::getInstance( 'Mime' );
218 $mainConfig = $services->getMainConfig();
219 $params = [
220 'typeFile' => $mainConfig->get( 'MimeTypeFile' ),
221 'infoFile' => $mainConfig->get( 'MimeInfoFile' ),
222 'xmlTypes' => $mainConfig->get( 'XMLMimeTypes' ),
223 'guessCallback' =>
224 function ( $mimeAnalyzer, &$head, &$tail, $file, &$mime ) use ( $logger ) {
225 // Also test DjVu
226 $deja = new DjVuImage( $file );
227 if ( $deja->isValid() ) {
228 $logger->info( __METHOD__ . ": detected $file as image/vnd.djvu\n" );
229 $mime = 'image/vnd.djvu';
230
231 return;
232 }
233 // Some strings by reference for performance - assuming well-behaved hooks
234 Hooks::run(
235 'MimeMagicGuessFromContent',
236 [ $mimeAnalyzer, &$head, &$tail, $file, &$mime ]
237 );
238 },
239 'extCallback' => function ( $mimeAnalyzer, $ext, &$mime ) {
240 // Media handling extensions can improve the MIME detected
241 Hooks::run( 'MimeMagicImproveFromExtension', [ $mimeAnalyzer, $ext, &$mime ] );
242 },
243 'initCallback' => function ( $mimeAnalyzer ) {
244 // Allow media handling extensions adding MIME-types and MIME-info
245 Hooks::run( 'MimeMagicInit', [ $mimeAnalyzer ] );
246 },
247 'logger' => $logger
248 ];
249
250 if ( $params['infoFile'] === 'includes/mime.info' ) {
251 $params['infoFile'] = __DIR__ . "/libs/mime/mime.info";
252 }
253
254 if ( $params['typeFile'] === 'includes/mime.types' ) {
255 $params['typeFile'] = __DIR__ . "/libs/mime/mime.types";
256 }
257
258 $detectorCmd = $mainConfig->get( 'MimeDetectorCommand' );
259 if ( $detectorCmd ) {
260 $params['detectCallback'] = function ( $file ) use ( $detectorCmd ) {
261 return wfShellExec( "$detectorCmd " . wfEscapeShellArg( $file ) );
262 };
263 }
264
265 // XXX: MimeMagic::singleton currently requires this service to return an instance of MimeMagic
266 return new MimeMagic( $params );
267 },
268
269 'ProxyLookup' => function( MediaWikiServices $services ) {
270 $mainConfig = $services->getMainConfig();
271 return new ProxyLookup(
272 $mainConfig->get( 'SquidServers' ),
273 $mainConfig->get( 'SquidServersNoPurge' )
274 );
275 },
276
277 'Parser' => function( MediaWikiServices $services ) {
278 $conf = $services->getMainConfig()->get( 'ParserConf' );
279 return ObjectFactory::constructClassInstance( $conf['class'], [ $conf ] );
280 },
281
282 'LinkCache' => function( MediaWikiServices $services ) {
283 return new LinkCache(
284 $services->getTitleFormatter(),
285 $services->getMainWANObjectCache()
286 );
287 },
288
289 'LinkRendererFactory' => function( MediaWikiServices $services ) {
290 return new LinkRendererFactory(
291 $services->getTitleFormatter(),
292 $services->getLinkCache()
293 );
294 },
295
296 'LinkRenderer' => function( MediaWikiServices $services ) {
297 global $wgUser;
298
299 if ( defined( 'MW_NO_SESSION' ) ) {
300 return $services->getLinkRendererFactory()->create();
301 } else {
302 return $services->getLinkRendererFactory()->createForUser( $wgUser );
303 }
304 },
305
306 'GenderCache' => function( MediaWikiServices $services ) {
307 return new GenderCache();
308 },
309
310 '_MediaWikiTitleCodec' => function( MediaWikiServices $services ) {
311 global $wgContLang;
312
313 return new MediaWikiTitleCodec(
314 $wgContLang,
315 $services->getGenderCache(),
316 $services->getMainConfig()->get( 'LocalInterwikis' )
317 );
318 },
319
320 'TitleFormatter' => function( MediaWikiServices $services ) {
321 return $services->getService( '_MediaWikiTitleCodec' );
322 },
323
324 'TitleParser' => function( MediaWikiServices $services ) {
325 return $services->getService( '_MediaWikiTitleCodec' );
326 },
327
328 'MainObjectStash' => function( MediaWikiServices $services ) {
329 $mainConfig = $services->getMainConfig();
330
331 $id = $mainConfig->get( 'MainStash' );
332 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
333 throw new UnexpectedValueException(
334 "Cache type \"$id\" is not present in \$wgObjectCaches." );
335 }
336
337 return \ObjectCache::newFromParams( $mainConfig->get( 'ObjectCaches' )[$id] );
338 },
339
340 'MainWANObjectCache' => function( MediaWikiServices $services ) {
341 $mainConfig = $services->getMainConfig();
342
343 $id = $mainConfig->get( 'MainWANCache' );
344 if ( !isset( $mainConfig->get( 'WANObjectCaches' )[$id] ) ) {
345 throw new UnexpectedValueException(
346 "WAN cache type \"$id\" is not present in \$wgWANObjectCaches." );
347 }
348
349 $params = $mainConfig->get( 'WANObjectCaches' )[$id];
350 $objectCacheId = $params['cacheId'];
351 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$objectCacheId] ) ) {
352 throw new UnexpectedValueException(
353 "Cache type \"$objectCacheId\" is not present in \$wgObjectCaches." );
354 }
355 $params['store'] = $mainConfig->get( 'ObjectCaches' )[$objectCacheId];
356
357 return \ObjectCache::newWANCacheFromParams( $params );
358 },
359
360 'LocalServerObjectCache' => function( MediaWikiServices $services ) {
361 $mainConfig = $services->getMainConfig();
362
363 if ( function_exists( 'apc_fetch' ) ) {
364 $id = 'apc';
365 } elseif ( function_exists( 'apcu_fetch' ) ) {
366 $id = 'apcu';
367 } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) {
368 $id = 'xcache';
369 } elseif ( function_exists( 'wincache_ucache_get' ) ) {
370 $id = 'wincache';
371 } else {
372 $id = CACHE_NONE;
373 }
374
375 if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
376 throw new UnexpectedValueException(
377 "Cache type \"$id\" is not present in \$wgObjectCaches." );
378 }
379
380 return \ObjectCache::newFromParams( $mainConfig->get( 'ObjectCaches' )[$id] );
381 },
382
383 'VirtualRESTServiceClient' => function( MediaWikiServices $services ) {
384 $config = $services->getMainConfig()->get( 'VirtualRestConfig' );
385
386 $vrsClient = new VirtualRESTServiceClient( new MultiHttpClient( [] ) );
387 foreach ( $config['paths'] as $prefix => $serviceConfig ) {
388 $class = $serviceConfig['class'];
389 // Merge in the global defaults
390 $constructArg = isset( $serviceConfig['options'] )
391 ? $serviceConfig['options']
392 : [];
393 $constructArg += $config['global'];
394 // Make the VRS service available at the mount point
395 $vrsClient->mount( $prefix, [ 'class' => $class, 'config' => $constructArg ] );
396 }
397
398 return $vrsClient;
399 },
400
401 ///////////////////////////////////////////////////////////////////////////
402 // NOTE: When adding a service here, don't forget to add a getter function
403 // in the MediaWikiServices class. The convenience getter should just call
404 // $this->getService( 'FooBarService' ).
405 ///////////////////////////////////////////////////////////////////////////
406
407 ];