| 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\MediaWikiServices; |
| 26 |
use MediaWiki\Preferences\PreferencesFactory; |
| 27 |
use MediaWiki\User\Options\UserOptionsManager; |
| 28 |
use Wikimedia\ParamValidator\ParamValidator; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ApiOptions extends ApiOptionsBase { |
| 37 |
public function __construct( |
| 38 |
ApiMain $main, |
| 39 |
string $action, |
| 40 |
?UserOptionsManager $userOptionsManager = null, |
| 41 |
?PreferencesFactory $preferencesFactory = null |
| 42 |
) { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
$services = MediaWikiServices::getInstance(); |
| 48 |
$userOptionsManager ??= $services->getUserOptionsManager(); |
| 49 |
$preferencesFactory ??= $services->getPreferencesFactory(); |
| 50 |
parent::__construct( $main, $action, $userOptionsManager, $preferencesFactory ); |
| 51 |
} |
| 52 |
|
| 53 |
protected function runHook( $user, $changes, $resetKinds ) { |
| 54 |
$this->getHookRunner()->onApiOptions( $this, $user, $changes, $resetKinds ); |
| 55 |
} |
| 56 |
|
| 57 |
protected function shouldIgnoreKey( $key ) { |
| 58 |
$user = $this->getUserForUpdates(); |
| 59 |
$manager = $this->getUserOptionsManager(); |
| 60 |
if ( $this->getGlobalParam() === 'ignore' && $manager->isOptionGlobal( $user, $key ) ) { |
| 61 |
$this->addWarning( $this->msg( 'apiwarn-global-option-ignored', $key ) ); |
| 62 |
return true; |
| 63 |
} |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
protected function resetPreferences( array $kinds ) { |
| 68 |
$optionNames = $this->getPreferencesFactory()->getOptionNamesForReset( |
| 69 |
$this->getUserForUpdates(), $this->getContext(), $kinds ); |
| 70 |
$this->getUserOptionsManager()->resetOptionsByName( $this->getUserForUpdates(), $optionNames ); |
| 71 |
} |
| 72 |
|
| 73 |
protected function setPreference( $preference, $value ) { |
| 74 |
$globalUpdateType = [ |
| 75 |
'ignore' => UserOptionsManager::GLOBAL_IGNORE, |
| 76 |
'update' => UserOptionsManager::GLOBAL_UPDATE, |
| 77 |
'override' => UserOptionsManager::GLOBAL_OVERRIDE |
| 78 |
][ $this->getGlobalParam() ]; |
| 79 |
|
| 80 |
$this->getUserOptionsManager()->setOption( |
| 81 |
$this->getUserForUpdates(), |
| 82 |
$preference, |
| 83 |
$value, |
| 84 |
$globalUpdateType |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
private function getGlobalParam() { |
| 89 |
return $this->extractRequestParams()['global']; |
| 90 |
} |
| 91 |
|
| 92 |
protected function commitChanges() { |
| 93 |
$this->getUserForUpdates()->saveSettings(); |
| 94 |
} |
| 95 |
|
| 96 |
public function getHelpUrls() { |
| 97 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options'; |
| 98 |
} |
| 99 |
|
| 100 |
protected function getExamplesMessages() { |
| 101 |
return [ |
| 102 |
'action=options&reset=&token=123ABC' |
| 103 |
=> 'apihelp-options-example-reset', |
| 104 |
'action=options&change=skin=vector|hideminor=1&token=123ABC' |
| 105 |
=> 'apihelp-options-example-change', |
| 106 |
'action=options&reset=&change=skin=monobook&optionname=nickname&' . |
| 107 |
'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC' |
| 108 |
=> 'apihelp-options-example-complex', |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
public function getAllowedParams() { |
| 113 |
return parent::getAllowedParams() + [ |
| 114 |
'global' => [ |
| 115 |
ParamValidator::PARAM_TYPE => [ 'ignore', 'update', 'override' ], |
| 116 |
ParamValidator::PARAM_DEFAULT => 'ignore' |
| 117 |
] |
| 118 |
]; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
class_alias( ApiOptions::class, 'ApiOptions' ); |