Merge "Allow passing detailed permission errors data to API"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 4 Nov 2015 22:38:47 +0000 (22:38 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 4 Nov 2015 22:38:47 +0000 (22:38 +0000)
RELEASE-NOTES-1.27
docs/hooks.txt
includes/GlobalFunctions.php
includes/Title.php
includes/api/ApiBase.php
includes/api/ApiUpload.php

index ec49ffb..13b9ba2 100644 (file)
@@ -64,6 +64,9 @@ production.
   setting in the relevant section of $wgLBFactoryConf.
 * User::newSystemUser() may be used to simplify the creation of passwordless
   "system" users for logged actions from scripts and extensions.
+* Extensions can now return detailed error information via the API when
+  preventing user actions using 'getUserPermissionsErrors' and similar hooks
+  by using ApiMessage instances instead of strings for the $result value.
 
 ==== External libraries ====
 
index 141a7e5..41cf488 100644 (file)
@@ -1521,7 +1521,9 @@ $lang: Language that will be used to render the timestamp
 'getUserPermissionsErrors': Add a permissions error when permissions errors are
 checked for. Use instead of userCan for most cases. Return false if the user
 can't do it, and populate $result with the reason in the form of
-array( messagename, param1, param2, ... ). For consistency, error messages
+array( messagename, param1, param2, ... ) or a MessageSpecifier instance (you
+might want to use ApiMessage to provide machine-readable details for the API).
+For consistency, error messages
 should be plain text with no special coloring, bolding, etc. to show that
 they're errors; presenting them properly to the user as errors is done by the
 caller.
@@ -1534,7 +1536,9 @@ $result: User permissions error to add. If none, return true.
 called only if expensive checks are enabled. Add a permissions error when
 permissions errors are checked for. Return false if the user can't do it, and
 populate $result with the reason in the form of array( messagename, param1,
-param2, ... ). For consistency, error messages should be plain text with no
+param2, ... ) or a MessageSpecifier instance (you might want to use ApiMessage
+to provide machine-readable details for the API). For consistency, error
+messages should be plain text with no
 special coloring, bolding, etc. to show that they're errors; presenting them
 properly to the user as errors is done by the caller.
 $title: Title object being checked against
