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
{
68 * Set the WebRequest object
70 * @param $r WebRequest object
72 public function setRequest( WebRequest
$r ) {
77 * Get the WebRequest object
81 public function getRequest() {
82 if ( $this->request
=== null ) {
83 global $wgRequest; # fallback to $wg till we can improve this
84 $this->request
= $wgRequest;
86 return $this->request
;
90 * Set the Title object
92 * @param $t Title object
94 public function setTitle( Title
$t ) {
99 * Get the Title object
103 public function getTitle() {
104 if ( $this->title
=== null ) {
105 global $wgTitle; # fallback to $wg till we can improve this
106 $this->title
= $wgTitle;
112 * Check whether a WikiPage object can be get with getWikiPage().
113 * Callers should expect that an exception is thrown from getWikiPage()
114 * if this method returns false.
119 public function canUseWikiPage() {
120 if ( $this->wikipage
!== null ) {
121 # If there's a WikiPage object set, we can for sure get it
124 $title = $this->getTitle();
125 if ( $title === null ) {
126 # No Title, no WikiPage
129 # Only namespaces whose pages are stored in the database can have WikiPage
130 return $title->canExist();
135 * Set the WikiPage object
138 * @param $p WikiPage object
140 public function setWikiPage( WikiPage
$p ) {
141 $this->wikipage
= $p;
145 * Get the WikiPage object.
146 * May throw an exception if there's no Title object set or the Title object
147 * belongs to a special namespace that doesn't have WikiPage, so use first
148 * canUseWikiPage() to check whether this method can be called safely.
151 * @throws MWException
154 public function getWikiPage() {
155 if ( $this->wikipage
=== null ) {
156 $title = $this->getTitle();
157 if ( $title === null ) {
158 throw new MWException( __METHOD__
. ' called without Title object set' );
160 $this->wikipage
= WikiPage
::factory( $title );
162 return $this->wikipage
;
166 * @param $o OutputPage
168 public function setOutput( OutputPage
$o ) {
173 * Get the OutputPage object
175 * @return OutputPage object
177 public function getOutput() {
178 if ( $this->output
=== null ) {
179 $this->output
= new OutputPage( $this );
181 return $this->output
;
185 * Set the User object
189 public function setUser( User
$u ) {
194 * Get the User object
198 public function getUser() {
199 if ( $this->user
=== null ) {
200 $this->user
= User
::newFromSession( $this->getRequest() );
206 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
207 * code and replaces with $wgLanguageCode if not sane.
208 * @param $code string
211 public static function sanitizeLangCode( $code ) {
212 global $wgLanguageCode;
214 // BCP 47 - letter case MUST NOT carry meaning
215 $code = strtolower( $code );
218 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
219 wfDebug( "Invalid user language code\n" );
220 $code = $wgLanguageCode;
227 * Set the Language object
229 * @deprecated 1.19 Use setLanguage instead
230 * @param $l Mixed Language instance or language code
232 public function setLang( $l ) {
233 wfDeprecated( __METHOD__
, '1.19' );
234 $this->setLanguage( $l );
238 * Set the Language object
240 * @param $l Mixed Language instance or language code
241 * @throws MWException
244 public function setLanguage( $l ) {
245 if ( $l instanceof Language
) {
247 } elseif ( is_string( $l ) ) {
248 $l = self
::sanitizeLangCode( $l );
249 $obj = Language
::factory( $l );
252 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
257 * @deprecated 1.19 Use getLanguage instead
260 public function getLang() {
261 wfDeprecated( __METHOD__
, '1.19' );
262 return $this->getLanguage();
266 * Get the Language object
271 public function getLanguage() {
272 if ( $this->lang
=== null ) {
273 global $wgLanguageCode, $wgContLang;
274 $code = $this->getRequest()->getVal(
276 $this->getUser()->getOption( 'language' )
278 $code = self
::sanitizeLangCode( $code );
280 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
282 if( $code === $wgLanguageCode ) {
283 $this->lang
= $wgContLang;
285 $obj = Language
::factory( $code );
293 * Set the Skin object
297 public function setSkin( Skin
$s ) {
298 $this->skin
= clone $s;
299 $this->skin
->setContext( $this );
303 * Get the Skin object
307 public function getSkin() {
308 if ( $this->skin
=== null ) {
309 wfProfileIn( __METHOD__
. '-createskin' );
312 wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
314 // If the hook worked try to set a skin from it
315 if ( $skin instanceof Skin
) {
317 } elseif ( is_string($skin) ) {
318 $this->skin
= Skin
::newFromKey( $skin );
321 // If this is still null (the hook didn't run or didn't work)
322 // then go through the normal processing to load a skin
323 if ( $this->skin
=== null ) {
324 global $wgHiddenPrefs;
325 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
327 $userSkin = $this->getUser()->getOption( 'skin' );
328 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
330 # if we're not allowing users to override, then use the default
331 global $wgDefaultSkin;
332 $userSkin = $wgDefaultSkin;
335 $this->skin
= Skin
::newFromKey( $userSkin );
338 // After all that set a context on whatever skin got created
339 $this->skin
->setContext( $this );
340 wfProfileOut( __METHOD__
. '-createskin' );
345 /** Helpful methods **/
348 * Get a Message object with context set
349 * Parameters are the same as wfMessage()
351 * @return Message object
353 public function msg() {
354 $args = func_get_args();
355 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
358 /** Static methods **/
361 * Get the RequestContext object associated with the main request
363 * @return RequestContext object
365 public static function getMain() {
366 static $instance = null;
367 if ( $instance === null ) {
368 $instance = new self
;
374 * Create a new extraneous context. The context is filled with information
375 * external to the current session.
376 * - Title is specified by argument
377 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
378 * - User is an anonymous user, for separation IPv4 localhost is used
379 * - Language will be based on the anonymous user and request, may be content
380 * language or a uselang param in the fauxrequest data may change the lang
381 * - Skin will be based on the anonymous user, should be the wiki's default skin
383 * @param $title Title Title to use for the extraneous request
384 * @param $request Mixed A WebRequest or data to use for a FauxRequest
385 * @return RequestContext
387 public static function newExtraneousContext( Title
$title, $request=array() ) {
389 $context->setTitle( $title );
390 if ( $request instanceof WebRequest
) {
391 $context->setRequest( $request );
393 $context->setRequest( new FauxRequest( $request ) );
395 $context->user
= User
::newFromName( '127.0.0.1', false );