Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
53.66% covered (warning)
53.66%
22 / 41
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryGlobalPreferences
53.66% covered (warning)
53.66%
22 / 41
0.00% covered (danger)
0.00%
0 / 4
23.04
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
84.62% covered (warning)
84.62%
22 / 26
0.00% covered (danger)
0.00%
0 / 1
8.23
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GlobalPreferences;
4
5use ApiBase;
6use ApiQuery;
7use ApiQueryBase;
8use ApiResult;
9use MediaWiki\User\Options\UserOptionsLookup;
10use Wikimedia\ParamValidator\ParamValidator;
11
12class ApiQueryGlobalPreferences extends ApiQueryBase {
13    /**
14     * @var GlobalPreferencesFactory
15     */
16    private $preferencesFactory;
17
18    /**
19     * @var UserOptionsLookup
20     */
21    private $userOptionsLookup;
22
23    /**
24     * @param ApiQuery $queryModule
25     * @param string $moduleName
26     * @param GlobalPreferencesFactory $factory
27     * @param UserOptionsLookup $userOptionsLookup
28     */
29    public function __construct(
30        ApiQuery $queryModule,
31        $moduleName,
32        GlobalPreferencesFactory $factory,
33        UserOptionsLookup $userOptionsLookup
34    ) {
35        parent::__construct( $queryModule, $moduleName, 'gpr' );
36        $this->preferencesFactory = $factory;
37        $this->userOptionsLookup = $userOptionsLookup;
38    }
39
40    /**
41     * @inheritDoc
42     */
43    public function execute() {
44        if ( !$this->getUser()->isNamed() ) {
45            $this->dieWithError(
46                [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin'
47            );
48        }
49
50        if ( !$this->preferencesFactory->isUserGlobalized( $this->getUser() ) ) {
51            $this->dieWithError( 'apierror-globalpreferences-notglobalized', 'notglobalized' );
52        }
53
54        $params = $this->extractRequestParams();
55        $prop = array_flip( $params['prop'] );
56        $result = [];
57        ApiResult::setArrayType( $result, 'assoc' );
58
59        if ( isset( $prop['preferences'] ) ) {
60            $prefs = $this->preferencesFactory->getGlobalPreferencesValues( $this->getUser() );
61            $result['preferences'] = $prefs;
62            ApiResult::setArrayType( $result['preferences'], 'assoc' );
63        }
64
65        if ( isset( $prop['localoverrides'] ) ) {
66            $overriddenPrefs = [];
67            $userOptions = $this->userOptionsLookup->getOptions( $this->getUser() );
68            foreach ( $userOptions as $pref => $value ) {
69                if ( GlobalPreferencesFactory::isLocalPrefName( $pref ) ) {
70                    $mainPref = substr( $pref, 0,
71                        -strlen( GlobalPreferencesFactory::LOCAL_EXCEPTION_SUFFIX ) );
72                    if ( isset( $userOptions[$mainPref] ) ) {
73                        $overriddenPrefs[$mainPref] = $userOptions[$mainPref];
74                    }
75                }
76            }
77            $result['localoverrides'] = $overriddenPrefs;
78            ApiResult::setArrayType( $result['localoverrides'], 'assoc' );
79        }
80
81        $this->getResult()->addValue( 'query', $this->getModuleName(), $result );
82    }
83
84    /**
85     * @inheritDoc
86     */
87    public function getAllowedParams() {
88        return [
89            'prop' => [
90                ParamValidator::PARAM_TYPE => [
91                    'preferences',
92                    'localoverrides',
93                ],
94                ParamValidator::PARAM_ISMULTI => true,
95                ParamValidator::PARAM_DEFAULT => 'preferences|localoverrides',
96                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
97            ],
98        ];
99    }
100
101    /**
102     * @inheritDoc
103     */
104    public function getHelpUrls() {
105        return [ 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:GlobalPreferences/API' ];
106    }
107}