Merge "Disabling profiling in the debug toolbar for ProfilerSimple (for now)"
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index 40edf4e..fc1bfd1 100644 (file)
@@ -801,6 +801,48 @@ abstract class ApiBase extends ContextSource {
                );
        }
 
+       /**
+        * Die if none of a certain set of parameters is set and not false.
+        *
+        * @since 1.23
+        * @param array $params User provided set of parameters
+        * @param string ... List of parameter names to check
+        */
+       public function requireAtLeastOneParameter( $params ) {
+               $required = func_get_args();
+               array_shift( $required );
+               $p = $this->getModulePrefix();
+
+               $intersection = array_intersect(
+                       array_keys( array_filter( $params, array( $this, "parameterNotEmpty" ) ) ),
+                       $required
+               );
+
+               if ( count( $intersection ) == 0 ) {
+                       $this->dieUsage( "At least one of the parameters {$p}" .
+                               implode( ", {$p}", $required ) . ' is required', "{$p}missingparam" );
+               }
+       }
+
+       /**
+        * Generates the possible errors requireAtLeastOneParameter() can die with
+        *
+        * @since 1.23
+        * @param $params array Array of parameter key names
+        * @return array
+        */
+       public function getRequireAtLeastOneParameterErrorMessages( $params ) {
+               $p = $this->getModulePrefix();
+               $params = implode( ", {$p}", $params );
+
+               return array(
+                       array(
+                               'code' => "{$p}missingparam",
+                               'info' => "At least one of the parameters {$p}{$params} is required",
+                       ),
+               );
+       }
+
        /**
         * @param $params array
         * @param bool|string $load Whether load the object's state from the database:
@@ -854,7 +896,7 @@ abstract class ApiBase extends ContextSource {
        /**
         * Callback function used in requireOnlyOneParameter to check whether required parameters are set
         *
-        * @param  $x object Parameter to check is not null/false
+        * @param $x object Parameter to check is not null/false
         * @return bool
         */
        private function parameterNotEmpty( $x ) {
@@ -1280,8 +1322,8 @@ abstract class ApiBase extends ContextSource {
        /**
         * Adds a warning to the output, else dies
         *
-        * @param  $msg String Message to show as a warning, or error message if dying
-        * @param  $enforceLimits Boolean Whether this is an enforce (die)
+        * @param $msg String Message to show as a warning, or error message if dying
+        * @param $enforceLimits Boolean Whether this is an enforce (die)
         */
        private function warnOrDie( $msg, $enforceLimits = false ) {
                if ( $enforceLimits ) {
@@ -1330,13 +1372,13 @@ abstract class ApiBase extends ContextSource {
        }
 
        /**
-        * Throw a UsageException based on the errors in the Status object.
+        * Get error (as code, string) from a Status object.
         *
-        * @since 1.22
+        * @since 1.23
         * @param Status $status Status object
-        * @throws MWException
+        * @return array of code and error string
         */
-       public function dieStatus( $status ) {
+       public function getErrorFromStatus( $status ) {
                if ( $status->isGood() ) {
                        throw new MWException( 'Successful status passed to ApiBase::dieStatus' );
                }
@@ -1364,7 +1406,21 @@ abstract class ApiBase extends ContextSource {
                        // Translate message to code, for backwards compatability
                        $code = ApiBase::$messageMap[$code]['code'];
                }
-               $this->dieUsage( $msg->inLanguage( 'en' )->useDatabase( false )->plain(), $code );
+
+               return array( $code, $msg->inLanguage( 'en' )->useDatabase( false )->plain() );
+       }
+
+       /**
+        * Throw a UsageException based on the errors in the Status object.
+        *
+        * @since 1.22
+        * @param Status $status Status object
+        * @throws MWException
+        */
+       public function dieStatus( $status ) {
+
+               list( $code, $msg ) = $this->getErrorFromStatus( $status );
+               $this->dieUsage( $msg, $code );
        }
 
        // @codingStandardsIgnoreStart Allow long lines. Cannot split these.