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 * Interface for objects which can provide a context on request.
30 interface IContextSource
{
33 * Get the WebRequest object
37 public function getRequest();
40 * Get the Title object
44 public function getTitle();
47 * Get the OutputPage object
49 * @return OutputPage object
51 public function getOutput();
58 public function getUser();
61 * Get the Language object
65 public function getLang();
72 public function getSkin();
76 * Group all the pieces relevant to the context of a request into one instance
78 class RequestContext
implements IContextSource
{
111 * Set the WebRequest object
113 * @param $r WebRequest object
115 public function setRequest( WebRequest
$r ) {
120 * Get the WebRequest object
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 ) {
142 * Get the Title object
146 public function getTitle() {
147 if ( $this->title
=== null ) {
148 global $wgTitle; # fallback to $wg till we can improve this
149 $this->title
= $wgTitle;
155 * @param $o OutputPage
157 public function setOutput( OutputPage
$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
178 public function setUser( User
$u ) {
183 * Get the User object
187 public function getUser() {
188 if ( $this->user
=== null ) {
189 $this->user
= User
::newFromSession( $this->getRequest() );
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 );
205 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
206 wfDebug( "Invalid user language code\n" );
207 $code = $wgLanguageCode;
214 * Set the Language object
216 * @param $l Mixed Language instance or language code
218 public function setLang( $l ) {
219 if ( $l instanceof Language
) {
221 } elseif ( is_string( $l ) ) {
222 $l = self
::sanitizeLangCode( $l );
223 $obj = Language
::factory( $l );
226 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
231 * Get the Language object
235 public function getLang() {
236 if ( $this->lang
=== null ) {
237 global $wgLanguageCode, $wgContLang;
238 $code = $this->getRequest()->getVal(
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;
249 $obj = Language
::factory( $code );
257 * Set the Skin object
261 public function setSkin( Skin
$s ) {
262 $this->skin
= clone $s;
263 $this->skin
->setContext( $this );
267 * Get the Skin object
271 public function getSkin() {
272 if ( $this->skin
=== null ) {
273 wfProfileIn( __METHOD__
. '-createskin' );
275 global $wgHiddenPrefs;
276 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
278 $userSkin = $this->getUser()->getOption( 'skin' );
279 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
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' );
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
;
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() ) {
337 $context->setTitle( $title );
338 if ( $request instanceof WebRequest
) {
339 $context->setRequest( $request );
341 $context->setRequest( new FauxRequest( $request ) );
343 $context->user
= User
::newFromName( '127.0.0.1', false );
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
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
388 public function getRequest() {
389 return $this->getContext()->getRequest();
393 * Get the Title object
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
415 public function getUser() {
416 return $this->getContext()->getUser();
420 * Get the Language object
424 public function getLang() {
425 return $this->getContext()->getLang();
429 * Get the Skin object
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() );