3 * Request-dependant objects containers.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 * @author Alexandre Emsenhuber
23 * @author Daniel Friesen
28 * Group all the pieces relevant to the context of a request into one instance
30 class RequestContext
implements IContextSource
{
67 * Set the WebRequest object
69 * @param WebRequest $r
71 public function setRequest( WebRequest
$r ) {
76 * Get the WebRequest object
80 public function getRequest() {
81 if ( $this->request
=== null ) {
82 global $wgRequest; # fallback to $wg till we can improve this
83 $this->request
= $wgRequest;
85 return $this->request
;
89 * Set the Title object
93 public function setTitle( Title
$t ) {
95 // Erase the WikiPage so a new one with the new title gets created.
96 $this->wikipage
= null;
100 * Get the Title object
104 public function getTitle() {
105 if ( $this->title
=== null ) {
106 global $wgTitle; # fallback to $wg till we can improve this
107 $this->title
= $wgTitle;
113 * Check whether a WikiPage object can be get with getWikiPage().
114 * Callers should expect that an exception is thrown from getWikiPage()
115 * if this method returns false.
120 public function canUseWikiPage() {
121 if ( $this->wikipage
!== null ) {
122 # If there's a WikiPage object set, we can for sure get it
125 $title = $this->getTitle();
126 if ( $title === null ) {
127 # No Title, no WikiPage
130 # Only namespaces whose pages are stored in the database can have WikiPage
131 return $title->canExist();
136 * Set the WikiPage object
141 public function setWikiPage( WikiPage
$p ) {
142 $contextTitle = $this->getTitle();
143 $pageTitle = $p->getTitle();
144 if ( !$contextTitle ||
!$pageTitle->equals( $contextTitle ) ) {
145 $this->setTitle( $pageTitle );
147 // Defer this to the end since setTitle sets it to null.
148 $this->wikipage
= $p;
152 * Get the WikiPage object.
153 * May throw an exception if there's no Title object set or the Title object
154 * belongs to a special namespace that doesn't have WikiPage, so use first
155 * canUseWikiPage() to check whether this method can be called safely.
158 * @throws MWException
161 public function getWikiPage() {
162 if ( $this->wikipage
=== null ) {
163 $title = $this->getTitle();
164 if ( $title === null ) {
165 throw new MWException( __METHOD__
. ' called without Title object set' );
167 $this->wikipage
= WikiPage
::factory( $title );
169 return $this->wikipage
;
173 * @param $o OutputPage
175 public function setOutput( OutputPage
$o ) {
180 * Get the OutputPage object
184 public function getOutput() {
185 if ( $this->output
=== null ) {
186 $this->output
= new OutputPage( $this );
188 return $this->output
;
192 * Set the User object
196 public function setUser( User
$u ) {
201 * Get the User object
205 public function getUser() {
206 if ( $this->user
=== null ) {
207 $this->user
= User
::newFromSession( $this->getRequest() );
213 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
214 * code and replaces with $wgLanguageCode if not sane.
215 * @param string $code Language code
218 public static function sanitizeLangCode( $code ) {
219 global $wgLanguageCode;
221 // BCP 47 - letter case MUST NOT carry meaning
222 $code = strtolower( $code );
225 if ( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
226 wfDebug( "Invalid user language code\n" );
227 $code = $wgLanguageCode;
234 * Set the Language object
236 * @deprecated 1.19 Use setLanguage instead
237 * @param Language|string $l Language instance or language code
239 public function setLang( $l ) {
240 wfDeprecated( __METHOD__
, '1.19' );
241 $this->setLanguage( $l );
245 * Set the Language object
247 * @param Language|string $l Language instance or language code
248 * @throws MWException
251 public function setLanguage( $l ) {
252 if ( $l instanceof Language
) {
254 } elseif ( is_string( $l ) ) {
255 $l = self
::sanitizeLangCode( $l );
256 $obj = Language
::factory( $l );
259 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
264 * @deprecated 1.19 Use getLanguage instead
267 public function getLang() {
268 wfDeprecated( __METHOD__
, '1.19' );
269 return $this->getLanguage();
273 * Get the Language object.
274 * Initialization of user or request objects can depend on this.
279 public function getLanguage() {
280 if ( isset( $this->recursion
) ) {
281 throw new MWException( 'Recursion detected' );
284 if ( $this->lang
=== null ) {
285 $this->recursion
= true;
287 global $wgLanguageCode, $wgContLang;
289 $request = $this->getRequest();
290 $user = $this->getUser();
292 $code = $request->getVal( 'uselang', $user->getOption( 'language' ) );
293 $code = self
::sanitizeLangCode( $code );
295 wfRunHooks( 'UserGetLanguageObject', array( $user, &$code, $this ) );
297 if ( $code === $wgLanguageCode ) {
298 $this->lang
= $wgContLang;
300 $obj = Language
::factory( $code );
304 unset( $this->recursion
);
311 * Set the Skin object
315 public function setSkin( Skin
$s ) {
316 $this->skin
= clone $s;
317 $this->skin
->setContext( $this );
321 * Get the Skin object
325 public function getSkin() {
326 if ( $this->skin
=== null ) {
327 wfProfileIn( __METHOD__
. '-createskin' );
330 wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
332 // If the hook worked try to set a skin from it
333 if ( $skin instanceof Skin
) {
335 } elseif ( is_string( $skin ) ) {
336 $this->skin
= Skin
::newFromKey( $skin );
339 // If this is still null (the hook didn't run or didn't work)
340 // then go through the normal processing to load a skin
341 if ( $this->skin
=== null ) {
342 global $wgHiddenPrefs;
343 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
345 $userSkin = $this->getUser()->getOption( 'skin' );
346 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
348 # if we're not allowing users to override, then use the default
349 global $wgDefaultSkin;
350 $userSkin = $wgDefaultSkin;
353 $this->skin
= Skin
::newFromKey( $userSkin );
356 // After all that set a context on whatever skin got created
357 $this->skin
->setContext( $this );
358 wfProfileOut( __METHOD__
. '-createskin' );
363 /** Helpful methods **/
366 * Get a Message object with context set
367 * Parameters are the same as wfMessage()
371 public function msg() {
372 $args = func_get_args();
373 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
376 /** Static methods **/
379 * Get the RequestContext object associated with the main request
381 * @return RequestContext
383 public static function getMain() {
384 static $instance = null;
385 if ( $instance === null ) {
386 $instance = new self
;
392 * Create a new extraneous context. The context is filled with information
393 * external to the current session.
394 * - Title is specified by argument
395 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
396 * - User is an anonymous user, for separation IPv4 localhost is used
397 * - Language will be based on the anonymous user and request, may be content
398 * language or a uselang param in the fauxrequest data may change the lang
399 * - Skin will be based on the anonymous user, should be the wiki's default skin
401 * @param Title $title Title to use for the extraneous request
402 * @param WebRequest|array $request A WebRequest or data to use for a FauxRequest
403 * @return RequestContext
405 public static function newExtraneousContext( Title
$title, $request = array() ) {
407 $context->setTitle( $title );
408 if ( $request instanceof WebRequest
) {
409 $context->setRequest( $request );
411 $context->setRequest( new FauxRequest( $request ) );
413 $context->user
= User
::newFromName( '127.0.0.1', false );