3 * Group all the pieces relevant to the context of a request into one instance
8 * @author Daniel Friesen
12 class RequestContext
{
45 * Set the WebRequest object
47 * @param $r WebRequest object
49 public function setRequest( WebRequest
$r ) {
54 * Get the WebRequest object
58 public function getRequest() {
59 if ( !isset($this->request
) ) {
60 global $wgRequest; # fallback to $wg till we can improve this
61 $this->request
= $wgRequest;
63 return $this->request
;
67 * Set the Title object
69 * @param $t Title object
71 public function setTitle( Title
$t ) {
76 * Get the Title object
80 public function getTitle() {
81 if ( !isset($this->title
) ) {
82 global $wgTitle; # fallback to $wg till we can improve this
83 $this->title
= $wgTitle;
89 * @param $o OutputPage
91 public function setOutput( OutputPage
$o ) {
96 * Get the OutputPage object
98 * @return OutputPage object
100 public function getOutput() {
101 if ( !isset( $this->output
) ) {
102 $this->output
= new OutputPage( $this );
104 return $this->output
;
108 * Set the User object
112 public function setUser( User
$u ) {
117 * Get the User object
121 public function getUser() {
122 if ( !isset($this->user
) ) {
123 global $wgCommandLineMode;
124 $this->user
= $wgCommandLineMode
126 : User
::newFromSession( $this->getRequest() );
132 * Get the Language object
136 public function getLang() {
137 if ( !isset($this->lang
) ) {
138 global $wgLanguageCode, $wgContLang;
139 $code = $this->getRequest()->getVal(
141 $this->getUser()->getOption( 'language' )
143 // BCP 47 - letter case MUST NOT carry meaning
144 $code = strtolower( $code );
147 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
148 wfDebug( "Invalid user language code\n" );
149 $code = $wgLanguageCode;
152 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
154 if( $code === $wgLanguageCode ) {
155 $this->lang
= $wgContLang;
157 $obj = Language
::factory( $code );
165 * Get the Skin object
169 public function getSkin() {
170 if ( !isset($this->skin
) ) {
171 wfProfileIn( __METHOD__
. '-createskin' );
173 global $wgHiddenPrefs;
174 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
176 $userSkin = $this->getUser()->getOption( 'skin' );
177 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
179 # if we're not allowing users to override, then use the default
180 global $wgDefaultSkin;
181 $userSkin = $wgDefaultSkin;
184 $this->skin
= Skin
::newFromKey( $userSkin );
185 $this->skin
->setContext( $this );
186 wfProfileOut( __METHOD__
. '-createskin' );
191 /** Helpful methods **/
194 * Get a Message object with context set
195 * Parameters are the same as wfMessage()
197 * @return Message object
199 public function msg() {
200 $args = func_get_args();
201 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
204 /** Static methods **/
207 * Get the RequestContext object associated with the main request
209 * @return RequestContext object
211 public static function getMain() {
212 static $instance = null;
213 if ( !isset($instance) ) {
214 $instance = new self
;
221 * Interface for objects which can provide a context on request.
223 interface IContextSource
{
226 * Get the WebRequest object
230 public function getRequest();
233 * Get the Title object
237 public function getTitle();
240 * Get the OutputPage object
242 * @return OutputPage object
244 public function getOutput();
247 * Get the User object
251 public function getUser();
254 * Get the Language object
258 public function getLang();
261 * Get the Skin object
265 public function getSkin();
269 * The simplest way of implementing IContextSource is to hold a RequestContext as a
270 * member variable and provide accessors to it.
272 abstract class ContextSource
implements IContextSource
{
275 * @var RequestContext
280 * Get the RequestContext object
282 * @return RequestContext
284 public function getContext() {
285 return $this->context
;
289 * Set the RequestContext object
291 * @param $context RequestContext
293 public function setContext( RequestContext
$context ) {
294 $this->context
= $context;
298 * Get the WebRequest object
302 public function getRequest() {
303 return $this->context
->getRequest();
307 * Get the Title object
311 public function getTitle() {
312 return $this->context
->getTitle();
316 * Get the OutputPage object
318 * @return OutputPage object
320 public function getOutput() {
321 return $this->context
->getOutput();
325 * Get the User object
329 public function getUser() {
330 return $this->context
->getUser();
334 * Get the Language object
338 public function getLang() {
339 return $this->context
->getLang();
343 * Get the Skin object
347 public function getSkin() {
348 return $this->context
->getSkin();