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 $out; /// 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 * Get the OutputPage object
65 * @return OutputPage object
67 public function getOutput() {
68 if ( !isset($this->output
) ) {
69 $this->output
= new OutputPage
;
70 $this->output
->setContext( $this );
80 public function setUser( User
$u ) {
89 public function getUser() {
90 if ( !isset($this->user
) ) {
91 global $wgCommandLineMode;
92 $this->user
= $wgCommandLineMode ?
new User
: User
::newFromSession( $this->getRequest() );
98 * Get the Language object
102 public function getLang() {
103 if ( !isset($this->lang
) ) {
104 global $wgLanguageCode, $wgContLang;
105 $code = $this->getRequest()->getVal( 'uselang', $this->getUser()->getOption( 'language' ) );
106 // BCP 47 - letter case MUST NOT carry meaning
107 $code = strtolower( $code );
110 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
111 wfDebug( "Invalid user language code\n" );
112 $code = $wgLanguageCode;
115 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
117 if( $code === $wgLanguageCode ) {
118 $this->lang
= $wgContLang;
120 $obj = Language
::factory( $code );
128 * Get the Skin object
132 public function getSkin() {
133 if ( !isset($this->skin
) ) {
134 wfProfileIn( __METHOD__
. '-createskin' );
136 global $wgHiddenPrefs;
137 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
139 $userSkin = $this->getUser()->getOption( 'skin' );
140 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
142 # if we're not allowing users to override, then use the default
143 global $wgDefaultSkin;
144 $userSkin = $wgDefaultSkin;
147 $this->skin
= Skin
::newFromKey( $userSkin );
148 $this->skin
->setContext( $this );
149 wfProfileOut( __METHOD__
. '-createskin' );
154 /** Helpful methods **/
157 * Get a Message object with context set
158 * Parameters are the same as wfMessage()
160 * @return Message object
162 public function msg() {
163 $args = function_get_args();
164 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->outputPage( $this->getOut() );
167 /** Static methods **/
170 * Get the RequestContext object associated with the main request
172 * @return RequestContext object
174 public static function getMain() {
175 static $instance = null;
176 if ( !isset($instance) ) {
177 $instance = new self
;