1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
<?php
/**
* Api module for querying message aids.
*
* @file
* @author Niklas Laxström
* @license GPL-2.0-or-later
*/
/**
* Api module for querying message aids.
*
* @ingroup API TranslateAPI
*/
class ApiTranslationAids extends ApiBase {
public function execute() {
$params = $this->extractRequestParams();
$title = Title::newFromText( $params['title'] );
if ( !$title ) {
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
}
$handle = new MessageHandle( $title );
if ( !$handle->isValid() ) {
$this->dieWithError( 'apierror-translate-nomessagefortitle', 'nomessagefortitle' );
}
if ( (string)$params['group'] !== '' ) {
$group = MessageGroups::getGroup( $params['group'] );
} else {
$group = $handle->getGroup();
}
if ( !$group ) {
$this->dieWithError( 'apierror-translate-invalidgroup', 'invalidgroup' );
}
$data = [];
$times = [];
$props = $params['prop'];
$aggregator = new QueryAggregator();
// Figure out the intersection of supported and requested aids
$types = $group->getTranslationAids();
$props = array_intersect( $props, array_keys( $types ) );
$result = $this->getResult();
// Create list of aids, populate web services queries
/** @var TranslationAid[] $aids */
$aids = [];
$dataProvider = new TranslationAidDataProvider( $handle );
foreach ( $props as $type ) {
// Do not proceed if translation aid is not supported for this message group
if ( !isset( $types[$type] ) ) {
$types[$type] = 'UnsupportedTranslationAid';
}
$class = $types[$type];
$obj = new $class( $group, $handle, $this, $dataProvider );
if ( $obj instanceof QueryAggregatorAware ) {
$obj->setQueryAggregator( $aggregator );
try {
$obj->populateQueries();
} catch ( TranslationHelperException $e ) {
$data[$type] = [ 'error' => $e->getMessage() ];
// Prevent processing this aids and thus overwriting our error
continue;
}
}
$aids[$type] = $obj;
}
// Execute all web service queries asynchronously to save time
$start = microtime( true );
$aggregator->run();
$times['query_aggregator'] = round( microtime( true ) - $start, 3 );
// Construct the result data structure
foreach ( $aids as $type => $obj ) {
$start = microtime( true );
try {
$aid = $obj->getData();
} catch ( TranslationHelperException $e ) {
$aid = [ 'error' => $e->getMessage() ];
}
if ( isset( $aid['**'] ) ) {
$result->setIndexedTagName( $aid, $aid['**'] );
unset( $aid['**'] );
}
$data[$type] = $aid;
$times[$type] = round( microtime( true ) - $start, 3 );
}
$result->addValue( null, 'helpers', $data );
$result->addValue( null, 'times', $times );
}
protected function getAllowedParams() {
$props = array_keys( TranslationAid::getTypes() );
Hooks::run( 'TranslateTranslationAids', [ &$props ] );
return [
'title' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'group' => [
ApiBase::PARAM_TYPE => 'string',
],
'prop' => [
ApiBase::PARAM_DFLT => implode( '|', $props ),
ApiBase::PARAM_TYPE => $props,
ApiBase::PARAM_ISMULTI => true,
],
];
}
protected function getExamplesMessages() {
return [
'action=translationaids&title=MediaWiki:January/fi'
=> 'apihelp-translationaids-example-1',
];
}
}
|