Added typehint per my own suggestion in bug 28827. Otherwise we get pretty useless...
[mediawiki.git] / includes / RequestContext.php
blobc0b49a1e8aafa9149b1c7177db3ee552982b18ea
1 <?php
2 /**
3 * Request-dependant objects containers.
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.
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.
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
20 * @since 1.18
22 * @author Alexandre Emsenhuber
23 * @author Daniel Friesen
24 * @file
27 /**
28 * Interface for objects which can provide a context on request.
30 interface IContextSource {
32 /**
33 * Get the WebRequest object
35 * @return WebRequest
37 public function getRequest();
39 /**
40 * Get the Title object
42 * @return Title
44 public function getTitle();
46 /**
47 * Get the OutputPage object
49 * @return OutputPage object
51 public function getOutput();
53 /**
54 * Get the User object
56 * @return User
58 public function getUser();
60 /**
61 * Get the Language object
63 * @return Language
65 public function getLang();
67 /**
68 * Get the Skin object
70 * @return Skin
72 public function getSkin();
75 /**
76 * Group all the pieces relevant to the context of a request into one instance
78 class RequestContext implements IContextSource {
80 /**
81 * @var WebRequest
83 private $request;
85 /**
86 * @var Title
88 private $title;
90 /**
91 * @var OutputPage
93 private $output;
95 /**
96 * @var User
98 private $user;
101 * @var Language
103 private $lang;
106 * @var Skin
108 private $skin;
111 * Set the WebRequest object
113 * @param $r WebRequest object
115 public function setRequest( WebRequest $r ) {
116 $this->request = $r;
120 * Get the WebRequest object
122 * @return WebRequest
124 public function getRequest() {
125 if ( $this->request === null ) {
126 global $wgRequest; # fallback to $wg till we can improve this
127 $this->request = $wgRequest;
129 return $this->request;
133 * Set the Title object
135 * @param $t Title object
137 public function setTitle( Title $t ) {
138 $this->title = $t;
142 * Get the Title object
144 * @return Title
146 public function getTitle() {
147 if ( $this->title === null ) {
148 global $wgTitle; # fallback to $wg till we can improve this
149 $this->title = $wgTitle;
151 return $this->title;
155 * @param $o OutputPage
157 public function setOutput( OutputPage $o ) {
158 $this->output = $o;
162 * Get the OutputPage object
164 * @return OutputPage object
166 public function getOutput() {
167 if ( $this->output === null ) {
168 $this->output = new OutputPage( $this );
170 return $this->output;
174 * Set the User object
176 * @param $u User
178 public function setUser( User $u ) {
179 $this->user = $u;
183 * Get the User object
185 * @return User
187 public function getUser() {
188 if ( $this->user === null ) {
189 $this->user = User::newFromSession( $this->getRequest() );
191 return $this->user;
195 * Set the Language object
197 * @param $l Language
199 public function setLang( Language $l ) {
200 $this->lang = $l;
204 * Get the Language object
206 * @return Language
208 public function getLang() {
209 if ( $this->lang === null ) {
210 global $wgLanguageCode, $wgContLang;
211 $code = $this->getRequest()->getVal(
212 'uselang',
213 $this->getUser()->getOption( 'language' )
215 // BCP 47 - letter case MUST NOT carry meaning
216 $code = strtolower( $code );
218 # Validate $code
219 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
220 wfDebug( "Invalid user language code\n" );
221 $code = $wgLanguageCode;
224 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
226 if( $code === $wgLanguageCode ) {
227 $this->lang = $wgContLang;
228 } else {
229 $obj = Language::factory( $code );
230 $this->lang = $obj;
233 return $this->lang;
237 * Set the Skin object
239 * @param $s Skin
241 public function setSkin( Skin $s ) {
242 $this->skin = clone $s;
243 $this->skin->setContext( $this );
247 * Get the Skin object
249 * @return Skin
251 public function getSkin() {
252 if ( $this->skin === null ) {
253 wfProfileIn( __METHOD__ . '-createskin' );
255 global $wgHiddenPrefs;
256 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
257 # get the user skin
258 $userSkin = $this->getUser()->getOption( 'skin' );
259 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
260 } else {
261 # if we're not allowing users to override, then use the default
262 global $wgDefaultSkin;
263 $userSkin = $wgDefaultSkin;
266 $this->skin = Skin::newFromKey( $userSkin );
267 $this->skin->setContext( $this );
268 wfProfileOut( __METHOD__ . '-createskin' );
270 return $this->skin;
273 /** Helpful methods **/
276 * Get a Message object with context set
277 * Parameters are the same as wfMessage()
279 * @return Message object
281 public function msg() {
282 $args = func_get_args();
283 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
286 /** Static methods **/
289 * Get the RequestContext object associated with the main request
291 * @return RequestContext object
293 public static function getMain() {
294 static $instance = null;
295 if ( $instance === null ) {
296 $instance = new self;
298 return $instance;
303 * The simplest way of implementing IContextSource is to hold a RequestContext as a
304 * member variable and provide accessors to it.
306 abstract class ContextSource implements IContextSource {
309 * @var RequestContext
311 private $context;
314 * Get the RequestContext object
316 * @return RequestContext
318 public function getContext() {
319 if ( $this->context === null ) {
320 $class = get_class( $this );
321 wfDebug( __METHOD__ . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
322 $this->context = RequestContext::getMain();
324 return $this->context;
328 * Set the RequestContext object
330 * @param $context RequestContext
332 public function setContext( RequestContext $context ) {
333 $this->context = $context;
337 * Get the WebRequest object
339 * @return WebRequest
341 public function getRequest() {
342 return $this->getContext()->getRequest();
346 * Get the Title object
348 * @return Title
350 public function getTitle() {
351 return $this->getContext()->getTitle();
355 * Get the OutputPage object
357 * @return OutputPage object
359 public function getOutput() {
360 return $this->getContext()->getOutput();
364 * Get the User object
366 * @return User
368 public function getUser() {
369 return $this->getContext()->getUser();
373 * Get the Language object
375 * @return Language
377 public function getLang() {
378 return $this->getContext()->getLang();
382 * Get the Skin object
384 * @return Skin
386 public function getSkin() {
387 return $this->getContext()->getSkin();
391 * Get a Message object with context set
392 * Parameters are the same as wfMessage()
394 * @return Message object
396 public function msg( /* $args */ ) {
397 return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );