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