Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslationAidsActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;
5
6use MediaWiki\Api\ApiBase;
13use MediaWiki\Logger\LoggerFactory;
14use MediaWiki\Title\Title;
15use Wikimedia\ParamValidator\ParamValidator;
16
23class TranslationAidsActionApi extends ApiBase {
24 public function execute() {
25 $params = $this->extractRequestParams();
26
27 $title = Title::newFromText( $params['title'] );
28 if ( !$title ) {
29 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
30 }
31
32 $handle = new MessageHandle( $title );
33 if ( !$handle->isValid() ) {
34 $this->dieWithError( 'apierror-translate-nomessagefortitle', 'nomessagefortitle' );
35 }
36
37 if ( (string)$params['group'] !== '' ) {
38 $group = MessageGroups::getGroup( $params['group'] );
39 } else {
40 $group = $handle->getGroup();
41 }
42
43 if ( !$group ) {
44 $this->dieWithError( 'apierror-translate-invalidgroup', 'invalidgroup' );
45 }
46
47 $data = [];
48 $times = [];
49
50 $props = $params['prop'];
51 $aggregator = new QueryAggregator();
52
53 // Figure out the intersection of supported and requested aids
54 $types = TranslationAid::getTypes();
55 $props = array_intersect( $props, array_keys( $types ) );
56
57 $result = $this->getResult();
58
59 // Create list of aids, populate web services queries
61 $aids = [];
62
63 $dataProvider = new TranslationAidDataProvider( $handle );
64
65 // Message definition should not be empty, but sometimes is.
66 // See: https://phabricator.wikimedia.org/T285830
67 // Identify and log.
68 if ( !$dataProvider->hasDefinition() ) {
69 LoggerFactory::getInstance( LogNames::MAIN )->warning(
70 'Message definition is empty! Title: {title}, group: {group}, key: {key}',
71 [
72 'title' => $handle->getTitle()->getPrefixedText(),
73 'group' => $group->getId(),
74 'key' => $handle->getKey()
75 ]
76 );
77 }
78
79 foreach ( $props as $type ) {
80 // Do not proceed if translation aid is not supported for this message group
81 if ( !isset( $types[$type] ) ) {
82 $types[$type] = UnsupportedTranslationAid::class;
83 }
84
85 $class = $types[$type];
86 $obj = new $class( $group, $handle, $this, $dataProvider );
87
88 if ( $obj instanceof QueryAggregatorAware ) {
89 $obj->setQueryAggregator( $aggregator );
90 try {
91 $obj->populateQueries();
92 } catch ( TranslationHelperException $e ) {
93 $data[$type] = [ 'error' => $e->getMessage() ];
94 // Prevent processing this aids and thus overwriting our error
95 continue;
96 }
97 }
98
99 $aids[$type] = $obj;
100 }
101
102 // Execute all web service queries asynchronously to save time
103 $start = microtime( true );
104 $aggregator->run();
105 $times['query_aggregator'] = round( microtime( true ) - $start, 3 );
106
107 // Construct the result data structure
108 foreach ( $aids as $type => $obj ) {
109 $start = microtime( true );
110
111 try {
112 $aid = $obj->getData();
113 } catch ( TranslationHelperException $e ) {
114 $aid = [ 'error' => $e->getMessage() ];
115 }
116
117 if ( isset( $aid['**'] ) ) {
118 $result->setIndexedTagName( $aid, $aid['**'] );
119 unset( $aid['**'] );
120 }
121
122 $data[$type] = $aid;
123 $times[$type] = round( microtime( true ) - $start, 3 );
124 }
125
126 $result->addValue( null, 'helpers', $data );
127 $result->addValue( null, 'times', $times );
128 }
129
130 protected function getAllowedParams(): array {
131 $props = array_keys( TranslationAid::getTypes() );
132
133 return [
134 'title' => [
135 ParamValidator::PARAM_TYPE => 'string',
136 ParamValidator::PARAM_REQUIRED => true,
137 ],
138 'group' => [
139 ParamValidator::PARAM_TYPE => 'string',
140 ],
141 'prop' => [
142 ParamValidator::PARAM_DEFAULT => implode( '|', $props ),
143 ParamValidator::PARAM_TYPE => $props,
144 ParamValidator::PARAM_ISMULTI => true,
145 ],
146 ];
147 }
148
149 protected function getExamplesMessages() {
150 return [
151 'action=translationaids&title=MediaWiki:January/fi'
152 => 'apihelp-translationaids-example-1',
153 ];
154 }
155}
Constants for log channel names used in this extension.
Definition LogNames.php:13
const MAIN
Default log channel for the extension.
Definition LogNames.php:15
Factory class for accessing message groups individually by id or all of them as a list.
Class for pointing to messages, like Title class is for titles.
static getTypes()
List of available message types mapped to the classes implementing them.
Translation helpers can throw this exception when they cannot do anything useful with the current mes...
Interface for classes that want to use QueryAggregator.