);
}
+ /**
+ * 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:
/**
* 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 ) {
/**
* 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 ) {
}
/**
- * 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' );
}
// 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.