Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
32 / 40 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ApiOptions | |
82.05% |
32 / 39 |
|
87.50% |
7 / 8 |
15.13 | |
0.00% |
0 / 1 |
| __construct | |
22.22% |
2 / 9 |
|
0.00% |
0 / 1 |
7.23 | |||
| runHook | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| shouldIgnoreKey | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| resetPreferences | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| setPreference | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| getGlobalParam | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| commitChanges | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHelpUrls | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| getExamplesMessages | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| getAllowedParams | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2012 Szymon Świerkosz beau@adres.pl |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\MediaWikiServices; |
| 12 | use MediaWiki\Preferences\PreferencesFactory; |
| 13 | use MediaWiki\User\Options\UserOptionsManager; |
| 14 | use MediaWiki\User\User; |
| 15 | use Wikimedia\ParamValidator\ParamValidator; |
| 16 | |
| 17 | /** |
| 18 | * API module that facilitates the changing of user's preferences. |
| 19 | * Requires API write mode to be enabled. |
| 20 | * |
| 21 | * @ingroup API |
| 22 | */ |
| 23 | class ApiOptions extends ApiOptionsBase { |
| 24 | public function __construct( |
| 25 | ApiMain $main, |
| 26 | string $action, |
| 27 | ?UserOptionsManager $userOptionsManager = null, |
| 28 | ?PreferencesFactory $preferencesFactory = null |
| 29 | ) { |
| 30 | if ( $userOptionsManager === null || $preferencesFactory === null ) { |
| 31 | wfDeprecatedMsg( |
| 32 | __METHOD__ . ': calling without $userOptionsManager and $preferencesFactory is deprecated', |
| 33 | '1.45' |
| 34 | ); |
| 35 | $services = MediaWikiServices::getInstance(); |
| 36 | $userOptionsManager ??= $services->getUserOptionsManager(); |
| 37 | $preferencesFactory ??= $services->getPreferencesFactory(); |
| 38 | } |
| 39 | parent::__construct( $main, $action, $userOptionsManager, $preferencesFactory ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param User $user |
| 44 | * @param array $changes |
| 45 | * @param string[] $resetKinds |
| 46 | */ |
| 47 | protected function runHook( $user, $changes, $resetKinds ) { |
| 48 | $this->getHookRunner()->onApiOptions( $this, $user, $changes, $resetKinds ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param string $key |
| 53 | * @return bool |
| 54 | */ |
| 55 | protected function shouldIgnoreKey( $key ) { |
| 56 | $user = $this->getUser(); |
| 57 | $manager = $this->getUserOptionsManager(); |
| 58 | if ( $this->getGlobalParam() === 'ignore' && $manager->isOptionGlobal( $user, $key ) ) { |
| 59 | $this->addWarning( $this->msg( 'apiwarn-global-option-ignored', $key ) ); |
| 60 | return true; |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | protected function resetPreferences( array $kinds ) { |
| 66 | $optionNames = $this->getPreferencesFactory()->getOptionNamesForReset( |
| 67 | $this->getUser(), $this->getContext(), $kinds ); |
| 68 | $this->getUserOptionsManager()->resetOptionsByName( $this->getUser(), $optionNames ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param string $preference |
| 73 | * @param mixed $value |
| 74 | */ |
| 75 | protected function setPreference( $preference, $value ) { |
| 76 | $globalUpdateType = match ( $this->getGlobalParam() ) { |
| 77 | 'ignore' => UserOptionsManager::GLOBAL_IGNORE, |
| 78 | 'update' => UserOptionsManager::GLOBAL_UPDATE, |
| 79 | 'override' => UserOptionsManager::GLOBAL_OVERRIDE, |
| 80 | 'create' => UserOptionsManager::GLOBAL_CREATE, |
| 81 | }; |
| 82 | |
| 83 | $this->getUserOptionsManager()->setOption( |
| 84 | $this->getUser(), |
| 85 | $preference, |
| 86 | $value, |
| 87 | $globalUpdateType |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | private function getGlobalParam(): string { |
| 92 | return $this->extractRequestParams()['global']; |
| 93 | } |
| 94 | |
| 95 | protected function commitChanges() { |
| 96 | $this->getUserOptionsManager()->saveOptions( $this->getUser() ); |
| 97 | } |
| 98 | |
| 99 | /** @codeCoverageIgnore Merely declarative */ |
| 100 | public function getHelpUrls(): string { |
| 101 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options'; |
| 102 | } |
| 103 | |
| 104 | /** @codeCoverageIgnore Merely declarative */ |
| 105 | protected function getExamplesMessages(): array { |
| 106 | return [ |
| 107 | 'action=options&reset=&token=123ABC' |
| 108 | => 'apihelp-options-example-reset', |
| 109 | 'action=options&change=skin=vector|hideminor=1&token=123ABC' |
| 110 | => 'apihelp-options-example-change', |
| 111 | 'action=options&reset=&change=skin=monobook&optionname=nickname&' . |
| 112 | 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC' |
| 113 | => 'apihelp-options-example-complex', |
| 114 | ]; |
| 115 | } |
| 116 | |
| 117 | /** @inheritDoc */ |
| 118 | public function getAllowedParams() { |
| 119 | return parent::getAllowedParams() + [ |
| 120 | 'global' => [ |
| 121 | ParamValidator::PARAM_TYPE => [ 'ignore', 'update', 'override', 'create' ], |
| 122 | ParamValidator::PARAM_DEFAULT => 'ignore' |
| 123 | ] |
| 124 | ]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** @deprecated class alias since 1.43 */ |
| 129 | class_alias( ApiOptions::class, 'ApiOptions' ); |