summaryrefslogtreecommitdiff
blob: 5b537c9d9b0777b619527c1e710ad1e06e1466d6 (plain)
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
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\Statistics;

use FormOptions;
use Html;
use JsSelectToInput;
use MessageGroup;
use MessageGroups;
use SpecialPage;
use TranslateUtils;
use Xml;
use XmlSelect;
use function wfEscapeWikiText;

/**
 * Includable special page for generating graphs for statistics.
 *
 * @file
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @license GPL-2.0-or-later
 */
class TranslationStatsSpecialPage extends SpecialPage {
	/** @var TranslationStatsDataProvider */
	private $dataProvider;
	private const GRAPH_CONTAINER_ID = 'translationStatsGraphContainer';
	private const GRAPH_CONTAINER_CLASS = 'mw-translate-translationstats-container';

	public function __construct( TranslationStatsDataProvider $dataProvider ) {
		parent::__construct( 'TranslationStats' );
		$this->dataProvider = $dataProvider;
	}

	/** @inheritDoc */
	public function isIncludable(): bool {
		return true;
	}

	/** @inheritDoc */
	protected function getGroupName(): string {
		return 'translation';
	}

	/** @inheritDoc */
	public function execute( $par ): void {
		$graphOpts = new TranslationStatsGraphOptions();
		$graphOpts->bindArray( $this->getRequest()->getValues() );

		$pars = explode( ';', (string)$par );
		foreach ( $pars as $item ) {
			if ( strpos( $item, '=' ) === false ) {
				continue;
			}

			[ $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.
	 *
	 * @suppress SecurityCheck-DoubleEscaped Intentionally outputting what user should type
	 */
	private function form( FormOptions $opts ): void {
		$script = $this->getConfig()->get( 'Script' );

		$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' => $script, '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, [ 'years', '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 ( is_array( $v ) ) {
				$v = implode( ',', $v );
				if ( !strlen( $v ) ) {
					continue;
				}
			}
			$spiParams[] = $key . '=' . wfEscapeWikiText( $v );
		}
		if ( $spiParams ) {
			$spiParams = '/' . implode( ';', $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}}}"
			)
		);
	}

	/// Construct HTML for a table row with label and input in two columns.
	private function eInput( string $name, FormOptions $opts, int $width = 4 ): string {
		$value = $opts[$name];
		return '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
			Xml::input( $name, $width, $value, [ 'id' => $name ] ) . '</td></tr>' . "\n";
	}

	/// Construct HTML for a label for option.
	private function eLabel( string $name ): string {
		// 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 );
	}

	/// Construct HTML for a table row with label and radio input in two columns.
	private function eRadio( string $name, FormOptions $opts, array $alts ): string {
		// 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;
	}

	/// Construct HTML for a table row with label and language selector in two columns.
	private function eLanguage( string $name, FormOptions $opts ): string {
		$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";
	}

	/// Construct a JavaScript enhanced language selector.
	private function languageSelector(): JsSelectToInput {
		$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 );
		}
		return new JsSelectToInput( $selector );
	}

	/// Constructs HTML for a table row with label and group selector in two columns.
	private function eGroup( string $name, FormOptions $opts ): string {
		$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";
	}

	/// Construct a JavaScript enhanced group selector.
	private function groupSelector(): JsSelectToInput {
		$groups = MessageGroups::singleton()->getGroups();
		/** @var MessageGroup $group */
		foreach ( $groups as $key => $group ) {
			if ( !$group->exists() ) {
				unset( $groups[$key] );
			}
		}
		ksort( $groups );
		$selector = new XmlSelect( 'mw-group-selector', 'mw-group-selector' );
		/** @var MessageGroup $name */
		foreach ( $groups as $code => $name ) {
			$selector->addOption( $name->getLabel(), $code );
		}
		return new JsSelectToInput( $selector );
	}

	private function embed( FormOptions $opts ): string {
		$this->getOutput()->addModules( 'ext.translate.translationstats.embedded' );
		return Html::rawElement(
			'div',
			[
				'class' => self::GRAPH_CONTAINER_CLASS,
			],
			Html::hidden(
				'translationStatsGraphOptions',
				json_encode( $opts->getAllValues() )
			)
		);
	}
}