From 01a238646870d6ac2230cfcb6780d499753237ff Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 15 Jan 2008 20:21:16 +0000 Subject: [PATCH] API: * Introducing ApiBase::dieUsageMsg() function which outputs the error associated with a certain message key * Updating rollback module to use dieUsageMsg(). Other modules will follow. --- includes/api/ApiBase.php | 37 +++++++++++++++++++++++++++++++- includes/api/ApiRollback.php | 41 +++++------------------------------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 0a10311af5..752e6e5024 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -549,6 +549,41 @@ abstract class ApiBase { public function dieUsage($description, $errorCode, $httpRespCode = 0) { throw new UsageException($description, $this->encodeParamName($errorCode), $httpRespCode); } + + /** + * Array that maps message keys to error messages. $1 and friends are replaced. + */ + public static $messageMap = array( + 'ns-specialprotected' => array('code' => 'unsupportednamespace', 'info' => "Pages in the Special namespace can't be edited"), + 'protectedinterface' => array('code' => 'protectednamespace-interface', 'info' => "You're not allowed to edit interface messages"), + 'namespaceprotected' => array('code' => 'protectednamespace', 'info' => "You're not allowed to edit pages in the ``\$1'' namespace"), + 'customcssjsprotected' => array('code' => 'customcssjsprotected', 'info' => "You're not allowed to edit custom CSS and JavaScript pages"), + 'cascadeprotected' => array('code' => 'cascadeprotected', 'info' =>"The page you're trying to edit is protected because it's included in a cascade-protected page"), + 'protectedpagetext' => array('code' => 'protectedpage', 'info' => "The ``\$1'' right is required to edit this page"), + 'protect-cantedit' => array('code' => 'cantedit', 'info' => "You can't protect this page because you can't edit it"), + 'badaccess-group0' => array('code' => 'permissiondenied', 'info' => "Permission denied"), // Generic permission denied message + 'badaccess-group1' => array('code' => 'permissiondenied', 'info' => "Permission denied"), // Can't use the parameter 'cause it's wikilinked + 'badaccess-group2' => array('code' => 'permissiondenied', 'info' => "Permission denied"), + 'badaccess-groups' => array('code' => 'permissiondenied', 'info' => "Permission denied"), + 'unknownerror' => array('code' => 'unknownerror', 'info' => "Unknown error"), + 'titleprotected' => array('code' => 'protectedtitle', 'info' => "This title has been protected from creation"), + 'nocreate-loggedin' => array('code' => 'cantcreate', 'info' => "You don't have permission to create new pages"), + 'nocreatetext' => array('code' => 'cantcreate-anon', 'info' => "Anonymous users can't create new pages"), + 'movenologintext' => array('code' => 'cantmove-anon', 'info' => "Anonymous users can't move pages"), + 'movenotallowed' => array('code' => 'cantmove', 'info' => "You don't have permission to move pages") + ); + + /** + * Output the error message related to a certain array + * @param array $error Element of a getUserPermissionsErrors() + */ + public function dieUsageMsg($error) { + $key = array_shift($error); + if(isset(self::$messageMap[$key])) + $this->dieUsage(wfMsgReplaceArgs(self::$messageMap[$key]['info'], $error), self::$messageMap[$key]['code']); + // If the key isn't present, throw an "unknown error + $this->dieUsage(self::$messageMap['unknownerror']['info'], self::$messageMap['unknownerror']['code']); + } /** * Internal code errors should be reported with this method @@ -679,6 +714,6 @@ abstract class ApiBase { */ public static function getBaseVersion() { return __CLASS__ . ': $Id$'; - } + } } diff --git a/includes/api/ApiRollback.php b/includes/api/ApiRollback.php index 1c7606eced..6b76be98c9 100644 --- a/includes/api/ApiRollback.php +++ b/includes/api/ApiRollback.php @@ -49,17 +49,10 @@ class ApiRollback extends ApiBase { if(!isset($params['token'])) $this->dieUsage('The token parameter must be set', 'notoken'); - // doRollback() also checks for these, but we wanna save some work - if($wgUser->isBlocked()) - $this->dieUsage('You have been blocked from editing', 'blocked'); if(wfReadOnly()) $this->dieUsage('The wiki is in read-only mode', 'readonly'); $titleObj = Title::newFromText($params['title']); - if(!$titleObj) - $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle'); - if(!$titleObj->userCan('rollback')) - $this->dieUsage('You don\'t have permission to rollback', 'permissiondenied'); $username = User::getCanonicalName($params['user']); if(!$username) @@ -67,39 +60,15 @@ class ApiRollback extends ApiBase { $articleObj = new Article($titleObj); $summary = (isset($params['summary']) ? $params['summary'] : ""); - $details = NULL; + $details = null; $dbw = wfGetDb(DB_MASTER); $dbw->begin(); $retval = $articleObj->doRollback($username, $summary, $params['token'], $params['markbot'], &$details); - switch($retval) - { - case Article::SUCCESS: - break; // We'll deal with that later - case Article::PERM_DENIED: - $this->dieUsage("You don't have permission to rollback", 'permissiondenied'); - case Article::BLOCKED: // If we get BLOCKED or PERM_DENIED that's very weird, but it's possible - $this->dieUsage('You have been blocked from editing', 'blocked'); - case Article::READONLY: - $this->dieUsage('The wiki is in read-only mode', 'readonly'); - case Article::BAD_TOKEN: - $this->dieUsage('Invalid token', 'badtoken'); - case Article::BAD_TITLE: - $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle'); - case Article::ALREADYROLLED: - $current = $details['current']; - $currentID = $current->getId(); - $this->dieUsage("The edit(s) you tried to rollback is/are already rolled back." . - "The current revision ID is ``$currentID''", 'alreadyrolled'); - case Article::ONLY_AUTHOR: - $this->dieUsage("User ``$username'' is the only author of the page", 'onlyauthor'); - case Article::RATE_LIMITED: - $this->dieUsage("You can't rollback too many articles in too short a time. Please wait a little while and try again", 'ratelimited'); - default: - // rollback() has apparently invented a new error, which is extremely weird - $this->dieDebug(__METHOD__, "rollback() returned an unknown error ($retval)"); - } - // $retval has to be Article::SUCCESS if we get here + if(!empty($retval)) + // We don't care about multiple errors, just report the first one + $this->dieUsageMsg(current($retval)); + $dbw->commit(); $current = $target = $summary = NULL; extract($details); -- 2.20.1