Remove Revision::getRevisionText from ApiQueryDeletedrevs
[lhc/web/wiklou.git] / includes / api / ApiOptions.php
1 <?php
2 /**
3 * Copyright © 2012 Szymon Świerkosz beau@adres.pl
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * API module that facilitates the changing of user's preferences.
27 * Requires API write mode to be enabled.
28 *
29 * @ingroup API
30 */
31 class ApiOptions extends ApiBase {
32 /** @var User User account to modify */
33 private $userForUpdates;
34
35 /**
36 * Changes preferences of the current user.
37 */
38 public function execute() {
39 $user = $this->getUserForUpdates();
40 if ( !$user || $user->isAnon() ) {
41 $this->dieWithError(
42 [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin'
43 );
44 }
45
46 $this->checkUserRightsAny( 'editmyoptions' );
47
48 $params = $this->extractRequestParams();
49 $changed = false;
50
51 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
52 $this->dieWithError( [ 'apierror-missingparam', 'optionname' ] );
53 }
54
55 $resetKinds = $params['resetkinds'];
56 if ( !$params['reset'] ) {
57 $resetKinds = [];
58 }
59
60 $changes = [];
61 if ( $params['change'] ) {
62 foreach ( $params['change'] as $entry ) {
63 $array = explode( '=', $entry, 2 );
64 $changes[$array[0]] = $array[1] ?? null;
65 }
66 }
67 if ( isset( $params['optionname'] ) ) {
68 $newValue = $params['optionvalue'] ?? null;
69 $changes[$params['optionname']] = $newValue;
70 }
71
72 Hooks::run( 'ApiOptions', [ $this, $user, $changes, $resetKinds ] );
73
74 if ( $resetKinds ) {
75 $this->resetPreferences( $resetKinds );
76 $changed = true;
77 }
78
79 if ( !$changed && !count( $changes ) ) {
80 $this->dieWithError( 'apierror-nochanges' );
81 }
82
83 $prefs = $this->getPreferences();
84 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
85
86 $htmlForm = null;
87 foreach ( $changes as $key => $value ) {
88 switch ( $prefsKinds[$key] ) {
89 case 'registered':
90 // Regular option.
91 if ( $value === null ) {
92 // Reset it
93 $validation = true;
94 } else {
95 // Validate
96 if ( $htmlForm === null ) {
97 // We need a dummy HTMLForm for the validate callback...
98 $htmlForm = new HTMLForm( [], $this );
99 }
100 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key], $htmlForm );
101 $validation = $field->validate( $value, $user->getOptions() );
102 }
103 break;
104 case 'registered-multiselect':
105 case 'registered-checkmatrix':
106 // A key for a multiselect or checkmatrix option.
107 $validation = true;
108 $value = $value !== null ? (bool)$value : null;
109 break;
110 case 'userjs':
111 // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
112 if ( strlen( $key ) > 255 ) {
113 $validation = $this->msg( 'apiwarn-validationfailed-keytoolong', Message::numParam( 255 ) );
114 } elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
115 $validation = $this->msg( 'apiwarn-validationfailed-badchars' );
116 } else {
117 $validation = true;
118 }
119 break;
120 case 'special':
121 $validation = $this->msg( 'apiwarn-validationfailed-cannotset' );
122 break;
123 case 'unused':
124 default:
125 $validation = $this->msg( 'apiwarn-validationfailed-badpref' );
126 break;
127 }
128 if ( $validation === true ) {
129 $this->setPreference( $key, $value );
130 $changed = true;
131 } else {
132 $this->addWarning( [ 'apiwarn-validationfailed', wfEscapeWikiText( $key ), $validation ] );
133 }
134 }
135
136 if ( $changed ) {
137 $this->commitChanges();
138 }
139
140 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
141 }
142
143 /**
144 * Load the user from the master to reduce CAS errors on double post (T95839)
145 *
146 * @return null|User
147 */
148 protected function getUserForUpdates() {
149 if ( !$this->userForUpdates ) {
150 $this->userForUpdates = $this->getUser()->getInstanceForUpdate();
151 }
152
153 return $this->userForUpdates;
154 }
155
156 /**
157 * Returns preferences form descriptor
158 * @return mixed[][]
159 */
160 protected function getPreferences() {
161 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
162 return $preferencesFactory->getFormDescriptor( $this->getUserForUpdates(),
163 $this->getContext() );
164 }
165
166 /**
167 * @param string[] $kinds One or more types returned by User::listOptionKinds() or 'all'
168 */
169 protected function resetPreferences( array $kinds ) {
170 $this->getUserForUpdates()->resetOptions( $kinds, $this->getContext() );
171 }
172
173 /**
174 * Sets one user preference to be applied by commitChanges()
175 *
176 * @param string $preference
177 * @param mixed $value
178 */
179 protected function setPreference( $preference, $value ) {
180 $this->getUserForUpdates()->setOption( $preference, $value );
181 }
182
183 /**
184 * Applies changes to user preferences
185 */
186 protected function commitChanges() {
187 $this->getUserForUpdates()->saveSettings();
188 }
189
190 public function mustBePosted() {
191 return true;
192 }
193
194 public function isWriteMode() {
195 return true;
196 }
197
198 public function getAllowedParams() {
199 $optionKinds = User::listOptionKinds();
200 $optionKinds[] = 'all';
201
202 return [
203 'reset' => false,
204 'resetkinds' => [
205 ApiBase::PARAM_TYPE => $optionKinds,
206 ApiBase::PARAM_DFLT => 'all',
207 ApiBase::PARAM_ISMULTI => true
208 ],
209 'change' => [
210 ApiBase::PARAM_ISMULTI => true,
211 ],
212 'optionname' => [
213 ApiBase::PARAM_TYPE => 'string',
214 ],
215 'optionvalue' => [
216 ApiBase::PARAM_TYPE => 'string',
217 ],
218 ];
219 }
220
221 public function needsToken() {
222 return 'csrf';
223 }
224
225 public function getHelpUrls() {
226 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
227 }
228
229 protected function getExamplesMessages() {
230 return [
231 'action=options&reset=&token=123ABC'
232 => 'apihelp-options-example-reset',
233 'action=options&change=skin=vector|hideminor=1&token=123ABC'
234 => 'apihelp-options-example-change',
235 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
236 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
237 => 'apihelp-options-example-complex',
238 ];
239 }
240 }