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.
153 public function getWikiPage() {
154 if ( $this->wikipage
=== null ) {
155 $title = $this->getTitle();
156 if ( $title === null ) {
157 throw new MWException( __METHOD__
. ' called without Title object set' );
159 $this->wikipage
= WikiPage
::factory( $title );
161 return $this->wikipage
;
165 * @param $o OutputPage
167 public function setOutput( OutputPage
$o ) {
172 * Get the OutputPage object
174 * @return OutputPage object
176 public function getOutput() {
177 if ( $this->output
=== null ) {
178 $this->output
= new OutputPage( $this );
180 return $this->output
;
184 * Set the User object
188 public function setUser( User
$u ) {
193 * Get the User object
197 public function getUser() {
198 if ( $this->user
=== null ) {
199 $this->user
= User
::newFromSession( $this->getRequest() );
205 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
206 * code and replaces with $wgLanguageCode if not sane.
207 * @param $code string
210 public static function sanitizeLangCode( $code ) {
211 global $wgLanguageCode;
213 // BCP 47 - letter case MUST NOT carry meaning
214 $code = strtolower( $code );
217 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
218 wfDebug( "Invalid user language code\n" );
219 $code = $wgLanguageCode;
226 * Set the Language object
228 * @deprecated 1.19 Use setLanguage instead
229 * @param $l Mixed Language instance or language code
231 public function setLang( $l ) {
232 wfDeprecated( __METHOD__
, '1.19' );
233 $this->setLanguage( $l );
237 * Set the Language object
239 * @param $l Mixed Language instance or language code
242 public function setLanguage( $l ) {
243 if ( $l instanceof Language
) {
245 } elseif ( is_string( $l ) ) {
246 $l = self
::sanitizeLangCode( $l );
247 $obj = Language
::factory( $l );
250 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
255 * @deprecated 1.19 Use getLanguage instead
258 public function getLang() {
259 wfDeprecated( __METHOD__
, '1.19' );
260 return $this->getLanguage();
264 * Get the Language object
269 public function getLanguage() {
270 if ( $this->lang
=== null ) {
271 global $wgLanguageCode, $wgContLang;
272 $code = $this->getRequest()->getVal(
274 $this->getUser()->getOption( 'language' )
276 $code = self
::sanitizeLangCode( $code );
278 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
280 if( $code === $wgLanguageCode ) {
281 $this->lang
= $wgContLang;
283 $obj = Language
::factory( $code );
291 * Set the Skin object
295 public function setSkin( Skin
$s ) {
296 $this->skin
= clone $s;
297 $this->skin
->setContext( $this );
301 * Get the Skin object
305 public function getSkin() {
306 if ( $this->skin
=== null ) {
307 wfProfileIn( __METHOD__
. '-createskin' );
310 wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
312 // If the hook worked try to set a skin from it
313 if ( $skin instanceof Skin
) {
315 } elseif ( is_string($skin) ) {
316 $this->skin
= Skin
::newFromKey( $skin );
319 // If this is still null (the hook didn't run or didn't work)
320 // then go through the normal processing to load a skin
321 if ( $this->skin
=== null ) {
322 global $wgHiddenPrefs;
323 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
325 $userSkin = $this->getUser()->getOption( 'skin' );
326 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
328 # if we're not allowing users to override, then use the default
329 global $wgDefaultSkin;
330 $userSkin = $wgDefaultSkin;
333 $this->skin
= Skin
::newFromKey( $userSkin );
336 // After all that set a context on whatever skin got created
337 $this->skin
->setContext( $this );
338 wfProfileOut( __METHOD__
. '-createskin' );
343 /** Helpful methods **/
346 * Get a Message object with context set
347 * Parameters are the same as wfMessage()
349 * @return Message object
351 public function msg() {
352 $args = func_get_args();
353 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
356 /** Static methods **/
359 * Get the RequestContext object associated with the main request
361 * @return RequestContext object
363 public static function getMain() {
364 static $instance = null;
365 if ( $instance === null ) {
366 $instance = new self
;
372 * Create a new extraneous context. The context is filled with information
373 * external to the current session.
374 * - Title is specified by argument
375 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
376 * - User is an anonymous user, for separation IPv4 localhost is used
377 * - Language will be based on the anonymous user and request, may be content
378 * language or a uselang param in the fauxrequest data may change the lang
379 * - Skin will be based on the anonymous user, should be the wiki's default skin
381 * @param $title Title Title to use for the extraneous request
382 * @param $request Mixed A WebRequest or data to use for a FauxRequest
383 * @return RequestContext
385 public static function newExtraneousContext( Title
$title, $request=array() ) {
387 $context->setTitle( $title );
388 if ( $request instanceof WebRequest
) {
389 $context->setRequest( $request );
391 $context->setRequest( new FauxRequest( $request ) );
393 $context->user
= User
::newFromName( '127.0.0.1', false );