index 3bc3a90..421cd90 100644 (file)
@@ -308,10 +308,15 @@ function wfMergeErrorArrays( /*...*/ ) {
        $out = array();
        foreach ( $args as $errors ) {
                foreach ( $errors as $params ) {
+                       $originalParams = $params;
+                       if ( $params[0] instanceof MessageSpecifier ) {
+                               $msg = $params[0];
+                               $params = array_merge( array( $msg->getKey() ), $msg->getParams() );
+                       }
                        # @todo FIXME: Sometimes get nested arrays for $params,
                        # which leads to E_NOTICEs
                        $spec = implode( "\t", $params );
-                       $out[$spec] = $params;
+                       $out[$spec] = $originalParams;
                }
        }
        return array_values( $out );
index 9ada4f3..13e7ffb 100644 (file)
@@ -1944,7 +1944,7 @@ class Title {
         *   - secure : does cheap and expensive checks, using the master as needed
         * @param array $ignoreErrors Array of Strings Set this to a list of message keys
         *   whose corresponding errors may be ignored.
-        * @return array Array of arguments to wfMessage to explain permissions problems.
+        * @return array Array of arrays of the arguments to wfMessage to explain permissions problems.
         */
        public function getUserPermissionsErrors(
                $action, $user, $rigor = 'secure', $ignoreErrors = array()
@@ -1953,9 +1953,12 @@ class Title {
 
                // Remove the errors being ignored.
                foreach ( $errors as $index => $error ) {
-                       $error_key = is_array( $error ) ? $error[0] : $error;
+                       $errKey = is_array( $error ) ? $error[0] : $error;
 
-                       if ( in_array( $error_key, $ignoreErrors ) ) {
+                       if ( in_array( $errKey, $ignoreErrors ) ) {
+                               unset( $errors[$index] );
+                       }
+                       if ( $errKey instanceof MessageSpecifier && in_array( $errKey->getKey(), $ignoreErrors ) ) {
                                unset( $errors[$index] );
                        }
                }
@@ -2054,6 +2057,9 @@ class Title {
                } elseif ( $result !== '' && is_string( $result ) ) {
                        // A string representing a message-id
                        $errors[] = array( $result );
+               } elseif ( $result instanceof MessageSpecifier ) {
+                       // A message specifier representing an error
+                       $errors[] = array( $result );
                } elseif ( $result === false ) {
                        // a generic "We don't want them to do that"
                        $errors[] = array( 'badaccess-group0' );
index 4f9a94d..1227899 100644 (file)
@@ -1386,10 +1386,11 @@ abstract class ApiBase extends ContextSource {
         *
         * @since 1.23
         * @param Status $status
+        * @param array|null &$extraData Set if extra data from IApiMessage is available (since 1.27)
         * @return array Array of code and error string
         * @throws MWException
         */
-       public function getErrorFromStatus( $status ) {
+       public function getErrorFromStatus( $status, &$extraData = null ) {
                if ( $status->isGood() ) {
                        throw new MWException( 'Successful status passed to ApiBase::dieStatus' );
                }
@@ -1408,7 +1409,12 @@ abstract class ApiBase extends ContextSource {
                // error messages.
                if ( $errors[0] instanceof Message ) {
                        $msg = $errors[0];
-                       $code = $msg->getKey();
+                       if ( $msg instanceof IApiMessage ) {
+                               $extraData = $msg->getApiData();
+                               $code = $msg->getApiCode();
+                       } else {
+                               $code = $msg->getKey();
+                       }
                } else {
                        $code = array_shift( $errors[0] );
                        $msg = wfMessage( $code, $errors[0] );
@@ -1429,8 +1435,9 @@ abstract class ApiBase extends ContextSource {
         * @throws UsageException always
         */
        public function dieStatus( $status ) {
-               list( $code, $msg ) = $this->getErrorFromStatus( $status );
-               $this->dieUsage( $msg, $code );
+               $extraData = null;
+               list( $code, $msg ) = $this->getErrorFromStatus( $status, $extraData );
+               $this->dieUsage( $msg, $code, 0, $extraData );
        }
 
        // @codingStandardsIgnoreStart Allow long lines. Cannot split these.
@@ -1963,7 +1970,8 @@ abstract class ApiBase extends ContextSource {
                        $error = array( $error );
                }
                $parsed = $this->parseMsg( $error );
-               $this->dieUsage( $parsed['info'], $parsed['code'] );
+               $extraData = isset( $parsed['data'] ) ? $parsed['data'] : null;
+               $this->dieUsage( $parsed['info'], $parsed['code'], 0, $extraData );
        }
 
        /**
@@ -2016,6 +2024,14 @@ abstract class ApiBase extends ContextSource {
                        $key = array_shift( $error );
                }
 
+               if ( $key instanceof IApiMessage ) {
+                       return array(
+                               'code' => $key->getApiCode(),
+                               'info' => $key->inLanguage( 'en' )->useDatabase( false )->text(),
+                               'data' => $key->getApiData()
+                       );
+               }
+
                if ( isset( self::$messageMap[$key] ) ) {
                        return array(
                                'code' => wfMsgReplaceArgs( self::$messageMap[$key]['code'], $error ),
index 5f34701..f51b389 100644 (file)
@@ -362,6 +362,10 @@ class ApiUpload extends ApiBase {
                $data['invalidparameter'] = $parameter;
 
                $parsed = $this->parseMsg( $error );
+               if ( isset( $parsed['data'] ) ) {
+                       $data = array_merge( $data, $parsed['data'] );
+               }
+
                $this->dieUsage( $parsed['info'], $parsed['code'], 0, $data );
        }