MediaWiki  REL1_20
ApiOptions.php
Go to the documentation of this file.
00001 <?php
00033 class ApiOptions extends ApiBase {
00034 
00035         public function __construct( $main, $action ) {
00036                 parent::__construct( $main, $action );
00037         }
00038 
00042         public function execute() {
00043                 $user = $this->getUser();
00044 
00045                 if ( $user->isAnon() ) {
00046                         $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' );
00047                 }
00048 
00049                 $params = $this->extractRequestParams();
00050                 $changed = false;
00051 
00052                 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
00053                         $this->dieUsageMsg( array( 'missingparam', 'optionname' ) );
00054                 }
00055 
00056                 if ( $params['reset'] ) {
00057                         $user->resetOptions();
00058                         $changed = true;
00059                 }
00060 
00061                 $changes = array();
00062                 if ( count( $params['change'] ) ) {
00063                         foreach ( $params['change'] as $entry ) {
00064                                 $array = explode( '=', $entry, 2 );
00065                                 $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null;
00066                         }
00067                 }
00068                 if ( isset( $params['optionname'] ) ) {
00069                         $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
00070                         $changes[$params['optionname']] = $newValue;
00071                 }
00072                 if ( !$changed && !count( $changes ) ) {
00073                         $this->dieUsage( 'No changes were requested', 'nochanges' );
00074                 }
00075 
00076                 $prefs = Preferences::getPreferences( $user, $this->getContext() );
00077                 foreach ( $changes as $key => $value ) {
00078                         if ( !isset( $prefs[$key] ) ) {
00079                                 $this->setWarning( "Not a valid preference: $key" );
00080                                 continue;
00081                         }
00082                         $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key] );
00083                         $validation = $field->validate( $value, $user->getOptions() );
00084                         if ( $validation === true ) {
00085                                 $user->setOption( $key, $value );
00086                                 $changed = true;
00087                         } else {
00088                                 $this->setWarning( "Validation error for '$key': $validation" );
00089                         }
00090                 }
00091 
00092                 if ( $changed ) {
00093                         // Commit changes
00094                         $user->saveSettings();
00095                 }
00096 
00097                 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
00098         }
00099 
00100         public function mustBePosted() {
00101                 return true;
00102         }
00103 
00104         public function isWriteMode() {
00105                 return true;
00106         }
00107 
00108         public function getAllowedParams() {
00109                 return array(
00110                         'token' => array(
00111                                 ApiBase::PARAM_TYPE => 'string',
00112                                 ApiBase::PARAM_REQUIRED => true
00113                         ),
00114                         'reset' => false,
00115                         'change' => array(
00116                                 ApiBase::PARAM_ISMULTI => true,
00117                         ),
00118                         'optionname' => array(
00119                                 ApiBase::PARAM_TYPE => 'string',
00120                         ),
00121                         'optionvalue' => array(
00122                                 ApiBase::PARAM_TYPE => 'string',
00123                         ),
00124                 );
00125         }
00126 
00127         public function getResultProperties() {
00128                 return array(
00129                         '' => array(
00130                                 '*' => array(
00131                                         ApiBase::PROP_TYPE => array(
00132                                                 'success'
00133                                         )
00134                                 )
00135                         )
00136                 );
00137         }
00138 
00139         public function getParamDescription() {
00140                 return array(
00141                         'token' => 'An options token previously obtained through the action=tokens',
00142                         'reset' => 'Resets all preferences to the site defaults',
00143                         'change' => 'List of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters',
00144                         'optionname' => 'A name of a option which should have an optionvalue set',
00145                         'optionvalue' => 'A value of the option specified by the optionname, can contain pipe characters',
00146                 );
00147         }
00148 
00149         public function getDescription() {
00150                 return 'Change preferences of the current user';
00151         }
00152 
00153         public function getPossibleErrors() {
00154                 return array_merge( parent::getPossibleErrors(), array(
00155                         array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot change preferences' ),
00156                         array( 'code' => 'nochanges', 'info' => 'No changes were requested' ),
00157                 ) );
00158         }
00159 
00160         public function needsToken() {
00161                 return true;
00162         }
00163 
00164         public function getTokenSalt() {
00165                 return '';
00166         }
00167 
00168         public function getHelpUrls() {
00169                 return 'https://www.mediawiki.org/wiki/API:Options';
00170         }
00171 
00172         public function getExamples() {
00173                 return array(
00174                         'api.php?action=options&reset=&token=123ABC',
00175                         'api.php?action=options&change=skin=vector|hideminor=1&token=123ABC',
00176                         'api.php?action=options&reset=&change=skin=monobook&optionname=nickname&optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC',
00177                 );
00178         }
00179 
00180         public function getVersion() {
00181                 return __CLASS__ . ': $Id$';
00182         }
00183 }