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
{
63 * Set the WebRequest object
65 * @param $r WebRequest object
67 public function setRequest( WebRequest
$r ) {
72 * Get the WebRequest object
76 public function getRequest() {
77 if ( $this->request
=== null ) {
78 global $wgRequest; # fallback to $wg till we can improve this
79 $this->request
= $wgRequest;
81 return $this->request
;
85 * Set the Title object
87 * @param $t Title object
89 public function setTitle( Title
$t ) {
94 * Get the Title object
98 public function getTitle() {
99 if ( $this->title
=== null ) {
100 global $wgTitle; # fallback to $wg till we can improve this
101 $this->title
= $wgTitle;
107 * @param $o OutputPage
109 public function setOutput( OutputPage
$o ) {
114 * Get the OutputPage object
116 * @return OutputPage object
118 public function getOutput() {
119 if ( $this->output
=== null ) {
120 $this->output
= new OutputPage( $this );
122 return $this->output
;
126 * Set the User object
130 public function setUser( User
$u ) {
135 * Get the User object
139 public function getUser() {
140 if ( $this->user
=== null ) {
141 $this->user
= User
::newFromSession( $this->getRequest() );
147 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
148 * code and replaces with $wgLanguageCode if not sane.
151 static function sanitizeLangCode( $code ) {
152 global $wgLanguageCode;
154 // BCP 47 - letter case MUST NOT carry meaning
155 $code = strtolower( $code );
158 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
159 wfDebug( "Invalid user language code\n" );
160 $code = $wgLanguageCode;
167 * Set the Language object
169 * @deprecated 1.19 Use setLanguage instead
170 * @param $l Mixed Language instance or language code
172 public function setLang( $l ) {
173 $this->setLanguage( $l );
177 * Set the Language object
179 * @param $l Mixed Language instance or language code
182 public function setLanguage( $l ) {
183 if ( $l instanceof Language
) {
185 } elseif ( is_string( $l ) ) {
186 $l = self
::sanitizeLangCode( $l );
187 $obj = Language
::factory( $l );
190 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
195 * @deprecated 1.19 Use getLanguage instead
198 public function getLang() {
199 return $this->getLanguage();
203 * Get the Language object
208 public function getLanguage() {
209 if ( $this->lang
=== null ) {
210 global $wgLanguageCode, $wgContLang;
211 $code = $this->getRequest()->getVal(
213 $this->getUser()->getOption( 'language' )
215 $code = self
::sanitizeLangCode( $code );
217 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
219 if( $code === $wgLanguageCode ) {
220 $this->lang
= $wgContLang;
222 $obj = Language
::factory( $code );
230 * Set the Skin object
234 public function setSkin( Skin
$s ) {
235 $this->skin
= clone $s;
236 $this->skin
->setContext( $this );
240 * Get the Skin object
244 public function getSkin() {
245 if ( $this->skin
=== null ) {
246 wfProfileIn( __METHOD__
. '-createskin' );
248 global $wgHiddenPrefs;
249 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
251 $userSkin = $this->getUser()->getOption( 'skin' );
252 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
254 # if we're not allowing users to override, then use the default
255 global $wgDefaultSkin;
256 $userSkin = $wgDefaultSkin;
259 $this->skin
= Skin
::newFromKey( $userSkin );
260 $this->skin
->setContext( $this );
261 wfProfileOut( __METHOD__
. '-createskin' );
266 /** Helpful methods **/
269 * Get a Message object with context set
270 * Parameters are the same as wfMessage()
272 * @return Message object
274 public function msg() {
275 $args = func_get_args();
276 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
279 /** Static methods **/
282 * Get the RequestContext object associated with the main request
284 * @return RequestContext object
286 public static function getMain() {
287 static $instance = null;
288 if ( $instance === null ) {
289 $instance = new self
;
295 * Create a new extraneous context. The context is filled with information
296 * external to the current session.
297 * - Title is specified by argument
298 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
299 * - User is an anonymous user, for separation IPv4 localhost is used
300 * - Language will be based on the anonymous user and request, may be content
301 * language or a uselang param in the fauxrequest data may change the lang
302 * - Skin will be based on the anonymous user, should be the wiki's default skin
304 * @param $title Title Title to use for the extraneous request
305 * @param $request Mixed A WebRequest or data to use for a FauxRequest
306 * @return RequestContext
308 public static function newExtraneousContext( Title
$title, $request=array() ) {
310 $context->setTitle( $title );
311 if ( $request instanceof WebRequest
) {
312 $context->setRequest( $request );
314 $context->setRequest( new FauxRequest( $request ) );
316 $context->user
= User
::newFromName( '127.0.0.1', false );