From c08158368cf30db10ea4b01c068f48bda3d11c2e Mon Sep 17 00:00:00 2001 From: Daniel Friesen Date: Sun, 3 Apr 2011 10:41:14 +0000 Subject: [PATCH] Implement the RequestContext class. Some credit to IAlex, ;) other credit for me and that plethora of bugs and hicoughs I had to deal with in impelenting it. http://www.mediawiki.org/wiki/Requests_for_comment/Context_object (it's little different though) --- includes/AutoLoader.php | 3 +- includes/OutputPage.php | 55 +++++++++++++------------- includes/Setup.php | 6 +-- includes/SpecialPage.php | 84 ++++++++++++++++++++-------------------- includes/StubObject.php | 57 ++++++++++++++------------- 5 files changed, 106 insertions(+), 99 deletions(-) diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 2d690dfe45..2e2754109f 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -197,6 +197,7 @@ $wgAutoloadLocalClasses = array( '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', @@ -231,8 +232,8 @@ $wgAutoloadLocalClasses = array( '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', diff --git a/includes/OutputPage.php b/includes/OutputPage.php index dc3ad88512..60057b5fe7 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -752,37 +752,43 @@ class OutputPage { } /** - * 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(); } /** @@ -792,12 +798,7 @@ class OutputPage { * @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(); } /** @@ -807,9 +808,7 @@ class OutputPage { * @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(); } /** @@ -2628,7 +2627,7 @@ class OutputPage { // 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 { diff --git a/includes/Setup.php b/includes/Setup.php index fce5941fb4..076541e1c2 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -365,17 +365,17 @@ $wgContLang = new StubContLang; // 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 diff --git a/includes/SpecialPage.php b/includes/SpecialPage.php index ee6d390fc4..5173c35397 100644 --- a/includes/SpecialPage.php +++ b/includes/SpecialPage.php @@ -74,20 +74,10 @@ class SpecialPage { */ 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. @@ -556,7 +546,7 @@ class SpecialPage { } # Page exists, set the context - $page->setContext( $wgRequest, $wgOut ); + $page->setContext( $wgOut->getContext() ); # Check for redirect if ( !$including ) { @@ -629,9 +619,10 @@ class SpecialPage { $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 ) { @@ -888,11 +879,9 @@ class SpecialPage { * 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) { @@ -993,13 +982,26 @@ class SpecialPage { /** * 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(); + } } /** @@ -1009,12 +1011,7 @@ class SpecialPage { * @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(); } /** @@ -1024,12 +1021,7 @@ class SpecialPage { * @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(); } /** @@ -1039,7 +1031,7 @@ class SpecialPage { * @since 1.18 */ public function getUser() { - return $this->getOutput()->getUser(); + return $this->getContext()->getUser(); } /** @@ -1049,7 +1041,17 @@ class SpecialPage { * @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(); } /** @@ -1059,7 +1061,7 @@ class SpecialPage { * @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() ); } } diff --git a/includes/StubObject.php b/includes/StubObject.php index d1055e10f5..f2a8b3327e 100644 --- a/includes/StubObject.php +++ b/includes/StubObject.php @@ -131,39 +131,44 @@ class StubContLang extends StubObject { } /** - * 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}(); } + } + -- 2.20.1