Follow-up r88054: register the file if a hook changed the target file.
[lhc/web/wiklou.git] / includes / RequestContext.php
1 <?php
2 /**
3 * Group all the pieces relevant to the context of a request into one instance
4 *
5 * @since 1.18
6 *
7 * @author IAlex
8 * @author Daniel Friesen
9 * @file
10 */
11
12 class RequestContext {
13
14 /**
15 * @var WebRequest
16 */
17 private $request;
18
19 /**
20 * @var Title
21 */
22 private $title;
23
24 /**
25 * @var OutputPage
26 */
27 private $output;
28
29 /**
30 * @var User
31 */
32 private $user;
33
34 /**
35 * @var Language
36 */
37 private $lang;
38
39 /**
40 * @var Skin
41 */
42 private $skin;
43
44 /**
45 * Set the WebRequest object
46 *
47 * @param $r WebRequest object
48 */
49 public function setRequest( WebRequest $r ) {
50 $this->request = $r;
51 }
52
53 /**
54 * Get the WebRequest object
55 *
56 * @return WebRequest
57 */
58 public function getRequest() {
59 if ( $this->request === null ) {
60 global $wgRequest; # fallback to $wg till we can improve this
61 $this->request = $wgRequest;
62 }
63 return $this->request;
64 }
65
66 /**
67 * Set the Title object
68 *
69 * @param $t Title object
70 */
71 public function setTitle( Title $t ) {
72 $this->title = $t;
73 }
74
75 /**
76 * Get the Title object
77 *
78 * @return Title
79 */
80 public function getTitle() {
81 if ( $this->title === null ) {
82 global $wgTitle; # fallback to $wg till we can improve this
83 $this->title = $wgTitle;
84 }
85 return $this->title;
86 }
87
88 /**
89 * @param $o OutputPage
90 */
91 public function setOutput( OutputPage $o ) {
92 $this->output = $o;
93 }
94
95 /**
96 * Get the OutputPage object
97 *
98 * @return OutputPage object
99 */
100 public function getOutput() {
101 if ( $this->output === null ) {
102 $this->output = new OutputPage( $this );
103 }
104 return $this->output;
105 }
106
107 /**
108 * Set the User object
109 *
110 * @param $u User
111 */
112 public function setUser( User $u ) {
113 $this->user = $u;
114 }
115
116 /**
117 * Get the User object
118 *
119 * @return User
120 */
121 public function getUser() {
122 if ( $this->user === null ) {
123 $this->user = User::newFromSession( $this->getRequest() );
124 }
125 return $this->user;
126 }
127
128 /**
129 * Get the Language object
130 *
131 * @return Language
132 */
133 public function getLang() {
134 if ( $this->lang === null ) {
135 global $wgLanguageCode, $wgContLang;
136 $code = $this->getRequest()->getVal(
137 'uselang',
138 $this->getUser()->getOption( 'language' )
139 );
140 // BCP 47 - letter case MUST NOT carry meaning
141 $code = strtolower( $code );
142
143 # Validate $code
144 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
145 wfDebug( "Invalid user language code\n" );
146 $code = $wgLanguageCode;
147 }
148
149 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
150
151 if( $code === $wgLanguageCode ) {
152 $this->lang = $wgContLang;
153 } else {
154 $obj = Language::factory( $code );
155 $this->lang = $obj;
156 }
157 }
158 return $this->lang;
159 }
160
161 /**
162 * Get the Skin object
163 *
164 * @return Skin
165 */
166 public function getSkin() {
167 if ( $this->skin === null ) {
168 wfProfileIn( __METHOD__ . '-createskin' );
169
170 global $wgHiddenPrefs;
171 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
172 # get the user skin
173 $userSkin = $this->getUser()->getOption( 'skin' );
174 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
175 } else {
176 # if we're not allowing users to override, then use the default
177 global $wgDefaultSkin;
178 $userSkin = $wgDefaultSkin;
179 }
180
181 $this->skin = Skin::newFromKey( $userSkin );
182 $this->skin->setContext( $this );
183 wfProfileOut( __METHOD__ . '-createskin' );
184 }
185 return $this->skin;
186 }
187
188 /** Helpful methods **/
189
190 /**
191 * Get a Message object with context set
192 * Parameters are the same as wfMessage()
193 *
194 * @return Message object
195 */
196 public function msg() {
197 $args = func_get_args();
198 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
199 }
200
201 /** Static methods **/
202
203 /**
204 * Get the RequestContext object associated with the main request
205 *
206 * @return RequestContext object
207 */
208 public static function getMain() {
209 static $instance = null;
210 if ( $instance === null ) {
211 $instance = new self;
212 }
213 return $instance;
214 }
215 }
216
217 /**
218 * Interface for objects which can provide a context on request.
219 */
220 interface IContextSource {
221
222 /**
223 * Get the WebRequest object
224 *
225 * @return WebRequest
226 */
227 public function getRequest();
228
229 /**
230 * Get the Title object
231 *
232 * @return Title
233 */
234 public function getTitle();
235
236 /**
237 * Get the OutputPage object
238 *
239 * @return OutputPage object
240 */
241 public function getOutput();
242
243 /**
244 * Get the User object
245 *
246 * @return User
247 */
248 public function getUser();
249
250 /**
251 * Get the Language object
252 *
253 * @return Language
254 */
255 public function getLang();
256
257 /**
258 * Get the Skin object
259 *
260 * @return Skin
261 */
262 public function getSkin();
263 }
264
265 /**
266 * The simplest way of implementing IContextSource is to hold a RequestContext as a
267 * member variable and provide accessors to it.
268 */
269 abstract class ContextSource implements IContextSource {
270
271 /**
272 * @var RequestContext
273 */
274 private $context;
275
276 /**
277 * Get the RequestContext object
278 *
279 * @return RequestContext
280 */
281 public function getContext() {
282 return $this->context;
283 }
284
285 /**
286 * Set the RequestContext object
287 *
288 * @param $context RequestContext
289 */
290 public function setContext( RequestContext $context ) {
291 $this->context = $context;
292 }
293
294 /**
295 * Get the WebRequest object
296 *
297 * @return WebRequest
298 */
299 public function getRequest() {
300 return $this->context->getRequest();
301 }
302
303 /**
304 * Get the Title object
305 *
306 * @return Title
307 */
308 public function getTitle() {
309 return $this->context->getTitle();
310 }
311
312 /**
313 * Get the OutputPage object
314 *
315 * @return OutputPage object
316 */
317 public function getOutput() {
318 return $this->context->getOutput();
319 }
320
321 /**
322 * Get the User object
323 *
324 * @return User
325 */
326 public function getUser() {
327 return $this->context->getUser();
328 }
329
330 /**
331 * Get the Language object
332 *
333 * @return Language
334 */
335 public function getLang() {
336 return $this->context->getLang();
337 }
338
339 /**
340 * Get the Skin object
341 *
342 * @return Skin
343 */
344 public function getSkin() {
345 return $this->context->getSkin();
346 }
347 }