Merge "prop=duplicatefiles does not show duplicates under same name"
[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
31 /* Protected Members */
32
33 protected $resourceLoader;
34 protected $request;
35 protected $modules;
36 protected $language;
37 protected $direction;
38 protected $skin;
39 protected $user;
40 protected $debug;
41 protected $only;
42 protected $version;
43 protected $hash;
44 protected $raw;
45
46 /* Methods */
47
48 /**
49 * @param $resourceLoader ResourceLoader
50 * @param $request WebRequest
51 */
52 public function __construct( $resourceLoader, WebRequest $request ) {
53 global $wgDefaultSkin, $wgResourceLoaderDebug;
54
55 $this->resourceLoader = $resourceLoader;
56 $this->request = $request;
57
58 // Interpret request
59 // List of modules
60 $modules = $request->getVal( 'modules' );
61 $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
62 // Various parameters
63 $this->skin = $request->getVal( 'skin' );
64 $this->user = $request->getVal( 'user' );
65 $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
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 = $wgDefaultSkin;
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 $modules String Packed module name list
82 * @return array of module names
83 */
84 public static function expandModuleNames( $modules ) {
85 $retval = array();
86 // For backwards compatibility with an earlier hack, replace ! with .
87 $modules = str_replace( '!', '.', $modules );
88 $exploded = explode( '|', $modules );
89 foreach ( $exploded as $group ) {
90 if ( strpos( $group, ',' ) === false ) {
91 // This is not a set of modules in foo.bar,baz notation
92 // but a single module
93 $retval[] = $group;
94 } else {
95 // This is a set of modules in foo.bar,baz notation
96 $pos = strrpos( $group, '.' );
97 if ( $pos === false ) {
98 // Prefixless modules, i.e. without dots
99 $retval = explode( ',', $group );
100 } else {
101 // We have a prefix and a bunch of suffixes
102 $prefix = substr( $group, 0, $pos ); // 'foo'
103 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
104 foreach ( $suffixes as $suffix ) {
105 $retval[] = "$prefix.$suffix";
106 }
107 }
108 }
109 }
110 return $retval;
111 }
112
113 /**
114 * Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need a context
115 * @return ResourceLoaderContext
116 */
117 public static function newDummyContext() {
118 return new self( null, new FauxRequest( array() ) );
119 }
120
121 /**
122 * @return ResourceLoader
123 */
124 public function getResourceLoader() {
125 return $this->resourceLoader;
126 }
127
128 /**
129 * @return WebRequest
130 */
131 public function getRequest() {
132 return $this->request;
133 }
134
135 /**
136 * @return array
137 */
138 public function getModules() {
139 return $this->modules;
140 }
141
142 /**
143 * @return string
144 */
145 public function getLanguage() {
146 if ( $this->language === null ) {
147 global $wgLang;
148 $this->language = $this->request->getVal( 'lang' );
149 if ( !$this->language ) {
150 $this->language = $wgLang->getCode();
151 }
152 }
153 return $this->language;
154 }
155
156 /**
157 * @return string
158 */
159 public function getDirection() {
160 if ( $this->direction === null ) {
161 $this->direction = $this->request->getVal( 'dir' );
162 if ( !$this->direction ) {
163 # directionality based on user language (see bug 6100)
164 $this->direction = Language::factory( $this->getLanguage() )->getDir();
165 }
166 }
167 return $this->direction;
168 }
169
170 /**
171 * @return string|null
172 */
173 public function getSkin() {
174 return $this->skin;
175 }
176
177 /**
178 * @return string|null
179 */
180 public function getUser() {
181 return $this->user;
182 }
183
184 /**
185 * @return bool
186 */
187 public function getDebug() {
188 return $this->debug;
189 }
190
191 /**
192 * @return String|null
193 */
194 public function getOnly() {
195 return $this->only;
196 }
197
198 /**
199 * @return String|null
200 */
201 public function getVersion() {
202 return $this->version;
203 }
204
205 /**
206 * @return bool
207 */
208 public function getRaw() {
209 return $this->raw;
210 }
211
212 /**
213 * @return bool
214 */
215 public function shouldIncludeScripts() {
216 return is_null( $this->only ) || $this->only === 'scripts';
217 }
218
219 /**
220 * @return bool
221 */
222 public function shouldIncludeStyles() {
223 return is_null( $this->only ) || $this->only === 'styles';
224 }
225
226 /**
227 * @return bool
228 */
229 public function shouldIncludeMessages() {
230 return is_null( $this->only ) || $this->only === 'messages';
231 }
232
233 /**
234 * @return string
235 */
236 public function getHash() {
237 if ( !isset( $this->hash ) ) {
238 $this->hash = implode( '|', array(
239 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
240 $this->debug, $this->only, $this->version
241 ) );
242 }
243 return $this->hash;
244 }
245 }