Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
UpdatedDefinitionAid.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;
5
6use DifferenceEngine;
10use MediaWiki\MediaWikiServices;
11use MediaWiki\Revision\SlotRecord;
12use MediaWiki\Title\Title;
13use WikitextContent;
14
24 public function getData(): array {
25 $db = Utilities::getSafeReadDB();
26
27 $revTagStore = Services::getInstance()->getRevTagStore();
28
29 $translationRevision = $revTagStore->getTransver( $this->handle->getTitle() );
30 if ( $translationRevision === null ) {
31 throw new TranslationHelperException( 'No definition revision recorded' );
32 }
33
34 $sourceLanguage = $this->group->getSourceLanguage();
35 $definitionTitle = Title::makeTitleSafe(
36 $this->handle->getTitle()->getNamespace(),
37 $this->handle->getKey() . '/' . $sourceLanguage
38 );
39
40 if ( !$definitionTitle || !$definitionTitle->exists() ) {
41 throw new TranslationHelperException( 'Definition page does not exist' );
42 }
43
44 // Using getRevisionById instead of byTitle, because the page might have been renamed
45 $mwInstance = MediaWikiServices::getInstance();
46 $revisionLookup = $mwInstance->getRevisionLookup();
47 $oldRevRecord = $revisionLookup->getRevisionById( $translationRevision );
48 if ( !$oldRevRecord ) {
49 throw new TranslationHelperException( 'Old definition version does not exist anymore' );
50 }
51
52 // Escaping legacy issue (T330453)
53 if ( $oldRevRecord->getPageId() !== $definitionTitle->getArticleID() ) {
54 throw new TranslationHelperException(
55 'Translation unit definition id does not match old revision definition id'
56 );
57 }
58
59 $oldContent = $oldRevRecord->getContent( SlotRecord::MAIN );
60 $newContent = $this->dataProvider->getDefinitionContent();
61
62 if ( !$oldContent ) {
63 throw new TranslationHelperException( 'Old definition version does not exist anymore' );
64 }
65
66 if ( !$oldContent instanceof WikitextContent || !$newContent instanceof WikitextContent ) {
67 throw new TranslationHelperException( 'Can only work on Wikitext content' );
68 }
69
70 if ( $oldContent->equals( $newContent ) ) {
71 throw new TranslationHelperException( 'No changes' );
72 }
73
74 $diff = new DifferenceEngine( $this->context );
75 $diff->setTextLanguage(
76 $mwInstance->getLanguageFactory()->getLanguage( $sourceLanguage )
77 );
78 $diff->setContent( $oldContent, $newContent );
79 $diff->setReducedLineNumbers();
80 $diff->showDiffStyle();
81
82 $html = $diff->getDiff(
83 $this->context->msg( 'tpt-diff-old' )->escaped(),
84 $this->context->msg( 'tpt-diff-new' )->escaped()
85 );
86
87 return [
88 'value_old' => $oldContent->getText(),
89 'value_new' => $newContent->getText(),
90 'revisionid_old' => $oldRevRecord->getId(),
91 'revisionid_new' => $definitionTitle->getLatestRevID(),
92 'language' => $this->group->getSourceLanguage(),
93 'html' => $html,
94 ];
95 }
96}
Minimal service container.
Definition Services.php:58
getData()
Translation aid class should implement this function.
Translation helpers can throw this exception when they cannot do anything useful with the current mes...
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31