Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslationPage.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\PageTranslation;
5
6use Content;
7use ContentHandler;
8use Language;
10use Title;
11use TMessage;
13
25 private $output;
27 private $group;
29 private $targetLanguage;
31 private $sourceLanguage;
33 private $showOutdated;
35 private $wrapUntranslated;
37 private $sourcePageTitle;
38
39 public function __construct(
40 ParserOutput $output,
42 Language $targetLanguage,
43 Language $sourceLanguage,
44 bool $showOutdated,
45 bool $wrapUntranslated,
46 Title $sourcePageTitle
47 ) {
48 $this->output = $output;
49 $this->group = $group;
50 $this->targetLanguage = $targetLanguage;
51 $this->sourceLanguage = $sourceLanguage;
52 $this->showOutdated = $showOutdated;
53 $this->wrapUntranslated = $wrapUntranslated;
54 $this->sourcePageTitle = $sourcePageTitle;
55 }
56
58 public function generateSource(): string {
59 $collection = $this->getMessageCollection();
60 $this->filterMessageCollection( $collection );
61 $messages = $this->extractMessages( $collection );
62 return $this->generateSourceFromTranslations( $messages );
63 }
64
66 public function getPageContent(): Content {
67 $text = $this->generateSource();
68 $model = $this->sourcePageTitle->getContentModel();
69 return ContentHandler::makeContent( $text, null, $model );
70 }
71
72 public function getMessageCollection(): MessageCollection {
73 return $this->group->initCollection( $this->targetLanguage->getCode() );
74 }
75
76 public function filterMessageCollection( MessageCollection $collection ): void {
77 $collection->loadTranslations();
78 if ( $this->showOutdated ) {
79 $collection->filter( 'hastranslation', false );
80 } else {
81 $collection->filter( 'translated', false );
82 }
83 }
84
86 public function extractMessages( MessageCollection $collection ): array {
87 $messages = [];
88 $prefix = $this->sourcePageTitle->getPrefixedDBkey() . '/';
89 foreach ( $this->output->units() as $unit ) {
90 $messages[$unit->id] = $collection[$prefix . $unit->id] ?? null;
91 }
92
93 return $messages;
94 }
95
97 public function generateSourceFromTranslations( array $messages ): string {
98 $replacements = [];
99 foreach ( $this->output->units() as $placeholder => $unit ) {
101 $msg = $messages[$unit->id] ?? null;
102 $replacements[$placeholder] = $unit->getTextForRendering(
103 $msg,
104 $this->sourceLanguage,
105 $this->targetLanguage,
106 $this->wrapUntranslated
107 );
108 }
109
110 $template = $this->output->translationPageTemplate();
111 return strtr( $template, $replacements );
112 }
113}
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), MessageIndex::singleton());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), new RevTagStore(), $services->getDBLoadBalancer());}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array
Represents a parsing output produced by TranslatablePageParser.
Generates wikitext source code for translation pages.
generateSource()
Generate translation page source using default options.
Core message collection class.
filter( $type, $condition=true, $value=null)
Filters messages based on some condition.
Interface for message objects used by MessageCollection.
Definition Message.php:14
Wraps the translatable page sections into a message group.