| 1 |
<?php |
| 2 |
|
| 3 |
pl |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\HTMLForm\HTMLForm; |
| 26 |
use MediaWiki\Logger\LoggerFactory; |
| 27 |
use MediaWiki\Message\Message; |
| 28 |
use MediaWiki\Preferences\DefaultPreferencesFactory; |
| 29 |
use MediaWiki\Preferences\PreferencesFactory; |
| 30 |
use MediaWiki\User\Options\UserOptionsManager; |
| 31 |
use MediaWiki\User\User; |
| 32 |
use Wikimedia\ParamValidator\ParamValidator; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
abstract class ApiOptionsBase extends ApiBase { |
| 41 |
|
| 42 |
private $userForUpdates; |
| 43 |
|
| 44 |
private UserOptionsManager $userOptionsManager; |
| 45 |
private PreferencesFactory $preferencesFactory; |
| 46 |
|
| 47 |
|
| 48 |
private $preferences; |
| 49 |
|
| 50 |
|
| 51 |
private $htmlForm; |
| 52 |
|
| 53 |
|
| 54 |
private $prefsKinds; |
| 55 |
|
| 56 |
|
| 57 |
private $params; |
| 58 |
|
| 59 |
public function __construct( |
| 60 |
ApiMain $main, |
| 61 |
string $action, |
| 62 |
UserOptionsManager $userOptionsManager, |
| 63 |
PreferencesFactory $preferencesFactory |
| 64 |
) { |
| 65 |
parent::__construct( $main, $action ); |
| 66 |
$this->userOptionsManager = $userOptionsManager; |
| 67 |
$this->preferencesFactory = $preferencesFactory; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function execute() { |
| 74 |
$user = $this->getUserForUpdatesOrNull(); |
| 75 |
if ( !$user || !$user->isNamed() ) { |
| 76 |
$this->dieWithError( |
| 77 |
[ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin' |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
$this->checkUserRightsAny( 'editmyoptions' ); |
| 82 |
|
| 83 |
$params = $this->extractRequestParams(); |
| 84 |
$changed = false; |
| 85 |
|
| 86 |
if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) { |
| 87 |
$this->dieWithError( [ 'apierror-missingparam', 'optionname' ] ); |
| 88 |
} |
| 89 |
|
| 90 |
$resetKinds = $params['resetkinds']; |
| 91 |
if ( !$params['reset'] ) { |
| 92 |
$resetKinds = []; |
| 93 |
} |
| 94 |
|
| 95 |
$changes = []; |
| 96 |
if ( $params['change'] ) { |
| 97 |
foreach ( $params['change'] as $entry ) { |
| 98 |
$array = explode( '=', $entry, 2 ); |
| 99 |
$changes[$array[0]] = $array[1] ?? null; |
| 100 |
} |
| 101 |
} |
| 102 |
if ( isset( $params['optionname'] ) ) { |
| 103 |
$newValue = $params['optionvalue'] ?? null; |
| 104 |
$changes[$params['optionname']] = $newValue; |
| 105 |
} |
| 106 |
|
| 107 |
$this->runHook( $user, $changes, $resetKinds ); |
| 108 |
|
| 109 |
if ( $resetKinds ) { |
| 110 |
$this->resetPreferences( $resetKinds ); |
| 111 |
$changed = true; |
| 112 |
} |
| 113 |
|
| 114 |
if ( !$changed && !count( $changes ) ) { |
| 115 |
$this->dieWithError( 'apierror-nochanges' ); |
| 116 |
} |
| 117 |
|
| 118 |
$this->prefsKinds = $this->preferencesFactory->getResetKinds( $user, $this->getContext(), $changes ); |
| 119 |
|
| 120 |
foreach ( $changes as $key => $value ) { |
| 121 |
if ( $this->shouldIgnoreKey( $key ) ) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
$validation = $this->validate( $key, $value ); |
| 125 |
if ( $validation === true ) { |
| 126 |
$this->setPreference( $key, $value ); |
| 127 |
$changed = true; |
| 128 |
} else { |
| 129 |
$this->addWarning( [ 'apiwarn-validationfailed', wfEscapeWikiText( $key ), $validation ] ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if ( $changed ) { |
| 134 |
$this->commitChanges(); |
| 135 |
} |
| 136 |
|
| 137 |
$this->getResult()->addValue( null, $this->getModuleName(), 'success' ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected function runHook( $user, $changes, $resetKinds ) { |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
protected function shouldIgnoreKey( $key ) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
protected function getPrefsKinds(): array { |
| 169 |
return $this->prefsKinds; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
protected function getHtmlForm() { |
| 178 |
if ( !$this->htmlForm ) { |
| 179 |
$this->htmlForm = new HTMLForm( |
| 180 |
DefaultPreferencesFactory::simplifyFormDescriptor( $this->getPreferences() ), |
| 181 |
$this |
| 182 |
); |
| 183 |
} |
| 184 |
return $this->htmlForm; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
protected function validate( $key, &$value ) { |
| 195 |
switch ( $this->getPrefsKinds()[$key] ) { |
| 196 |
case 'registered': |
| 197 |
|
| 198 |
if ( $value === null ) { |
| 199 |
|
| 200 |
$validation = true; |
| 201 |
} else { |
| 202 |
|
| 203 |
$field = $this->getHtmlForm()->getField( $key ); |
| 204 |
$validation = $field->validate( |
| 205 |
$value, |
| 206 |
$this->userOptionsManager->getOptions( $this->getUserForUpdates() ) |
| 207 |
); |
| 208 |
} |
| 209 |
break; |
| 210 |
case 'registered-multiselect': |
| 211 |
case 'registered-checkmatrix': |
| 212 |
|
| 213 |
|
| 214 |
$validation = true; |
| 215 |
$value = $value !== null ? (bool)$value : null; |
| 216 |
break; |
| 217 |
case 'userjs': |
| 218 |
|
| 219 |
if ( strlen( $key ) > 255 ) { |
| 220 |
$validation = $this->msg( 'apiwarn-validationfailed-keytoolong', Message::numParam( 255 ) ); |
| 221 |
} elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) { |
| 222 |
$validation = $this->msg( 'apiwarn-validationfailed-badchars' ); |
| 223 |
} else { |
| 224 |
$validation = true; |
| 225 |
} |
| 226 |
|
| 227 |
LoggerFactory::getInstance( 'api-warning' )->info( |
| 228 |
'ApiOptions: Setting userjs option', |
| 229 |
[ |
| 230 |
'phab' => 'T259073', |
| 231 |
'OptionName' => substr( $key, 0, 255 ), |
| 232 |
'OptionValue' => substr( $value ?? '', 0, 255 ), |
| 233 |
'OptionSize' => strlen( $value ?? '' ), |
| 234 |
'OptionValidation' => $validation, |
| 235 |
'UserId' => $this->getUserForUpdates()->getId(), |
| 236 |
'RequestIP' => $this->getRequest()->getIP(), |
| 237 |
'RequestUA' => $this->getRequest()->getHeader( 'User-Agent' ) |
| 238 |
] |
| 239 |
); |
| 240 |
break; |
| 241 |
case 'special': |
| 242 |
$validation = $this->msg( 'apiwarn-validationfailed-cannotset' ); |
| 243 |
break; |
| 244 |
case 'unused': |
| 245 |
default: |
| 246 |
$validation = $this->msg( 'apiwarn-validationfailed-badpref' ); |
| 247 |
break; |
| 248 |
} |
| 249 |
if ( $validation === true && is_string( $value ) && |
| 250 |
strlen( $value ) > UserOptionsManager::MAX_BYTES_OPTION_VALUE |
| 251 |
) { |
| 252 |
$validation = $this->msg( |
| 253 |
'apiwarn-validationfailed-valuetoolong', |
| 254 |
Message::numParam( UserOptionsManager::MAX_BYTES_OPTION_VALUE ) |
| 255 |
); |
| 256 |
} |
| 257 |
return $validation; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
protected function getUserForUpdates(): User { |
| 267 |
|
| 268 |
return $this->getUserForUpdatesOrNull(); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
protected function getUserForUpdatesOrNull(): ?User { |
| 277 |
if ( !$this->userForUpdates ) { |
| 278 |
$this->userForUpdates = $this->getUser()->getInstanceForUpdate(); |
| 279 |
} |
| 280 |
|
| 281 |
return $this->userForUpdates; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
protected function getPreferences() { |
| 289 |
if ( !$this->preferences ) { |
| 290 |
$this->preferences = $this->preferencesFactory->getFormDescriptor( |
| 291 |
$this->getUserForUpdates(), |
| 292 |
$this->getContext() |
| 293 |
); |
| 294 |
} |
| 295 |
return $this->preferences; |
| 296 |
} |
| 297 |
|
| 298 |
protected function getUserOptionsManager(): UserOptionsManager { |
| 299 |
return $this->userOptionsManager; |
| 300 |
} |
| 301 |
|
| 302 |
protected function getPreferencesFactory(): PreferencesFactory { |
| 303 |
return $this->preferencesFactory; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
abstract protected function resetPreferences( array $kinds ); |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
abstract protected function setPreference( $preference, $value ); |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
abstract protected function commitChanges(); |
| 325 |
|
| 326 |
public function mustBePosted() { |
| 327 |
return true; |
| 328 |
} |
| 329 |
|
| 330 |
public function isWriteMode() { |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
public function getAllowedParams() { |
| 335 |
$optionKinds = $this->preferencesFactory->listResetKinds(); |
| 336 |
$optionKinds[] = 'all'; |
| 337 |
|
| 338 |
return [ |
| 339 |
'reset' => false, |
| 340 |
'resetkinds' => [ |
| 341 |
ParamValidator::PARAM_TYPE => $optionKinds, |
| 342 |
ParamValidator::PARAM_DEFAULT => 'all', |
| 343 |
ParamValidator::PARAM_ISMULTI => true |
| 344 |
], |
| 345 |
'change' => [ |
| 346 |
ParamValidator::PARAM_ISMULTI => true, |
| 347 |
], |
| 348 |
'optionname' => [ |
| 349 |
ParamValidator::PARAM_TYPE => 'string', |
| 350 |
], |
| 351 |
'optionvalue' => [ |
| 352 |
ParamValidator::PARAM_TYPE => 'string', |
| 353 |
], |
| 354 |
]; |
| 355 |
} |
| 356 |
|
| 357 |
public function needsToken() { |
| 358 |
return 'csrf'; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
class_alias( ApiOptionsBase::class, 'ApiOptionsBase' ); |