Followup r95753 per CR: prevent extensions from making isMovable() return true for...
[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' )
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 $exploded = explode( '|', $modules );
77 foreach ( $exploded as $group ) {
78 if ( strpos( $group, ',' ) === false ) {
79 // This is not a set of modules in foo.bar,baz notation
80 // but a single module
81 $retval[] = $group;
82 } else {
83 // This is a set of modules in foo.bar,baz notation
84 $pos = strrpos( $group, '.' );
85 if ( $pos === false ) {
86 // Prefixless modules, i.e. without dots
87 $retval = explode( ',', $group );
88 } else {
89 // We have a prefix and a bunch of suffixes
90 $prefix = substr( $group, 0, $pos ); // 'foo'
91 $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
92 foreach ( $suffixes as $suffix ) {
93 $retval[] = "$prefix.$suffix";
94 }
95 }
96 }
97 }
98 return $retval;
99 }
100
101 /**
102 * @return ResourceLoader
103 */
104 public function getResourceLoader() {
105 return $this->resourceLoader;
106 }
107
108 /**
109 * @return WebRequest
110 */
111 public function getRequest() {
112 return $this->request;
113 }
114
115 /**
116 * @return array
117 */
118 public function getModules() {
119 return $this->modules;
120 }
121
122 /**
123 * @return string
124 */
125 public function getLanguage() {
126 if ( $this->language === null ) {
127 global $wgLang;
128 $this->language = $this->request->getVal( 'lang' );
129 if ( !$this->language ) {
130 $this->language = $wgLang->getCode();
131 }
132 }
133 return $this->language;
134 }
135
136 /**
137 * @return string
138 */
139 public function getDirection() {
140 if ( $this->direction === null ) {
141 $this->direction = $this->request->getVal( 'dir' );
142 if ( !$this->direction ) {
143 # directionality based on user language (see bug 6100)
144 $this->direction = Language::factory( $this->language )->getDir();
145 }
146 }
147 return $this->direction;
148 }
149
150 /**
151 * @return string
152 */
153 public function getSkin() {
154 return $this->skin;
155 }
156
157 /**
158 * @return string
159 */
160 public function getUser() {
161 return $this->user;
162 }
163
164 /**
165 * @return bool
166 */
167 public function getDebug() {
168 return $this->debug;
169 }
170
171 /**
172 * @return String
173 */
174 public function getOnly() {
175 return $this->only;
176 }
177
178 /**
179 * @return String
180 */
181 public function getVersion() {
182 return $this->version;
183 }
184
185 /**
186 * @return bool
187 */
188 public function shouldIncludeScripts() {
189 return is_null( $this->only ) || $this->only === 'scripts';
190 }
191
192 /**
193 * @return bool
194 */
195 public function shouldIncludeStyles() {
196 return is_null( $this->only ) || $this->only === 'styles';
197 }
198
199 /**
200 * @return bool
201 */
202 public function shouldIncludeMessages() {
203 return is_null( $this->only ) || $this->only === 'messages';
204 }
205
206 /**
207 * @return string
208 */
209 public function getHash() {
210 if ( !isset( $this->hash ) ) {
211 $this->hash = implode( '|', array(
212 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
213 $this->debug, $this->only, $this->version
214 ) );
215 }
216 return $this->hash;
217 }
218 }