Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryBabel
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
42
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
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21declare( strict_types = 1 );
22
23namespace MediaWiki\Babel;
24
25use MediaWiki\Api\ApiQuery;
26use MediaWiki\Api\ApiQueryBase;
27use MediaWiki\Api\ApiResult;
28use MediaWiki\User\UserIdentityLookup;
29use Wikimedia\ParamValidator\ParamValidator;
30
31class ApiQueryBabel extends ApiQueryBase {
32    public function __construct(
33        ApiQuery $queryModule,
34        string $moduleName,
35        private readonly UserIdentityLookup $userIdentityLookup,
36    ) {
37        parent::__construct( $queryModule, $moduleName, 'bab' );
38    }
39
40    public function execute(): void {
41        $params = $this->extractRequestParams();
42        $userName = $params['user'];
43        $user = $this->userIdentityLookup->getUserIdentityByName( $userName );
44        if ( !$user || !$user->getId() ) {
45            $this->dieWithError( [ 'nosuchusershort', wfEscapeWikiText( $userName ) ], 'baduser' );
46        }
47
48        $data = Babel::getUserLanguageInfo( $user );
49        // Force a JSON object
50        $data[ApiResult::META_TYPE] = 'assoc';
51
52        $this->getResult()->addValue(
53            'query',
54            $this->getModuleName(),
55            $data
56        );
57    }
58
59    /** @inheritDoc */
60    public function getAllowedParams(): array {
61        return [
62            'user' => [
63                ParamValidator::PARAM_REQUIRED => true,
64                ParamValidator::PARAM_TYPE => 'user',
65            ]
66        ];
67    }
68
69    /** @inheritDoc */
70    protected function getExamplesMessages(): array {
71        return [
72            'action=query&meta=babel&babuser=Example'
73                => 'apihelp-query+babel-example-1',
74        ];
75    }
76
77}