MediaWiki  master
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                 $changes = 0;
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                         $changes++;
00059                 }
00060                 if ( count( $params['change'] ) ) {
00061                         foreach ( $params['change'] as $entry ) {
00062                                 $array = explode( '=', $entry, 2 );
00063                                 $user->setOption( $array[0], isset( $array[1] ) ? $array[1] : null );
00064                                 $changes++;
00065                         }
00066                 }
00067                 if ( isset( $params['optionname'] ) ) {
00068                         $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
00069                         $user->setOption( $params['optionname'], $newValue );
00070                         $changes++;
00071                 }
00072 
00073                 if ( $changes ) {
00074                         // Commit changes
00075                         $user->saveSettings();
00076                 } else {
00077                         $this->dieUsage( 'No changes were requested', 'nochanges' );
00078                 }
00079 
00080                 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
00081         }
00082 
00083         public function mustBePosted() {
00084                 return true;
00085         }
00086 
00087         public function isWriteMode() {
00088                 return true;
00089         }
00090 
00091         public function getAllowedParams() {
00092                 return array(
00093                         'token' => array(
00094                                 ApiBase::PARAM_TYPE => 'string',
00095                                 ApiBase::PARAM_REQUIRED => true
00096                         ),
00097                         'reset' => false,
00098                         'change' => array(
00099                                 ApiBase::PARAM_ISMULTI => true,
00100                         ),
00101                         'optionname' => array(
00102                                 ApiBase::PARAM_TYPE => 'string',
00103                         ),
00104                         'optionvalue' => array(
00105                                 ApiBase::PARAM_TYPE => 'string',
00106                         ),
00107                 );
00108         }
00109 
00110         public function getResultProperties() {
00111                 return array(
00112                         '' => array(
00113                                 '*' => array(
00114                                         ApiBase::PROP_TYPE => array(
00115                                                 'success'
00116                                         )
00117                                 )
00118                         )
00119                 );
00120         }
00121 
00122         public function getParamDescription() {
00123                 return array(
00124                         'token' => 'An options token previously obtained through the action=tokens',
00125                         'reset' => 'Resets all preferences to the site defaults',
00126                         'change' => 'List of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters',
00127                         'optionname' => 'A name of a option which should have an optionvalue set',
00128                         'optionvalue' => 'A value of the option specified by the optionname, can contain pipe characters',
00129                 );
00130         }
00131 
00132         public function getDescription() {
00133                 return 'Change preferences of the current user';
00134         }
00135 
00136         public function getPossibleErrors() {
00137                 return array_merge( parent::getPossibleErrors(), array(
00138                         array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot change preferences' ),
00139                         array( 'code' => 'nochanges', 'info' => 'No changes were requested' ),
00140                 ) );
00141         }
00142 
00143         public function needsToken() {
00144                 return true;
00145         }
00146 
00147         public function getTokenSalt() {
00148                 return '';
00149         }
00150 
00151         public function getHelpUrls() {
00152                 return 'https://www.mediawiki.org/wiki/API:Options';
00153         }
00154 
00155         public function getExamples() {
00156                 return array(
00157                         'api.php?action=options&reset=&token=123ABC',
00158                         'api.php?action=options&change=skin=vector|hideminor=1&token=123ABC',
00159                         'api.php?action=options&reset=&change=skin=monobook&optionname=nickname&optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC',
00160                 );
00161         }
00162 
00163         public function getVersion() {
00164                 return __CLASS__ . ': $Id$';
00165         }
00166 }