From 86583c7c8f5f10b06412321299c3c2285577b3c0 Mon Sep 17 00:00:00 2001 From: Daniel Friesen Date: Sun, 3 Apr 2011 03:59:47 +0000 Subject: [PATCH] Start better utalizing OutputPage as the focal point for things related to the output of the page. Like getTitle use $out->get{User,Skin} instead of $wgUser and $wgUser->getSkin() for things where the user or skin is being used for output generation back to $out instead of using globals with bad structure. --- includes/Article.php | 56 ++++++++--------- includes/OutputPage.php | 124 +++++++++++++++++++++++--------------- includes/SpecialPage.php | 3 +- includes/api/ApiParse.php | 1 + 4 files changed, 107 insertions(+), 77 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index 53334b5db8..0ae10323a7 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -1580,7 +1580,7 @@ class Article { * Builds trackback links for article display if $wgUseTrackbacks is set to true */ public function addTrackbacks() { - global $wgOut, $wgUser; + global $wgOut; $dbr = wfGetDB( DB_SLAVE ); $tbs = $dbr->select( 'trackbacks', @@ -1598,9 +1598,9 @@ class Article { foreach ( $tbs as $o ) { $rmvtxt = ""; - if ( $wgUser->isAllowed( 'trackback' ) ) { + if ( $wgOut->getUser()->isAllowed( 'trackback' ) ) { $delurl = $this->mTitle->getFullURL( "action=deletetrackback&tbid=" . - $o->tb_id . "&token=" . urlencode( $wgUser->editToken() ) ); + $o->tb_id . "&token=" . urlencode( $wgOut->getUser()->editToken() ) ); $rmvtxt = wfMsg( 'trackbackremove', htmlspecialchars( $delurl ) ); } @@ -1620,15 +1620,15 @@ class Article { * Removes trackback record for current article from trackbacks table */ public function deletetrackback() { - global $wgUser, $wgRequest, $wgOut; + global $wgRequest, $wgOut; - if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'token' ) ) ) { + if ( !$wgOut->getUser()->matchEditToken( $wgRequest->getVal( 'token' ) ) ) { $wgOut->addWikiMsg( 'sessionfailure' ); return; } - $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgUser ); + $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgOut->getUser() ); if ( count( $permission_errors ) ) { $wgOut->showPermissionsErrorPage( $permission_errors ); @@ -1658,9 +1658,9 @@ class Article { * Handle action=purge */ public function purge() { - global $wgUser, $wgRequest, $wgOut; + global $wgRequest, $wgOut; - if ( $wgUser->isAllowed( 'purge' ) || $wgRequest->wasPosted() ) { + if ( $wgOut->getUser()->isAllowed( 'purge' ) || $wgRequest->wasPosted() ) { //FIXME: shouldn't this be in doPurge()? if ( wfRunHooks( 'ArticlePurge', array( &$this ) ) ) { $this->doPurge(); @@ -2273,14 +2273,14 @@ class Article { * Mark this particular edit/page as patrolled */ public function markpatrolled() { - global $wgOut, $wgUser, $wgRequest; + global $wgOut, $wgRequest; $wgOut->setRobotPolicy( 'noindex,nofollow' ); # If we haven't been given an rc_id value, we can't do anything $rcid = (int) $wgRequest->getVal( 'rcid' ); - if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'token' ), $rcid ) ) { + if ( !$wgOut->getUser()->matchEditToken( $wgRequest->getVal( 'token' ), $rcid ) ) { $wgOut->showErrorPage( 'sessionfailure-title', 'sessionfailure' ); return; } @@ -2333,9 +2333,9 @@ class Article { * User-interface handler for the "watch" action */ public function watch() { - global $wgUser, $wgOut; + global $wgOut; - if ( $wgUser->isAnon() ) { + if ( $wgOut->getUser()->isAnon() ) { $wgOut->showErrorPage( 'watchnologin', 'watchnologintext' ); return; } @@ -2380,9 +2380,9 @@ class Article { * User interface handler for the "unwatch" action. */ public function unwatch() { - global $wgUser, $wgOut; + global $wgOut; - if ( $wgUser->isAnon() ) { + if ( $wgOut->getUser()->isAnon() ) { $wgOut->showErrorPage( 'watchnologin', 'watchnologintext' ); return; } @@ -2742,10 +2742,10 @@ class Article { * UI entry point for page deletion */ public function delete() { - global $wgUser, $wgOut, $wgRequest; + global $wgOut, $wgRequest; $confirm = $wgRequest->wasPosted() && - $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ); + $wgOut->getUser()->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ); $this->DeleteReasonList = $wgRequest->getText( 'wpDeleteReasonList', 'other' ); $this->DeleteReason = $wgRequest->getText( 'wpReason' ); @@ -2760,7 +2760,7 @@ class Article { } # Flag to hide all contents of the archived revisions - $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed( 'suppressrevision' ); + $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgOut->getUser()->isAllowed( 'suppressrevision' ); # This code desperately needs to be totally rewritten @@ -2772,7 +2772,7 @@ class Article { } # Check permissions - $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgUser ); + $permission_errors = $this->mTitle->getUserPermissionsErrors( 'delete', $wgOut->getUser() ); if ( count( $permission_errors ) > 0 ) { $wgOut->showPermissionsErrorPage( $permission_errors ); @@ -2818,7 +2818,7 @@ class Article { if ( $confirm ) { $this->doDelete( $reason, $suppress ); - if ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) { + if ( $wgRequest->getCheck( 'wpWatch' ) && $wgOut->getUser()->isLoggedIn() ) { $this->doWatch(); } elseif ( $this->mTitle->userIsWatching() ) { $this->doUnwatch(); @@ -2837,7 +2837,7 @@ class Article { if ( $hasHistory && !$confirm ) { global $wgLang; - $skin = $wgUser->getSkin(); + $skin = $wgOut->getSkin(); $revisions = $this->estimateRevisionCount(); //FIXME: lego $wgOut->addHTML( '' . @@ -2944,18 +2944,18 @@ class Article { * @param $reason String: prefilled reason */ public function confirmDelete( $reason ) { - global $wgOut, $wgUser; + global $wgOut; wfDebug( "Article::confirmDelete\n" ); - $deleteBackLink = $wgUser->getSkin()->linkKnown( $this->mTitle ); + $deleteBackLink = $wgOut->getSkin()->linkKnown( $this->mTitle ); $wgOut->setSubtitle( wfMsgHtml( 'delete-backlink', $deleteBackLink ) ); $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->addWikiMsg( 'confirmdeletetext' ); wfRunHooks( 'ArticleConfirmDelete', array( $this, $wgOut, &$reason ) ); - if ( $wgUser->isAllowed( 'suppressrevision' ) ) { + if ( $wgOut->getUser()->isAllowed( 'suppressrevision' ) ) { $suppress = " " . @@ -2966,7 +2966,7 @@ class Article { } else { $suppress = ''; } - $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->mTitle->userIsWatching(); + $checkWatch = $wgOut->getUser()->getBoolOption( 'watchdeletion' ) || $this->mTitle->userIsWatching(); $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle->getLocalURL( 'action=delete' ), 'id' => 'deleteconfirm' ) ) . @@ -2999,7 +2999,7 @@ class Article { "; # Disallow watching if user is not logged in - if ( $wgUser->isLoggedIn() ) { + if ( $wgOut->getUser()->isLoggedIn() ) { $form .= " @@ -3021,11 +3021,11 @@ class Article { " . Xml::closeElement( 'table' ) . Xml::closeElement( 'fieldset' ) . - Html::hidden( 'wpEditToken', $wgUser->editToken() ) . + Html::hidden( 'wpEditToken', $wgOut->getUser()->editToken() ) . Xml::closeElement( 'form' ); - if ( $wgUser->isAllowed( 'editinterface' ) ) { - $skin = $wgUser->getSkin(); + if ( $wgOut->getUser()->isAllowed( 'editinterface' ) ) { + $skin = $wgOut->getSkin(); $title = Title::makeTitle( NS_MEDIAWIKI, 'Deletereason-dropdown' ); $link = $skin->link( $title, diff --git a/includes/OutputPage.php b/includes/OutputPage.php index cd53914978..cc24ea661c 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -193,6 +193,9 @@ class OutputPage { /// Stores a Title object (of the current page). protected $mTitle = null; + /// Stores a User object (the one the page is being rendered for) + protected $mUser = null; + /** * An array of stylesheet filenames (relative from skins path), with options * for CSS media, IE conditions, and RTL/LTR direction. @@ -549,7 +552,7 @@ class OutputPage { * @return Boolean: true iff cache-ok headers was sent. */ public function checkLastModified( $timestamp ) { - global $wgCachePages, $wgCacheEpoch, $wgUser, $wgRequest; + global $wgCachePages, $wgCacheEpoch, $wgRequest; if ( !$timestamp || $timestamp == '19700101000000' ) { wfDebug( __METHOD__ . ": CACHE DISABLED, NO TIMESTAMP\n" ); @@ -559,7 +562,7 @@ class OutputPage { wfDebug( __METHOD__ . ": CACHE DISABLED\n", false ); return false; } - if( $wgUser->getOption( 'nocache' ) ) { + if( $this->getUser()->getOption( 'nocache' ) ) { wfDebug( __METHOD__ . ": USER DISABLED CACHE\n", false ); return false; } @@ -567,7 +570,7 @@ class OutputPage { $timestamp = wfTimestamp( TS_MW, $timestamp ); $modifiedTimes = array( 'page' => $timestamp, - 'user' => $wgUser->getTouched(), + 'user' => $this->getUser()->getTouched(), 'epoch' => $wgCacheEpoch ); wfRunHooks( 'OutputPageCheckLastModified', array( &$modifiedTimes ) ); @@ -772,6 +775,40 @@ class OutputPage { } } + /** + * Set the User object to use + * + * @param $u User object + */ + public function setUser( $u ) { + $this->mUser = $u; + } + + /** + * Get the User object used in this instance + * + * @return User + */ + 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; + } + + /** + * Get the Skin object used to render this instance + * + * @return Skin + */ + 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(); + } + /** * Replace the subtile with $str * @@ -1009,7 +1046,7 @@ class OutputPage { * @param $categories Array mapping category name => sort key */ public function addCategoryLinks( $categories ) { - global $wgUser, $wgContLang; + global $wgContLang; if ( !is_array( $categories ) || count( $categories ) == 0 ) { return; @@ -1048,7 +1085,6 @@ class OutputPage { # Add the remaining categories to the skin if ( wfRunHooks( 'OutputPageMakeCategoryLinks', array( &$this, $categories, &$this->mCategoryLinks ) ) ) { - $sk = $wgUser->getSkin(); foreach ( $categories as $category => $type ) { $origcategory = $category; $title = Title::makeTitleSafe( NS_CATEGORY, $category ); @@ -1060,7 +1096,7 @@ class OutputPage { } $text = $wgContLang->convertHtml( $title->getText() ); $this->mCategories[] = $title->getText(); - $this->mCategoryLinks[$type][] = $sk->link( $title, $text ); + $this->mCategoryLinks[$type][] = $this->getSkin()->link( $title, $text ); } } } @@ -1768,7 +1804,7 @@ class OutputPage { * the object, let's actually output it: */ public function output() { - global $wgUser, $wgOutputEncoding, $wgRequest; + global $wgOutputEncoding, $wgRequest; global $wgLanguageCode, $wgDebugRedirects, $wgMimeType; if( $this->mDoNothing ) { @@ -1826,7 +1862,7 @@ class OutputPage { } else { $this->addDefaultModules(); - $sk = $wgUser->getSkin( $this->getTitle() ); + $sk = $this->getSkin( $this->getTitle() ); // Hook that allows last minute changes to the output page, e.g. // adding of CSS or Javascript by extensions. @@ -1868,29 +1904,29 @@ class OutputPage { * @return nothing */ function blockedPage( $return = true ) { - global $wgUser, $wgContLang, $wgLang; + global $wgContLang, $wgLang; $this->setPageTitle( wfMsg( 'blockedtitle' ) ); $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); - $name = $wgUser->blockedBy(); - $reason = $wgUser->blockedFor(); + $name = $this->getUser()->blockedBy(); + $reason = $this->getUser()->blockedFor(); if( $reason == '' ) { $reason = wfMsg( 'blockednoreason' ); } $blockTimestamp = $wgLang->timeanddate( - wfTimestamp( TS_MW, $wgUser->mBlock->mTimestamp ), true + wfTimestamp( TS_MW, $this->getUser()->mBlock->mTimestamp ), true ); $ip = wfGetIP(); $link = '[[' . $wgContLang->getNsText( NS_USER ) . ":{$name}|{$name}]]"; - $blockid = $wgUser->mBlock->getId(); + $blockid = $this->getUser()->mBlock->getId(); - $blockExpiry = $wgLang->formatExpiry( $wgUser->mBlock->mExpiry ); + $blockExpiry = $wgLang->formatExpiry( $this->getUser()->mBlock->mExpiry ); - if ( $wgUser->mBlock->mAuto ) { + if ( $this->getUser()->mBlock->mAuto ) { $msg = 'autoblockedtext'; } else { $msg = 'blockedtext'; @@ -1898,7 +1934,7 @@ class OutputPage { /* $ip returns who *is* being blocked, $intended contains who was meant to be blocked. * This could be a username, an IP range, or a single IP. */ - $intended = $wgUser->mBlock->getTarget(); + $intended = $this->getUser()->mBlock->getTarget(); $this->addWikiMsg( $msg, $link, $reason, $ip, $name, $blockid, $blockExpiry, @@ -2004,22 +2040,18 @@ class OutputPage { * Produce the stock "please login to use the wiki" page */ public function loginToUse() { - global $wgUser; - - if( $wgUser->isLoggedIn() ) { + if( $this->getUser()->isLoggedIn() ) { $this->permissionRequired( 'read' ); return; } - $skin = $wgUser->getSkin(); - $this->setPageTitle( wfMsg( 'loginreqtitle' ) ); $this->setHtmlTitle( wfMsg( 'errorpagetitle' ) ); $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); $loginTitle = SpecialPage::getTitleFor( 'Userlogin' ); - $loginLink = $skin->link( + $loginLink = $this->getSkin()->link( $loginTitle, wfMsgHtml( 'loginreqlink' ), array(), @@ -2095,9 +2127,6 @@ class OutputPage { * @param $action String: action that was denied or null if unknown */ public function readOnlyPage( $source = null, $protected = false, $reasons = array(), $action = null ) { - global $wgUser; - $skin = $wgUser->getSkin(); - $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); @@ -2112,7 +2141,7 @@ class OutputPage { if( $source ) { $this->setPageTitle( wfMsg( 'viewsource' ) ); $this->setSubtitle( - wfMsg( 'viewsourcefor', $skin->linkKnown( $this->getTitle() ) ) + wfMsg( 'viewsourcefor', $this->getSkin()->linkKnown( $this->getTitle() ) ) ); } else { $this->setPageTitle( wfMsg( 'badaccess' ) ); @@ -2132,14 +2161,14 @@ class OutputPage { $params = array( 'id' => 'wpTextbox1', 'name' => 'wpTextbox1', - 'cols' => $wgUser->getOption( 'cols' ), - 'rows' => $wgUser->getOption( 'rows' ), + 'cols' => $this->getUser()->getOption( 'cols' ), + 'rows' => $this->getUser()->getOption( 'rows' ), 'readonly' => 'readonly' ); $this->addHTML( Html::element( 'textarea', $params, $source ) ); // Show templates used by this article - $skin = $wgUser->getSkin(); + $skin = $this->getSkin(); $article = new Article( $this->getTitle() ); $this->addHTML( "
{$skin->formatTemplates( $article->getUsedTemplates() )} @@ -2213,11 +2242,10 @@ class OutputPage { * @param $text String text of the link (input is not escaped) */ public function addReturnTo( $title, $query = array(), $text = null ) { - global $wgUser; $this->addLink( array( 'rel' => 'next', 'href' => $title->getFullURL() ) ); $link = wfMsgHtml( 'returnto', - $wgUser->getSkin()->link( $title, $text, array(), $query ) + $this->getSkin()->link( $title, $text, array(), $query ) ); $this->addHTML( "

{$link}

\n" ); } @@ -2265,7 +2293,7 @@ class OutputPage { public function headElement( Skin $sk, $includeStyle = true ) { global $wgOutputEncoding, $wgMimeType; global $wgUseTrackbacks, $wgHtml5; - global $wgUser, $wgRequest, $wgLang; + global $wgRequest, $wgLang; if ( $sk->commonPrintStylesheet() ) { $this->addModuleStyles( 'mediawiki.legacy.wikiprintable' ); @@ -2310,7 +2338,7 @@ class OutputPage { if ( $this->getTitle()->getNamespace() != NS_SPECIAL && in_array( $action, array( 'view', 'purge' ) ) && - $wgUser->getOption( 'editondblclick' ) + $this->getUser()->getOption( 'editondblclick' ) ) { $bodyAttrs['ondblclick'] = "document.location = '" . Xml::escapeJsString( $this->getTitle()->getEditURL() ) . "'"; @@ -2339,7 +2367,7 @@ class OutputPage { * Add the default ResourceLoader modules to this object */ private function addDefaultModules() { - global $wgUser, $wgIncludeLegacyJavaScript, + global $wgIncludeLegacyJavaScript, $wgUseAjax, $wgAjaxWatch, $wgEnableMWSuggest; // Add base resources @@ -2354,16 +2382,16 @@ class OutputPage { wfRunHooks( 'AjaxAddScript', array( &$this ) ); - if( $wgAjaxWatch && $wgUser->isLoggedIn() ) { + if( $wgAjaxWatch && $this->getUser()->isLoggedIn() ) { $this->addModules( 'mediawiki.action.watch.ajax' ); } - if ( $wgEnableMWSuggest && !$wgUser->getOption( 'disablesuggest', false ) ) { + if ( $wgEnableMWSuggest && !$this->getUser()->getOption( 'disablesuggest', false ) ) { $this->addModules( 'mediawiki.legacy.mwsuggest' ); } } - if( $wgUser->getBoolOption( 'editsectiononrightclick' ) ) { + if( $this->getUser()->getBoolOption( 'editsectiononrightclick' ) ) { $this->addModules( 'mediawiki.action.view.rightClickEdit' ); } } @@ -2389,7 +2417,7 @@ class OutputPage { * @return string html