From d6defb96794deb944c4dde422582f8c08c8d1a9c Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Sun, 3 Apr 2011 19:39:39 +0000 Subject: [PATCH] Store the WebRequest and OutputPage in the MediaWiki class, don't pass the global variables in to each function separately. --- includes/Wiki.php | 158 ++++++++++++++++++++++++++-------------------- index.php | 10 +-- 2 files changed, 95 insertions(+), 73 deletions(-) diff --git a/includes/Wiki.php b/includes/Wiki.php index c4e2acaef4..723351dfcb 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -14,6 +14,20 @@ 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; + /** * Stores key/value pairs to circumvent global variables * Note that keys are case-insensitive! @@ -42,47 +56,65 @@ class MediaWiki { return $default; } + public function request( WebRequest &$x = null ){ + return wfSetVar( $this->request, $x ); + } + + public function output( OutputPage &$x = null ){ + return wfSetVar( $this->output, $x ); + } + + public function __construct( WebRequest &$request, /*OutputPage*/ &$output ){ + $this->request =& $request; + $this->output =& $output; + } + /** * Initialization of ... everything * Performs the request too * * @param $title Title ($wgTitle) * @param $article Article - * @param $output OutputPage * @param $user User - * @param $request WebRequest */ - public function performRequestForTitle( &$title, &$article, &$output, &$user, $request ) { + public function performRequestForTitle( &$title, &$article, &$user ) { wfProfileIn( __METHOD__ ); - $output->setTitle( $title ); - if ( $request->getVal( 'printable' ) === 'yes' ) { - $output->setPrintable(); + $this->output->setTitle( $title ); + if ( $this->request->getVal( 'printable' ) === 'yes' ) { + $this->output->setPrintable(); } - wfRunHooks( 'BeforeInitialize', array( &$title, &$article, &$output, &$user, $request, $this ) ); + wfRunHooks( 'BeforeInitialize', array( + &$title, + &$article, + &$this->output, + &$user, + $this->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( $title ) && !$title->userCanRead() ) { - $output->loginToUse(); - $this->finalCleanup( $output ); - $output->disable(); + $this->output->loginToUse(); + $this->finalCleanup(); + $this->output->disable(); wfProfileOut( __METHOD__ ); return false; } // Call handleSpecialCases() to deal with all special requests... - if ( !$this->handleSpecialCases( $title, $output, $request ) ) { + if ( !$this->handleSpecialCases( $title ) ) { // ...otherwise treat it as an article view. The article // may be a redirect to another article or URL. - $new_article = $this->initializeArticle( $title, $output, $request ); + $new_article = $this->initializeArticle( $title ); if ( is_object( $new_article ) ) { $article = $new_article; - $this->performAction( $output, $article, $title, $user, $request ); + $this->performAction( $article, $title, $user ); } elseif ( is_string( $new_article ) ) { - $output->redirect( $new_article ); + $this->output->redirect( $new_article ); } else { wfProfileOut( __METHOD__ ); throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" ); @@ -95,16 +127,15 @@ class MediaWiki { * Checks some initial queries * FIXME: rename to parseTitle() ? * - * @param $request WebRequest * @return Title object to be $wgTitle */ - /* private */ function checkInitialQueries( WebRequest $request ) { + /* private */ function checkInitialQueries() { global $wgContLang; - $curid = $request->getInt( 'curid' ); - $title = $request->getVal( 'title' ); + $curid = $this->request->getInt( 'curid' ); + $title = $this->request->getVal( 'title' ); - if ( $request->getCheck( 'search' ) ) { + if ( $this->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). @@ -112,7 +143,7 @@ class MediaWiki { } elseif ( $curid ) { // URLs like this are generated by RC, because rc_title isn't always accurate $ret = Title::newFromID( $curid ); - } elseif ( $title == '' && $this->getAction( $request ) != 'delete' ) { + } elseif ( $title == '' && $this->getAction() != 'delete' ) { $ret = Title::newMainPage(); } else { $ret = Title::newFromURL( $title ); @@ -124,8 +155,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 = $request->getInt( 'oldid' ); - $oldid = $oldid ? $oldid : $request->getInt( 'diff' ); + $oldid = $this->request->getInt( 'oldid' ); + $oldid = $oldid ? $oldid : $this->request->getInt( 'diff' ); // Allow oldid to override a changed or missing title if ( $oldid ) { $rev = Revision::newFromId( $oldid ); @@ -143,44 +174,42 @@ class MediaWiki { * - special pages * * @param $title Title - * @param $output OutputPage - * @param $request WebRequest * @return bool true if the request is already executed */ - private function handleSpecialCases( &$title, &$output, $request ) { + private function handleSpecialCases( &$title ) { wfProfileIn( __METHOD__ ); // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty. if ( is_null( $title ) || ( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) { $title = SpecialPage::getTitleFor( 'Badtitle' ); - $output->setTitle( $title ); // bug 21456 + $this->output->setTitle( $title ); // bug 21456 // Die now before we mess up $wgArticle and the skin stops working throw new ErrorPageError( 'badtitle', 'badtitletext' ); // Interwiki redirects } else if ( $title->getInterwiki() != '' ) { - $rdfrom = $request->getVal( 'rdfrom' ); + $rdfrom = $this->request->getVal( 'rdfrom' ); if ( $rdfrom ) { $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) ); } else { - $query = $request->getValues(); + $query = $this->request->getValues(); unset( $query['title'] ); $url = $title->getFullURL( $query ); } /* Check for a redirect loop */ if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $title->isLocal() ) { // 301 so google et al report the target as the actual url. - $output->redirect( $url, 301 ); + $this->output->redirect( $url, 301 ); } else { $title = SpecialPage::getTitleFor( 'Badtitle' ); - $output->setTitle( $title ); // bug 21456 + $this->output->setTitle( $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 ( $request->getVal( 'action', 'view' ) == 'view' && !$request->wasPosted() - && ( $request->getVal( 'title' ) === null || $title->getPrefixedDBKey() != $request->getVal( 'title' ) ) - && !count( array_diff( array_keys( $request->getValues() ), array( 'action', 'title' ) ) ) ) + } else if ( $this->request->getVal( 'action', 'view' ) == 'view' && !$this->request->wasPosted() + && ( $this->request->getVal( 'title' ) === null || $title->getPrefixedDBKey() != $this->request->getVal( 'title' ) ) + && !count( array_diff( array_keys( $this->request->getValues() ), array( 'action', 'title' ) ) ) ) { if ( $title->getNamespace() == NS_SPECIAL ) { list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() ); @@ -190,7 +219,7 @@ class MediaWiki { } $targetUrl = $title->getFullURL(); // Redirect to canonical url, make it a 301 to allow caching - if ( $targetUrl == $request->getFullRequestURL() ) { + if ( $targetUrl == $this->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 " . @@ -214,8 +243,8 @@ class MediaWiki { wfProfileOut( __METHOD__ ); return false; } else { - $output->setSquidMaxage( 1200 ); - $output->redirect( $targetUrl, '301' ); + $this->output->setSquidMaxage( 1200 ); + $this->output->redirect( $targetUrl, '301' ); } // Special pages } else if ( NS_SPECIAL == $title->getNamespace() ) { @@ -264,13 +293,12 @@ class MediaWiki { * passed through the "action" parameter. Actions disabled in * $wgDisabledActions will be replaced by "nosuchaction" * - * @param $request WebRequest * @return String: action */ - public function getAction( WebRequest $request ) { + public function getAction() { global $wgDisabledActions; - $action = $request->getVal( 'action', 'view' ); + $action = $this->request->getVal( 'action', 'view' ); // Check for disabled actions if ( in_array( $action, $wgDisabledActions ) ) { @@ -280,9 +308,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 ( $request->getBool( 'revisiondelete' ) ) { + if ( $this->request->getBool( 'revisiondelete' ) ) { return 'revisiondelete'; - } elseif ( $request->getBool( 'revisionmove' ) ) { + } elseif ( $this->request->getBool( 'revisionmove' ) ) { return 'revisionmove'; } else { return 'view'; @@ -299,14 +327,12 @@ class MediaWiki { * Create an Article object for the page, following redirects if needed. * * @param $title Title ($wgTitle) - * @param $output OutputPage ($wgOut) - * @param $request WebRequest ($wgRequest) * @return mixed an Article, or a string to redirect to another URL */ - private function initializeArticle( &$title, &$output, $request ) { + private function initializeArticle( &$title ) { wfProfileIn( __METHOD__ ); - $action = $request->getVal( 'action', 'view' ); + $action = $this->request->getVal( 'action', 'view' ); $article = self::articleFromTitle( $title ); // NS_MEDIAWIKI has no redirects. // It is also used for CSS/JS, so performance matters here... @@ -318,9 +344,9 @@ class MediaWiki { // Check for redirects ... $file = ( $title->getNamespace() == NS_FILE ) ? $article->getFile() : null; if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content - && !$request->getVal( 'oldid' ) && // ... and are not old revisions - !$request->getVal( 'diff' ) && // ... and not when showing diff - $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to + && !$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 // ... and the article is not a non-redirect image page with associated file !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) ) { @@ -328,7 +354,7 @@ class MediaWiki { $ignoreRedirect = $target = false; wfRunHooks( 'InitializeArticleMaybeRedirect', - array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) ); + array( &$title, &$this->request, &$ignoreRedirect, &$target, &$article ) ); // Follow redirects only for... redirects. // If $target is set, then a hook wanted to redirect. @@ -350,7 +376,7 @@ class MediaWiki { $rarticle->setRedirectedFrom( $title ); $article = $rarticle; $title = $target; - $output->setTitle( $title ); + $this->output->setTitle( $title ); } } } else { @@ -363,17 +389,15 @@ class MediaWiki { /** * Cleaning up request by doing deferred updates, DB transaction, and the output - * - * @param $output OutputPage */ - public function finalCleanup( &$output ) { + public function finalCleanup() { wfProfileIn( __METHOD__ ); // Now commit any transactions, so that unreported errors after // output() don't roll back the whole DB transaction $factory = wfGetLBFactory(); $factory->commitMasterChanges(); // Output everything! - $output->output(); + $this->output->output(); // Do any deferred jobs wfDoUpdates( 'commit' ); // Close the session so that jobs don't access the current session @@ -432,25 +456,23 @@ class MediaWiki { /** * Perform one of the "standard" actions * - * @param $output OutputPage * @param $article Article * @param $title Title * @param $user User - * @param $request WebRequest */ - private function performAction( &$output, &$article, &$title, &$user, &$request ) { + private function performAction( &$article, &$title, &$user ) { wfProfileIn( __METHOD__ ); - if ( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) { + if ( !wfRunHooks( 'MediaWikiPerformAction', array( $this->output, $article, $title, $user, $this->request, $this ) ) ) { wfProfileOut( __METHOD__ ); return; } - $action = $this->getAction( $request ); + $action = $this->getAction(); switch( $action ) { case 'view': - $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) ); + $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) ); $article->view(); break; case 'raw': // includes JS/CSS @@ -503,24 +525,24 @@ class MediaWiki { /* Continue... */ case 'edit': if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) { - $internal = $request->getVal( 'internaledit' ); - $external = $request->getVal( 'externaledit' ); - $section = $request->getVal( 'section' ); - $oldid = $request->getVal( 'oldid' ); + $internal = $this->request->getVal( 'internaledit' ); + $external = $this->request->getVal( 'externaledit' ); + $section = $this->request->getVal( 'section' ); + $oldid = $this->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 = $request->getVal( 'mode' ); + $mode = $this->request->getVal( 'mode' ); $extedit = new ExternalEdit( $article, $mode ); $extedit->edit(); } } break; case 'history': - if ( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) { - $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) ); + if ( $this->request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) { + $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) ); } $history = new HistoryPage( $article ); $history->history(); @@ -537,7 +559,7 @@ class MediaWiki { break; default: if ( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) { - $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' ); + $this->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' ); } } wfProfileOut( __METHOD__ ); diff --git a/index.php b/index.php index 89a6bd0ec2..7184ebf34a 100644 --- a/index.php +++ b/index.php @@ -44,7 +44,7 @@ wfProfileIn( 'index.php' ); wfProfileIn( 'index.php-setup' ); # Initialize MediaWiki base class -$mediaWiki = new MediaWiki(); +$mediaWiki = new MediaWiki( $wgRequest, $wgOut ); $maxLag = $wgRequest->getVal( 'maxlag' ); if ( !is_null( $maxLag ) ) { @@ -56,7 +56,7 @@ if ( !is_null( $maxLag ) ) { } # Set title from request parameters -$wgTitle = $mediaWiki->checkInitialQueries( $wgRequest ); +$wgTitle = $mediaWiki->checkInitialQueries(); $action = $wgRequest->getVal( 'action', 'view' ); wfProfileOut( 'index.php-setup' ); @@ -88,7 +88,7 @@ if ( $wgUseFileCache && $wgTitle !== null ) { # Tell $wgOut that output is taken care of $wgOut->disable(); wfProfileOut( 'index.php-filecache' ); - $mediaWiki->finalCleanup( $wgOut ); + $mediaWiki->finalCleanup(); wfProfileOut( 'index.php' ); $mediaWiki->restInPeace(); exit; @@ -106,8 +106,8 @@ $mediaWiki->setVal( 'SquidMaxage', $wgSquidMaxage ); $mediaWiki->setVal( 'UseExternalEditor', $wgUseExternalEditor ); $mediaWiki->setVal( 'UsePathInfo', $wgUsePathInfo ); -$mediaWiki->performRequestForTitle( $wgTitle, $wgArticle, $wgOut, $wgUser, $wgRequest ); -$mediaWiki->finalCleanup( $wgOut ); +$mediaWiki->performRequestForTitle( $wgTitle, $wgArticle, $wgUser ); +$mediaWiki->finalCleanup(); wfProfileOut( 'index.php' ); -- 2.20.1