Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
UpdatedDefinitionAid | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
110 | |
0.00% |
0 / 1 |
getData | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
110 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid; |
5 | |
6 | use DifferenceEngine; |
7 | use MediaWiki\Content\WikitextContent; |
8 | use MediaWiki\Extension\Translate\Services; |
9 | use MediaWiki\Extension\Translate\TranslatorInterface\TranslationHelperException; |
10 | use MediaWiki\Extension\Translate\Utilities\Utilities; |
11 | use MediaWiki\MediaWikiServices; |
12 | use MediaWiki\Revision\MutableRevisionRecord; |
13 | use MediaWiki\Revision\SlotRecord; |
14 | use MediaWiki\Title\Title; |
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 | */ |
24 | class UpdatedDefinitionAid extends TranslationAid { |
25 | public function getData(): array { |
26 | $db = Utilities::getSafeReadDB(); |
27 | |
28 | $revTagStore = Services::getInstance()->getRevTagStore(); |
29 | |
30 | $translationRevision = $revTagStore->getTransver( $this->handle->getTitle() ); |
31 | if ( $translationRevision === null ) { |
32 | throw new TranslationHelperException( 'No definition revision recorded' ); |
33 | } |
34 | |
35 | $sourceLanguage = $this->group->getSourceLanguage(); |
36 | $definitionTitle = Title::makeTitleSafe( |
37 | $this->handle->getTitle()->getNamespace(), |
38 | $this->handle->getKey() . '/' . $sourceLanguage |
39 | ); |
40 | |
41 | if ( !$definitionTitle || !$definitionTitle->exists() ) { |
42 | throw new TranslationHelperException( 'Definition page does not exist' ); |
43 | } |
44 | |
45 | // Using getRevisionById instead of byTitle, because the page might have been renamed |
46 | $mwInstance = MediaWikiServices::getInstance(); |
47 | $revisionLookup = $mwInstance->getRevisionLookup(); |
48 | $oldRevRecord = $revisionLookup->getRevisionById( $translationRevision ); |
49 | if ( !$oldRevRecord ) { |
50 | throw new TranslationHelperException( 'Old definition version does not exist anymore' ); |
51 | } |
52 | |
53 | // Escaping legacy issue (T330453) |
54 | if ( $oldRevRecord->getPageId() !== $definitionTitle->getArticleID() ) { |
55 | throw new TranslationHelperException( |
56 | 'Translation unit definition id does not match old revision definition id' |
57 | ); |
58 | } |
59 | |
60 | $oldContent = $oldRevRecord->getContent( SlotRecord::MAIN ); |
61 | $newContent = $this->dataProvider->getDefinitionContent(); |
62 | |
63 | if ( !$oldContent ) { |
64 | throw new TranslationHelperException( 'Old definition version does not exist anymore' ); |
65 | } |
66 | |
67 | if ( !$oldContent instanceof WikitextContent || !$newContent instanceof WikitextContent ) { |
68 | throw new TranslationHelperException( 'Can only work on Wikitext content' ); |
69 | } |
70 | |
71 | if ( $oldContent->equals( $newContent ) ) { |
72 | throw new TranslationHelperException( 'No changes' ); |
73 | } |
74 | |
75 | $newRevRecord = new MutableRevisionRecord( $definitionTitle ); |
76 | $newRevRecord->setContent( SlotRecord::MAIN, $newContent ); |
77 | |
78 | $diff = new DifferenceEngine( $this->context ); |
79 | $diff->setTextLanguage( $mwInstance->getLanguageFactory()->getLanguage( $sourceLanguage ) ); |
80 | $diff->setRevisions( $oldRevRecord, $newRevRecord ); |
81 | $diff->setReducedLineNumbers(); |
82 | $diff->showDiffStyle(); |
83 | |
84 | $html = $diff->getDiff( |
85 | $this->context->msg( 'tpt-diff-old' )->escaped(), |
86 | $this->context->msg( 'tpt-diff-new' )->escaped() |
87 | ); |
88 | |
89 | return [ |
90 | 'value_old' => $oldContent->getText(), |
91 | 'value_new' => $newContent->getText(), |
92 | 'revisionid_old' => $oldRevRecord->getId(), |
93 | 'revisionid_new' => $definitionTitle->getLatestRevID(), |
94 | 'language' => $this->group->getSourceLanguage(), |
95 | 'html' => $html, |
96 | ]; |
97 | } |
98 | } |