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;
12use Title;
13use Wikimedia\ParamValidator\ParamValidator;
14
21class QueryMessageTranslationsActionApi extends ApiQueryBase {
22 public function __construct( ApiQuery $query, string $moduleName ) {
23 parent::__construct( $query, $moduleName, 'mt' );
24 }
25
26 public function getCacheMode( $params ) {
27 return 'public';
28 }
29
30 public function execute(): void {
31 $params = $this->extractRequestParams();
32
33 $title = Title::newFromText( $params['title'] );
34 if ( !$title ) {
35 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
36 }
37
38 $handle = new MessageHandle( $title );
39 if ( !$handle->isValid() ) {
40 $this->dieWithError( 'apierror-translate-nomessagefortitle', 'nomessagefortitle' );
41 }
42
43 $namespace = $title->getNamespace();
44 $pageInfo = Utilities::getTranslations( $handle );
45
46 $result = $this->getResult();
47 $count = 0;
48
49 foreach ( $pageInfo as $key => $info ) {
50 if ( ++$count <= $params['offset'] ) {
51 continue;
52 }
53
54 $tTitle = Title::makeTitle( $namespace, $key );
55 $tHandle = new MessageHandle( $tTitle );
56
57 $data = [
58 'title' => $tTitle->getPrefixedText(),
59 'language' => $tHandle->getCode(),
60 'lasttranslator' => $info[1],
61 ];
62
63 $fuzzy = MessageHandle::hasFuzzyString( $info[0] ) || $tHandle->isFuzzy();
64
65 if ( $fuzzy ) {
66 $data['fuzzy'] = 'fuzzy';
67 }
68
69 $translation = str_replace( TRANSLATE_FUZZY, '', $info[0] );
70 ApiResult::setContentValue( $data, 'translation', $translation );
71
72 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
73 if ( !$fit ) {
74 $this->setContinueEnumParameter( 'offset', $count );
75 break;
76 }
77 }
78
79 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
80 }
81
82 protected function getAllowedParams(): array {
83 return [
84 'title' => [
85 ParamValidator::PARAM_TYPE => 'string',
86 ParamValidator::PARAM_REQUIRED => true,
87 ],
88 'offset' => [
89 ParamValidator::PARAM_DEFAULT => 0,
90 ParamValidator::PARAM_TYPE => 'integer',
91 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
92 ],
93 ];
94 }
95
96 protected function getExamplesMessages(): array {
97 return [
98 'action=query&meta=messagetranslations&mttitle=MediaWiki:January'
99 => 'apihelp-query+messagetranslations-example-1',
100 ];
101 }
102}
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31
Class for pointing to messages, like Title class is for titles.