Documentation
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderContext.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
21 */
22
23 /**
24 * Object passed around to modules which contains information about the state
25 * of a specific loader request
26 */
27 class ResourceLoaderContext {
28
29 /* Protected Members */
30
31 protected $resourceLoader;
32 protected $request;
33 protected $modules;
34 protected $language;
35 protected $direction;
36 protected $skin;
37 protected $user;
38 protected $debug;
39 protected $only;
40 protected $version;
41 protected $hash;
42
43 /* Methods */
44
45 /**
46 * @param $resourceLoader ResourceLoader
47 * @param $request WebRequest
48 */
49 public function __construct( $resourceLoader, WebRequest $request ) {
50 global $wgDefaultSkin, $wgResourceLoaderDebug;
51
52 $this->resourceLoader = $resourceLoader;
53 $this->request = $request;
54
55 // Interpret request
56 // List of modules
57 $modules = $request->getVal( 'modules' );
58 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
59 // Various parameters
60 $this->skin = $request->getVal( 'skin' );
61 $this->user = $request->getVal( 'user' );
62 $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
63 $this->only = $request->getVal( 'only' );
64 $this->version = $request->getVal( 'version' );
65
66 if ( !$this->skin ) {
67 $this->skin = $wgDefaultSkin;
68 }
69 }
70
71 /**
72 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
73 * an array of module names like array( 'jquery.foo', 'jquery.bar',
74 * 'jquery.ui.baz', 'jquery.ui.quux' )
75 * @param $modules String Packed module name list
76 * @return array of module names
77 */
78 public static function expandModuleNames( $modules ) {
79 $retval = array();
80 // For backwards compatibility with an earlier hack, replace ! with .
81 $modules = str_replace( '!', '.', $modules );
82 $exploded = explode( '|', $modules );
83 foreach ( $exploded as $group ) {
84 if ( strpos( $group, ',' ) === false ) {
85 // This is not a set of modules in foo.bar,baz notation
86 // but a single module
87 $retval[] = $group;
88 } else {
89 // This is a set of modules in foo.bar,baz notation
90 $pos = strrpos( $group, '.' );
91 if ( $pos === false ) {
92 // Prefixless modules, i.e. without dots
93 $retval = explode( ',', $group );
94 } else {
95 // We have a prefix and a bunch of suffixes
96 $prefix = substr( $group, 0, $pos ); // 'foo'
97 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
98 foreach ( $suffixes as $suffix ) {
99 $retval[] = "$prefix.$suffix";
100 }
101 }
102 }
103 }
104 return $retval;
105 }
106
107 /**
108 * Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need a context
109 * @return ResourceLoaderContext
110 */
111 public static function newDummyContext() {
112 return new self( null, new FauxRequest( array() ) );
113 }
114
115 /**
116 * @return ResourceLoader
117 */
118 public function getResourceLoader() {
119 return $this->resourceLoader;
120 }
121
122 /**
123 * @return WebRequest
124 */
125 public function getRequest() {
126 return $this->request;
127 }
128
129 /**
130 * @return array
131 */
132 public function getModules() {
133 return $this->modules;
134 }
135
136 /**
137 * @return string
138 */
139 public function getLanguage() {
140 if ( $this->language === null ) {
141 global $wgLang;
142 $this->language = $this->request->getVal( 'lang' );
143 if ( !$this->language ) {
144 $this->language = $wgLang->getCode();
145 }
146 }
147 return $this->language;
148 }
149
150 /**
151 * @return string
152 */
153 public function getDirection() {
154 if ( $this->direction === null ) {
155 $this->direction = $this->request->getVal( 'dir' );
156 if ( !$this->direction ) {
157 # directionality based on user language (see bug 6100)
158 $this->direction = Language::factory( $this->language )->getDir();
159 }
160 }
161 return $this->direction;
162 }
163
164 /**
165 * @return string|null
166 */
167 public function getSkin() {
168 return $this->skin;
169 }
170
171 /**
172 * @return string|null
173 */
174 public function getUser() {
175 return $this->user;
176 }
177
178 /**
179 * @return bool
180 */
181 public function getDebug() {
182 return $this->debug;
183 }
184
185 /**
186 * @return String|null
187 */
188 public function getOnly() {
189 return $this->only;
190 }
191
192 /**
193 * @return String|null
194 */
195 public function getVersion() {
196 return $this->version;
197 }
198
199 /**
200 * @return bool
201 */
202 public function shouldIncludeScripts() {
203 return is_null( $this->only ) || $this->only === 'scripts';
204 }
205
206 /**
207 * @return bool
208 */
209 public function shouldIncludeStyles() {
210 return is_null( $this->only ) || $this->only === 'styles';
211 }
212
213 /**
214 * @return bool
215 */
216 public function shouldIncludeMessages() {
217 return is_null( $this->only ) || $this->only === 'messages';
218 }
219
220 /**
221 * @return string
222 */
223 public function getHash() {
224 if ( !isset( $this->hash ) ) {
225 $this->hash = implode( '|', array(
226 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
227 $this->debug, $this->only, $this->version
228 ) );
229 }
230 return $this->hash;
231 }
232 }