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