Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdatedDefinitionAid
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 1
 getData
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;
5
6use DifferenceEngine;
7use MediaWiki\Extension\Translate\MessageGroupProcessing\RevTagStore;
8use MediaWiki\Extension\Translate\TranslatorInterface\TranslationHelperException;
9use MediaWiki\Extension\Translate\Utilities\Utilities;
10use MediaWiki\MediaWikiServices;
11use MediaWiki\Revision\SlotRecord;
12use MediaWiki\Title\Title;
13use Wikimedia\Rdbms\SelectQueryBuilder;
14use WikitextContent;
15
16/**
17 * Translation aid that provides the message definition.
18 * This usually matches the content of the page ns:key/source_language.
19 * @author Niklas Laxström
20 * @license GPL-2.0-or-later
21 * @since 2013-01-01
22 * @ingroup TranslationAids
23 */
24class UpdatedDefinitionAid extends TranslationAid {
25    public function getData(): array {
26        $db = Utilities::getSafeReadDB();
27
28        $translationRevision = $db->newSelectQueryBuilder()
29            ->select( 'rt_value' )
30            ->from( 'revtag' )
31            ->where( [
32                'rt_page' => $this->handle->getTitle()->getArticleID(),
33                'rt_type' => RevTagStore::TRANSVER_PROP,
34            ] )
35            ->orderBy( 'rt_revision', SelectQueryBuilder::SORT_DESC )
36            ->caller( __METHOD__ )
37            ->fetchField();
38        if ( $translationRevision === false ) {
39            throw new TranslationHelperException( 'No definition revision recorded' );
40        }
41
42        $sourceLanguage = $this->group->getSourceLanguage();
43        $definitionTitle = Title::makeTitleSafe(
44            $this->handle->getTitle()->getNamespace(),
45            $this->handle->getKey() . '/' . $sourceLanguage
46        );
47
48        if ( !$definitionTitle || !$definitionTitle->exists() ) {
49            throw new TranslationHelperException( 'Definition page does not exist' );
50        }
51
52        // Using getRevisionById instead of byTitle, because the page might have been renamed
53        $mwInstance = MediaWikiServices::getInstance();
54        $revisionLookup = $mwInstance->getRevisionLookup();
55        $oldRevRecord = $revisionLookup->getRevisionById( $translationRevision );
56        if ( !$oldRevRecord ) {
57            throw new TranslationHelperException( 'Old definition version does not exist anymore' );
58        }
59
60        // Escaping legacy issue (T330453)
61        if ( $oldRevRecord->getPageId() !== $definitionTitle->getArticleID() ) {
62            throw new TranslationHelperException(
63                'Translation unit definition id does not match old revision definition id'
64            );
65        }
66
67        $oldContent = $oldRevRecord->getContent( SlotRecord::MAIN );
68        $newContent = $this->dataProvider->getDefinitionContent();
69
70        if ( !$oldContent ) {
71            throw new TranslationHelperException( 'Old definition version does not exist anymore' );
72        }
73
74        if ( !$oldContent instanceof WikitextContent || !$newContent instanceof WikitextContent ) {
75            throw new TranslationHelperException( 'Can only work on Wikitext content' );
76        }
77
78        if ( $oldContent->equals( $newContent ) ) {
79            throw new TranslationHelperException( 'No changes' );
80        }
81
82        $diff = new DifferenceEngine( $this->context );
83        $diff->setTextLanguage(
84            $mwInstance->getLanguageFactory()->getLanguage( $sourceLanguage )
85        );
86        $diff->setContent( $oldContent, $newContent );
87        $diff->setReducedLineNumbers();
88        $diff->showDiffStyle();
89
90        $html = $diff->getDiff(
91            $this->context->msg( 'tpt-diff-old' )->escaped(),
92            $this->context->msg( 'tpt-diff-new' )->escaped()
93        );
94
95        return [
96            'value_old' => $oldContent->getText(),
97            'value_new' => $newContent->getText(),
98            'revisionid_old' => $oldRevRecord->getId(),
99            'revisionid_new' => $definitionTitle->getLatestRevID(),
100            'language' => $this->group->getSourceLanguage(),
101            'html' => $html,
102        ];
103    }
104}