Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
TtmServerActionApi
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 5
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 getAvailableTranslationServices
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
6
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TtmServer;
5
6use ApiBase;
7use ApiMain;
8use Config;
9use MediaWiki\Config\ServiceOptions;
10use Wikimedia\ParamValidator\ParamValidator;
11
12/**
13 * API module for TTMServer
14 * @ingroup API TranslateAPI TTMServer
15 * @author Niklas Laxström
16 * @license GPL-2.0-or-later
17 * @since 2012-01-26
18 */
19class TtmServerActionApi extends ApiBase {
20    /** @var TtmServerFactory */
21    private $ttmServerFactory;
22    /** @var ServiceOptions */
23    private $options;
24
25    private const CONSTRUCTOR_OPTIONS = [
26        'LanguageCode',
27        'TranslateTranslationDefaultService',
28        'TranslateTranslationServices',
29    ];
30
31    public function __construct(
32        ApiMain $mainModule,
33        string $moduleName,
34        TtmServerFactory $ttmServerFactory,
35        Config $config
36    ) {
37        parent::__construct( $mainModule, $moduleName );
38        $this->ttmServerFactory = $ttmServerFactory;
39        $this->options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
40    }
41
42    public function execute(): void {
43        if ( !$this->getAvailableTranslationServices() ) {
44            $this->dieWithError( 'apierror-translate-notranslationservices' );
45        }
46
47        $params = $this->extractRequestParams();
48
49        $server = $this->ttmServerFactory->create( $params[ 'service' ] );
50        if ( !$server instanceof ReadableTtmServer ) {
51            $this->dieWithError( 'apierror-translate-notranslationservices' );
52        }
53
54        $suggestions = $server->query(
55            $params['sourcelanguage'],
56            $params['targetlanguage'],
57            $params['text']
58        );
59
60        $result = $this->getResult();
61        foreach ( $suggestions as $sug ) {
62            $sug['location'] = $server->expandLocation( $sug );
63            unset( $sug['wiki'] );
64            $result->addValue( $this->getModuleName(), null, $sug );
65        }
66
67        $result->addIndexedTagName( $this->getModuleName(), 'suggestion' );
68    }
69
70    private function getAvailableTranslationServices(): array {
71        $translationServices = $this->options->get( 'TranslateTranslationServices' );
72
73        $good = [];
74        foreach ( $translationServices as $id => $config ) {
75            $public = $config['public'] ?? false;
76            if ( $config['type'] === 'ttmserver' && $public ) {
77                $good[] = $id;
78            }
79        }
80
81        return $good;
82    }
83
84    protected function getAllowedParams(): array {
85        $available = $this->getAvailableTranslationServices();
86
87        $ret = [
88            'service' => [
89                ParamValidator::PARAM_TYPE => $available,
90            ],
91            'sourcelanguage' => [
92                ParamValidator::PARAM_TYPE => 'string',
93                ParamValidator::PARAM_REQUIRED => true,
94            ],
95            'targetlanguage' => [
96                ParamValidator::PARAM_TYPE => 'string',
97                ParamValidator::PARAM_REQUIRED => true,
98            ],
99            'text' => [
100                ParamValidator::PARAM_TYPE => 'string',
101                ParamValidator::PARAM_REQUIRED => true,
102            ],
103        ];
104
105        if ( $available ) {
106            // Don't add this if no services are available, it makes
107            // ApiStructureTest unhappy
108            $ret['service'][ParamValidator::PARAM_DEFAULT] =
109                $this->options->get( 'TranslateTranslationDefaultService' );
110        }
111
112        return $ret;
113    }
114
115    protected function getExamplesMessages(): array {
116        return [
117            'action=ttmserver&sourcelanguage=en&targetlanguage=fi&text=Help'
118                => 'apihelp-ttmserver-example-1',
119        ];
120    }
121}