Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
71.79% |
28 / 39 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
ApiGlobalPreferences | |
71.79% |
28 / 39 |
|
50.00% |
4 / 8 |
20.05 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
getFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
resetPreferences | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setPreference | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
commitChanges | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
5.01 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GlobalPreferences; |
4 | |
5 | use MediaWiki\Api\ApiMain; |
6 | use MediaWiki\Api\ApiOptionsBase; |
7 | use MediaWiki\User\Options\UserOptionsManager; |
8 | |
9 | class ApiGlobalPreferences extends ApiOptionsBase { |
10 | |
11 | /** @var mixed[] */ |
12 | private $prefs = []; |
13 | |
14 | /** @var string[] */ |
15 | private $resetPrefTypes = []; |
16 | |
17 | private GlobalPreferencesFactory $factory; |
18 | |
19 | public function __construct( |
20 | ApiMain $mainModule, |
21 | string $moduleName, |
22 | GlobalPreferencesFactory $factory, |
23 | UserOptionsManager $userOptionsManager |
24 | ) { |
25 | parent::__construct( $mainModule, $moduleName, $userOptionsManager, $factory ); |
26 | $this->factory = $factory; |
27 | } |
28 | |
29 | /** |
30 | * @inheritDoc |
31 | */ |
32 | public function execute() { |
33 | $user = $this->getUserForUpdatesOrNull(); |
34 | if ( $user ) { |
35 | $factory = $this->getFactory(); |
36 | if ( !$factory->isUserGlobalized( $user ) ) { |
37 | $this->dieWithError( 'apierror-globalpreferences-notglobalized', 'notglobalized' ); |
38 | } |
39 | } |
40 | parent::execute(); |
41 | } |
42 | |
43 | /** |
44 | * @return GlobalPreferencesFactory |
45 | */ |
46 | private function getFactory() { |
47 | return $this->factory; |
48 | } |
49 | |
50 | /** |
51 | * @inheritDoc |
52 | */ |
53 | protected function resetPreferences( array $kinds ) { |
54 | if ( in_array( 'all', $kinds ) ) { |
55 | $this->getFactory()->resetGlobalUserSettings( $this->getUserForUpdates() ); |
56 | } else { |
57 | $this->resetPrefTypes = $kinds; |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * @inheritDoc |
63 | */ |
64 | protected function setPreference( $preference, $value ) { |
65 | $this->prefs[$preference] = $value; |
66 | } |
67 | |
68 | /** |
69 | * @inheritDoc |
70 | */ |
71 | protected function commitChanges() { |
72 | $factory = $this->getFactory(); |
73 | $user = $this->getUserForUpdates(); |
74 | $prefs = $this->getFactory()->getGlobalPreferencesValues( $user, true ); |
75 | if ( $prefs === false ) { |
76 | return; |
77 | } |
78 | if ( $this->resetPrefTypes ) { |
79 | $kinds = $this->getFactory()->getResetKinds( |
80 | $this->getUserForUpdates(), |
81 | $this->getContext(), |
82 | $prefs |
83 | ); |
84 | foreach ( $prefs as $pref => $value ) { |
85 | $kind = $kinds[$pref]; |
86 | if ( in_array( $kind, $this->resetPrefTypes ) ) { |
87 | $prefs[$pref] = null; |
88 | } |
89 | } |
90 | } |
91 | $prefs = array_merge( $prefs, $this->prefs ); |
92 | $factory->setGlobalPreferences( $user, $prefs, $this->getContext() ); |
93 | } |
94 | |
95 | /** |
96 | * @inheritDoc |
97 | */ |
98 | public function getHelpUrls() { |
99 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Globalpreferences'; |
100 | } |
101 | |
102 | /** |
103 | * @inheritDoc |
104 | */ |
105 | protected function getExamplesMessages() { |
106 | return [ |
107 | 'action=globalpreferences&change=skin=&token=123ABC' |
108 | => 'apihelp-globalpreferences-example-reset-one', |
109 | 'action=globalpreferences&reset=&token=123ABC' |
110 | => 'apihelp-globalpreferences-example-reset', |
111 | 'action=globalpreferences&change=skin=vector|hideminor=1&token=123ABC' |
112 | => 'apihelp-globalpreferences-example-change', |
113 | ]; |
114 | } |
115 | } |