Merge "Genderize Special:Preferences"
[lhc/web/wiklou.git] / includes / api / ApiOptions.php
index 2b3d5e3..265c2cc 100644 (file)
@@ -47,7 +47,7 @@ class ApiOptions extends ApiBase {
                }
 
                $params = $this->extractRequestParams();
-               $changes = 0;
+               $changed = false;
 
                if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
                        $this->dieUsageMsg( array( 'missingparam', 'optionname' ) );
@@ -55,26 +55,43 @@ class ApiOptions extends ApiBase {
 
                if ( $params['reset'] ) {
                        $user->resetOptions();
-                       $changes++;
+                       $changed = true;
                }
+
+               $changes = array();
                if ( count( $params['change'] ) ) {
                        foreach ( $params['change'] as $entry ) {
                                $array = explode( '=', $entry, 2 );
-                               $user->setOption( $array[0], isset( $array[1] ) ? $array[1] : null );
-                               $changes++;
+                               $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null;
                        }
                }
                if ( isset( $params['optionname'] ) ) {
                        $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
-                       $user->setOption( $params['optionname'], $newValue );
-                       $changes++;
+                       $changes[$params['optionname']] = $newValue;
+               }
+               if ( !$changed && !count( $changes ) ) {
+                       $this->dieUsage( 'No changes were requested', 'nochanges' );
+               }
+
+               $prefs = Preferences::getPreferences( $user, $this->getContext() );
+               foreach ( $changes as $key => $value ) {
+                       if ( !isset( $prefs[$key] ) ) {
+                               $this->setWarning( "Not a valid preference: $key" );
+                               continue;
+                       }
+                       $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key] );
+                       $validation = $field->validate( $value, $user->getOptions() );
+                       if ( $validation === true ) {
+                               $user->setOption( $key, $value );
+                               $changed = true;
+                       } else {
+                               $this->setWarning( "Validation error for '$key': $validation" );
+                       }
                }
 
-               if ( $changes ) {
+               if ( $changed ) {
                        // Commit changes
                        $user->saveSettings();
-               } else {
-                       $this->dieUsage( 'No changes were requested', 'nochanges' );
                }
 
                $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
@@ -123,7 +140,7 @@ class ApiOptions extends ApiBase {
                return array(
                        'token' => 'An options token previously obtained through the action=tokens',
                        'reset' => 'Resets all preferences to the site defaults',
-                       'change' => 'Pipe-separated list of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters',
+                       'change' => 'List of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters',
                        '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',
                );
@@ -135,8 +152,8 @@ class ApiOptions extends ApiBase {
 
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
-                       array( 'notloggedin' ),
-                       array( 'nochanges' ),
+                       array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot change preferences' ),
+                       array( 'code' => 'nochanges', 'info' => 'No changes were requested' ),
                ) );
        }