summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/translationaids/UpdatedDefinitionAid.php
blob: 42ece6b6fa7795c27c83ef9fa5f7ea4819a95618 (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
<?php
/**
 * Translation aid provider.
 *
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2012-2013, Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Translation aid which gives the message definition.
 * This usually matches the content of the page ns:key/source_language.
 *
 * @ingroup TranslationAids
 * @since 2013-01-01
 */
class UpdatedDefinitionAid extends TranslationAid {
	public function getData() {
		$db = TranslateUtils::getSafeReadDB();
		$conds = [
			'rt_page' => $this->handle->getTitle()->getArticleID(),
			'rt_type' => RevTag::getType( 'tp:transver' ),
		];
		$options = [
			'ORDER BY' => 'rt_revision DESC',
		];

		$translationRevision = $db->selectField( 'revtag', 'rt_value', $conds, __METHOD__, $options );
		if ( $translationRevision === false ) {
			throw new TranslationHelperException( 'No definition revision recorded' );
		}

		$definitionTitle = Title::makeTitleSafe(
			$this->handle->getTitle()->getNamespace(),
			$this->handle->getKey() . '/' . $this->group->getSourceLanguage()
		);

		if ( !$definitionTitle || !$definitionTitle->exists() ) {
			throw new TranslationHelperException( 'Definition page does not exist' );
		}

		// Using newFromId instead of newFromTitle, because the page might have been renamed
		$oldrev = Revision::newFromId( $translationRevision );
		if ( !$oldrev ) {
			throw new TranslationHelperException( 'Old definition version does not exist anymore' );
		}

		$oldContent = $oldrev->getContent();
		$newContent = $this->dataProvider->getDefinitionContent();

		if ( !$oldContent ) {
			throw new TranslationHelperException( 'Old definition version does not exist anymore' );
		}

		if ( !$oldContent instanceof WikitextContent || !$newContent instanceof WikitextContent ) {
			throw new TranslationHelperException( 'Can only work on Wikitext content' );
		}

		if ( $oldContent->equals( $newContent ) ) {
			throw new TranslationHelperException( 'No changes' );
		}

		$diff = new DifferenceEngine( $this->context );
		$diff->setTextLanguage( wfGetLangObj( $this->group->getSourceLanguage() ) );
		$diff->setContent( $oldContent, $newContent );
		$diff->setReducedLineNumbers();
		$diff->showDiffStyle();

		$html = $diff->getDiff(
			$this->context->msg( 'tpt-diff-old' )->escaped(),
			$this->context->msg( 'tpt-diff-new' )->escaped()
		);

		return [
			'value_old' => $oldContent->getNativeData(),
			'value_new' => $newContent->getNativeData(),
			'revisionid_old' => $oldrev->getId(),
			'revisionid_new' => $definitionTitle->getLatestRevID(),
			'language' => $this->group->getSourceLanguage(),
			'html' => $html,
		];
	}
}