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 ) {
96 // Erase the WikiPage so a new one with the new title gets created.
97 $this->wikipage
= null;
101 * Get the Title object
105 public function getTitle() {
106 if ( $this->title
=== null ) {
107 global $wgTitle; # fallback to $wg till we can improve this
108 $this->title
= $wgTitle;
114 * Check whether a WikiPage object can be get with getWikiPage().
115 * Callers should expect that an exception is thrown from getWikiPage()
116 * if this method returns false.
121 public function canUseWikiPage() {
122 if ( $this->wikipage
!== null ) {
123 # If there's a WikiPage object set, we can for sure get it
126 $title = $this->getTitle();
127 if ( $title === null ) {
128 # No Title, no WikiPage
131 # Only namespaces whose pages are stored in the database can have WikiPage
132 return $title->canExist();
137 * Set the WikiPage object
140 * @param $p WikiPage object
142 public function setWikiPage( WikiPage
$p ) {
143 $contextTitle = $this->getTitle();
144 $pageTitle = $p->getTitle();
145 if ( !$contextTitle ||
!$pageTitle->equals( $contextTitle ) ) {
146 $this->setTitle( $pageTitle );
148 // Defer this to the end since setTitle sets it to null.
149 $this->wikipage
= $p;
153 * Get the WikiPage object.
154 * May throw an exception if there's no Title object set or the Title object
155 * belongs to a special namespace that doesn't have WikiPage, so use first
156 * canUseWikiPage() to check whether this method can be called safely.
159 * @throws MWException
162 public function getWikiPage() {
163 if ( $this->wikipage
=== null ) {
164 $title = $this->getTitle();
165 if ( $title === null ) {
166 throw new MWException( __METHOD__
. ' called without Title object set' );
168 $this->wikipage
= WikiPage
::factory( $title );
170 return $this->wikipage
;
174 * @param $o OutputPage
176 public function setOutput( OutputPage
$o ) {
181 * Get the OutputPage object
183 * @return OutputPage object
185 public function getOutput() {
186 if ( $this->output
=== null ) {
187 $this->output
= new OutputPage( $this );
189 return $this->output
;
193 * Set the User object
197 public function setUser( User
$u ) {
202 * Get the User object
206 public function getUser() {
207 if ( $this->user
=== null ) {
208 $this->user
= User
::newFromSession( $this->getRequest() );
214 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
215 * code and replaces with $wgLanguageCode if not sane.
216 * @param $code string
219 public static function sanitizeLangCode( $code ) {
220 global $wgLanguageCode;
222 // BCP 47 - letter case MUST NOT carry meaning
223 $code = strtolower( $code );
226 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
227 wfDebug( "Invalid user language code\n" );
228 $code = $wgLanguageCode;
235 * Set the Language object
237 * @deprecated 1.19 Use setLanguage instead
238 * @param $l Mixed Language instance or language code
240 public function setLang( $l ) {
241 wfDeprecated( __METHOD__
, '1.19' );
242 $this->setLanguage( $l );
246 * Set the Language object
248 * @param $l Mixed Language instance or language code
249 * @throws MWException
252 public function setLanguage( $l ) {
253 if ( $l instanceof Language
) {
255 } elseif ( is_string( $l ) ) {
256 $l = self
::sanitizeLangCode( $l );
257 $obj = Language
::factory( $l );
260 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
265 * @deprecated 1.19 Use getLanguage instead
268 public function getLang() {
269 wfDeprecated( __METHOD__
, '1.19' );
270 return $this->getLanguage();
274 * Get the Language object
279 public function getLanguage() {
280 if ( $this->lang
=== null ) {
281 global $wgLanguageCode, $wgContLang;
282 $code = $this->getRequest()->getVal(
284 $this->getUser()->getOption( 'language' )
286 $code = self
::sanitizeLangCode( $code );
288 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
290 if( $code === $wgLanguageCode ) {
291 $this->lang
= $wgContLang;
293 $obj = Language
::factory( $code );
301 * Set the Skin object
305 public function setSkin( Skin
$s ) {
306 $this->skin
= clone $s;
307 $this->skin
->setContext( $this );
311 * Get the Skin object
315 public function getSkin() {
316 if ( $this->skin
=== null ) {
317 wfProfileIn( __METHOD__
. '-createskin' );
320 wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
322 // If the hook worked try to set a skin from it
323 if ( $skin instanceof Skin
) {
325 } elseif ( is_string($skin) ) {
326 $this->skin
= Skin
::newFromKey( $skin );
329 // If this is still null (the hook didn't run or didn't work)
330 // then go through the normal processing to load a skin
331 if ( $this->skin
=== null ) {
332 global $wgHiddenPrefs;
333 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
335 $userSkin = $this->getUser()->getOption( 'skin' );
336 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
338 # if we're not allowing users to override, then use the default
339 global $wgDefaultSkin;
340 $userSkin = $wgDefaultSkin;
343 $this->skin
= Skin
::newFromKey( $userSkin );
346 // After all that set a context on whatever skin got created
347 $this->skin
->setContext( $this );
348 wfProfileOut( __METHOD__
. '-createskin' );
353 /** Helpful methods **/
356 * Get a Message object with context set
357 * Parameters are the same as wfMessage()
359 * @return Message object
361 public function msg() {
362 $args = func_get_args();
363 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
366 /** Static methods **/
369 * Get the RequestContext object associated with the main request
371 * @return RequestContext object
373 public static function getMain() {
374 static $instance = null;
375 if ( $instance === null ) {
376 $instance = new self
;
382 * Create a new extraneous context. The context is filled with information
383 * external to the current session.
384 * - Title is specified by argument
385 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
386 * - User is an anonymous user, for separation IPv4 localhost is used
387 * - Language will be based on the anonymous user and request, may be content
388 * language or a uselang param in the fauxrequest data may change the lang
389 * - Skin will be based on the anonymous user, should be the wiki's default skin
391 * @param $title Title Title to use for the extraneous request
392 * @param $request Mixed A WebRequest or data to use for a FauxRequest
393 * @return RequestContext
395 public static function newExtraneousContext( Title
$title, $request=array() ) {
397 $context->setTitle( $title );
398 if ( $request instanceof WebRequest
) {
399 $context->setRequest( $request );
401 $context->setRequest( new FauxRequest( $request ) );
403 $context->user
= User
::newFromName( '127.0.0.1', false );