http://www.mediawiki.org/wiki/Requests_for_comment/Context_object (it's little different though)
'RegexlikeReplacer' => 'includes/StringUtils.php',
'ReplacementArray' => 'includes/StringUtils.php',
'Replacer' => 'includes/StringUtils.php',
+ 'RequestContext' => 'includes/RequestContext.php',
'ResourceLoader' => 'includes/resourceloader/ResourceLoader.php',
'ResourceLoaderContext' => 'includes/resourceloader/ResourceLoaderContext.php',
'ResourceLoaderModule' => 'includes/resourceloader/ResourceLoaderModule.php',
'SquidPurgeClientPool' => 'includes/SquidPurgeClient.php',
'Status' => 'includes/Status.php',
'StubContLang' => 'includes/StubObject.php',
- 'StubUserLang' => 'includes/StubObject.php',
'StubObject' => 'includes/StubObject.php',
+ 'StubRequestContext' => 'includes/StubObject.php',
'StringUtils' => 'includes/StringUtils.php',
'TablePager' => 'includes/Pager.php',
'TitleDependency' => 'includes/CacheDependency.php',
}
/**
- * Set the Title object to use
+ * Set the RequestContext used in this instance
*
- * @param $t Title object
+ * @param RequestContext $context
*/
- public function setTitle( $t ) {
- $this->mTitle = $t;
+ public function setContext( RequestContext $context ) {
+ $this->mContext = $context;
}
/**
- * Get the Title object used in this instance
+ * Get the RequestContext used in this instance
*
- * @return Title
+ * @return RequestContext
*/
- public function getTitle() {
- if ( $this->mTitle instanceof Title ) {
- return $this->mTitle;
- } else {
- wfDebug( __METHOD__ . " called and \$mTitle is null. Return \$wgTitle for sanity\n" );
- global $wgTitle;
- return $wgTitle;
+ public function getContext() {
+ if ( !isset($this->mContext) ) {
+ wfDebug( __METHOD__ . " called and \$mContext is null. Using RequestContext::getMain(); for sanity\n" );
+ $this->mContext = RequestContext::getMain();
}
+ return $this->mContext;
}
/**
- * Set the User object to use
+ * Set the Title object to use
*
- * @param $u User object
- * @since 1.18
+ * @param $t Title object
*/
- public function setUser( $u ) {
- $this->mUser = $u;
+ public function setTitle( $t ) {
+ $this->getContext()->setTitle($t);
+ }
+
+ /**
+ * Get the Title object used in this instance
+ *
+ * @return Title
+ */
+ public function getTitle() {
+ return $this->getContext()->getTitle();
}
/**
* @since 1.18
*/
public function getUser() {
- if ( !isset($this->mUser) ) {
- wfDebug( __METHOD__ . " called and \$mUser is null. Return \$wgUser for sanity\n" );
- global $wgUser;
- return $wgUser;
- }
- return $this->mUser;
+ return $this->getContext()->getUser();
}
/**
* @since 1.18
*/
public function getSkin() {
- // For now we'll just proxy to the user. In the future a saner location for
- // organizing what skin to use may be chosen
- return $this->getUser()->getSkin();
+ return $this->getContext()->getSkin();
}
/**
// Add user JS if enabled
if ( $wgAllowUserJs && $this->getUser()->isLoggedIn() ) {
$action = $wgRequest->getVal( 'action', 'view' );
- if( $this->mTitle && $this->mTitle->isJsSubpage() && $sk->userCanPreview( $action ) ) {
+ if( $this->getTitle() && $this->getTitle()->isJsSubpage() && $sk->userCanPreview( $action ) ) {
# XXX: additional security check/prompt?
$scripts .= Html::inlineScript( "\n" . $wgRequest->getText( 'wpTextbox1' ) . "\n" ) . "\n";
} else {
// Now that variant lists may be available...
$wgRequest->interpolateTitle();
-$wgUser = $wgCommandLineMode ? new User : User::newFromSession();
+$wgUser = new StubRequestContext( 'wgUser', 'getUser' );
/**
* @var Language
*/
-$wgLang = new StubUserLang;
+$wgLang = new StubRequestContext( 'wgLang', 'getLang' );
/**
* @var OutputPage
*/
-$wgOut = new StubObject( 'wgOut', 'OutputPage' );
+$wgOut = new StubRequestContext( 'wgOut', 'getOutput' );
/**
* @var Parser
*/
var $mAddedRedirectParams = array();
/**
- * Current request
- * @var WebRequest
+ * Current request context
+ * @var RequestContext
*/
- protected $mRequest;
- /**
- * Current output page
- * @var OutputPage
- */
- protected $mOutput;
- /**
- * Full title including $par
- * @var Title
- */
- protected $mFullTitle;
+ protected $mContext;
/**
* List of special pages, followed by parameters.
}
# Page exists, set the context
- $page->setContext( $wgRequest, $wgOut );
+ $page->setContext( $wgOut->getContext() );
# Check for redirect
if ( !$including ) {
$oldTitle = $wgTitle;
$oldOut = $wgOut;
- $wgOut = new OutputPage;
- $wgOut->setTitle( $title );
- $wgOut->setUser( $wgUser ); # for now, there may be a better idea in the future
+
+ $context = new RequestContext;
+ $context->setTitle( $title );
+ $wgOut = $context->getOutput();
$ret = SpecialPage::executePath( $title, true );
if ( $ret === true ) {
* This may be overridden by subclasses.
*/
function execute( $par ) {
- global $wgUser;
-
$this->setHeaders();
- if ( $this->userCanExecute( $wgUser ) ) {
+ if ( $this->userCanExecute( $this->getUser() ) ) {
$func = $this->mFunction;
// only load file if the function does not exist
if(!is_callable($func) and $this->mFile) {
/**
* Sets the context this SpecialPage is executed in
*
- * @param $request WebRequest
- * @param $output OutputPage
+ * @param $context RequestContext
+ * @since 1.18
+ */
+ protected function setContext( $context ) {
+ $this->mContext = $context;
+ }
+
+ /**
+ * Gets the context this SpecialPage is executed in
+ *
+ * @return RequestContext
+ * @since 1.18
*/
- protected function setContext( $request, $output ) {
- $this->mRequest = $request;
- $this->mOutput = $output;
- $this->mFullTitle = $output->getTitle();
+ public function getContext() {
+ if ( $this->mContext instanceof RequestContext ) {
+ return $this->mContext;
+ } else {
+ wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
+ return RequestContext::getMain();
+ }
}
/**
* @since 1.18
*/
public function getRequest() {
- if ( !isset($this->mRequest) ) {
- wfDebug( __METHOD__ . " called and \$mRequest is null. Return \$wgRequest for sanity\n" );
- global $wgRequest;
- return $wgRequest;
- }
- return $this->mRequest;
+ return $this->getContext()->getRequest();
}
/**
* @since 1.18
*/
public function getOutput() {
- if ( !isset($this->mOutput) ) {
- wfDebug( __METHOD__ . " called and \$mOutput is null. Return \$wgOut for sanity\n" );
- global $wgOut;
- return $wgOut;
- }
- return $this->mOutput;
+ return $this->getContext()->getOutput();
}
/**
* @since 1.18
*/
public function getUser() {
- return $this->getOutput()->getUser();
+ return $this->getContext()->getUser();
}
/**
* @since 1.18
*/
public function getSkin() {
- return $this->getOutput()->getSkin();
+ return $this->getContext()->getSkin();
+ }
+
+ /**
+ * Return the full title, including $par
+ *
+ * @return Title
+ * @since 1.18
+ */
+ public function getFullTitle() {
+ return $this->getContext()->getTitle();
}
/**
* @see wfMessage
*/
public function msg( /* $args */ ) {
- return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->mFullTitle );
+ return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->getFullTitle() );
}
}
}
/**
- * Stub object for the user language. It depends of the user preferences and
- * "uselang" parameter that can be passed to index.php. This object have to be
- * in $wgLang global.
+ * Stub object for the $wg globals replaced by RequestContext
*/
-class StubUserLang extends StubObject {
-
- function __construct() {
- parent::__construct( 'wgLang' );
+class StubRequestContext extends StubObject {
+
+ private $method = null;
+
+ function __construct( $global, $method ) {
+ parent::__construct( $global, 'RequestContext' );
+ $this->method = $method;
}
-
+
function __call( $name, $args ) {
return $this->_call( $name, $args );
}
+
+ function __get( $name ) {
+ // __get doesn't seam to play nice with _unstub
+ return RequestContext::getMain()->{$this->method}()->{$name};
+ }
- function _newObject() {
- global $wgLanguageCode, $wgRequest, $wgUser, $wgContLang;
- $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
- // BCP 47 - letter case MUST NOT carry meaning
- $code = strtolower( $code );
-
- # Validate $code
- if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
- wfDebug( "Invalid user language code\n" );
- $code = $wgLanguageCode;
- }
+ function __set( $name, $val ) {
+ // __set doesn't seam to play nice with _unstub
+ RequestContext::getMain()->{$this->method}()->{$name} = $val;
+ }
- wfRunHooks( 'UserGetLanguageObject', array( $wgUser, &$code ) );
+ function __isset( $name ) {
+ // __isset doesn't seam to play nice with _unstub
+ return isset( RequestContext::getMain()->{$this->method}()->{$name} );
+ }
- if( $code === $wgLanguageCode ) {
- return $wgContLang;
- } else {
- $obj = Language::factory( $code );
- return $obj;
- }
+ function __unset( $name ) {
+ // __unset doesn't seam to play nice with _unstub
+ unset( RequestContext::getMain()->{$this->method}()->{$name} );
+ }
+
+ function _newObject() {
+ return RequestContext::getMain()->{$this->method}();
}
+
}
+