Ensure users are able to edit the page after changing the content model
[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\MediaWikiServices;
43
44 return [
45 'DBLoadBalancerFactory' => function( MediaWikiServices $services ) {
46 $config = $services->getMainConfig()->get( 'LBFactoryConf' );
47
48 $class = LBFactory::getLBFactoryClass( $config );
49 if ( !isset( $config['readOnlyReason'] ) ) {
50 // TODO: replace the global wfConfiguredReadOnlyReason() with a service.
51 $config['readOnlyReason'] = wfConfiguredReadOnlyReason();
52 }
53
54 return new $class( $config );
55 },
56
57 'DBLoadBalancer' => function( MediaWikiServices $services ) {
58 // just return the default LB from the DBLoadBalancerFactory service
59 return $services->getDBLoadBalancerFactory()->getMainLB();
60 },
61
62 'SiteStore' => function( MediaWikiServices $services ) {
63 $rawSiteStore = new DBSiteStore( $services->getDBLoadBalancer() );
64
65 // TODO: replace wfGetCache with a CacheFactory service.
66 // TODO: replace wfIsHHVM with a capabilities service.
67 $cache = wfGetCache( wfIsHHVM() ? CACHE_ACCEL : CACHE_ANYTHING );
68
69 return new CachingSiteStore( $rawSiteStore, $cache );
70 },
71
72 'SiteLookup' => function( MediaWikiServices $services ) {
73 // Use the default SiteStore as the SiteLookup implementation for now
74 return $services->getSiteStore();
75 },
76
77 'ConfigFactory' => function( MediaWikiServices $services ) {
78 // Use the bootstrap config to initialize the ConfigFactory.
79 $registry = $services->getBootstrapConfig()->get( 'ConfigRegistry' );
80 $factory = new ConfigFactory();
81
82 foreach ( $registry as $name => $callback ) {
83 $factory->register( $name, $callback );
84 }
85 return $factory;
86 },
87
88 'MainConfig' => function( MediaWikiServices $services ) {
89 // Use the 'main' config from the ConfigFactory service.
90 return $services->getConfigFactory()->makeConfig( 'main' );
91 },
92
93 'InterwikiLookup' => function( MediaWikiServices $services ) {
94 global $wgContLang; // TODO: manage $wgContLang as a service
95 $config = $services->getMainConfig();
96 return new ClassicInterwikiLookup(
97 $wgContLang,
98 ObjectCache::getMainWANInstance(),
99 $config->get( 'InterwikiExpiry' ),
100 $config->get( 'InterwikiCache' ),
101 $config->get( 'InterwikiScopes' ),
102 $config->get( 'InterwikiFallbackSite' )
103 );
104 },
105
106 'StatsdDataFactory' => function( MediaWikiServices $services ) {
107 return new BufferingStatsdDataFactory(
108 rtrim( $services->getMainConfig()->get( 'StatsdMetricPrefix' ), '.' )
109 );
110 },
111
112 'EventRelayerGroup' => function( MediaWikiServices $services ) {
113 return new EventRelayerGroup( $services->getMainConfig()->get( 'EventRelayerConfig' ) );
114 },
115
116 'SearchEngineFactory' => function( MediaWikiServices $services ) {
117 return new SearchEngineFactory( $services->getSearchEngineConfig() );
118 },
119
120 'SearchEngineConfig' => function( MediaWikiServices $services ) {
121 global $wgContLang;
122 return new SearchEngineConfig( $services->getMainConfig(), $wgContLang );
123 },
124
125 'SkinFactory' => function( MediaWikiServices $services ) {
126 $factory = new SkinFactory();
127
128 $names = $services->getMainConfig()->get( 'ValidSkinNames' );
129
130 foreach ( $names as $name => $skin ) {
131 $factory->register( $name, $skin, function () use ( $name, $skin ) {
132 $class = "Skin$skin";
133 return new $class( $name );
134 } );
135 }
136 // Register a hidden "fallback" skin
137 $factory->register( 'fallback', 'Fallback', function () {
138 return new SkinFallback;
139 } );
140 // Register a hidden skin for api output
141 $factory->register( 'apioutput', 'ApiOutput', function () {
142 return new SkinApi;
143 } );
144
145 return $factory;
146 },
147
148 'WatchedItemStore' => function( MediaWikiServices $services ) {
149 $store = new WatchedItemStore(
150 $services->getDBLoadBalancer(),
151 new HashBagOStuff( [ 'maxKeys' => 100 ] )
152 );
153 $store->setStatsdDataFactory( $services->getStatsdDataFactory() );
154 return $store;
155 },
156
157 'WatchedItemQueryService' => function( MediaWikiServices $services ) {
158 return new WatchedItemQueryService( $services->getDBLoadBalancer() );
159 },
160
161 'MediaHandlerFactory' => function( MediaWikiServices $services ) {
162 return new MediaHandlerFactory(
163 $services->getMainConfig()->get( 'MediaHandlers' )
164 );
165 },
166
167 'LinkCache' => function( MediaWikiServices $services ) {
168 return new LinkCache(
169 $services->getTitleFormatter(),
170 ObjectCache::getMainWANInstance()
171 );
172 },
173
174 'LinkRendererFactory' => function( MediaWikiServices $services ) {
175 return new LinkRendererFactory(
176 $services->getTitleFormatter(),
177 $services->getLinkCache()
178 );
179 },
180
181 'LinkRenderer' => function( MediaWikiServices $services ) {
182 global $wgUser;
183
184 if ( defined( 'MW_NO_SESSION' ) ) {
185 return $services->getLinkRendererFactory()->create();
186 } else {
187 return $services->getLinkRendererFactory()->createForUser( $wgUser );
188 }
189 },
190
191 'GenderCache' => function( MediaWikiServices $services ) {
192 return new GenderCache();
193 },
194
195 '_MediaWikiTitleCodec' => function( MediaWikiServices $services ) {
196 global $wgContLang;
197
198 return new MediaWikiTitleCodec(
199 $wgContLang,
200 $services->getGenderCache(),
201 $services->getMainConfig()->get( 'LocalInterwikis' )
202 );
203 },
204
205 'TitleFormatter' => function( MediaWikiServices $services ) {
206 return $services->getService( '_MediaWikiTitleCodec' );
207 },
208
209 'TitleParser' => function( MediaWikiServices $services ) {
210 return $services->getService( '_MediaWikiTitleCodec' );
211 },
212
213 'VirtualRESTServiceClient' => function( MediaWikiServices $services ) {
214 $config = $services->getMainConfig()->get( 'VirtualRestConfig' );
215
216 $vrsClient = new VirtualRESTServiceClient( new MultiHttpClient( [] ) );
217 foreach ( $config['paths'] as $prefix => $serviceConfig ) {
218 $class = $serviceConfig['class'];
219 // Merge in the global defaults
220 $constructArg = isset( $serviceConfig['options'] )
221 ? $serviceConfig['options']
222 : [];
223 $constructArg += $config['global'];
224 // Make the VRS service available at the mount point
225 $vrsClient->mount( $prefix, [ 'class' => $class, 'config' => $constructArg ] );
226 }
227
228 return $vrsClient;
229 },
230
231 ///////////////////////////////////////////////////////////////////////////
232 // NOTE: When adding a service here, don't forget to add a getter function
233 // in the MediaWikiServices class. The convenience getter should just call
234 // $this->getService( 'FooBarService' ).
235 ///////////////////////////////////////////////////////////////////////////
236
237 ];