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