Merge "CoreTagHooks: Use parse() for output to HTML rather than text()"
[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 /**
26 * Object passed around to modules which contains information about the state
27 * of a specific loader request
28 */
29 class ResourceLoaderContext {
30 /* Protected Members */
31
32 protected $resourceLoader;
33 protected $request;
34 protected $modules;
35 protected $language;
36 protected $direction;
37 protected $skin;
38 protected $user;
39 protected $debug;
40 protected $only;
41 protected $version;
42 protected $hash;
43 protected $raw;
44 protected $userObj;
45
46 /* Methods */
47
48 /**
49 * @param ResourceLoader $resourceLoader
50 * @param WebRequest $request
51 */
52 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
53 $this->resourceLoader = $resourceLoader;
54 $this->request = $request;
55
56 // Interpret request
57 // List of modules
58 $modules = $request->getVal( 'modules' );
59 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
60 // Various parameters
61 $this->skin = $request->getVal( 'skin' );
62 $this->user = $request->getVal( 'user' );
63 $this->debug = $request->getFuzzyBool(
64 'debug', $resourceLoader->getConfig()->get( 'ResourceLoaderDebug' )
65 );
66 $this->only = $request->getVal( 'only' );
67 $this->version = $request->getVal( 'version' );
68 $this->raw = $request->getFuzzyBool( 'raw' );
69
70 $skinnames = Skin::getSkinNames();
71 // If no skin is specified, or we don't recognize the skin, use the default skin
72 if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
73 $this->skin = $resourceLoader->getConfig()->get( 'DefaultSkin' );
74 }
75 }
76
77 /**
78 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
79 * an array of module names like array( 'jquery.foo', 'jquery.bar',
80 * 'jquery.ui.baz', 'jquery.ui.quux' )
81 * @param string $modules Packed module name list
82 * @return array Array of module names
83 */
84 public static function expandModuleNames( $modules ) {
85 $retval = array();
86 $exploded = explode( '|', $modules );
87 foreach ( $exploded as $group ) {
88 if ( strpos( $group, ',' ) === false ) {
89 // This is not a set of modules in foo.bar,baz notation
90 // but a single module
91 $retval[] = $group;
92 } else {
93 // This is a set of modules in foo.bar,baz notation
94 $pos = strrpos( $group, '.' );
95 if ( $pos === false ) {
96 // Prefixless modules, i.e. without dots
97 $retval = array_merge( $retval, explode( ',', $group ) );
98 } else {
99 // We have a prefix and a bunch of suffixes
100 $prefix = substr( $group, 0, $pos ); // 'foo'
101 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
102 foreach ( $suffixes as $suffix ) {
103 $retval[] = "$prefix.$suffix";
104 }
105 }
106 }
107 }
108 return $retval;
109 }
110
111 /**
112 * Return a dummy ResourceLoaderContext object suitable for passing into
113 * things that don't "really" need a context.
114 * @return ResourceLoaderContext
115 */
116 public static function newDummyContext() {
117 return new self( new ResourceLoader(
118 ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
119 ), new FauxRequest( array() ) );
120 }
121
122 /**
123 * @return ResourceLoader
124 */
125 public function getResourceLoader() {
126 return $this->resourceLoader;
127 }
128
129 /**
130 * @return WebRequest
131 */
132 public function getRequest() {
133 return $this->request;
134 }
135
136 /**
137 * @return array
138 */
139 public function getModules() {
140 return $this->modules;
141 }
142
143 /**
144 * @return string
145 */
146 public function getLanguage() {
147 if ( $this->language === null ) {
148 // Must be a valid language code after this point (bug 62849)
149 $this->language = RequestContext::sanitizeLangCode( $this->request->getVal( 'lang' ) );
150 }
151 return $this->language;
152 }
153
154 /**
155 * @return string
156 */
157 public function getDirection() {
158 if ( $this->direction === null ) {
159 $this->direction = $this->request->getVal( 'dir' );
160 if ( !$this->direction ) {
161 // Determine directionality based on user language (bug 6100)
162 $this->direction = Language::factory( $this->getLanguage() )->getDir();
163 }
164 }
165 return $this->direction;
166 }
167
168 /**
169 * @return string|null
170 */
171 public function getSkin() {
172 return $this->skin;
173 }
174
175 /**
176 * @return string|null
177 */
178 public function getUser() {
179 return $this->user;
180 }
181
182 /**
183 * Get the possibly-cached User object for the specified username
184 *
185 * @since 1.25
186 * @return User|bool false if a valid object cannot be created
187 */
188 public function getUserObj() {
189 if ( $this->userObj === null ) {
190 $username = $this->getUser();
191 if ( $username ) {
192 // Optimize: Avoid loading a new User object if possible
193 global $wgUser;
194 if ( is_object( $wgUser ) && $wgUser->getName() === $username ) {
195 $this->userObj = $wgUser;
196 } else {
197 $this->userObj = User::newFromName( $username );
198 }
199 } else {
200 $this->userObj = new User; // Anonymous user
201 }
202 }
203
204 return $this->userObj;
205 }
206
207 /**
208 * @return bool
209 */
210 public function getDebug() {
211 return $this->debug;
212 }
213
214 /**
215 * @return string|null
216 */
217 public function getOnly() {
218 return $this->only;
219 }
220
221 /**
222 * @return string|null
223 */
224 public function getVersion() {
225 return $this->version;
226 }
227
228 /**
229 * @return bool
230 */
231 public function getRaw() {
232 return $this->raw;
233 }
234
235 /**
236 * @return bool
237 */
238 public function shouldIncludeScripts() {
239 return is_null( $this->getOnly() ) || $this->getOnly() === 'scripts';
240 }
241
242 /**
243 * @return bool
244 */
245 public function shouldIncludeStyles() {
246 return is_null( $this->getOnly() ) || $this->getOnly() === 'styles';
247 }
248
249 /**
250 * @return bool
251 */
252 public function shouldIncludeMessages() {
253 return is_null( $this->getOnly() ) || $this->getOnly() === 'messages';
254 }
255
256 /**
257 * @return string
258 */
259 public function getHash() {
260 if ( !isset( $this->hash ) ) {
261 $this->hash = implode( '|', array(
262 $this->getLanguage(), $this->getDirection(), $this->getSkin(), $this->getUser(),
263 $this->getDebug(), $this->getOnly(), $this->getVersion()
264 ) );
265 }
266 return $this->hash;
267 }
268 }