Use title accessor
[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 ( !isset($this->request) ) {
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 ( !isset($this->title) ) {
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 ( !isset( $this->output ) ) {
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 ( !isset($this->user) ) {
123 global $wgCommandLineMode;
124 $this->user = $wgCommandLineMode
125 ? new User
126 : User::newFromSession( $this->getRequest() );
127 }
128 return $this->user;
129 }
130
131 /**
132 * Get the Language object
133 *
134 * @return Language
135 */
136 public function getLang() {
137 if ( !isset($this->lang) ) {
138 global $wgLanguageCode, $wgContLang;
139 $code = $this->getRequest()->getVal(
140 'uselang',
141 $this->getUser()->getOption( 'language' )
142 );
143 // BCP 47 - letter case MUST NOT carry meaning
144 $code = strtolower( $code );
145
146 # Validate $code
147 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
148 wfDebug( "Invalid user language code\n" );
149 $code = $wgLanguageCode;
150 }
151
152 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
153
154 if( $code === $wgLanguageCode ) {
155 $this->lang = $wgContLang;
156 } else {
157 $obj = Language::factory( $code );
158 $this->lang = $obj;
159 }
160 }
161 return $this->lang;
162 }
163
164 /**
165 * Get the Skin object
166 *
167 * @return Skin
168 */
169 public function getSkin() {
170 if ( !isset($this->skin) ) {
171 wfProfileIn( __METHOD__ . '-createskin' );
172
173 global $wgHiddenPrefs;
174 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
175 # get the user skin
176 $userSkin = $this->getUser()->getOption( 'skin' );
177 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
178 } else {
179 # if we're not allowing users to override, then use the default
180 global $wgDefaultSkin;
181 $userSkin = $wgDefaultSkin;
182 }
183
184 $this->skin = Skin::newFromKey( $userSkin );
185 $this->skin->setContext( $this );
186 wfProfileOut( __METHOD__ . '-createskin' );
187 }
188 return $this->skin;
189 }
190
191 /** Helpful methods **/
192
193 /**
194 * Get a Message object with context set
195 * Parameters are the same as wfMessage()
196 *
197 * @return Message object
198 */
199 public function msg() {
200 $args = func_get_args();
201 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
202 }
203
204 /** Static methods **/
205
206 /**
207 * Get the RequestContext object associated with the main request
208 *
209 * @return RequestContext object
210 */
211 public static function getMain() {
212 static $instance = null;
213 if ( !isset($instance) ) {
214 $instance = new self;
215 }
216 return $instance;
217 }
218 }
219
220 /**
221 * Interface for objects which can provide a context on request.
222 */
223 interface IContextSource {
224
225 /**
226 * Get the WebRequest object
227 *
228 * @return WebRequest
229 */
230 public function getRequest();
231
232 /**
233 * Get the Title object
234 *
235 * @return Title
236 */
237 public function getTitle();
238
239 /**
240 * Get the OutputPage object
241 *
242 * @return OutputPage object
243 */
244 public function getOutput();
245
246 /**
247 * Get the User object
248 *
249 * @return User
250 */
251 public function getUser();
252
253 /**
254 * Get the Language object
255 *
256 * @return Language
257 */
258 public function getLang();
259
260 /**
261 * Get the Skin object
262 *
263 * @return Skin
264 */
265 public function getSkin();
266 }
267
268 /**
269 * The simplest way of implementing IContextSource is to hold a RequestContext as a
270 * member variable and provide accessors to it.
271 */
272 abstract class ContextSource implements IContextSource {
273
274 /**
275 * @var RequestContext
276 */
277 private $context;
278
279 /**
280 * Get the RequestContext object
281 *
282 * @return RequestContext
283 */
284 public function getContext() {
285 return $this->context;
286 }
287
288 /**
289 * Set the RequestContext object
290 *
291 * @param $context RequestContext
292 */
293 public function setContext( RequestContext $context ) {
294 $this->context = $context;
295 }
296
297 /**
298 * Get the WebRequest object
299 *
300 * @return WebRequest
301 */
302 public function getRequest() {
303 return $this->context->getRequest();
304 }
305
306 /**
307 * Get the Title object
308 *
309 * @return Title
310 */
311 public function getTitle() {
312 return $this->context->getTitle();
313 }
314
315 /**
316 * Get the OutputPage object
317 *
318 * @return OutputPage object
319 */
320 public function getOutput() {
321 return $this->context->getOutput();
322 }
323
324 /**
325 * Get the User object
326 *
327 * @return User
328 */
329 public function getUser() {
330 return $this->context->getUser();
331 }
332
333 /**
334 * Get the Language object
335 *
336 * @return Language
337 */
338 public function getLang() {
339 return $this->context->getLang();
340 }
341
342 /**
343 * Get the Skin object
344 *
345 * @return Skin
346 */
347 public function getSkin() {
348 return $this->context->getSkin();
349 }
350 }