Enable 4:2:0 chroma subsampling for JPEG thumbnails
[lhc/web/wiklou.git] / includes / MediaWikiServices.php
1 <?php
2 namespace MediaWiki;
3
4 use ConfigFactory;
5 use EventRelayerGroup;
6 use GlobalVarConfig;
7 use Config;
8 use Hooks;
9 use LBFactory;
10 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
11 use LoadBalancer;
12 use MediaWiki\Services\ServiceContainer;
13 use SiteLookup;
14 use SiteStore;
15 use SkinFactory;
16
17 /**
18 * Service locator for MediaWiki core services.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 * http://www.gnu.org/copyleft/gpl.html
34 *
35 * @file
36 *
37 * @since 1.27
38 */
39
40 /**
41 * MediaWikiServices is the service locator for the application scope of MediaWiki.
42 * Its implemented as a simple configurable DI container.
43 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
44 * the network of service objects that defines MediaWiki's application logic.
45 * It acts as an entry point to MediaWiki's dependency injection mechanism.
46 *
47 * Services are defined in the "wiring" array passed to the constructor,
48 * or by calling defineService().
49 *
50 * @see docs/injection.txt for an overview of using dependency injection in the
51 * MediaWiki code base.
52 */
53 class MediaWikiServices extends ServiceContainer {
54
55 /**
56 * Returns the global default instance of the top level service locator.
57 *
58 * The default instance is initialized using the service instantiator functions
59 * defined in ServiceWiring.php.
60 *
61 * @note This should only be called by static functions! The instance returned here
62 * should not be passed around! Objects that need access to a service should have
63 * that service injected into the constructor, never a service locator!
64 *
65 * @return MediaWikiServices
66 */
67 public static function getInstance() {
68 static $instance = null;
69
70 if ( $instance === null ) {
71 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
72 // but some information from the global scope has to be injected here,
73 // even if it's just a file name or database credentials to load
74 // configuration from.
75 $config = new GlobalVarConfig();
76 $instance = new self( $config );
77
78 // Load the default wiring from the specified files.
79 $wiringFiles = $config->get( 'ServiceWiringFiles' );
80 $instance->loadWiringFiles( $wiringFiles );
81
82 // Provide a traditional hook point to allow extensions to configure services.
83 Hooks::run( 'MediaWikiServices', [ $instance ] );
84 }
85
86 return $instance;
87 }
88
89 /**
90 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
91 * This has to contain at least the information needed to set up the 'ConfigFactory'
92 * service.
93 */
94 public function __construct( Config $config ) {
95 parent::__construct();
96
97 // register the given Config object as the bootstrap config service.
98 $this->defineService( 'BootstrapConfig', function() use ( $config ) {
99 return $config;
100 } );
101 }
102
103 /**
104 * Returns the Config object containing the bootstrap configuration.
105 * Bootstrap configuration would typically include database credentials
106 * and other information that may be needed before the ConfigFactory
107 * service can be instantiated.
108 *
109 * @note This should only be used during bootstrapping, in particular
110 * when creating the MainConfig service. Application logic should
111 * use getMainConfig() to get a Config instances.
112 *
113 * @return Config
114 */
115 public function getBootstrapConfig() {
116 return $this->getService( 'BootstrapConfig' );
117 }
118
119 /**
120 * @return ConfigFactory
121 */
122 public function getConfigFactory() {
123 return $this->getService( 'ConfigFactory' );
124 }
125
126 /**
127 * Returns the Config object that provides configuration for MediaWiki core.
128 * This may or may not be the same object that is returned by getBootstrapConfig().
129 *
130 * @return Config
131 */
132 public function getMainConfig() {
133 return $this->getService( 'MainConfig' );
134 }
135
136 /**
137 * @return SiteLookup
138 */
139 public function getSiteLookup() {
140 return $this->getService( 'SiteLookup' );
141 }
142
143 /**
144 * @return SiteStore
145 */
146 public function getSiteStore() {
147 return $this->getService( 'SiteStore' );
148 }
149
150 /**
151 * @return StatsdDataFactory
152 */
153 public function getStatsdDataFactory() {
154 return $this->getService( 'StatsdDataFactory' );
155 }
156
157 /**
158 * @return EventRelayerGroup
159 */
160 public function getEventRelayerGroup() {
161 return $this->getService( 'EventRelayerGroup' );
162 }
163
164 /**
165 * @return SearchEngine
166 */
167 public function newSearchEngine() {
168 // New engine object every time, since they keep state
169 return $this->getService( 'SearchEngineFactory' )->create();
170 }
171
172 /**
173 * @return SearchEngineFactory
174 */
175 public function getSearchEngineFactory() {
176 return $this->getService( 'SearchEngineFactory' );
177 }
178
179 /**
180 * @return SearchEngineConfig
181 */
182 public function getSearchEngineConfig() {
183 return $this->getService( 'SearchEngineConfig' );
184 }
185
186 /**
187 * @return SkinFactory
188 */
189 public function getSkinFactory() {
190 return $this->getService( 'SkinFactory' );
191 }
192
193 ///////////////////////////////////////////////////////////////////////////
194 // NOTE: When adding a service getter here, don't forget to add a test
195 // case for it in MediaWikiServicesTest::provideGetters() and in
196 // MediaWikiServicesTest::provideGetService()!
197 ///////////////////////////////////////////////////////////////////////////
198
199 }