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