Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
32.14% covered (danger)
32.14%
9 / 28
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiGlobalPreferenceOverrides
32.14% covered (danger)
32.14%
9 / 28
28.57% covered (danger)
28.57%
2 / 7
41.25
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 resetPreferences
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 setPreference
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
2.98
 commitChanges
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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 MediaWiki\Api\ApiMain;
6use MediaWiki\Api\ApiOptionsBase;
7use MediaWiki\User\Options\UserOptionsLookup;
8use MediaWiki\User\Options\UserOptionsManager;
9
10class ApiGlobalPreferenceOverrides extends ApiOptionsBase {
11
12    private GlobalPreferencesFactory $globalPrefs;
13
14    public function __construct(
15        ApiMain $mainModule,
16        string $moduleName,
17        GlobalPreferencesFactory $factory,
18        UserOptionsManager $userOptionsManager
19    ) {
20        parent::__construct( $mainModule, $moduleName, $userOptionsManager, $factory );
21        $this->globalPrefs = $factory;
22    }
23
24    /**
25     * @inheritDoc
26     */
27    public function execute() {
28        $user = $this->getUserForUpdatesOrNull();
29        if ( $user && !$this->globalPrefs->isUserGlobalized( $user ) ) {
30            $this->dieWithError( 'apierror-globalpreferences-notglobalized', 'notglobalized' );
31        }
32        parent::execute();
33    }
34
35    /**
36     * @inheritDoc
37     */
38    protected function resetPreferences( array $kinds ) {
39        $optionNames = array_map(
40            static fn ( $name ) => $name . UserOptionsLookup::LOCAL_EXCEPTION_SUFFIX,
41            $this->globalPrefs->getOptionNamesForReset(
42                $this->getUserForUpdates(), $this->getContext(), $kinds )
43        );
44        $this->getUserOptionsManager()->resetOptionsByName( $this->getUserForUpdates(), $optionNames );
45    }
46
47    /**
48     * @inheritDoc
49     */
50    protected function setPreference( $preference, $value ) {
51        if ( $value === null ) {
52            $this->getUserOptionsManager()->setOption(
53                $this->getUserForUpdates(),
54                $preference . UserOptionsLookup::LOCAL_EXCEPTION_SUFFIX,
55                null
56            );
57        } else {
58            $this->getUserOptionsManager()->setOption(
59                $this->getUserForUpdates(), $preference, $value, UserOptionsManager::GLOBAL_OVERRIDE );
60        }
61    }
62
63    /**
64     * @inheritDoc
65     */
66    protected function commitChanges() {
67        $this->getUserOptionsManager()->saveOptions( $this->getUserForUpdates() );
68    }
69
70    /**
71     * @inheritDoc
72     */
73    public function getHelpUrls() {
74        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:GlobalPreferences/API';
75    }
76
77    /**
78     * @inheritDoc
79     */
80    protected function getExamplesMessages() {
81        return [
82            'action=globalpreferenceoverrides&reset=&token=123ABC'
83                => 'apihelp-globalpreferenceoverrides-example-reset',
84            'action=globalpreferenceoverrides&change=skin=vector|hideminor=1&token=123ABC'
85                => 'apihelp-globalpreferenceoverrides-example-change',
86        ];
87    }
88}