for all query modules.
* ApiQueryBase adds 'badcontinue' error code if module has 'continue' parameter.
* (bug 35885) Removed version parameter and all getVersion() methods.
+* action=options now takes a "resetkinds" option, which allows only resetting
+ certain types of preferences when the "reset" option is set.
=== API internal changes in 1.21 ===
* For debugging only, a new global $wgDebugAPI removes many API restrictions when true.
}
/**
- * Return an associative array mapping preferences keys to the kind of a preference they're
- * used for. Different kinds are handled differently when setting or reading preferences.
+ * Return a list of the types of user options currently returned by
+ * User::getOptionKinds().
*
- * Currently, the kind is one of:
+ * Currently, the option kinds are:
* - 'registered' - preferences which are registered in core MediaWiki or
* by extensions using the UserGetDefaultOptions hook.
* - 'registered-multiselect' - as above, using the 'multiselect' type.
* - 'unused' - preferences about which MediaWiki doesn't know anything.
* These are usually legacy options, removed in newer versions.
*
+ * The API (and possibly others) use this function to determine the possible
+ * option types for validation purposes, so make sure to update this when a
+ * new option kind is added.
+ *
+ * @see User::getOptionKinds
+ * @return array Option kinds
+ */
+ public static function listOptionKinds() {
+ return array(
+ 'registered',
+ 'registered-multiselect',
+ 'userjs',
+ 'unused'
+ );
+ }
+
+ /**
+ * Return an associative array mapping preferences keys to the kind of a preference they're
+ * used for. Different kinds are handled differently when setting or reading preferences.
+ *
+ * See User::listOptionKinds for the list of valid option types that can be provided.
+ *
+ * @see User::listOptionKinds
* @param $context IContextSource
* @param $options array assoc. array with options keys to check as keys. Defaults to $this->mOptions.
* @return array the key => kind mapping data
}
$optionKinds = $this->getOptionKinds( $context );
+ $resetKinds = array_intersect( $resetKinds, self::listOptionKinds() );
$newOptions = array();
// Use default values for the options that should be deleted, and
}
if ( $params['reset'] ) {
- $user->resetOptions( 'all' );
+ $user->resetOptions( $params['resetkinds'] );
$changed = true;
}
case 'registered-multiselect':
// A key for a multiselect option.
$validation = true;
- $value = (bool)$value;
+ $value = $value !== null ? (bool) $value : null;
break;
case 'userjs':
// Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
}
public function getAllowedParams() {
+ $optionKinds = User::listOptionKinds();
+ $optionKinds[] = 'all';
+
return array(
'token' => array(
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true
),
'reset' => false,
+ 'resetkinds' => array(
+ ApiBase::PARAM_TYPE => $optionKinds,
+ ApiBase::PARAM_DFLT => 'all',
+ ApiBase::PARAM_ISMULTI => true
+ ),
'change' => array(
ApiBase::PARAM_ISMULTI => true,
),
public function getParamDescription() {
return array(
'token' => 'An options token previously obtained through the action=tokens',
- 'reset' => 'Resets all preferences to the site defaults',
- 'change' => 'List of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters',
+ 'reset' => 'Resets preferences to the site defaults',
+ 'resetkinds' => 'List of types of options to reset when the "reset" option is set',
+ 'change' => 'List of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters. If no value is given (not even an equals sign), e.g., optionname|otheroption|..., the option will be reset to its default value',
'optionname' => 'A name of a option which should have an optionvalue set',
'optionvalue' => 'A value of the option specified by the optionname, can contain pipe characters',
);
public function testReset() {
$this->mUserMock->expects( $this->once() )
- ->method( 'resetOptions' );
+ ->method( 'resetOptions' )
+ ->with( $this->equalTo( array( 'all' ) ) );
$this->mUserMock->expects( $this->never() )
->method( 'setOption' );
$this->assertEquals( self::$Success, $response );
}
+ public function testResetKinds() {
+ $this->mUserMock->expects( $this->once() )
+ ->method( 'resetOptions' )
+ ->with( $this->equalTo( array( 'registered' ) ) );
+
+ $this->mUserMock->expects( $this->never() )
+ ->method( 'setOption' );
+
+ $this->mUserMock->expects( $this->once() )
+ ->method( 'saveSettings' );
+
+ $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
+
+ $response = $this->executeQuery( $request );
+
+ $this->assertEquals( self::$Success, $response );
+ }
+
public function testOptionWithValue() {
$this->mUserMock->expects( $this->never() )
->method( 'resetOptions' );
$this->mUserMock->expects( $this->once() )
->method( 'setOption' )
- ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
+ ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
$this->mUserMock->expects( $this->once() )
->method( 'saveSettings' );
$this->mUserMock->expects( $this->at( 3 ) )
->method( 'setOption' )
- ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
+ ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
$this->mUserMock->expects( $this->at( 4 ) )
->method( 'getOptions' );
$this->mUserMock->expects( $this->at( 2 ) )
->method( 'setOption' )
- ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->equalTo( true ) );
+ ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
$this->mUserMock->expects( $this->at( 3 ) )
->method( 'setOption' )
- ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->equalTo( false ) );
+ ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
$this->mUserMock->expects( $this->at( 4 ) )
->method( 'setOption' )
- ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->equalTo( false ) );
+ ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
$this->mUserMock->expects( $this->at( 5 ) )
->method( 'setOption' )
- ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->equalTo( false ) );
+ ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
$this->mUserMock->expects( $this->once() )
->method( 'saveSettings' );