Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
QueryMessageTranslationsActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageLoading;
5
6use ApiBase;
7use ApiQuery;
8use ApiQueryBase;
9use ApiResult;
11use MediaWiki\Title\Title;
12use Wikimedia\ParamValidator\ParamValidator;
13
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}
Class for pointing to messages, like Title class is for titles.
static hasFuzzyString(string $text)
Check if a string contains the fuzzy string.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31