Update button focus and hover state according to spec
[mediawiki.git] / includes / resourceloader / ResourceLoaderContext.php
blob2e1752a6b16f613d0622643516cccb0ebd9226f2
1 <?php
2 /**
3 * Context for resource loader modules.
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.
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.
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
20 * @file
21 * @author Trevor Parscal
22 * @author Roan Kattouw
25 use MediaWiki\Logger\LoggerFactory;
27 /**
28 * Object passed around to modules which contains information about the state
29 * of a specific loader request
31 class ResourceLoaderContext {
32 /* Protected Members */
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;
52 /* Methods */
54 /**
55 * @param ResourceLoader $resourceLoader
56 * @param WebRequest $request
58 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
59 $this->resourceLoader = $resourceLoader;
60 $this->request = $request;
62 // List of modules
63 $modules = $request->getVal( 'modules' );
64 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
66 // Various parameters
67 $this->user = $request->getVal( 'user' );
68 $this->debug = $request->getFuzzyBool(
69 'debug',
70 $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
72 $this->only = $request->getVal( 'only', null );
73 $this->version = $request->getVal( 'version', null );
74 $this->raw = $request->getFuzzyBool( 'raw' );
76 // Image requests
77 $this->image = $request->getVal( 'image' );
78 $this->variant = $request->getVal( 'variant' );
79 $this->format = $request->getVal( 'format' );
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' );
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
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";
120 return $retval;
124 * Return a dummy ResourceLoaderContext object suitable for passing into
125 * things that don't "really" need a context.
126 * @return ResourceLoaderContext
128 public static function newDummyContext() {
129 return new self( new ResourceLoader(
130 ConfigFactory::getDefaultInstance()->makeConfig( 'main' ),
131 LoggerFactory::getInstance( 'resourceloader' )
132 ), new FauxRequest( array() ) );
136 * @return ResourceLoader
138 public function getResourceLoader() {
139 return $this->resourceLoader;
143 * @return WebRequest
145 public function getRequest() {
146 return $this->request;
150 * @return array
152 public function getModules() {
153 return $this->modules;
157 * @return string
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' ) );
164 return $this->language;
168 * @return string
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();
178 return $this->direction;
182 * @return string
184 public function getSkin() {
185 return $this->skin;
189 * @return string|null
191 public function getUser() {
192 return $this->user;
196 * Get the possibly-cached User object for the specified username
198 * @since 1.25
199 * @return User|bool false if a valid object cannot be created
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 );
212 } else {
213 $this->userObj = new User; // Anonymous user
217 return $this->userObj;
221 * @return bool
223 public function getDebug() {
224 return $this->debug;
228 * @return string|null
230 public function getOnly() {
231 return $this->only;
235 * @see ResourceLoaderModule::getVersionHash
236 * @see OutputPage::makeResourceLoaderLink
237 * @return string|null
239 public function getVersion() {
240 return $this->version;
244 * @return bool
246 public function getRaw() {
247 return $this->raw;
251 * @return string|null
253 public function getImage() {
254 return $this->image;
258 * @return string|null
260 public function getVariant() {
261 return $this->variant;
265 * @return string|null
267 public function getFormat() {
268 return $this->format;
272 * If this is a request for an image, get the ResourceLoaderImage object.
274 * @since 1.25
275 * @return ResourceLoaderImage|bool false if a valid object cannot be created
277 public function getImageObj() {
278 if ( $this->imageObj === null ) {
279 $this->imageObj = false;
281 if ( !$this->image ) {
282 return $this->imageObj;
285 $modules = $this->getModules();
286 if ( count( $modules ) !== 1 ) {
287 return $this->imageObj;
290 $module = $this->getResourceLoader()->getModule( $modules[0] );
291 if ( !$module || !$module instanceof ResourceLoaderImageModule ) {
292 return $this->imageObj;
295 $image = $module->getImage( $this->image, $this );
296 if ( !$image ) {
297 return $this->imageObj;
300 $this->imageObj = $image;
303 return $this->imageObj;
307 * @return bool
309 public function shouldIncludeScripts() {
310 return $this->getOnly() === null || $this->getOnly() === 'scripts';
314 * @return bool
316 public function shouldIncludeStyles() {
317 return $this->getOnly() === null || $this->getOnly() === 'styles';
321 * @return bool
323 public function shouldIncludeMessages() {
324 return $this->getOnly() === null;
328 * @return string
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 ) );
338 return $this->hash;