Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
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 / 24
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 / 2
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 ApiQuery;
26use ApiQueryBase;
27use ApiResult;
28use MediaWiki\User\UserIdentityLookup;
29use Wikimedia\ParamValidator\ParamValidator;
30
31class ApiQueryBabel extends ApiQueryBase {
32    /** @var UserIdentityLookup */
33    private $userIdentityLookup;
34
35    public function __construct(
36        ApiQuery $queryModule,
37        string $moduleName,
38        UserIdentityLookup $userIdentityLookup
39    ) {
40        parent::__construct( $queryModule, $moduleName, 'bab' );
41        $this->userIdentityLookup = $userIdentityLookup;
42    }
43
44    public function execute(): void {
45        $params = $this->extractRequestParams();
46        $userName = $params['user'];
47        $user = $this->userIdentityLookup->getUserIdentityByName( $userName );
48        if ( !$user || !$user->getId() ) {
49            $this->dieWithError( [ 'nosuchusershort', wfEscapeWikiText( $userName ) ], 'baduser' );
50        }
51
52        $data = Babel::getUserLanguageInfo( $user );
53        // Force a JSON object
54        $data[ApiResult::META_TYPE] = 'assoc';
55
56        $this->getResult()->addValue(
57            'query',
58            $this->getModuleName(),
59            $data
60        );
61    }
62
63    public function getAllowedParams( /* $flags = 0 */ ): array {
64        return [
65            'user' => [
66                ParamValidator::PARAM_REQUIRED => true,
67                ParamValidator::PARAM_TYPE => 'user',
68            ]
69        ];
70    }
71
72    /**
73     * @inheritDoc
74     */
75    protected function getExamplesMessages(): array {
76        return [
77            'action=query&meta=babel&babuser=Example'
78                => 'apihelp-query+babel-example-1',
79        ];
80    }
81
82}