Revert r101492, broken, see CR. Also revert followup r101496.
[mediawiki.git] / includes / context / RequestContext.php
blob3cbe253e3b2a9269621948b47428b943179a26da
1 <?php
2 /**
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
20 * @since 1.18
22 * @author Alexandre Emsenhuber
23 * @author Daniel Friesen
24 * @file
27 /**
28 * Group all the pieces relevant to the context of a request into one instance
30 class RequestContext implements IContextSource {
32 /**
33 * @var WebRequest
35 private $request;
37 /**
38 * @var Title
40 private $title;
42 /**
43 * @var OutputPage
45 private $output;
47 /**
48 * @var User
50 private $user;
52 /**
53 * @var Language
55 private $lang;
57 /**
58 * @var Skin
60 private $skin;
62 /**
63 * Set the WebRequest object
65 * @param $r WebRequest object
67 public function setRequest( WebRequest $r ) {
68 $this->request = $r;
71 /**
72 * Get the WebRequest object
74 * @return WebRequest
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;
84 /**
85 * Set the Title object
87 * @param $t Title object
89 public function setTitle( Title $t ) {
90 $this->title = $t;
93 /**
94 * Get the Title object
96 * @return Title
98 public function getTitle() {
99 if ( $this->title === null ) {
100 global $wgTitle; # fallback to $wg till we can improve this
101 $this->title = $wgTitle;
103 return $this->title;
107 * @param $o OutputPage
109 public function setOutput( OutputPage $o ) {
110 $this->output = $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
128 * @param $u User
130 public function setUser( User $u ) {
131 $this->user = $u;
135 * Get the User object
137 * @return User
139 public function getUser() {
140 if ( $this->user === null ) {
141 $this->user = User::newFromSession( $this->getRequest() );
143 return $this->user;
147 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
148 * code and replaces with $wgLanguageCode if not sane.
150 private static function sanitizeLangCode( $code ) {
151 global $wgLanguageCode;
153 // BCP 47 - letter case MUST NOT carry meaning
154 $code = strtolower( $code );
156 # Validate $code
157 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
158 wfDebug( "Invalid user language code\n" );
159 $code = $wgLanguageCode;
162 return $code;
166 * Set the Language object
168 * @param $l Mixed Language instance or language code
170 public function setLang( $l ) {
171 if ( $l instanceof Language ) {
172 $this->lang = $l;
173 } elseif ( is_string( $l ) ) {
174 $l = self::sanitizeLangCode( $l );
175 $obj = Language::factory( $l );
176 $this->lang = $obj;
177 } else {
178 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
183 * Get the Language object
185 * @return Language
187 public function getLang() {
188 if ( $this->lang === null ) {
189 global $wgLanguageCode, $wgContLang;
190 $code = $this->getRequest()->getVal(
191 'uselang',
192 $this->getUser()->getOption( 'language' )
194 $code = self::sanitizeLangCode( $code );
196 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
198 if( $code === $wgLanguageCode ) {
199 $this->lang = $wgContLang;
200 } else {
201 $obj = Language::factory( $code );
202 $this->lang = $obj;
205 return $this->lang;
209 * Set the Skin object
211 * @param $s Skin
213 public function setSkin( Skin $s ) {
214 $this->skin = clone $s;
215 $this->skin->setContext( $this );
219 * Get the Skin object
221 * @return Skin
223 public function getSkin() {
224 if ( $this->skin === null ) {
225 wfProfileIn( __METHOD__ . '-createskin' );
227 global $wgHiddenPrefs;
228 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
229 # get the user skin
230 $userSkin = $this->getUser()->getOption( 'skin' );
231 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
232 } else {
233 # if we're not allowing users to override, then use the default
234 global $wgDefaultSkin;
235 $userSkin = $wgDefaultSkin;
238 $this->skin = Skin::newFromKey( $userSkin );
239 $this->skin->setContext( $this );
240 wfProfileOut( __METHOD__ . '-createskin' );
242 return $this->skin;
245 /** Helpful methods **/
248 * Get a Message object with context set
249 * Parameters are the same as wfMessage()
251 * @return Message object
253 public function msg() {
254 $args = func_get_args();
255 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
258 /** Static methods **/
261 * Get the RequestContext object associated with the main request
263 * @return RequestContext object
265 public static function getMain() {
266 static $instance = null;
267 if ( $instance === null ) {
268 $instance = new self;
270 return $instance;
274 * Create a new extraneous context. The context is filled with information
275 * external to the current session.
276 * - Title is specified by argument
277 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
278 * - User is an anonymous user, for separation IPv4 localhost is used
279 * - Language will be based on the anonymous user and request, may be content
280 * language or a uselang param in the fauxrequest data may change the lang
281 * - Skin will be based on the anonymous user, should be the wiki's default skin
283 * @param $title Title Title to use for the extraneous request
284 * @param $request Mixed A WebRequest or data to use for a FauxRequest
285 * @return RequestContext
287 public static function newExtraneousContext( Title $title, $request=array() ) {
288 $context = new self;
289 $context->setTitle( $title );
290 if ( $request instanceof WebRequest ) {
291 $context->setRequest( $request );
292 } else {
293 $context->setRequest( new FauxRequest( $request ) );
295 $context->user = User::newFromName( '127.0.0.1', false );
296 return $context;