(bug 18533) Add readonly reason to readonly exception
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Sun, 12 Jul 2009 12:38:03 +0000 (12:38 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Sun, 12 Jul 2009 12:38:03 +0000 (12:38 +0000)
RELEASE-NOTES
includes/api/ApiBase.php
includes/api/ApiEditPage.php
includes/api/ApiMain.php

index 5ec592f..2a9ce51 100644 (file)
@@ -286,6 +286,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
   rather than UI lang
 * Added snippet field to list=search output
 * (bug 17809) Add number of users in user groups to meta=siteinfo
+* (bug 18533) Add readonly reason to readonly exception
 
 === Languages updated in 1.16 ===
 
index 32b800f..cdd4c5a 100644 (file)
@@ -726,9 +726,9 @@ abstract class ApiBase {
         * @param $errorCode string Error code
         * @param $httpRespCode int HTTP response code
         */
-       public function dieUsage($description, $errorCode, $httpRespCode = 0) {
+       public function dieUsage($description, $errorCode, $httpRespCode = 0, $extradata = null) {
                wfProfileClose();
-               throw new UsageException($description, $this->encodeParamName($errorCode), $httpRespCode);
+               throw new UsageException($description, $this->encodeParamName($errorCode), $httpRespCode, $extradata);
        }
 
        /**
@@ -856,6 +856,15 @@ abstract class ApiBase {
                'undo-failure' => array('code' => 'undofailure', 'info' => 'Undo failed due to conflicting intermediate edits'),
        );
 
+       /**
+        * Helper function for readonly errors
+        */
+       public function dieReadOnly() {
+               $parsed = $this->parseMsg( array( 'readonlytext' ) );
+               $this->dieUsage($parsed['info'], $parsed['code'], /* http error */ 0, 
+                       array( 'readonlyreason' => wfReadOnlyReason() ) );
+       }
+
        /**
         * Output the error message related to a certain array
         * @param $error array Element of a getUserPermissionsErrors()-style array
index 09731e7..f3cf492 100644 (file)
@@ -231,7 +231,7 @@ class ApiEditPage extends ApiBase {
                        case EditPage::AS_READ_ONLY_PAGE_LOGGED:
                                $this->dieUsageMsg(array('noedit'));
                        case EditPage::AS_READ_ONLY_PAGE:
-                               $this->dieUsageMsg(array('readonlytext'));
+                               $this->dieReadOnly();
                        case EditPage::AS_RATE_LIMITED:
                                $this->dieUsageMsg(array('actionthrottledtext'));
                        case EditPage::AS_ARTICLE_WAS_DELETED:
index f39a2fc..5a7d3a2 100644 (file)
@@ -312,9 +312,7 @@ class ApiMain extends ApiBase {
                                //
                                // User entered incorrect parameters - print usage screen
                                //
-                               $errMessage = array (
-                               'code' => $e->getCodeString(),
-                               'info' => $e->getMessage());
+                               $errMessage = $e->getMessageArray();
 
                                // Only print the help message when this is for the developer, not runtime
                                if ($this->mPrinter->getWantsHelp() || $this->mAction == 'help')
@@ -396,7 +394,7 @@ class ApiMain extends ApiBase {
                        if (!$wgUser->isAllowed('writeapi'))
                                $this->dieUsageMsg(array('writerequired'));
                        if (wfReadOnly())
-                               $this->dieUsageMsg(array('readonlytext'));
+                               $this->dieReadOnly();
                }
 
                if (!$this->mInternalMode) {
@@ -703,14 +701,25 @@ class ApiMain extends ApiBase {
 class UsageException extends Exception {
 
        private $mCodestr;
+       private $mExtraData;
 
-       public function __construct($message, $codestr, $code = 0) {
+       public function __construct($message, $codestr, $code = 0, $extradata = null) {
                parent :: __construct($message, $code);
                $this->mCodestr = $codestr;
+               $this->mExtraData = $extradata;
        }
        public function getCodeString() {
                return $this->mCodestr;
        }
+       public function getMessageArray() {
+               $result = array (
+                               'code' => $this->mCodestr,
+                               'info' => $this->getMessage()
+               );
+               if ( is_array( $this->mExtraData ) )
+                       $result = array_merge( $result, $this->mExtraData );
+               return $result;
+       }
        public function __toString() {
                return "{$this->getCodeString()}: {$this->getMessage()}";
        }