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 * Set the Language object
133 public function setLang( Language
$l ) {
138 * Get the Language object
142 public function getLang() {
143 if ( $this->lang
=== null ) {
144 global $wgLanguageCode, $wgContLang;
145 $code = $this->getRequest()->getVal(
147 $this->getUser()->getOption( 'language' )
149 // BCP 47 - letter case MUST NOT carry meaning
150 $code = strtolower( $code );
153 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
154 wfDebug( "Invalid user language code\n" );
155 $code = $wgLanguageCode;
158 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
160 if( $code === $wgLanguageCode ) {
161 $this->lang
= $wgContLang;
163 $obj = Language
::factory( $code );
171 * Set the Skin object
175 public function setSkin( Skin
$s ) {
176 $this->skin
= clone $s;
177 $this->skin
->setContext( $this );
181 * Get the Skin object
185 public function getSkin() {
186 if ( $this->skin
=== null ) {
187 wfProfileIn( __METHOD__
. '-createskin' );
189 global $wgHiddenPrefs;
190 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
192 $userSkin = $this->getUser()->getOption( 'skin' );
193 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
195 # if we're not allowing users to override, then use the default
196 global $wgDefaultSkin;
197 $userSkin = $wgDefaultSkin;
200 $this->skin
= Skin
::newFromKey( $userSkin );
201 $this->skin
->setContext( $this );
202 wfProfileOut( __METHOD__
. '-createskin' );
207 /** Helpful methods **/
210 * Get a Message object with context set
211 * Parameters are the same as wfMessage()
213 * @return Message object
215 public function msg() {
216 $args = func_get_args();
217 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
220 /** Static methods **/
223 * Get the RequestContext object associated with the main request
225 * @return RequestContext object
227 public static function getMain() {
228 static $instance = null;
229 if ( $instance === null ) {
230 $instance = new self
;
237 * Interface for objects which can provide a context on request.
239 interface IContextSource
{
242 * Get the WebRequest object
246 public function getRequest();
249 * Get the Title object
253 public function getTitle();
256 * Get the OutputPage object
258 * @return OutputPage object
260 public function getOutput();
263 * Get the User object
267 public function getUser();
270 * Get the Language object
274 public function getLang();
277 * Get the Skin object
281 public function getSkin();
285 * The simplest way of implementing IContextSource is to hold a RequestContext as a
286 * member variable and provide accessors to it.
288 abstract class ContextSource
implements IContextSource
{
291 * @var RequestContext
296 * Get the RequestContext object
298 * @return RequestContext
300 public function getContext() {
301 return $this->context
;
305 * Set the RequestContext object
307 * @param $context RequestContext
309 public function setContext( RequestContext
$context ) {
310 $this->context
= $context;
314 * Get the WebRequest object
318 public function getRequest() {
319 return $this->context
->getRequest();
323 * Get the Title object
327 public function getTitle() {
328 return $this->context
->getTitle();
332 * Get the OutputPage object
334 * @return OutputPage object
336 public function getOutput() {
337 return $this->context
->getOutput();
341 * Get the User object
345 public function getUser() {
346 return $this->context
->getUser();
350 * Get the Language object
354 public function getLang() {
355 return $this->context
->getLang();
359 * Get the Skin object
363 public function getSkin() {
364 return $this->context
->getSkin();