API: Restore ability for dieStatus() to be passed a non-fatal Status
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index e249810..5332d7e 100644 (file)
@@ -24,6 +24,8 @@
  * @file
  */
 
+use Wikimedia\Rdbms\IDatabase;
+
 /**
  * This abstract class implements many basic API functions, and is the base of
  * all API classes.
@@ -186,6 +188,13 @@ abstract class ApiBase extends ContextSource {
         */
        const PARAM_EXTRA_NAMESPACES = 18;
 
+       /*
+        * (boolean) Is the parameter sensitive? Note 'password'-type fields are
+        * always sensitive regardless of the value of this field.
+        * @since 1.29
+        */
+       const PARAM_SENSITIVE = 19;
+
        /**@}*/
 
        const ALL_DEFAULT_STRING = '*';
@@ -376,6 +385,13 @@ abstract class ApiBase extends ContextSource {
 
        /**
         * Indicates whether this module requires write mode
+        *
+        * This should return true for modules that may require synchronous database writes.
+        * Modules that do not need such writes should also not rely on master database access,
+        * since only read queries are needed and each master DB is a single point of failure.
+        * Additionally, requests that only need replica DBs can be efficiently routed to any
+        * datacenter via the Promise-Non-Write-API-Action header.
+        *
         * @return bool
         */
        public function isWriteMode() {
@@ -617,7 +633,7 @@ abstract class ApiBase extends ContextSource {
 
        /**
         * Gets a default replica DB connection object
-        * @return Database
+        * @return IDatabase
         */
        protected function getDB() {
                if ( !isset( $this->mSlaveDB ) ) {
@@ -700,7 +716,7 @@ abstract class ApiBase extends ContextSource {
         * @return array
         */
        public function extractRequestParams( $parseLimit = true ) {
-               // Cache parameters, for performance and to avoid bug 24564.
+               // Cache parameters, for performance and to avoid T26564.
                if ( !isset( $this->mParamCache[$parseLimit] ) ) {
                        $params = $this->getFinalParams();
                        $results = [];
@@ -1023,6 +1039,10 @@ abstract class ApiBase extends ContextSource {
                        } else {
                                $type = 'NULL'; // allow everything
                        }
+
+                       if ( $type == 'password' || !empty( $paramSettings[self::PARAM_SENSITIVE] ) ) {
+                               $this->getMain()->markParamsSensitive( $encParamName );
+                       }
                }
 
                if ( $type == 'boolean' ) {
@@ -1326,7 +1346,7 @@ abstract class ApiBase extends ContextSource {
                }
 
                if ( !$allowMultiple && count( $valuesList ) != 1 ) {
-                       // Bug 33482 - Allow entries with | in them for non-multiple values
+                       // T35482 - Allow entries with | in them for non-multiple values
                        if ( in_array( $value, $allowedValues, true ) ) {
                                return $value;
                        }
@@ -1843,6 +1863,23 @@ abstract class ApiBase extends ContextSource {
                        throw new MWException( 'Successful status passed to ApiBase::dieStatus' );
                }
 
+               // ApiUsageException needs a fatal status, but this method has
+               // historically accepted any non-good status. Convert it if necessary.
+               $status->setOK( false );
+               if ( !$status->getErrorsByType( 'error' ) ) {
+                       $newStatus = Status::newGood();
+                       foreach ( $status->getErrorsByType( 'warning' ) as $err ) {
+                               call_user_func_array(
+                                       [ $newStatus, 'fatal' ],
+                                       array_merge( [ $err['message'] ], $err['params'] )
+                               );
+                       }
+                       if ( !$newStatus->getErrorsByType( 'error' ) ) {
+                               $newStatus->fatal( 'unknownerror-nocode' );
+                       }
+                       $status = $newStatus;
+               }
+
                throw new ApiUsageException( $this, $status );
        }
 
@@ -2028,6 +2065,7 @@ abstract class ApiBase extends ContextSource {
                        $params['token'] = [
                                ApiBase::PARAM_TYPE => 'string',
                                ApiBase::PARAM_REQUIRED => true,
+                               ApiBase::PARAM_SENSITIVE => true,
                                ApiBase::PARAM_HELP_MSG => [
                                        'api-help-param-token',
                                        $this->needsToken(),