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