From 47e3f922dab3d26966c00a929f25e2659f6d9881 Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Wed, 13 Apr 2011 14:30:55 +0000 Subject: [PATCH] Implement user-is-blocked and wiki-is-read-only as exceptions. --- includes/AutoLoader.php | 2 ++ includes/Exception.php | 50 ++++++++++++++++++++++++++++++++++++++ includes/OutputPage.php | 53 ++++------------------------------------- 3 files changed, 56 insertions(+), 49 deletions(-) diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 751a6d6bdb..9ba9d863c5 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -196,6 +196,7 @@ $wgAutoloadLocalClasses = array( 'RawPage' => 'includes/RawPage.php', 'RCCacheEntry' => 'includes/ChangesList.php', 'RdfMetaData' => 'includes/Metadata.php', + 'ReadOnlyError' => 'includes/Exception.php', 'RecentChange' => 'includes/RecentChange.php', 'RegexlikeReplacer' => 'includes/StringUtils.php', 'ReplacementArray' => 'includes/StringUtils.php', @@ -249,6 +250,7 @@ $wgAutoloadLocalClasses = array( 'User' => 'includes/User.php', 'UserArray' => 'includes/UserArray.php', 'UserArrayFromResult' => 'includes/UserArray.php', + 'UserBlockedError' => 'includes/Exception.php', 'UserMailer' => 'includes/UserMailer.php', 'UserRightsProxy' => 'includes/UserRightsProxy.php', 'ViewCountUpdate' => 'includes/ViewCountUpdate.php', diff --git a/includes/Exception.php b/includes/Exception.php index ea8986d883..ad345a3bbf 100644 --- a/includes/Exception.php +++ b/includes/Exception.php @@ -370,6 +370,56 @@ class PermissionsError extends ErrorPageError { } } +/** + * Show an error when the wiki is locked/read-only and the user tries to do + * something that requires write access + */ +class ReadOnlyError extends ErrorPageError { + public function __construct(){ + parent::__construct( + 'readonly', + 'readonlytext', + wfReadOnlyReason() + ); + } +} + +/** + * Show an error when the user tries to do something whilst blocked + */ +class UserBlockedError extends ErrorPageError { + public function __construct( Block $block ){ + global $wgLang; + + $blockerUserpage = $block->getBlocker()->getUserPage(); + $link = "[[{$blockerUserpage->getPrefixedText()}|{$blockerUserpage->getText()}]]"; + + $reason = $block->mReason; + if( $reason == '' ) { + $reason = wfMsg( 'blockednoreason' ); + } + + /* $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 = $block->getTarget(); + + parent::__construct( + 'blockedtitle', + $block->mAuto ? 'autoblocketext' : 'blockedtext', + array( + $link, + $reason, + wfGetIP(), + $block->getBlocker()->getName(), + $block->getId(), + $wgLang->formatExpiry( $block->mExpiry ), + $intended, + $wgLang->timeanddate( wfTimestamp( TS_MW, $block->mTimestamp ), true ) + ) + ); + } +} + /** * Install an exception handler for MediaWiki exception types. */ diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 1d1df0efcb..34eac1e71b 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -1933,53 +1933,10 @@ class OutputPage { /** * Produce a "user is blocked" page. - * - * @param $return Boolean: whether to have a "return to $wgTitle" message or not. - * @return nothing + * @deprecated since 1.18 */ - function blockedPage( $return = true ) { - global $wgContLang; - - $this->setPageTitle( wfMsg( 'blockedtitle' ) ); - $this->setRobotPolicy( 'noindex,nofollow' ); - $this->setArticleRelated( false ); - - $name = $this->getUser()->blockedBy(); - $reason = $this->getUser()->blockedFor(); - if( $reason == '' ) { - $reason = wfMsg( 'blockednoreason' ); - } - $blockTimestamp = $this->getContext()->getLang()->timeanddate( - wfTimestamp( TS_MW, $this->getUser()->mBlock->mTimestamp ), true - ); - $ip = wfGetIP(); - - $link = '[[' . $wgContLang->getNsText( NS_USER ) . ":{$name}|{$name}]]"; - - $blockid = $this->getUser()->mBlock->getId(); - - $blockExpiry = $this->getContext()->getLang()->formatExpiry( $this->getUser()->mBlock->mExpiry ); - - if ( $this->getUser()->mBlock->mAuto ) { - $msg = 'autoblockedtext'; - } else { - $msg = 'blockedtext'; - } - - /* $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 = $this->getUser()->mBlock->getTarget(); - - $this->addWikiMsg( - $msg, $link, $reason, $ip, $name, $blockid, $blockExpiry, - $intended, $blockTimestamp - ); - - # Don't auto-return to special pages - if( $return ) { - $return = $this->getTitle()->getNamespace() > -1 ? $this->getTitle() : null; - $this->returnToMain( null, $return ); - } + function blockedPage() { + throw new UserBlockedError( $this->getUser()->mBlock ); } /** @@ -2163,9 +2120,7 @@ class OutputPage { $this->addWikiText( $this->formatPermissionsErrorMessage( $reasons, $action ) ); } else { // Wiki is read only - $this->setPageTitle( wfMsg( 'readonly' ) ); - $reason = wfReadOnlyReason(); - $this->wrapWikiMsg( "
\n$1\n
", array( 'readonlytext', $reason ) ); + throw new ReadOnlyError; } // Show source, if supplied -- 2.20.1