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