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