3 * Group all the pieces relevant to the context of a request into one instance
6 * @author Daniel Friesen
10 class RequestContext
{
11 private $request; /// WebRequest object
12 private $title; /// Title object
13 private $output; /// OutputPage object
14 private $user; /// User object
15 private $lang; /// Language object
16 private $skin; /// Skin object
19 * Set the WebRequest object
21 * @param $r WebRequest object
23 public function setRequest( WebRequest
$r ) {
28 * Get the WebRequest object
32 public function getRequest() {
33 if ( !isset($this->request
) ) {
34 global $wgRequest; # fallback to $wg till we can improve this
35 $this->request
= $wgRequest;
37 return $this->request
;
41 * Set the Title object
43 * @param $t Title object
45 public function setTitle( Title
$t ) {
50 * Get the Title object
54 public function getTitle() {
55 if ( !isset($this->title
) ) {
56 global $wgTitle; # fallback to $wg till we can improve this
57 $this->title
= $wgTitle;
63 * Set the OutputPage object
65 * @param $u OutputPage
67 public function setOutput( OutputPage
$u ) {
72 * Get the OutputPage object
74 * @return OutputPage object
76 public function getOutput() {
77 if ( !isset($this->output
) ) {
78 $this->output
= new OutputPage
;
79 $this->output
->setContext( $this );
89 public function setUser( User
$u ) {
98 public function getUser() {
99 if ( !isset($this->user
) ) {
100 global $wgCommandLineMode;
101 $this->user
= $wgCommandLineMode ?
new User
: User
::newFromSession( $this->getRequest() );
107 * Get the Language object
111 public function getLang() {
112 if ( !isset($this->lang
) ) {
113 global $wgLanguageCode, $wgContLang;
114 $code = $this->getRequest()->getVal( 'uselang', $this->getUser()->getOption( 'language' ) );
115 // BCP 47 - letter case MUST NOT carry meaning
116 $code = strtolower( $code );
119 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
120 wfDebug( "Invalid user language code\n" );
121 $code = $wgLanguageCode;
124 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
126 if( $code === $wgLanguageCode ) {
127 $this->lang
= $wgContLang;
129 $obj = Language
::factory( $code );
137 * Get the Skin object
141 public function getSkin() {
142 if ( !isset($this->skin
) ) {
143 wfProfileIn( __METHOD__
. '-createskin' );
145 global $wgHiddenPrefs;
146 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
148 $userSkin = $this->getUser()->getOption( 'skin' );
149 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
151 # if we're not allowing users to override, then use the default
152 global $wgDefaultSkin;
153 $userSkin = $wgDefaultSkin;
156 $this->skin
= Skin
::newFromKey( $userSkin );
157 $this->skin
->setContext( $this );
158 wfProfileOut( __METHOD__
. '-createskin' );
163 /** Helpful methods **/
166 * Get a Message object with context set
167 * Parameters are the same as wfMessage()
169 * @return Message object
171 public function msg() {
172 $args = func_get_args();
173 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
176 /** Static methods **/
179 * Get the RequestContext object associated with the main request
181 * @return RequestContext object
183 public static function getMain() {
184 static $instance = null;
185 if ( !isset($instance) ) {
186 $instance = new self
;