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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
<?php
/**
* Contains logic for special page Special:TranslationStats.
*
* @file
* @author Niklas Laxström
* @author Siebrand Mazeland
* @license GPL-2.0-or-later
*/
use MediaWiki\Extension\Translate\Services;
use MediaWiki\Extension\Translate\Statistics\TranslationStatsGraphOptions;
/**
* @defgroup Stats Statistics
* Collection of code to produce various kinds of statistics.
*/
/**
* Includable special page for generating graphs on translations.
*
* @ingroup SpecialPage TranslateSpecialPage Stats
*/
class SpecialTranslationStats extends SpecialPage {
/** @var \MediaWiki\Extension\Translate\Statistics\TranslationStatsDataProvider */
private $dataProvider;
private const GRAPH_CONTAINER_ID = 'translationStatsGraphContainer';
private const GRAPH_CONTAINER_CLASS = 'mw-translate-translationstats-container';
public function __construct() {
parent::__construct( 'TranslationStats' );
$this->dataProvider = Services::getInstance()->getTranslationStatsDataProvider();
}
public function isIncludable() {
return true;
}
protected function getGroupName() {
return 'translation';
}
public function execute( $par ) {
$graphOpts = new TranslationStatsGraphOptions();
$graphOpts->bindArray( $this->getRequest()->getValues() );
$pars = explode( ';', $par );
foreach ( $pars as $item ) {
if ( strpos( $item, '=' ) === false ) {
continue;
}
list( $key, $value ) = array_map( 'trim', explode( '=', $item, 2 ) );
if ( $graphOpts->hasValue( $key ) ) {
$graphOpts->setValue( $key, $value );
}
}
$graphOpts->normalize( $this->dataProvider->getGraphTypes() );
$opts = $graphOpts->getFormOptions();
if ( $this->including() ) {
$this->getOutput()->addHTML( $this->embed( $opts ) );
} else {
$this->form( $opts );
}
}
/**
* Constructs the form which can be used to generate custom graphs.
* @param FormOptions $opts
* @suppress SecurityCheck-DoubleEscaped Intentionally outputting what user should type
*/
protected function form( FormOptions $opts ) {
global $wgScript;
$this->setHeaders();
$out = $this->getOutput();
$out->addModules( 'ext.translate.special.translationstats' );
$out->addHelpLink( 'Help:Extension:Translate/Statistics_and_reporting' );
$out->addWikiMsg( 'translate-statsf-intro' );
$out->addHTML(
Xml::fieldset( $this->msg( 'translate-statsf-options' )->text() ) .
Html::openElement( 'form', [ 'action' => $wgScript, 'id' => 'translationStatsConfig' ] ) .
Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
Html::hidden( 'preview', 1 ) .
'<table>'
);
$submit = Xml::submitButton( $this->msg( 'translate-statsf-submit' )->text() );
$out->addHTML(
$this->eInput( 'width', $opts ) .
$this->eInput( 'height', $opts ) .
'<tr><td colspan="2"><hr /></td></tr>' .
$this->eInput( 'start', $opts, 24 ) .
$this->eInput( 'days', $opts ) .
$this->eRadio( 'scale', $opts, [ 'months', 'weeks', 'days', 'hours' ] ) .
$this->eRadio( 'count', $opts, $this->dataProvider->getGraphTypes() ) .
'<tr><td colspan="2"><hr /></td></tr>' .
$this->eLanguage( 'language', $opts ) .
$this->eGroup( 'group', $opts ) .
'<tr><td colspan="2"><hr /></td></tr>' .
'<tr><td colspan="2">' . $submit . '</td></tr>'
);
$out->addHTML(
'</table>' .
'</form>' .
'</fieldset>'
);
if ( !$opts['preview'] ) {
return;
}
$spiParams = '';
foreach ( $opts->getChangedValues() as $key => $v ) {
if ( $key === 'preview' ) {
continue;
}
if ( $spiParams !== '' ) {
$spiParams .= ';';
}
if ( is_array( $v ) ) {
$v = implode( ',', $v );
if ( !strlen( $v ) ) {
continue;
}
}
$spiParams .= wfEscapeWikiText( "$key=$v" );
}
if ( $spiParams !== '' ) {
$spiParams = '/' . $spiParams;
}
$titleText = $this->getPageTitle()->getPrefixedText();
$out->addHTML(
Html::element( 'hr' )
);
// Element to render the graph
$out->addHTML(
Html::rawElement(
'div',
[
'id' => self::GRAPH_CONTAINER_ID ,
'style' => 'margin: 2em auto; display: block',
'class' => self::GRAPH_CONTAINER_CLASS
]
)
);
$out->addHTML(
Html::element(
'pre',
[ 'aria-label' => $this->msg( 'translate-statsf-embed' )->text() ],
"{{{$titleText}{$spiParams}}}"
)
);
}
/**
* Constructs a table row with label and input in two columns.
* @param string $name Option name.
* @param FormOptions $opts
* @param int $width
* @return string Html.
*/
protected function eInput( $name, FormOptions $opts, $width = 4 ) {
$value = $opts[$name];
return '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
Xml::input( $name, $width, $value, [ 'id' => $name ] ) .
'</td></tr>' . "\n";
}
/**
* Constructs a label for option.
* @param string $name Option name.
* @return string Html.
*/
protected function eLabel( $name ) {
// Give grep a chance to find the usages:
// translate-statsf-width, translate-statsf-height, translate-statsf-start,
// translate-statsf-days, translate-statsf-scale, translate-statsf-count,
// translate-statsf-language, translate-statsf-group
$label = 'translate-statsf-' . $name;
$label = $this->msg( $label )->escaped();
return Xml::tags( 'label', [ 'for' => $name ], $label );
}
/**
* Constructs a table row with label and radio input in two columns.
* @param string $name Option name.
* @param FormOptions $opts
* @param string[] $alts List of alternatives.
* @return string Html.
*/
protected function eRadio( $name, FormOptions $opts, array $alts ) {
// Give grep a chance to find the usages:
// translate-statsf-scale, translate-statsf-count
$label = 'translate-statsf-' . $name;
$label = $this->msg( $label )->escaped();
$s = '<tr><td>' . $label . '</td><td>';
$options = [];
foreach ( $alts as $alt ) {
$id = "$name-$alt";
$radio = Xml::radio( $name, $alt, $alt === $opts[$name],
[ 'id' => $id ] ) . ' ';
$options[] = $radio . ' ' . $this->eLabel( $id );
}
$s .= implode( ' ', $options );
$s .= '</td></tr>' . "\n";
return $s;
}
/**
* Constructs a table row with label and language selector in two columns.
* @param string $name Option name.
* @param FormOptions $opts
* @return string Html.
*/
protected function eLanguage( $name, FormOptions $opts ) {
$value = implode( ',', $opts[$name] );
$select = $this->languageSelector();
$select->setTargetId( 'language' );
return '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
$select->getHtmlAndPrepareJS() . '<br />' .
Xml::input( $name, 20, $value, [ 'id' => $name ] ) .
'</td></tr>' . "\n";
}
/**
* Constructs a JavaScript enhanced language selector.
* @return JsSelectToInput
*/
protected function languageSelector() {
$languages = TranslateUtils::getLanguageNames( $this->getLanguage()->getCode() );
ksort( $languages );
$selector = new XmlSelect( 'mw-language-selector', 'mw-language-selector' );
foreach ( $languages as $code => $name ) {
$selector->addOption( "$code - $name", $code );
}
$jsSelect = new JsSelectToInput( $selector );
return $jsSelect;
}
/**
* Constructs a table row with label and group selector in two columns.
* @param string $name Option name.
* @param FormOptions $opts
* @return string Html.
*/
protected function eGroup( $name, FormOptions $opts ) {
$value = implode( ',', $opts[$name] );
$select = $this->groupSelector();
$select->setTargetId( 'group' );
return '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
$select->getHtmlAndPrepareJS() . '<br />' .
Xml::input( $name, 20, $value, [ 'id' => $name ] ) .
'</td></tr>' . "\n";
}
/**
* Constructs a JavaScript enhanced group selector.
* @return JsSelectToInput
*/
protected function groupSelector() {
$groups = MessageGroups::singleton()->getGroups();
/** @var MessageGroup $group */
foreach ( $groups as $key => $group ) {
if ( !$group->exists() ) {
unset( $groups[$key] );
continue;
}
}
ksort( $groups );
$selector = new XmlSelect( 'mw-group-selector', 'mw-group-selector' );
/** @var MessageGroup $name */
foreach ( $groups as $code => $name ) {
$selector->addOption( $name->getLabel(), $code );
}
$jsSelect = new JsSelectToInput( $selector );
return $jsSelect;
}
protected function embed( FormOptions $opts ) {
$this->getOutput()->addModules( 'ext.translate.translationstats.embedded' );
return Html::rawElement(
'div',
[
'class' => self::GRAPH_CONTAINER_CLASS
],
Html::hidden(
'translationStatsGraphOptions',
json_encode( $opts->getAllValues() )
)
);
}
}
|