summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/utils/TranslationEditPage.php
blob: eb7a9aacc483f6deff7c9ac3c450a22d8d3f31bc (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
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
<?php
/**
 * Contains classes that imeplement the server side component of AJAX
 * translation page.
 *
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0+
 */

/**
 * This class together with some JavaScript implements the AJAX translation
 * page.
 */
class TranslationEditPage {
	// Instance of an Title object
	protected $title;
	protected $suggestions = 'sync';

	/**
	 * Constructor.
	 * @param $title  Title  A title object
	 */
	public function __construct( Title $title ) {
		$this->setTitle( $title );
	}

	/**
	 * Constructs a page from WebRequest.
	 * This interface is a big klunky.
	 * @param $request WebRequest
	 * @return TranslationEditPage
	 */
	public static function newFromRequest( WebRequest $request ) {
		$title = Title::newFromText( $request->getText( 'page' ) );

		if ( !$title ) {
			return null;
		}

		$obj = new self( $title );
		$obj->suggestions = $request->getText( 'suggestions' );

		return $obj;
	}

	/**
	 * Change the title of the page we are working on.
	 * @param $title Title
	 */
	public function setTitle( Title $title ) {
		$this->title = $title;
	}

	/**
	 * Get the title of the page we are working on.
	 * @return Title
	 */
	public function getTitle() {
		return $this->title;
	}

	/**
	 * Generates the html snippet for ajax edit. Echoes it to the output and
	 * disabled all other output.
	 */
	public function execute() {
		global $wgServer, $wgScriptPath;

		$context = RequestContext::getMain();

		$context->getOutput()->disable();

		$data = $this->getEditInfo();
		$helpers = new TranslationHelpers( $this->getTitle(), '' );

		$id = "tm-target-{$helpers->dialogID()}";
		$helpers->setTextareaId( $id );

		if ( $this->suggestions === 'checks' ) {
			echo $helpers->getBoxes( $this->suggestions );

			return;
		}

		$handle = new MessageHandle( $this->getTitle() );
		$groupId = MessageIndex::getPrimaryGroupId( $handle );

		$translation = '';
		if ( $groupId ) {
			$translation = $helpers->getTranslation();
		}

		$targetLang = Language::factory( $helpers->getTargetLanguage() );
		$textareaParams = array(
			'name' => 'text',
			'class' => 'mw-translate-edit-area',
			'id' => $id,
			/* Target language might differ from interface language. Set
			 * a suitable default direction */
			'lang' => $targetLang->getHtmlCode(),
			'dir' => $targetLang->getDir(),
		);

		if ( !$groupId || !$context->getUser()->isAllowed( 'translate' ) ) {
			$textareaParams['readonly'] = 'readonly';
		}

		$extraInputs = '';
		Hooks::run( 'TranslateGetExtraInputs', array( &$translation, &$extraInputs ) );

		$textarea = Html::element( 'textarea', $textareaParams, $translation );

		$hidden = array();
		$hidden[] = Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() );

		if ( isset( $data['revisions'][0]['timestamp'] ) ) {
			$hidden[] = Html::hidden( 'basetimestamp', $data['revisions'][0]['timestamp'] );
		}

		$hidden[] = Html::hidden( 'starttimestamp', $data['starttimestamp'] );
		if ( isset( $data['edittoken'] ) ) {
			$hidden[] = Html::hidden( 'token', $data['edittoken'] );
		}
		$hidden[] = Html::hidden( 'format', 'json' );
		$hidden[] = Html::hidden( 'action', 'edit' );

		$summary = Xml::inputLabel(
			$context->msg( 'translate-js-summary' )->text(),
			'summary',
			'summary',
			40
		);
		$save = Xml::submitButton(
			$context->msg( 'translate-js-save' )->text(),
			array( 'class' => 'mw-translate-save' )
		);
		$saveAndNext = Xml::submitButton(
			$context->msg( 'translate-js-next' )->text(),
			array( 'class' => 'mw-translate-next' )
		);
		$skip = Html::element( 'input', array(
			'class' => 'mw-translate-skip',
			'type' => 'button',
			'value' => $context->msg( 'translate-js-skip' )->text()
		) );

		if ( $this->getTitle()->exists() ) {
			$history = Html::element(
				'input',
				array(
					'class' => 'mw-translate-history',
					'type' => 'button',
					'value' => $context->msg( 'translate-js-history' )->text()
				)
			);
		} else {
			$history = '';
		}

		$support = $this->getSupportButton( $this->getTitle() );

		if ( $context->getUser()->isAllowed( 'translate' ) ) {
			$bottom = "$summary$save$saveAndNext$skip$history$support";
		} else {
			$text = $context->msg( 'translate-edit-nopermission' )->escaped();
			$button = $this->getPermissionPageButton();
			$bottom = "$text $button$skip$history$support";
		}

		// Use the api to submit edits
		$formParams = array(
			'action' => "{$wgServer}{$wgScriptPath}/api.php",
			'method' => 'post',
		);

		$form = Html::rawElement( 'form', $formParams,
			implode( "\n", $hidden ) . "\n" .
				$helpers->getBoxes( $this->suggestions ) . "\n" .
				Html::rawElement(
					'div',
					array( 'class' => 'mw-translate-inputs' ),
					"$textarea\n$extraInputs"
				) . "\n" .
				Html::rawElement( 'div', array( 'class' => 'mw-translate-bottom' ), $bottom )
		);

		echo Html::rawElement( 'div', array( 'class' => 'mw-ajax-dialog' ), $form );
	}

	/**
	 * Gets the edit token and timestamps in some ugly array structure. Needs to
	 * be cleaned up.
	 * @throws MWException
	 * @return \array
	 */
	protected function getEditInfo() {
		$params = new FauxRequest( array(
			'action' => 'query',
			'prop' => 'info|revisions',
			'intoken' => 'edit',
			'titles' => $this->getTitle(),
			'rvprop' => 'timestamp',
		) );

		$api = new ApiMain( $params );
		$api->execute();

		$data = $api->getResult()->getResultData();

		if ( !isset( $data['query']['pages'] ) ) {
			throw new MWException( 'Api query failed' );
		}
		$data = $data['query']['pages'];
		if ( defined( 'ApiResult::META_CONTENT' ) ) {
			$data = ApiResult::stripMetadataNonRecursive( $data );
		}
		$data = array_shift( $data );

		return $data;
	}

	/**
	 * Returns link attributes that enable javascript translation dialog.
	 * Will degrade gracefully if user does not have permissions or JavaScript
	 * is not enabled.
	 * @param $title Title Title object for the translatable message.
	 * @param $group \string The group in which this message belongs to.
	 *   Optional, but avoids a lookup later if provided.
	 * @param $type \string Force the type of editor to be used. Use dialog
	 *   where embedded editor is no applicable.
	 * @return \array
	 */
	public static function jsEdit( Title $title, $group = '', $type = 'default' ) {
		$context = RequestContext::getMain();

		if ( $type === 'default' ) {
			$text = 'tqe-anchor-' . substr( sha1( $title->getPrefixedText() ), 0, 12 );
			$onclick = "jQuery( '#$text' ).dblclick(); return false;";
		} else {
			$onclick = Xml::encodeJsCall(
				'return mw.translate.openDialog', array( $title->getPrefixedDBkey(), $group )
			);
		}

		return array(
			'onclick' => $onclick,
			'title' => $context->msg( 'translate-edit-title', $title->getPrefixedText() )->text()
		);
	}

	protected function getSupportButton( $title ) {
		try {
			$supportUrl = SupportAid::getSupportUrl( $title );
		} catch ( TranslationHelperException $e ) {
			return '';
		}

		$support = Html::element(
			'input',
			array(
				'class' => 'mw-translate-support',
				'type' => 'button',
				'value' => wfMessage( 'translate-js-support' )->text(),
				'title' => wfMessage( 'translate-js-support-title' )->text(),
				'data-load-url' => $supportUrl,
			)
		);

		return $support;
	}

	protected function getPermissionPageButton() {
		global $wgTranslatePermissionUrl;
		if ( !$wgTranslatePermissionUrl ) {
			return '';
		}

		$title = Title::newFromText( $wgTranslatePermissionUrl );
		if ( !$title ) {
			return '';
		}

		$button = Html::element(
			'input',
			array(
				'class' => 'mw-translate-askpermission',
				'type' => 'button',
				'value' => wfMessage( 'translate-edit-askpermission' )->text(),
				'data-load-url' => $title->getLocalURL(),
			)
		);

		return $button;
	}
}