Rm old options from commandLine.inc days
[mediawiki.git] / includes / RequestContext.php
blobe3a19a3251130b6c704a09898e95222c6a20700b
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 * Interface for objects which can provide a context on request.
30 interface IContextSource {
32 /**
33 * Get the WebRequest object
35 * @return WebRequest
37 public function getRequest();
39 /**
40 * Get the Title object
42 * @return Title
44 public function getTitle();
46 /**
47 * Get the OutputPage object
49 * @return OutputPage object
51 public function getOutput();
53 /**
54 * Get the User object
56 * @return User
58 public function getUser();
60 /**
61 * Get the Language object
63 * @return Language
65 public function getLang();
67 /**
68 * Get the Skin object
70 * @return Skin
72 public function getSkin();
75 /**
76 * Group all the pieces relevant to the context of a request into one instance
78 class RequestContext implements IContextSource {
80 /**
81 * @var WebRequest
83 private $request;
85 /**
86 * @var Title
88 private $title;
90 /**
91 * @var OutputPage
93 private $output;
95 /**
96 * @var User
98 private $user;
101 * @var Language
103 private $lang;
106 * @var Skin
108 private $skin;
111 * Set the WebRequest object
113 * @param $r WebRequest object
115 public function setRequest( WebRequest $r ) {
116 $this->request = $r;
120 * Get the WebRequest object
122 * @return WebRequest
124 public function getRequest() {
125 if ( $this->request === null ) {
126 global $wgRequest; # fallback to $wg till we can improve this
127 $this->request = $wgRequest;
129 return $this->request;
133 * Set the Title object
135 * @param $t Title object
137 public function setTitle( Title $t ) {
138 $this->title = $t;
142 * Get the Title object
144 * @return Title
146 public function getTitle() {
147 if ( $this->title === null ) {
148 global $wgTitle; # fallback to $wg till we can improve this
149 $this->title = $wgTitle;
151 return $this->title;
155 * @param $o OutputPage
157 public function setOutput( OutputPage $o ) {
158 $this->output = $o;
162 * Get the OutputPage object
164 * @return OutputPage object
166 public function getOutput() {
167 if ( $this->output === null ) {
168 $this->output = new OutputPage( $this );
170 return $this->output;
174 * Set the User object
176 * @param $u User
178 public function setUser( User $u ) {
179 $this->user = $u;
183 * Get the User object
185 * @return User
187 public function getUser() {
188 if ( $this->user === null ) {
189 $this->user = User::newFromSession( $this->getRequest() );
191 return $this->user;
195 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
196 * code and replaces with $wgLanguageCode if not sane.
198 private static function sanitizeLangCode( $code ) {
199 global $wgLanguageCode;
201 // BCP 47 - letter case MUST NOT carry meaning
202 $code = strtolower( $code );
204 # Validate $code
205 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
206 wfDebug( "Invalid user language code\n" );
207 $code = $wgLanguageCode;
210 return $code;
214 * Set the Language object
216 * @param $l Mixed Language instance or language code
218 public function setLang( $l ) {
219 if ( $l instanceof Language ) {
220 $this->lang = $l;
221 } elseif ( is_string( $l ) ) {
222 $l = self::sanitizeLangCode( $l );
223 $obj = Language::factory( $l );
224 $this->lang = $obj;
225 } else {
226 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
231 * Get the Language object
233 * @return Language
235 public function getLang() {
236 if ( $this->lang === null ) {
237 global $wgLanguageCode, $wgContLang;
238 $code = $this->getRequest()->getVal(
239 'uselang',
240 $this->getUser()->getOption( 'language' )
242 $code = self::sanitizeLangCode( $code );
244 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
246 if( $code === $wgLanguageCode ) {
247 $this->lang = $wgContLang;
248 } else {
249 $obj = Language::factory( $code );
250 $this->lang = $obj;
253 return $this->lang;
257 * Set the Skin object
259 * @param $s Skin
261 public function setSkin( Skin $s ) {
262 $this->skin = clone $s;
263 $this->skin->setContext( $this );
267 * Get the Skin object
269 * @return Skin
271 public function getSkin() {
272 if ( $this->skin === null ) {
273 wfProfileIn( __METHOD__ . '-createskin' );
275 global $wgHiddenPrefs;
276 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
277 # get the user skin
278 $userSkin = $this->getUser()->getOption( 'skin' );
279 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
280 } else {
281 # if we're not allowing users to override, then use the default
282 global $wgDefaultSkin;
283 $userSkin = $wgDefaultSkin;
286 $this->skin = Skin::newFromKey( $userSkin );
287 $this->skin->setContext( $this );
288 wfProfileOut( __METHOD__ . '-createskin' );
290 return $this->skin;
293 /** Helpful methods **/
296 * Get a Message object with context set
297 * Parameters are the same as wfMessage()
299 * @return Message object
301 public function msg() {
302 $args = func_get_args();
303 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
306 /** Static methods **/
309 * Get the RequestContext object associated with the main request
311 * @return RequestContext object
313 public static function getMain() {
314 static $instance = null;
315 if ( $instance === null ) {
316 $instance = new self;
318 return $instance;
322 * Create a new extraneous context. The context is filled with information
323 * external to the current session.
324 * - Title is specified by argument
325 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
326 * - User is an anonymous user, for separation IPv4 localhost is used
327 * - Language will be based on the anonymous user and request, may be content
328 * language or a uselang param in the fauxrequest data may change the lang
329 * - Skin will be based on the anonymous user, should be the wiki's default skin
331 * @param $title Title Title to use for the extraneous request
332 * @param $request Mixed A WebRequest or data to use for a FauxRequest
333 * @return RequestContext
335 public static function newExtraneousContext( Title $title, $request=array() ) {
336 $context = new self;
337 $context->setTitle( $title );
338 if ( $request instanceof WebRequest ) {
339 $context->setRequest( $request );
340 } else {
341 $context->setRequest( new FauxRequest( $request ) );
343 $context->user = User::newFromName( '127.0.0.1', false );
344 return $context;
350 * The simplest way of implementing IContextSource is to hold a RequestContext as a
351 * member variable and provide accessors to it.
353 abstract class ContextSource implements IContextSource {
356 * @var RequestContext
358 private $context;
361 * Get the RequestContext object
363 * @return RequestContext
365 public function getContext() {
366 if ( $this->context === null ) {
367 $class = get_class( $this );
368 wfDebug( __METHOD__ . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
369 $this->context = RequestContext::getMain();
371 return $this->context;
375 * Set the RequestContext object
377 * @param $context RequestContext
379 public function setContext( RequestContext $context ) {
380 $this->context = $context;
384 * Get the WebRequest object
386 * @return WebRequest
388 public function getRequest() {
389 return $this->getContext()->getRequest();
393 * Get the Title object
395 * @return Title
397 public function getTitle() {
398 return $this->getContext()->getTitle();
402 * Get the OutputPage object
404 * @return OutputPage object
406 public function getOutput() {
407 return $this->getContext()->getOutput();
411 * Get the User object
413 * @return User
415 public function getUser() {
416 return $this->getContext()->getUser();
420 * Get the Language object
422 * @return Language
424 public function getLang() {
425 return $this->getContext()->getLang();
429 * Get the Skin object
431 * @return Skin
433 public function getSkin() {
434 return $this->getContext()->getSkin();
438 * Get a Message object with context set
439 * Parameters are the same as wfMessage()
441 * @return Message object
443 public function msg( /* $args */ ) {
444 return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );