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