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