Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryMessageTranslationsActionApi
0.00% covered (danger)
0.00%
0 / 48
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheMode
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 / 31
0.00% covered (danger)
0.00%
0 / 1
72
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 11
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
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageLoading;
5
6use MediaWiki\Api\ApiBase;
7use MediaWiki\Api\ApiQuery;
8use MediaWiki\Api\ApiQueryBase;
9use MediaWiki\Api\ApiResult;
10use MediaWiki\Extension\Translate\Utilities\Utilities;
11use MediaWiki\Title\Title;
12use Wikimedia\ParamValidator\ParamValidator;
13
14/**
15 * Api module for querying message translations.
16 * @author Niklas Laxström
17 * @license GPL-2.0-or-later
18 * @ingroup API TranslateAPI
19 */
20class QueryMessageTranslationsActionApi extends ApiQueryBase {
21    public function __construct( ApiQuery $query, string $moduleName ) {
22        parent::__construct( $query, $moduleName, 'mt' );
23    }
24
25    public function getCacheMode( $params ) {
26        return 'public';
27    }
28
29    public function execute(): void {
30        $params = $this->extractRequestParams();
31
32        $title = Title::newFromText( $params['title'] );
33        if ( !$title ) {
34            $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
35        }
36
37        $handle = new MessageHandle( $title );
38        if ( !$handle->isValid() ) {
39            $this->dieWithError( 'apierror-translate-nomessagefortitle', 'nomessagefortitle' );
40        }
41
42        $namespace = $title->getNamespace();
43        $pageInfo = Utilities::getTranslations( $handle );
44
45        $result = $this->getResult();
46        $count = 0;
47
48        foreach ( $pageInfo as $key => $info ) {
49            if ( ++$count <= $params['offset'] ) {
50                continue;
51            }
52
53            $tTitle = Title::makeTitle( $namespace, $key );
54            $tHandle = new MessageHandle( $tTitle );
55
56            $data = [
57                'title' => $tTitle->getPrefixedText(),
58                'language' => $tHandle->getCode(),
59                'lasttranslator' => $info[1],
60            ];
61
62            $fuzzy = MessageHandle::hasFuzzyString( $info[0] ) || $tHandle->isFuzzy();
63
64            if ( $fuzzy ) {
65                $data['fuzzy'] = 'fuzzy';
66            }
67
68            $translation = str_replace( TRANSLATE_FUZZY, '', $info[0] );
69            ApiResult::setContentValue( $data, 'translation', $translation );
70
71            $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
72            if ( !$fit ) {
73                $this->setContinueEnumParameter( 'offset', $count );
74                break;
75            }
76        }
77
78        $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
79    }
80
81    protected function getAllowedParams(): array {
82        return [
83            'title' => [
84                ParamValidator::PARAM_TYPE => 'string',
85                ParamValidator::PARAM_REQUIRED => true,
86            ],
87            'offset' => [
88                ParamValidator::PARAM_DEFAULT => 0,
89                ParamValidator::PARAM_TYPE => 'integer',
90                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
91            ],
92        ];
93    }
94
95    protected function getExamplesMessages(): array {
96        return [
97            'action=query&meta=messagetranslations&mttitle=MediaWiki:January'
98                => 'apihelp-query+messagetranslations-example-1',
99        ];
100    }
101}