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