MediaWiki
REL1_21
|
00001 <?php 00033 class ApiOptions extends ApiBase { 00034 00038 public function execute() { 00039 $user = $this->getUser(); 00040 00041 if ( $user->isAnon() ) { 00042 $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' ); 00043 } 00044 00045 $params = $this->extractRequestParams(); 00046 $changed = false; 00047 00048 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) { 00049 $this->dieUsageMsg( array( 'missingparam', 'optionname' ) ); 00050 } 00051 00052 if ( $params['reset'] ) { 00053 $user->resetOptions( $params['resetkinds'] ); 00054 $changed = true; 00055 } 00056 00057 $changes = array(); 00058 if ( count( $params['change'] ) ) { 00059 foreach ( $params['change'] as $entry ) { 00060 $array = explode( '=', $entry, 2 ); 00061 $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null; 00062 } 00063 } 00064 if ( isset( $params['optionname'] ) ) { 00065 $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null; 00066 $changes[$params['optionname']] = $newValue; 00067 } 00068 if ( !$changed && !count( $changes ) ) { 00069 $this->dieUsage( 'No changes were requested', 'nochanges' ); 00070 } 00071 00072 $prefs = Preferences::getPreferences( $user, $this->getContext() ); 00073 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes ); 00074 00075 foreach ( $changes as $key => $value ) { 00076 switch ( $prefsKinds[$key] ) { 00077 case 'registered': 00078 // Regular option. 00079 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key] ); 00080 $validation = $field->validate( $value, $user->getOptions() ); 00081 break; 00082 case 'registered-multiselect': 00083 case 'registered-checkmatrix': 00084 // A key for a multiselect or checkmatrix option. 00085 $validation = true; 00086 $value = $value !== null ? (bool) $value : null; 00087 break; 00088 case 'userjs': 00089 // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts 00090 if ( strlen( $key ) > 255 ) { 00091 $validation = "key too long (no more than 255 bytes allowed)"; 00092 } elseif ( preg_match( "/[^a-zA-Z0-9_-]/", $key ) !== 0 ) { 00093 $validation = "invalid key (only a-z, A-Z, 0-9, _, - allowed)"; 00094 } else { 00095 $validation = true; 00096 } 00097 break; 00098 case 'unused': 00099 default: 00100 $validation = "not a valid preference"; 00101 break; 00102 } 00103 if ( $validation === true ) { 00104 $user->setOption( $key, $value ); 00105 $changed = true; 00106 } else { 00107 $this->setWarning( "Validation error for '$key': $validation" ); 00108 } 00109 } 00110 00111 if ( $changed ) { 00112 // Commit changes 00113 $user->saveSettings(); 00114 } 00115 00116 $this->getResult()->addValue( null, $this->getModuleName(), 'success' ); 00117 } 00118 00119 public function mustBePosted() { 00120 return true; 00121 } 00122 00123 public function isWriteMode() { 00124 return true; 00125 } 00126 00127 public function getAllowedParams() { 00128 $optionKinds = User::listOptionKinds(); 00129 $optionKinds[] = 'all'; 00130 00131 return array( 00132 'token' => array( 00133 ApiBase::PARAM_TYPE => 'string', 00134 ApiBase::PARAM_REQUIRED => true 00135 ), 00136 'reset' => false, 00137 'resetkinds' => array( 00138 ApiBase::PARAM_TYPE => $optionKinds, 00139 ApiBase::PARAM_DFLT => 'all', 00140 ApiBase::PARAM_ISMULTI => true 00141 ), 00142 'change' => array( 00143 ApiBase::PARAM_ISMULTI => true, 00144 ), 00145 'optionname' => array( 00146 ApiBase::PARAM_TYPE => 'string', 00147 ), 00148 'optionvalue' => array( 00149 ApiBase::PARAM_TYPE => 'string', 00150 ), 00151 ); 00152 } 00153 00154 public function getResultProperties() { 00155 return array( 00156 '' => array( 00157 '*' => array( 00158 ApiBase::PROP_TYPE => array( 00159 'success' 00160 ) 00161 ) 00162 ) 00163 ); 00164 } 00165 00166 public function getParamDescription() { 00167 return array( 00168 'token' => 'An options token previously obtained through the action=tokens', 00169 'reset' => 'Resets preferences to the site defaults', 00170 'resetkinds' => 'List of types of options to reset when the "reset" option is set', 00171 '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', 00172 'optionname' => 'A name of a option which should have an optionvalue set', 00173 'optionvalue' => 'A value of the option specified by the optionname, can contain pipe characters', 00174 ); 00175 } 00176 00177 public function getDescription() { 00178 return array( 00179 'Change preferences of the current user', 00180 'Only options which are registered in core or in one of installed extensions,', 00181 'or as options with keys prefixed with \'userjs-\' (intended to be used by user scripts), can be set.' 00182 ); 00183 } 00184 00185 public function getPossibleErrors() { 00186 return array_merge( parent::getPossibleErrors(), array( 00187 array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot change preferences' ), 00188 array( 'code' => 'nochanges', 'info' => 'No changes were requested' ), 00189 ) ); 00190 } 00191 00192 public function needsToken() { 00193 return true; 00194 } 00195 00196 public function getTokenSalt() { 00197 return ''; 00198 } 00199 00200 public function getHelpUrls() { 00201 return 'https://www.mediawiki.org/wiki/API:Options'; 00202 } 00203 00204 public function getExamples() { 00205 return array( 00206 'api.php?action=options&reset=&token=123ABC', 00207 'api.php?action=options&change=skin=vector|hideminor=1&token=123ABC', 00208 'api.php?action=options&reset=&change=skin=monobook&optionname=nickname&optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC', 00209 ); 00210 } 00211 }