Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
WikiPageMessageGroup.php
Go to the documentation of this file.
1<?php
15use MediaWiki\MediaWikiServices;
16use MediaWiki\Revision\SlotRecord;
17
22class WikiPageMessageGroup extends MessageGroupOld implements IDBAccessObject {
24 protected $title;
26 protected $namespace = NS_TRANSLATIONS;
27
32 public function __construct( $id, $title ) {
33 $this->id = $id;
34 $this->title = $title;
35 }
36
37 public function getSourceLanguage() {
38 return $this->getTitle()->getPageLanguage()->getCode();
39 }
40
42 public function getTitle() {
43 if ( is_string( $this->title ) ) {
44 $this->title = Title::newFromText( $this->title );
45 }
46
47 return $this->title;
48 }
49
54 protected $definitions;
55
57 public function getDefinitions() {
58 if ( is_array( $this->definitions ) ) {
59 return $this->definitions;
60 }
61
62 $title = $this->getTitle();
63
64 $dbr = TranslateUtils::getSafeReadDB();
65 $tables = [ 'page', 'translate_sections' ];
66 $vars = [ 'trs_key', 'trs_text' ];
67 $conds = [
68 'page_namespace' => $title->getNamespace(),
69 'page_title' => $title->getDBkey(),
70 // The join condition
71 'page_id = trs_page',
72 ];
73 $options = [ 'ORDER BY' => 'trs_order' ];
74 $res = $dbr->select( $tables, $vars, $conds, __METHOD__, $options );
75
76 $defs = [];
77
78 foreach ( $res as $r ) {
79 $section = new TranslationUnit( $r->trs_text );
80 $defs[$r->trs_key] = $section->getTextWithVariables();
81 }
82
83 $groupKeys = $this->makeGroupKeys( array_keys( $defs ) );
84 $this->definitions = array_combine( $groupKeys, array_values( $defs ) );
85
86 return $this->definitions;
87 }
88
93 public function makeGroupKeys( array $keys ): array {
94 $prefix = $this->getTitle()->getPrefixedDBkey() . '/';
95 return array_map( static function ( string $key ) use ( $prefix ): string {
96 return $prefix . str_replace( ' ', '_', $key );
97 }, $keys );
98 }
99
107 public function getLabel( IContextSource $context = null ) {
108 return $this->getTitle()->getPrefixedText();
109 }
110
119 public function clearCaches() {
120 $this->definitions = null;
121 }
122
123 public function load( $code ) {
124 if ( $this->isSourceLanguage( $code ) ) {
125 return $this->getDefinitions();
126 }
127
128 return [];
129 }
130
140 public function getMessage( $key, $code, $flags = self::READ_LATEST ) {
141 if ( $this->isSourceLanguage( $code ) ) {
142 $stuff = $this->load( $code );
143
144 $title = Title::newFromText( $key );
145 if ( $title ) {
146 $key = $title->getPrefixedDBkey();
147 }
148
149 return $stuff[$key] ?? null;
150 }
151
152 $title = Title::makeTitleSafe( $this->getNamespace(), "$key/$code" );
153 if ( Hooks::$renderingContext ) {
154 $revFlags = IDBAccessObject::READ_NORMAL; // bug T95753
155 } else {
156 $revFlags = ( $flags & self::READ_LATEST ) == self::READ_LATEST
157 ? IDBAccessObject::READ_LATEST
158 : IDBAccessObject::READ_NORMAL;
159 }
160 $rev = MediaWikiServices::getInstance()
161 ->getRevisionLookup()
162 ->getRevisionByTitle( $title, false, $revFlags );
163
164 if ( !$rev ) {
165 return null;
166 }
167
168 $content = $rev->getContent( SlotRecord::MAIN );
169 return ( $content instanceof TextContent ) ? $content->getText() : null;
170 }
171
173 public function getValidator() {
174 $validator = new ValidationRunner( $this->getId() );
175 $validator->setValidators( [
176 [ 'id' => 'MediaWikiPlural' ],
177 [ 'id' => 'BraceBalance' ]
178 ] );
179
180 return $validator;
181 }
182
183 public function getInsertablesSuggester() {
185 }
186
187 public function getDescription( IContextSource $context = null ) {
188 $title = $this->getTitle()->getPrefixedText();
189 $target = ":$title";
190 $pageLanguageCode = $this->getSourceLanguage();
191 $inLanguageCode = $context ? $context->getLanguage()->getCode() : null;
192 $languageName = Language::fetchLanguageName( $pageLanguageCode, $inLanguageCode );
193
194 // Allow for adding a custom group description by using
195 // "MediaWiki:Tp-custom-<group ID>".
196 $customText = '';
197 $msg = wfMessage( 'tp-custom-' . $this->id );
198 self::addContext( $msg, $context );
199 if ( $msg->exists() ) {
200 $customText = $msg->plain();
201 }
202
203 $msg = wfMessage( 'translate-tag-page-desc', $title, $target, $languageName, $pageLanguageCode );
204 self::addContext( $msg, $context );
205
206 return $msg->plain() . $customText;
207 }
208}
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
This class represents one translation unit in a translatable page.
Message validator is used to run validators to find common mistakes so that translators can fix them ...
This is the interface and base implementation of unmanaged message groups.
$id
Group-wide unique id of this group.
Wraps the translatable page sections into a message group.
clearCaches()
Clear caches to avoid stale data.
load( $code)
Returns a list of messages in a given language code.
$definitions
Only used for caching to avoid repeating database queries for example during message index rebuild.
getLabel(IContextSource $context=null)
Overriding the getLabel method and deriving the label from the title.
getMessage( $key, $code, $flags=self::READ_LATEST)
Returns of stored translation of message specified by the $key in language code $code.
getSourceLanguage()
Returns language code depicting the language of source text.
getDescription(IContextSource $context=null)
Returns a longer description about the group.