Fixup/add 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 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
46 global $wgDefaultSkin, $wgResourceLoaderDebug;
47
48 $this->resourceLoader = $resourceLoader;
49 $this->request = $request;
50
51 // Interpret request
52 // List of modules
53 $modules = $request->getVal( 'modules' );
54 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
55 // Various parameters
56 $this->skin = $request->getVal( 'skin' );
57 $this->user = $request->getVal( 'user' );
58 $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
59 $this->only = $request->getVal( 'only' );
60 $this->version = $request->getVal( 'version' );
61
62 if ( !$this->skin ) {
63 $this->skin = $wgDefaultSkin;
64 }
65 }
66
67 /**
68 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
69 * an array of module names like array( 'jquery.foo', 'jquery.bar',
70 * 'jquery.ui.baz', 'jquery.ui.quux' ) Also translating ! to .
71 * @param $modules String Packed module name list
72 * @return array of module names
73 */
74 public static function expandModuleNames( $modules ) {
75 $retval = array();
76 $modules = str_replace( "!", ".", $modules ); # bug 28840 - IE is stupid.
77 $exploded = explode( '|', $modules );
78 foreach ( $exploded as $group ) {
79 if ( strpos( $group, ',' ) === false ) {
80 // This is not a set of modules in foo.bar,baz notation
81 // but a single module
82 $retval[] = $group;
83 } else {
84 // This is a set of modules in foo.bar,baz notation
85 $pos = strrpos( $group, '.' );
86 if ( $pos === false ) {
87 // Prefixless modules, i.e. without dots
88 $retval = explode( ',', $group );
89 } else {
90 // We have a prefix and a bunch of suffixes
91 $prefix = substr( $group, 0, $pos ); // 'foo'
92 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
93 foreach ( $suffixes as $suffix ) {
94 $retval[] = "$prefix.$suffix";
95 }
96 }
97 }
98 }
99 return $retval;
100 }
101
102 /**
103 * @return ResourceLoader
104 */
105 public function getResourceLoader() {
106 return $this->resourceLoader;
107 }
108
109 /**
110 * @return WebRequest
111 */
112 public function getRequest() {
113 return $this->request;
114 }
115
116 /**
117 * @return array
118 */
119 public function getModules() {
120 return $this->modules;
121 }
122
123 /**
124 * @return string
125 */
126 public function getLanguage() {
127 if ( $this->language === null ) {
128 global $wgLang;
129 $this->language = $this->request->getVal( 'lang' );
130 if ( !$this->language ) {
131 $this->language = $wgLang->getCode();
132 }
133 }
134 return $this->language;
135 }
136
137 /**
138 * @return string
139 */
140 public function getDirection() {
141 if ( $this->direction === null ) {
142 $this->direction = $this->request->getVal( 'dir' );
143 if ( !$this->direction ) {
144 global $wgContLang;
145 $this->direction = $wgContLang->getDir();
146 }
147 }
148 return $this->direction;
149 }
150
151 /**
152 * @return string
153 */
154 public function getSkin() {
155 return $this->skin;
156 }
157
158 /**
159 * @return string
160 */
161 public function getUser() {
162 return $this->user;
163 }
164
165 /**
166 * @return bool
167 */
168 public function getDebug() {
169 return $this->debug;
170 }
171
172 /**
173 * @return String
174 */
175 public function getOnly() {
176 return $this->only;
177 }
178
179 /**
180 * @return String
181 */
182 public function getVersion() {
183 return $this->version;
184 }
185
186 /**
187 * @return bool
188 */
189 public function shouldIncludeScripts() {
190 return is_null( $this->only ) || $this->only === 'scripts';
191 }
192
193 /**
194 * @return bool
195 */
196 public function shouldIncludeStyles() {
197 return is_null( $this->only ) || $this->only === 'styles';
198 }
199
200 /**
201 * @return bool
202 */
203 public function shouldIncludeMessages() {
204 return is_null( $this->only ) || $this->only === 'messages';
205 }
206
207 /**
208 * @return string
209 */
210 public function getHash() {
211 if ( !isset( $this->hash ) ) {
212 $this->hash = implode( '|', array(
213 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
214 $this->debug, $this->only, $this->version
215 ) );
216 }
217 return $this->hash;
218 }
219 }