API:
authorRoan Kattouw <catrope@users.mediawiki.org>
Fri, 18 Jan 2008 14:34:14 +0000 (14:34 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Fri, 18 Jan 2008 14:34:14 +0000 (14:34 +0000)
* Refactored ApiDelete to use the new dieUsageMsg() system
* Adding some more messages to ApiBase::$messageMap
* Also using dieUsageMsg() for API-related errors in ApiRollback
* Removing 'lo' prefix for ApiLogout as it doesn't have any parameters anyway

includes/api/ApiBase.php
includes/api/ApiDelete.php
includes/api/ApiLogout.php
includes/api/ApiRollback.php

index 752e6e5..d690603 100644 (file)
@@ -554,6 +554,10 @@ abstract class ApiBase {
         * Array that maps message keys to error messages. $1 and friends are replaced.
         */
        public static $messageMap = array(
+               // This one MUST be present, or dieUsageMsg() will recurse infinitely
+               'unknownerror' => array('code' => 'unknownerror', 'info' => "Unknown error: ``\$1''"),
+               
+               // Messages from Title::getUserPermissionsErrors()
                '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"),
@@ -565,12 +569,27 @@ abstract class ApiBase {
                '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")
+               'movenotallowed' => array('code' => 'cantmove', 'info' => "You don't have permission to move pages"),
+               
+               // Miscellaneous interface messages
+               'alreadyrolled' => array('code' => 'alreadyrolled', 'info' => "The page you tried to rollback was already rolled back"),
+               'cantrollback' => array('code' => 'onlyauthor', 'info' => "The page you tried to rollback only has one author"), 
+               'blocked' => array('code' => 'blocked', 'info' => "You have been blocked from editing"),
+               'readonlytext' => array('code' => 'readonly', 'info' => "The wiki is currently in read-only mode"),
+               'sessionfailure' => array('code' => 'badtoken', 'info' => "Invalid token"),
+               'cannotdelete' => array('code' => 'cantdelete', 'info' => "Couldn't delete ``\$1''. Maybe it was deleted already by someone else"),
+               'notanarticle' => array('code' => 'missingtitle', 'info' => "The page you requested doesn't exist"),
+               
+               // API-specific messages
+               'notitle' => array('code' => 'notitle', 'info' => "The title parameter must be set"),
+               'notoken' => array('code' => 'notoken', 'info' => "The token parameter must be set"),
+               'nouser' => array('code' => 'nouser', 'info' => "The user parameter must be set"),
+               'invalidtitle' => array('code' => 'invalidtitle', 'info' => "Bad title ``\$1''"),
+               'invaliduser' => array('code' => 'invaliduser', 'info' => "Invalid username ``\$1''")
        );
        
        /**
@@ -581,8 +600,8 @@ abstract class ApiBase {
                $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']);
+               // If the key isn't present, throw an "unknown error"
+               $this->dieUsageMsg(array('unknownerror', $key));
        }
 
        /**
index 22360fb..1c40301 100644 (file)
@@ -40,14 +40,6 @@ class ApiDelete extends ApiBase {
                parent :: __construct($main, $action);
        }
 
-       /* Return values for the delete function. */
-       const DELETE_SUCCESS = 0;
-       const DELETE_PERM = 1;
-       const DELETE_BLOCKED = 2;
-       const DELETE_READONLY = 3;
-       const DELETE_BADTOKEN = 4;
-       const DELETE_BADARTICLE = 5;
-
        /**
         * Extracts the title, token, and reason from the request parameters and invokes
         * the local delete() function with these as arguments. It does not make use of 
@@ -62,49 +54,26 @@ class ApiDelete extends ApiBase {
                
                $titleObj = NULL;
                if(!isset($params['title']))
-                       $this->dieUsage('The title parameter must be set', 'notitle');
+                       $this->dieUsageMsg(array('notitle'));
                if(!isset($params['token']))
-                       $this->dieUsage('The token parameter must be set', 'notoken');
-
-               // delete() also checks for these, but we wanna save some work
-               if(!$wgUser->isAllowed('delete'))
-                       $this->dieUsage('You don\'t have permission to delete pages', 'permissiondenied');
-               if($wgUser->isBlocked())
-                       $this->dieUsage('You have been blocked from editing', 'blocked');
-               if(wfReadOnly())
-                       $this->dieUsage('The wiki is in read-only mode', 'readonly');
+                       $this->dieUsageMsg(array('notoken'));
 
                $titleObj = Title::newFromText($params['title']);
                if(!$titleObj)
-                       $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
+                       $this->dieUsageMsg(array('invalidtitle', $params['title']));
                if(!$titleObj->exists())
-                       $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');
+                       $this->dieUsageMsg(array('notanarticle', $params['title']));
 
                $articleObj = new Article($titleObj);
                $reason = (isset($params['reason']) ? $params['reason'] : NULL);
                $dbw = wfGetDb(DB_MASTER);
                $dbw->begin();
-               $retval = self::delete(&$articleObj, $params['token'], &$reason);
+               $retval = self::delete($articleObj, $params['token'],   $reason);
+               
+               if(!empty($retval))
+                       // We don't care about multiple errors, just report one of them
+                       $this->dieUsageMsg(current($retval));
 
-               switch($retval)
-               {
-                       case self::DELETE_SUCCESS:
-                               break; // We'll deal with that later
-                       case self::DELETE_PERM:  // If we get PERM, BLOCKED or READONLY that's weird, but it's possible
-                               $this->dieUsage('You don\'t have permission to delete', 'permissiondenied');
-                       case self::DELETE_BLOCKED:
-                               $this->dieUsage('You have been blocked from editing', 'blocked');
-                       case self::DELETE_READONLY:
-                               $this->dieUsage('The wiki is in read-only mode', 'readonly');
-                       case self::DELETE_BADTOKEN:
-                               $this->dieUsage('Invalid token', 'badtoken');
-                       case self::DELETE_BADARTICLE:
-                               $this->dieUsage("The article ``{$params['title']}'' doesn't exist or has already been deleted", 'missingtitle');
-                       default:
-                               // delete() has apparently invented a new error, which is extremely weird
-                               $this->dieDebug(__METHOD__, "delete() returned an unknown error ($retval)");
-               }
-               // $retval has to be self::DELETE_SUCCESS if we get here
                $dbw->commit();
                $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason);
                $this->getResult()->addValue(null, $this->getModuleName(), $r);
@@ -116,36 +85,37 @@ class ApiDelete extends ApiBase {
         * @param Article $article - Article object to work on
         * @param string $token - Delete token (same as edit token)
         * @param string $reason - Reason for the deletion. Autogenerated if NULL
-        * @return DELETE_SUCCESS on success, DELETE_* on failure
+        * @return Title::getUserPermissionsErrors()-like array
         */
        public static function delete(&$article, $token, &$reason = NULL)
        {
                global $wgUser;
 
-               // Check permissions first
-               if(!$article->mTitle->userCan('delete'))
-                       return self::DELETE_PERM;
-               if($wgUser->isBlocked())
-                       return self::DELETE_BLOCKED;
+               // Check permissions
+               $errors = $article->mTitle->getUserPermissionsErrors('delete', $wgUser);
+               if(!empty($errors))
+                       return $errors;
                if(wfReadOnly())
-                       return self::DELETE_READONLY;
+                       return array(array('readonlytext'));
+               if($wgUser->isBlocked())
+                       return array(array('blocked'));
 
                // Check token
                if(!$wgUser->matchEditToken($token))
-                       return self::DELETE_BADTOKEN;
+                       return array(array('sessionfailure'));
 
                // Auto-generate a summary, if necessary
                if(is_null($reason))
                {
                        $reason = $article->generateReason($hasHistory);
                        if($reason === false)
-                               return self::DELETE_BADARTICLE;
+                               return array(array('cannotdelete'));
                }
 
                // Luckily, Article.php provides a reusable delete function that does the hard work for us
                if($article->doDeleteArticle($reason))
-                       return self::DELETE_SUCCESS;
-               return self::DELETE_BADARTICLE;
+                       return array();
+               return array(array('cannotdelete', $article->mTitle->getPrefixedText()));
        }
        
        protected function getAllowedParams() {
index 0d74d6b..6b09e8f 100644 (file)
@@ -37,7 +37,7 @@ if (!defined('MEDIAWIKI')) {
 class ApiLogout extends ApiBase {
 
        public function __construct($main, $action) {
-               parent :: __construct($main, $action, 'lo');
+               parent :: __construct($main, $action);
        }
 
        public function execute() {
index 039ca8b..232ff3a 100644 (file)
@@ -43,32 +43,31 @@ class ApiRollback extends ApiBase {
                
                $titleObj = NULL;
                if(!isset($params['title']))
-                       $this->dieUsage('The title parameter must be set', 'notitle');
+                       $this->dieUsageMsg(array('notitle'));
                if(!isset($params['user']))
-                       $this->dieUsage('The user parameter must be set', 'nouser');
+                       $this->dieUsageMsg(array('nouser'));
                if(!isset($params['token']))
-                       $this->dieUsage('The token parameter must be set', 'notoken');
-
-               if(wfReadOnly())
-                       $this->dieUsage('The wiki is in read-only mode', 'readonly');
+                       $this->dieUsageMsg(array('notoken'));
 
                $titleObj = Title::newFromText($params['title']);
                if(!$titleObj)
-                       $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
+                       $this->dieUsageMsg(array('invalidtitle', $params['title']));
+               if(!$titleObj->exists())
+                       $this->dieUsageMsg(array('notanarticle'));
 
                $username = User::getCanonicalName($params['user']);
                if(!$username)
-                       $this->dieUsage("Invalid username ``{$params['user']}''", 'invaliduser');
+                       $this->dieUsageMsg(array('invaliduser', $params['user']));
 
                $articleObj = new Article($titleObj);
                $summary = (isset($params['summary']) ? $params['summary'] : "");
                $details = null;
                $dbw = wfGetDb(DB_MASTER);
                $dbw->begin();
-               $retval = $articleObj->doRollback($username, $summary, $params['token'], $params['markbot'], &$details);
+               $retval = $articleObj->doRollback($username, $summary, $params['token'], $params['markbot'], $details);
 
                if(!empty($retval))
-                       // We don't care about multiple errors, just report the first one
+                       // We don't care about multiple errors, just report one of them
                        $this->dieUsageMsg(current($retval));
 
                $dbw->commit();