Switch some HTMLForms in special pages to OOUI
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderContext.php
1 <?php
2 /**
3 * Context for resource loader modules.
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 * @author Trevor Parscal
22 * @author Roan Kattouw
23 */
24
25 use MediaWiki\Logger\LoggerFactory;
26
27 /**
28 * Object passed around to modules which contains information about the state
29 * of a specific loader request
30 */
31 class ResourceLoaderContext {
32 /* Protected Members */
33
34 protected $resourceLoader;
35 protected $request;
36 protected $modules;
37 protected $language;
38 protected $direction;
39 protected $skin;
40 protected $user;
41 protected $debug;
42 protected $only;
43 protected $version;
44 protected $hash;
45 protected $raw;
46 protected $image;
47 protected $variant;
48 protected $format;
49 protected $userObj;
50 protected $imageObj;
51
52 /* Methods */
53
54 /**
55 * @param ResourceLoader $resourceLoader
56 * @param WebRequest $request
57 */
58 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
59 $this->resourceLoader = $resourceLoader;
60 $this->request = $request;
61
62 // List of modules
63 $modules = $request->getVal( 'modules' );
64 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
65
66 // Various parameters
67 $this->user = $request->getVal( 'user' );
68 $this->debug = $request->getFuzzyBool(
69 'debug',
70 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
71 );
72 $this->only = $request->getVal( 'only', null );
73 $this->version = $request->getVal( 'version', null );
74 $this->raw = $request->getFuzzyBool( 'raw' );
75
76 // Image requests
77 $this->image = $request->getVal( 'image' );
78 $this->variant = $request->getVal( 'variant' );
79 $this->format = $request->getVal( 'format' );
80
81 $this->skin = $request->getVal( 'skin' );
82 $skinnames = Skin::getSkinNames();
83 // If no skin is specified, or we don't recognize the skin, use the default skin
84 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
85 $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' );
86 }
87 }
88
89 /**
90 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
91 * an array of module names like array( 'jquery.foo', 'jquery.bar',
92 * 'jquery.ui.baz', 'jquery.ui.quux' )
93 * @param string $modules Packed module name list
94 * @return array Array of module names
95 */
96 public static function expandModuleNames( $modules ) {
97 $retval = array();
98 $exploded = explode( '|', $modules );
99 foreach ( $exploded as $group ) {
100 if ( strpos( $group, ',' ) === false ) {
101 // This is not a set of modules in foo.bar,baz notation
102 // but a single module
103 $retval[] = $group;
104 } else {
105 // This is a set of modules in foo.bar,baz notation
106 $pos = strrpos( $group, '.' );
107 if ( $pos === false ) {
108 // Prefixless modules, i.e. without dots
109 $retval = array_merge( $retval, explode( ',', $group ) );
110 } else {
111 // We have a prefix and a bunch of suffixes
112 $prefix = substr( $group, 0, $pos ); // 'foo'
113 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
114 foreach ( $suffixes as $suffix ) {
115 $retval[] = "$prefix.$suffix";
116 }
117 }
118 }
119 }
120 return $retval;
121 }
122
123 /**
124 * Return a dummy ResourceLoaderContext object suitable for passing into
125 * things that don't "really" need a context.
126 * @return ResourceLoaderContext
127 */
128 public static function newDummyContext() {
129 return new self( new ResourceLoader(
130 ConfigFactory::getDefaultInstance()->makeConfig( 'main' ),
131 LoggerFactory::getInstance( 'resourceloader' )
132 ), new FauxRequest( array() ) );
133 }
134
135 /**
136 * @return ResourceLoader
137 */
138 public function getResourceLoader() {
139 return $this->resourceLoader;
140 }
141
142 /**
143 * @return WebRequest
144 */
145 public function getRequest() {
146 return $this->request;
147 }
148
149 /**
150 * @return array
151 */
152 public function getModules() {
153 return $this->modules;
154 }
155
156 /**
157 * @return string
158 */
159 public function getLanguage() {
160 if ( $this->language === null ) {
161 // Must be a valid language code after this point (bug 62849)
162 $this->language = RequestContext::sanitizeLangCode( $this->getRequest()->getVal( 'lang' ) );
163 }
164 return $this->language;
165 }
166
167 /**
168 * @return string
169 */
170 public function getDirection() {
171 if ( $this->direction === null ) {
172 $this->direction = $this->getRequest()->getVal( 'dir' );
173 if ( !$this->direction ) {
174 // Determine directionality based on user language (bug 6100)
175 $this->direction = Language::factory( $this->getLanguage() )->getDir();
176 }
177 }
178 return $this->direction;
179 }
180
181 /**
182 * @return string
183 */
184 public function getSkin() {
185 return $this->skin;
186 }
187
188 /**
189 * @return string|null
190 */
191 public function getUser() {
192 return $this->user;
193 }
194
195 /**
196 * Get the possibly-cached User object for the specified username
197 *
198 * @since 1.25
199 * @return User|bool false if a valid object cannot be created
200 */
201 public function getUserObj() {
202 if ( $this->userObj === null ) {
203 $username = $this->getUser();
204 if ( $username ) {
205 // Optimize: Avoid loading a new User object if possible
206 global $wgUser;
207 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
208 $this->userObj = $wgUser;
209 } else {
210 $this->userObj = User::newFromName( $username );
211 }
212 } else {
213 $this->userObj = new User; // Anonymous user
214 }
215 }
216
217 return $this->userObj;
218 }
219
220 /**
221 * @return bool
222 */
223 public function getDebug() {
224 return $this->debug;
225 }
226
227 /**
228 * @return string|null
229 */
230 public function getOnly() {
231 return $this->only;
232 }
233
234 /**
235 * @see ResourceLoaderModule::getVersionHash
236 * @see OutputPage::makeResourceLoaderLink
237 * @return string|null
238 */
239 public function getVersion() {
240 return $this->version;
241 }
242
243 /**
244 * @return bool
245 */
246 public function getRaw() {
247 return $this->raw;
248 }
249
250 /**
251 * @return string|null
252 */
253 public function getImage() {
254 return $this->image;
255 }
256
257 /**
258 * @return string|null
259 */
260 public function getVariant() {
261 return $this->variant;
262 }
263
264 /**
265 * @return string|null
266 */
267 public function getFormat() {
268 return $this->format;
269 }
270
271 /**
272 * If this is a request for an image, get the ResourceLoaderImage object.
273 *
274 * @since 1.25
275 * @return ResourceLoaderImage|bool false if a valid object cannot be created
276 */
277 public function getImageObj() {
278 if ( $this->imageObj === null ) {
279 $this->imageObj = false;
280
281 if ( !$this->image ) {
282 return $this->imageObj;
283 }
284
285 $modules = $this->getModules();
286 if ( count( $modules ) !== 1 ) {
287 return $this->imageObj;
288 }
289
290 $module = $this->getResourceLoader()->getModule( $modules[0] );
291 if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
292 return $this->imageObj;
293 }
294
295 $image = $module->getImage( $this->image, $this );
296 if ( !$image ) {
297 return $this->imageObj;
298 }
299
300 $this->imageObj = $image;
301 }
302
303 return $this->imageObj;
304 }
305
306 /**
307 * @return bool
308 */
309 public function shouldIncludeScripts() {
310 return $this->getOnly() === null || $this->getOnly() === 'scripts';
311 }
312
313 /**
314 * @return bool
315 */
316 public function shouldIncludeStyles() {
317 return $this->getOnly() === null || $this->getOnly() === 'styles';
318 }
319
320 /**
321 * @return bool
322 */
323 public function shouldIncludeMessages() {
324 return $this->getOnly() === null;
325 }
326
327 /**
328 * @return string
329 */
330 public function getHash() {
331 if ( !isset( $this->hash ) ) {
332 $this->hash = implode( '|', array(
333 $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
334 $this->getImage(), $this->getVariant(), $this->getFormat(),
335 $this->getDebug(), $this->getOnly(), $this->getVersion()
336 ) );
337 }
338 return $this->hash;
339 }
340 }