From df5f627e568552bd45c756c3195737227384b5de Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Sun, 3 Apr 2011 22:09:35 +0000 Subject: [PATCH] Implement magic accessors for RequestContext variables: you can now just call $context->request->stuff(), and that is internally mapped to the get accessor. Rename the private variables to the old $mName syntax: I know that that's "discouraged in new code", but in this case it stops over-clever IDEs highlighting the magic accesses as potential visibility errors and sticking big error tags on them. --- includes/RequestContext.php | 104 ++++++++++++++-------- includes/Wiki.php | 172 ++++++++++++++++-------------------- 2 files changed, 139 insertions(+), 137 deletions(-) diff --git a/includes/RequestContext.php b/includes/RequestContext.php index c78c112485..b0c4ecdf3c 100644 --- a/includes/RequestContext.php +++ b/includes/RequestContext.php @@ -1,19 +1,19 @@ request = $r; + $this->mRequest = $r; } /** @@ -30,11 +30,11 @@ class RequestContext { * @return WebRequest */ public function getRequest() { - if ( !isset($this->request) ) { + if ( !isset( $this->mRequest ) ) { global $wgRequest; # fallback to $wg till we can improve this - $this->request = $wgRequest; + $this->mRequest = $wgRequest; } - return $this->request; + return $this->mRequest; } /** @@ -43,7 +43,7 @@ class RequestContext { * @param $t Title object */ public function setTitle( Title $t ) { - $this->title = $t; + $this->mTitle = $t; } /** @@ -52,11 +52,11 @@ class RequestContext { * @return Title */ public function getTitle() { - if ( !isset($this->title) ) { + if ( !isset( $this->mTitle ) ) { global $wgTitle; # fallback to $wg till we can improve this - $this->title = $wgTitle; + $this->mTitle = $wgTitle; } - return $this->title; + return $this->mTitle; } /** @@ -64,8 +64,8 @@ class RequestContext { * * @param $u OutputPage */ - public function setOutput( OutputPage $u ) { - $this->output = $u; + public function setOutput( OutputPage $o ) { + $this->mOutput = $o; } /** @@ -74,11 +74,11 @@ class RequestContext { * @return OutputPage object */ public function getOutput() { - if ( !isset($this->output) ) { - $this->output = new OutputPage; - $this->output->setContext( $this ); + if ( !isset( $this->mOutput ) ) { + $this->mOutput = new OutputPage; + $this->mOutput->setContext( $this ); } - return $this->output; + return $this->mOutput; } /** @@ -87,7 +87,7 @@ class RequestContext { * @param $u User */ public function setUser( User $u ) { - $this->user = $u; + $this->mUser = $u; } /** @@ -96,11 +96,13 @@ class RequestContext { * @return User */ public function getUser() { - if ( !isset($this->user) ) { + if ( !isset( $this->mUser ) ) { global $wgCommandLineMode; - $this->user = $wgCommandLineMode ? new User : User::newFromSession( $this->getRequest() ); + $this->mUser = $wgCommandLineMode + ? new User + : User::newFromSession( $this->getRequest() ); } - return $this->user; + return $this->mUser; } /** @@ -109,28 +111,31 @@ class RequestContext { * @return Language */ public function getLang() { - if ( !isset($this->lang) ) { + if ( !isset( $this->mLang ) ) { global $wgLanguageCode, $wgContLang; - $code = $this->getRequest()->getVal( 'uselang', $this->getUser()->getOption( 'language' ) ); + $code = $this->getRequest()->getVal( + 'uselang', + $this->getUser()->getOption( 'language' ) + ); // BCP 47 - letter case MUST NOT carry meaning $code = strtolower( $code ); # Validate $code - if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) { + if ( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) { wfDebug( "Invalid user language code\n" ); $code = $wgLanguageCode; } wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) ); - if( $code === $wgLanguageCode ) { - $this->lang = $wgContLang; + if ( $code === $wgLanguageCode ) { + $this->mLang = $wgContLang; } else { $obj = Language::factory( $code ); - $this->lang = $obj; + $this->mLang = $obj; } } - return $this->lang; + return $this->mLang; } /** @@ -139,11 +144,11 @@ class RequestContext { * @return Skin */ public function getSkin() { - if ( !isset($this->skin) ) { + if ( !isset( $this->mSkin ) ) { wfProfileIn( __METHOD__ . '-createskin' ); - + global $wgHiddenPrefs; - if( !in_array( 'skin', $wgHiddenPrefs ) ) { + if ( !in_array( 'skin', $wgHiddenPrefs ) ) { # get the user skin $userSkin = $this->getUser()->getOption( 'skin' ); $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin ); @@ -153,11 +158,11 @@ class RequestContext { $userSkin = $wgDefaultSkin; } - $this->skin = Skin::newFromKey( $userSkin ); - $this->skin->setContext( $this ); + $this->mSkin = Skin::newFromKey( $userSkin ); + $this->mSkin->setContext( $this ); wfProfileOut( __METHOD__ . '-createskin' ); } - return $this->skin; + return $this->mSkin; } /** Helpful methods **/ @@ -182,10 +187,31 @@ class RequestContext { */ public static function getMain() { static $instance = null; - if ( !isset($instance) ) { + if ( !isset( $instance ) ) { $instance = new self; } return $instance; } + + /** + * Make these C#-style accessors, so you can do $context->user->getName() which is + * internally mapped to $context->__get('user')->getName() which is mapped to + * $context->getUser()->getName() + */ + public function __get( $name ) { + if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) { + $fname = 'get' . ucfirst( $name ); + return $this->$fname(); + } + trigger_error( "Undefined property {$name}", E_NOTICE ); + } + + public function __set( $name, $value ) { + if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) { + $fname = 'set' . ucfirst( $name ); + return $this->$fname( $value ); + } + trigger_error( "Undefined property {$name}", E_NOTICE ); + } } diff --git a/includes/Wiki.php b/includes/Wiki.php index b610b3f2a1..ba498f8c7c 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -14,26 +14,6 @@ class MediaWiki { */ private $params = array(); - /** - * The request object - * @var WebRequest - */ - private $request; - - /** - * Output to deliver content to - * FIXME: most stuff in the codebase doesn't actually write to this, it'll write to - * $wgOut instead. So in practice this has to be $wgOut; - * @var OutputPage - */ - private $output; - - /** - * The Title we'll be working on - * @var Title - */ - private $title; - /** * TODO: fold $output, etc, into this * @var RequestContext @@ -69,22 +49,18 @@ class MediaWiki { } public function request( WebRequest &$x = null ){ - return wfSetVar( $this->request, $x ); + return wfSetVar( $this->context->request, $x ); } public function output( OutputPage &$x = null ){ - return wfSetVar( $this->output, $x ); + return wfSetVar( $this->context->output, $x ); } public function __construct( WebRequest &$request, /*OutputPage*/ &$output ){ - $this->request =& $request; - $this->output =& $output; - $this->title = $this->parseTitle(); - $this->context = new RequestContext(); - $this->context->setRequest( $this->request ); - $this->context->setOutput( $this->output ); - $this->context->setTitle( $this->title ); + $this->context->setRequest( $request ); + $this->context->setOutput( $output ); + $this->context->setTitle( $this->parseTitle() ); } /** @@ -97,27 +73,27 @@ class MediaWiki { public function performRequestForTitle( &$article, &$user ) { wfProfileIn( __METHOD__ ); - $this->output->setTitle( $this->title ); - if ( $this->request->getVal( 'printable' ) === 'yes' ) { - $this->output->setPrintable(); + $this->context->output->setTitle( $this->context->title ); + if ( $this->context->request->getVal( 'printable' ) === 'yes' ) { + $this->context->output->setPrintable(); } wfRunHooks( 'BeforeInitialize', array( - &$this->title, + &$this->context->title, &$article, - &$this->output, + &$this->context->output, &$user, - $this->request, + $this->context->request, $this ) ); // If the user is not logged in, the Namespace:title of the article must be in // the Read array in order for the user to see it. (We have to check here to // catch special pages etc. We check again in Article::view()) - if ( !is_null( $this->title ) && !$this->title->userCanRead() ) { - $this->output->loginToUse(); + if ( !is_null( $this->context->title ) && !$this->context->title->userCanRead() ) { + $this->context->output->loginToUse(); $this->finalCleanup(); - $this->output->disable(); + $this->context->output->disable(); wfProfileOut( __METHOD__ ); return false; } @@ -131,7 +107,7 @@ class MediaWiki { $article = $new_article; $this->performAction( $article, $user ); } elseif ( is_string( $new_article ) ) { - $this->output->redirect( $new_article ); + $this->context->output->redirect( $new_article ); } else { wfProfileOut( __METHOD__ ); throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" ); @@ -148,10 +124,10 @@ class MediaWiki { private function parseTitle() { global $wgContLang; - $curid = $this->request->getInt( 'curid' ); - $title = $this->request->getVal( 'title' ); + $curid = $this->context->request->getInt( 'curid' ); + $title = $this->context->request->getVal( 'title' ); - if ( $this->request->getCheck( 'search' ) ) { + if ( $this->context->request->getCheck( 'search' ) ) { // Compatibility with old search URLs which didn't use Special:Search // Just check for presence here, so blank requests still // show the search page when using ugly URLs (bug 8054). @@ -171,8 +147,8 @@ class MediaWiki { // For non-special titles, check for implicit titles if ( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) { // We can have urls with just ?diff=,?oldid= or even just ?diff= - $oldid = $this->request->getInt( 'oldid' ); - $oldid = $oldid ? $oldid : $this->request->getInt( 'diff' ); + $oldid = $this->context->request->getInt( 'oldid' ); + $oldid = $oldid ? $oldid : $this->context->request->getInt( 'diff' ); // Allow oldid to override a changed or missing title if ( $oldid ) { $rev = Revision::newFromId( $oldid ); @@ -187,10 +163,10 @@ class MediaWiki { * @return Title */ public function getTitle(){ - if( $this->title === null ){ - $this->title = $this->parseTitle(); + if( $this->context->title === null ){ + $this->context->title = $this->parseTitle(); } - return $this->title; + return $this->context->title; } /** @@ -206,46 +182,46 @@ class MediaWiki { wfProfileIn( __METHOD__ ); // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty. - if ( is_null( $this->title ) || ( ( $this->title->getDBkey() == '' ) && ( $this->title->getInterwiki() == '' ) ) ) { - $this->title = SpecialPage::getTitleFor( 'Badtitle' ); - $this->output->setTitle( $this->title ); // bug 21456 + if ( is_null( $this->context->title ) || ( ( $this->context->title->getDBkey() == '' ) && ( $this->context->title->getInterwiki() == '' ) ) ) { + $this->context->title = SpecialPage::getTitleFor( 'Badtitle' ); + $this->context->output->setTitle( $this->context->title ); // bug 21456 // Die now before we mess up $wgArticle and the skin stops working throw new ErrorPageError( 'badtitle', 'badtitletext' ); // Interwiki redirects - } else if ( $this->title->getInterwiki() != '' ) { - $rdfrom = $this->request->getVal( 'rdfrom' ); + } else if ( $this->context->title->getInterwiki() != '' ) { + $rdfrom = $this->context->request->getVal( 'rdfrom' ); if ( $rdfrom ) { - $url = $this->title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) ); + $url = $this->context->title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) ); } else { - $query = $this->request->getValues(); + $query = $this->context->request->getValues(); unset( $query['title'] ); - $url = $this->title->getFullURL( $query ); + $url = $this->context->title->getFullURL( $query ); } /* Check for a redirect loop */ - if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $this->title->isLocal() ) { + if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $this->context->title->isLocal() ) { // 301 so google et al report the target as the actual url. - $this->output->redirect( $url, 301 ); + $this->context->output->redirect( $url, 301 ); } else { - $this->title = SpecialPage::getTitleFor( 'Badtitle' ); - $this->output->setTitle( $this->title ); // bug 21456 + $this->context->title = SpecialPage::getTitleFor( 'Badtitle' ); + $this->context->output->setTitle( $this->context->title ); // bug 21456 wfProfileOut( __METHOD__ ); throw new ErrorPageError( 'badtitle', 'badtitletext' ); } // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant - } else if ( $this->request->getVal( 'action', 'view' ) == 'view' && !$this->request->wasPosted() - && ( $this->request->getVal( 'title' ) === null || $this->title->getPrefixedDBKey() != $this->request->getVal( 'title' ) ) - && !count( array_diff( array_keys( $this->request->getValues() ), array( 'action', 'title' ) ) ) ) + } else if ( $this->context->request->getVal( 'action', 'view' ) == 'view' && !$this->context->request->wasPosted() + && ( $this->context->request->getVal( 'title' ) === null || $this->context->title->getPrefixedDBKey() != $this->context->request->getVal( 'title' ) ) + && !count( array_diff( array_keys( $this->context->request->getValues() ), array( 'action', 'title' ) ) ) ) { - if ( $this->title->getNamespace() == NS_SPECIAL ) { - list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $this->title->getDBkey() ); + if ( $this->context->title->getNamespace() == NS_SPECIAL ) { + list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $this->context->title->getDBkey() ); if ( $name ) { - $this->title = SpecialPage::getTitleFor( $name, $subpage ); + $this->context->title = SpecialPage::getTitleFor( $name, $subpage ); } } - $targetUrl = $this->title->getFullURL(); + $targetUrl = $this->context->title->getFullURL(); // Redirect to canonical url, make it a 301 to allow caching - if ( $targetUrl == $this->request->getFullRequestURL() ) { + if ( $targetUrl == $this->context->request->getFullRequestURL() ) { $message = "Redirect loop detected!\n\n" . "This means the wiki got confused about what page was " . "requested; this sometimes happens when moving a wiki " . @@ -269,13 +245,13 @@ class MediaWiki { wfProfileOut( __METHOD__ ); return false; } else { - $this->output->setSquidMaxage( 1200 ); - $this->output->redirect( $targetUrl, '301' ); + $this->context->output->setSquidMaxage( 1200 ); + $this->context->output->redirect( $targetUrl, '301' ); } // Special pages - } else if ( NS_SPECIAL == $this->title->getNamespace() ) { + } else if ( NS_SPECIAL == $this->context->title->getNamespace() ) { /* actions that need to be made when we have a special pages */ - SpecialPage::executePath( $this->title, $this->context ); + SpecialPage::executePath( $this->context->title, $this->context ); } else { /* No match to special cases */ wfProfileOut( __METHOD__ ); @@ -324,7 +300,7 @@ class MediaWiki { public function getAction() { global $wgDisabledActions; - $action = $this->request->getVal( 'action', 'view' ); + $action = $this->context->request->getVal( 'action', 'view' ); // Check for disabled actions if ( in_array( $action, $wgDisabledActions ) ) { @@ -334,9 +310,9 @@ class MediaWiki { // Workaround for bug #20966: inability of IE to provide an action dependent // on which submit button is clicked. if ( $action === 'historysubmit' ) { - if ( $this->request->getBool( 'revisiondelete' ) ) { + if ( $this->context->request->getBool( 'revisiondelete' ) ) { return 'revisiondelete'; - } elseif ( $this->request->getBool( 'revisionmove' ) ) { + } elseif ( $this->context->request->getBool( 'revisionmove' ) ) { return 'revisionmove'; } else { return 'view'; @@ -357,21 +333,21 @@ class MediaWiki { private function initializeArticle() { wfProfileIn( __METHOD__ ); - $action = $this->request->getVal( 'action', 'view' ); - $article = self::articleFromTitle( $this->title ); + $action = $this->context->request->getVal( 'action', 'view' ); + $article = self::articleFromTitle( $this->context->title ); // NS_MEDIAWIKI has no redirects. // It is also used for CSS/JS, so performance matters here... - if ( $this->title->getNamespace() == NS_MEDIAWIKI ) { + if ( $this->context->title->getNamespace() == NS_MEDIAWIKI ) { wfProfileOut( __METHOD__ ); return $article; } // Namespace might change when using redirects // Check for redirects ... - $file = ( $this->title->getNamespace() == NS_FILE ) ? $article->getFile() : null; + $file = ( $this->context->title->getNamespace() == NS_FILE ) ? $article->getFile() : null; if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content - && !$this->request->getVal( 'oldid' ) && // ... and are not old revisions - !$this->request->getVal( 'diff' ) && // ... and not when showing diff - $this->request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to + && !$this->context->request->getVal( 'oldid' ) && // ... and are not old revisions + !$this->context->request->getVal( 'diff' ) && // ... and not when showing diff + $this->context->request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to // ... and the article is not a non-redirect image page with associated file !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) ) { @@ -379,7 +355,7 @@ class MediaWiki { $ignoreRedirect = $target = false; wfRunHooks( 'InitializeArticleMaybeRedirect', - array( &$this->title, &$this->request, &$ignoreRedirect, &$target, &$article ) ); + array( &$this->context->title, &$this->context->request, &$ignoreRedirect, &$target, &$article ) ); // Follow redirects only for... redirects. // If $target is set, then a hook wanted to redirect. @@ -398,14 +374,14 @@ class MediaWiki { $rarticle = self::articleFromTitle( $target ); $rarticle->loadPageData(); if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) { - $rarticle->setRedirectedFrom( $this->title ); + $rarticle->setRedirectedFrom( $this->context->title ); $article = $rarticle; - $this->title = $target; - $this->output->setTitle( $this->title ); + $this->context->title = $target; + $this->context->output->setTitle( $this->context->title ); } } } else { - $this->title = $article->getTitle(); + $this->context->title = $article->getTitle(); } } wfProfileOut( __METHOD__ ); @@ -422,7 +398,7 @@ class MediaWiki { $factory = wfGetLBFactory(); $factory->commitMasterChanges(); // Output everything! - $this->output->output(); + $this->context->output->output(); // Do any deferred jobs wfDoUpdates( 'commit' ); // Close the session so that jobs don't access the current session @@ -488,8 +464,8 @@ class MediaWiki { wfProfileIn( __METHOD__ ); if ( !wfRunHooks( 'MediaWikiPerformAction', array( - $this->output, $article, $this->title, - $user, $this->request, $this ) ) ) + $this->context->output, $article, $this->context->title, + $user, $this->context->request, $this ) ) ) { wfProfileOut( __METHOD__ ); return; @@ -499,7 +475,7 @@ class MediaWiki { switch( $action ) { case 'view': - $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) ); + $this->context->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) ); $article->view(); break; case 'raw': // includes JS/CSS @@ -552,24 +528,24 @@ class MediaWiki { /* Continue... */ case 'edit': if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) { - $internal = $this->request->getVal( 'internaledit' ); - $external = $this->request->getVal( 'externaledit' ); - $section = $this->request->getVal( 'section' ); - $oldid = $this->request->getVal( 'oldid' ); + $internal = $this->context->request->getVal( 'internaledit' ); + $external = $this->context->request->getVal( 'externaledit' ); + $section = $this->context->request->getVal( 'section' ); + $oldid = $this->context->request->getVal( 'oldid' ); if ( !$this->getVal( 'UseExternalEditor' ) || $action == 'submit' || $internal || $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) { $editor = new EditPage( $article ); $editor->submit(); } elseif ( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) { - $mode = $this->request->getVal( 'mode' ); + $mode = $this->context->request->getVal( 'mode' ); $extedit = new ExternalEdit( $article, $mode ); $extedit->edit(); } } break; case 'history': - if ( $this->request->getFullRequestURL() == $this->title->getInternalURL( 'action=history' ) ) { - $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) ); + if ( $this->context->request->getFullRequestURL() == $this->context->title->getInternalURL( 'action=history' ) ) { + $this->context->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) ); } $history = new HistoryPage( $article ); $history->history(); @@ -586,7 +562,7 @@ class MediaWiki { break; default: if ( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) { - $this->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' ); + $this->context->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' ); } } wfProfileOut( __METHOD__ ); -- 2.20.1