Add LinkRenderer (rewrite of Linker::link())
[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\LinkRenderer;
42 use MediaWiki\Linker\LinkRendererFactory;
43 use MediaWiki\MediaWikiServices;
44
45 return [
46 'DBLoadBalancerFactory' => function( MediaWikiServices $services ) {
47 $config = $services->getMainConfig()->get( 'LBFactoryConf' );
48
49 $class = LBFactory::getLBFactoryClass( $config );
50 if ( !isset( $config['readOnlyReason'] ) ) {
51 // TODO: replace the global wfConfiguredReadOnlyReason() with a service.
52 $config['readOnlyReason'] = wfConfiguredReadOnlyReason();
53 }
54
55 return new $class( $config );
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 ObjectCache::getMainWANInstance(),
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 'LinkCache' => function( MediaWikiServices $services ) {
159 return new LinkCache(
160 $services->getTitleFormatter()
161 );
162 },
163
164 'LinkRendererFactory' => function( MediaWikiServices $services ) {
165 return new LinkRendererFactory(
166 $services->getTitleFormatter()
167 );
168 },
169
170 'LinkRenderer' => function( MediaWikiServices $services ) {
171 global $wgUser;
172
173 return $services->getLinkRendererFactory()->createForUser( $wgUser );
174 },
175
176 'GenderCache' => function( MediaWikiServices $services ) {
177 return new GenderCache();
178 },
179
180 '_MediaWikiTitleCodec' => function( MediaWikiServices $services ) {
181 global $wgContLang;
182
183 return new MediaWikiTitleCodec(
184 $wgContLang,
185 $services->getGenderCache(),
186 $services->getMainConfig()->get( 'LocalInterwikis' )
187 );
188 },
189
190 'TitleFormatter' => function( MediaWikiServices $services ) {
191 return $services->getService( '_MediaWikiTitleCodec' );
192 },
193
194 'TitleParser' => function( MediaWikiServices $services ) {
195 return $services->getService( '_MediaWikiTitleCodec' );
196 },
197
198 ///////////////////////////////////////////////////////////////////////////
199 // NOTE: When adding a service here, don't forget to add a getter function
200 // in the MediaWikiServices class. The convenience getter should just call
201 // $this->getService( 'FooBarService' ).
202 ///////////////////////////////////////////////////////////////////////////
203
204 ];