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 * Set the Language object
199 public function setLang( Language
$l ) {
204 * Get the Language object
208 public function getLang() {
209 if ( $this->lang
=== null ) {
210 global $wgLanguageCode, $wgContLang;
211 $code = $this->getRequest()->getVal(
213 $this->getUser()->getOption( 'language' )
215 // BCP 47 - letter case MUST NOT carry meaning
216 $code = strtolower( $code );
219 if( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
220 wfDebug( "Invalid user language code\n" );
221 $code = $wgLanguageCode;
224 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
226 if( $code === $wgLanguageCode ) {
227 $this->lang
= $wgContLang;
229 $obj = Language
::factory( $code );
237 * Set the Skin object
241 public function setSkin( Skin
$s ) {
242 $this->skin
= clone $s;
243 $this->skin
->setContext( $this );
247 * Get the Skin object
251 public function getSkin() {
252 if ( $this->skin
=== null ) {
253 wfProfileIn( __METHOD__
. '-createskin' );
255 global $wgHiddenPrefs;
256 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
258 $userSkin = $this->getUser()->getOption( 'skin' );
259 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
261 # if we're not allowing users to override, then use the default
262 global $wgDefaultSkin;
263 $userSkin = $wgDefaultSkin;
266 $this->skin
= Skin
::newFromKey( $userSkin );
267 $this->skin
->setContext( $this );
268 wfProfileOut( __METHOD__
. '-createskin' );
273 /** Helpful methods **/
276 * Get a Message object with context set
277 * Parameters are the same as wfMessage()
279 * @return Message object
281 public function msg() {
282 $args = func_get_args();
283 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
286 /** Static methods **/
289 * Get the RequestContext object associated with the main request
291 * @return RequestContext object
293 public static function getMain() {
294 static $instance = null;
295 if ( $instance === null ) {
296 $instance = new self
;
303 * The simplest way of implementing IContextSource is to hold a RequestContext as a
304 * member variable and provide accessors to it.
306 abstract class ContextSource
implements IContextSource
{
309 * @var RequestContext
314 * Get the RequestContext object
316 * @return RequestContext
318 public function getContext() {
319 if ( $this->context
=== null ) {
320 $class = get_class( $this );
321 wfDebug( __METHOD__
. " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
322 $this->context
= RequestContext
::getMain();
324 return $this->context
;
328 * Set the RequestContext object
330 * @param $context RequestContext
332 public function setContext( RequestContext
$context ) {
333 $this->context
= $context;
337 * Get the WebRequest object
341 public function getRequest() {
342 return $this->getContext()->getRequest();
346 * Get the Title object
350 public function getTitle() {
351 return $this->getContext()->getTitle();
355 * Get the OutputPage object
357 * @return OutputPage object
359 public function getOutput() {
360 return $this->getContext()->getOutput();
364 * Get the User object
368 public function getUser() {
369 return $this->getContext()->getUser();
373 * Get the Language object
377 public function getLang() {
378 return $this->getContext()->getLang();
382 * Get the Skin object
386 public function getSkin() {
387 return $this->getContext()->getSkin();
391 * Get a Message object with context set
392 * Parameters are the same as wfMessage()
394 * @return Message object
396 public function msg( /* $args */ ) {
397 return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );