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 Title;
13use WikitextContent;
14
24 public function getData(): array {
25 $db = Utilities::getSafeReadDB();
26 $conds = [
27 'rt_page' => $this->handle->getTitle()->getArticleID(),
28 'rt_type' => RevTagStore::TRANSVER_PROP,
29 ];
30 $options = [
31 'ORDER BY' => 'rt_revision DESC',
32 ];
33
34 $translationRevision = $db->selectField( 'revtag', 'rt_value', $conds, __METHOD__, $options );
35 if ( $translationRevision === false ) {
36 throw new TranslationHelperException( 'No definition revision recorded' );
37 }
38
39 $sourceLanguage = $this->group->getSourceLanguage();
40 $definitionTitle = Title::makeTitleSafe(
41 $this->handle->getTitle()->getNamespace(),
42 $this->handle->getKey() . '/' . $sourceLanguage
43 );
44
45 if ( !$definitionTitle || !$definitionTitle->exists() ) {
46 throw new TranslationHelperException( 'Definition page does not exist' );
47 }
48
49 // Using getRevisionById instead of byTitle, because the page might have been renamed
50 $mwInstance = MediaWikiServices::getInstance();
51 $revisionLookup = $mwInstance->getRevisionLookup();
52 $oldRevRecord = $revisionLookup->getRevisionById( $translationRevision );
53 if ( !$oldRevRecord ) {
54 throw new TranslationHelperException( 'Old definition version does not exist anymore' );
55 }
56
57 // Escaping legacy issue (T330453)
58 if ( $oldRevRecord->getPageId() !== $definitionTitle->getArticleID() ) {
59 throw new TranslationHelperException(
60 'Translation unit definition id does not match old revision definition id'
61 );
62 }
63
64 $oldContent = $oldRevRecord->getContent( SlotRecord::MAIN );
65 $newContent = $this->dataProvider->getDefinitionContent();
66
67 if ( !$oldContent ) {
68 throw new TranslationHelperException( 'Old definition version does not exist anymore' );
69 }
70
71 if ( !$oldContent instanceof WikitextContent || !$newContent instanceof WikitextContent ) {
72 throw new TranslationHelperException( 'Can only work on Wikitext content' );
73 }
74
75 if ( $oldContent->equals( $newContent ) ) {
76 throw new TranslationHelperException( 'No changes' );
77 }
78
79 $diff = new DifferenceEngine( $this->context );
80 $diff->setTextLanguage(
81 $mwInstance->getLanguageFactory()->getLanguage( $sourceLanguage )
82 );
83 $diff->setContent( $oldContent, $newContent );
84 $diff->setReducedLineNumbers();
85 $diff->showDiffStyle();
86
87 $html = $diff->getDiff(
88 $this->context->msg( 'tpt-diff-old' )->escaped(),
89 $this->context->msg( 'tpt-diff-new' )->escaped()
90 );
91
92 return [
93 'value_old' => $oldContent->getText(),
94 'value_new' => $newContent->getText(),
95 'revisionid_old' => $oldRevRecord->getId(),
96 'revisionid_new' => $definitionTitle->getLatestRevID(),
97 'language' => $this->group->getSourceLanguage(),
98 'html' => $html,
99 ];
100 }
101}
Class to manage revision tags for translatable bundles.
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