Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiULSLocalization
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 6
56
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 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 getCustomPrinter
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 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 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Localization API for ULS
4 *
5 * Copyright (C) 2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
6 * Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other
7 * contributors. See CREDITS for a list.
8 *
9 * UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't
10 * have to do anything special to choose one license or the other and you don't
11 * have to notify anyone which license you are using. You are free to use
12 * UniversalLanguageSelector in commercial projects as long as the copyright
13 * header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
14 *
15 * @file
16 * @ingroup Extensions
17 * @license GPL-2.0-or-later
18 * @license MIT
19 */
20
21namespace UniversalLanguageSelector\Api;
22
23use ApiBase;
24use ApiFormatRaw;
25use ApiMain;
26use MediaWiki\Languages\LanguageNameUtils;
27use UniversalLanguageSelector\ULSJsonMessageLoader;
28use Wikimedia\ParamValidator\ParamValidator;
29
30/**
31 * @ingroup API
32 */
33class ApiULSLocalization extends ApiBase {
34    /** @var LanguageNameUtils */
35    private $languageNameUtils;
36
37    /**
38     * @param ApiMain $main
39     * @param string $action
40     * @param LanguageNameUtils $languageNameUtils
41     */
42    public function __construct(
43        ApiMain $main,
44        $action,
45        LanguageNameUtils $languageNameUtils
46    ) {
47        parent::__construct( $main, $action );
48        $this->languageNameUtils = $languageNameUtils;
49    }
50
51    /**
52     * @inheritDoc
53     */
54    public function execute() {
55        $this->getMain()->setCacheMode( 'public' );
56        $this->getMain()->setCacheMaxAge( 2419200 );
57
58        $params = $this->extractRequestParams();
59        $language = $params['language'];
60        if ( !$this->languageNameUtils->isValidCode( $language ) ) {
61            $this->dieWithError( [ 'apierror-invalidlang', 'language' ], 'invalidlanguage' );
62        }
63        $contents = ULSJsonMessageLoader::getMessages( $language );
64        // Output the file's contents raw
65        $this->getResult()->addValue( null, 'text', json_encode( $contents ) );
66        $this->getResult()->addValue( null, 'mime', 'application/json' );
67    }
68
69    /**
70     * @inheritDoc
71     */
72    public function getCustomPrinter() {
73        return new ApiFormatRaw(
74            $this->getMain(),
75            $this->getMain()->createPrinterByName( 'json' )
76        );
77    }
78
79    /**
80     * @inheritDoc
81     */
82    public function getAllowedParams() {
83        return [
84            'language' => [
85                ParamValidator::PARAM_REQUIRED => true,
86                ParamValidator::PARAM_TYPE => 'string',
87            ],
88        ];
89    }
90
91    /**
92     * @inheritDoc
93     */
94    protected function getExamplesMessages() {
95        return [
96            'action=ulslocalization&language=ta'
97                => 'apihelp-ulslocalization-example-1',
98            'action=ulslocalization&language=hi'
99                => 'apihelp-ulslocalization-example-2',
100        ];
101    }
102
103    /**
104     * @inheritDoc
105     */
106    public function isInternal() {
107        // Try to scare people away from using this externally
108        return true;
109    }
110}