From: Sam Reed Date: Mon, 6 Jun 2011 16:45:40 +0000 (+0000) Subject: * (bug 27716) Make a method to do checking of 0 or 1 of the parameters existence... X-Git-Tag: 1.31.0-rc.0~29663 X-Git-Url: http://git.cyclocoop.org/ecrire?a=commitdiff_plain;h=67df4ef4c0eb4fd174934d8ecccfdce718c8fbb1;p=lhc%2Fweb%2Fwiklou.git * (bug 27716) Make a method to do checking of 0 or 1 of the parameters existence (like requireOnlyOneParameter), but without needing one of the parameters --- diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index ce7fd484c1..572c3c4f95 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -581,6 +581,38 @@ abstract class ApiBase { ); } + /** + * Die if more than one of a certain set of parameters is set and not false. + * + * @param $params array + */ + public function requireMaxOneParameter( $params ) { + $required = func_get_args(); + array_shift( $required ); + + $intersection = array_intersect( array_keys( array_filter( $params, + array( $this, "parameterNotEmpty" ) ) ), $required ); + + if ( count( $intersection ) > 1 ) { + $this->dieUsage( 'The parameters ' . implode( ', ', $intersection ) . ' can not be used together', 'invalidparammix' ); + } + } + + /** + * Generates the possible error requireMaxOneParameter() can die with + * + * @param $params array + * @return array + */ + public function getRequireMaxOneParameterErrorMessages( $params ) { + $p = $this->getModulePrefix(); + $params = implode( ", {$p}", $params ); + + return array( + array( 'code' => "{$p}invalidparammix", 'info' => "The parameters {$p}{$params} can not be used together" ) + ); + } + /** * Callback function used in requireOnlyOneParameter to check whether reequired parameters are set * diff --git a/includes/api/ApiQueryBlocks.php b/includes/api/ApiQueryBlocks.php index 878a9ed495..d96452c5b0 100644 --- a/includes/api/ApiQueryBlocks.php +++ b/includes/api/ApiQueryBlocks.php @@ -49,9 +49,7 @@ class ApiQueryBlocks extends ApiQueryBase { global $wgUser, $wgContLang; $params = $this->extractRequestParams(); - if ( isset( $params['users'] ) && isset( $params['ip'] ) ) { - $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' ); - } + $this->requireMaxOneParameter( $params, 'users', 'ip' ); $prop = array_flip( $params['prop'] ); $fld_id = isset( $prop['id'] ); @@ -290,7 +288,7 @@ class ApiQueryBlocks extends ApiQueryBase { public function getPossibleErrors() { return array_merge( parent::getPossibleErrors(), array( - array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ), + $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ), array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ), array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ), array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),