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 ( $this->request
=== null ) {
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 ( $this->title
=== null ) {
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 ( $this->output
=== null ) {
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 ( $this->user
=== null ) {
123 $this->user
= User
::newFromSession( $this->getRequest() );
129 * Get the Language object
133 public function getLang() {
134 if ( $this->lang
=== null ) {
135 global $wgLanguageCode, $wgContLang;
136 $code = $this->getRequest()->getVal(
138 $this->getUser()->getOption( 'language' )
140 // BCP 47 - letter case MUST NOT carry meaning
141 $code = strtolower( $code );
144 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
145 wfDebug( "Invalid user language code\n" );
146 $code = $wgLanguageCode;
149 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
151 if( $code === $wgLanguageCode ) {
152 $this->lang
= $wgContLang;
154 $obj = Language
::factory( $code );
162 * Get the Skin object
166 public function getSkin() {
167 if ( $this->skin
=== null ) {
168 wfProfileIn( __METHOD__
. '-createskin' );
170 global $wgHiddenPrefs;
171 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
173 $userSkin = $this->getUser()->getOption( 'skin' );
174 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
176 # if we're not allowing users to override, then use the default
177 global $wgDefaultSkin;
178 $userSkin = $wgDefaultSkin;
181 $this->skin
= Skin
::newFromKey( $userSkin );
182 $this->skin
->setContext( $this );
183 wfProfileOut( __METHOD__
. '-createskin' );
188 /** Helpful methods **/
191 * Get a Message object with context set
192 * Parameters are the same as wfMessage()
194 * @return Message object
196 public function msg() {
197 $args = func_get_args();
198 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
201 /** Static methods **/
204 * Get the RequestContext object associated with the main request
206 * @return RequestContext object
208 public static function getMain() {
209 static $instance = null;
210 if ( $instance === null ) {
211 $instance = new self
;
218 * Interface for objects which can provide a context on request.
220 interface IContextSource
{
223 * Get the WebRequest object
227 public function getRequest();
230 * Get the Title object
234 public function getTitle();
237 * Get the OutputPage object
239 * @return OutputPage object
241 public function getOutput();
244 * Get the User object
248 public function getUser();
251 * Get the Language object
255 public function getLang();
258 * Get the Skin object
262 public function getSkin();
266 * The simplest way of implementing IContextSource is to hold a RequestContext as a
267 * member variable and provide accessors to it.
269 abstract class ContextSource
implements IContextSource
{
272 * @var RequestContext
277 * Get the RequestContext object
279 * @return RequestContext
281 public function getContext() {
282 return $this->context
;
286 * Set the RequestContext object
288 * @param $context RequestContext
290 public function setContext( RequestContext
$context ) {
291 $this->context
= $context;
295 * Get the WebRequest object
299 public function getRequest() {
300 return $this->context
->getRequest();
304 * Get the Title object
308 public function getTitle() {
309 return $this->context
->getTitle();
313 * Get the OutputPage object
315 * @return OutputPage object
317 public function getOutput() {
318 return $this->context
->getOutput();
322 * Get the User object
326 public function getUser() {
327 return $this->context
->getUser();
331 * Get the Language object
335 public function getLang() {
336 return $this->context
->getLang();
340 * Get the Skin object
344 public function getSkin() {
345 return $this->context
->getSkin();