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