Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageHandle.php
Go to the documentation of this file.
1<?php
12use MediaWiki\Linker\LinkTarget;
13use MediaWiki\Logger\LoggerFactory;
14use MediaWiki\MediaWikiServices;
15
22 protected $title;
24 protected $key;
26 protected $code;
28 protected $groupIds;
29
30 public function __construct( LinkTarget $title ) {
31 $this->title = $title;
32 }
33
38 public function isMessageNamespace() {
39 global $wgTranslateMessageNamespaces;
40 $namespace = $this->title->getNamespace();
41
42 return in_array( $namespace, $wgTranslateMessageNamespaces );
43 }
44
49 public function figureMessage() {
50 if ( $this->key === null ) {
51 // Check if this is a valid message first
52 $this->key = $this->title->getDBkey();
53 $known = MessageIndex::singleton()->getGroupIds( $this ) !== [];
54
55 $pos = strrpos( $this->key, '/' );
56 if ( $known || $pos === false ) {
57 $this->code = '';
58 } else {
59 // For keys like Foo/, substr returns false instead of ''
60 $this->code = (string)( substr( $this->key, $pos + 1 ) );
61 $this->key = substr( $this->key, 0, $pos );
62 }
63 }
64
65 return [ $this->key, $this->code ];
66 }
67
72 public function getKey() {
73 $this->figureMessage();
74
75 return $this->key;
76 }
77
83 public function getCode() {
84 $this->figureMessage();
85
86 return $this->code;
87 }
88
95 public function getEffectiveLanguage() {
96 $code = $this->getCode();
97 $mwInstance = MediaWikiServices::getInstance();
98 if ( !$mwInstance->getLanguageNameUtils()->isKnownLanguageTag( $code ) ||
99 $this->isDoc()
100 ) {
101 return $mwInstance->getContentLanguage();
102 }
103
104 return $mwInstance->getLanguageFactory()->getLanguage( $code );
105 }
106
111 public function isDoc() {
112 global $wgTranslateDocumentationLanguageCode;
113
114 return $this->getCode() === $wgTranslateDocumentationLanguageCode;
115 }
116
122 public function isPageTranslation() {
123 return $this->title->inNamespace( NS_TRANSLATIONS );
124 }
125
133 public function getGroupIds() {
134 if ( $this->groupIds === null ) {
135 $this->groupIds = MessageIndex::singleton()->getGroupIds( $this );
136 }
137
138 return $this->groupIds;
139 }
140
147 public function getGroup() {
148 $ids = $this->getGroupIds();
149 if ( !isset( $ids[0] ) ) {
150 throw new MWException( 'called before isValid' );
151 }
152
153 return MessageGroups::getGroup( $ids[0] );
154 }
155
161 public function isValid() {
162 if ( !$this->isMessageNamespace() ) {
163 return false;
164 }
165
166 $groups = $this->getGroupIds();
167 if ( !$groups ) {
168 return false;
169 }
170
171 // Do another check that the group actually exists
172 $group = $this->getGroup();
173 if ( !$group ) {
174 $logger = LoggerFactory::getInstance( 'Translate' );
175 $logger->warning(
176 '[MessageHandle] MessageIndex is out of date. Page {pagename} refers to ' .
177 'unknown group {messagegroup}',
178 [
179 'pagename' => $this->getTitle()->getPrefixedText(),
180 'messagegroup' => $groups[0],
181 ]
182 );
183
184 // Schedule a job in the job queue (with deduplication)
185 $job = MessageIndexRebuildJob::newJob();
186 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
187
188 return false;
189 }
190
191 return true;
192 }
193
198 public function getTitle() {
199 return Title::newFromLinkTarget( $this->title );
200 }
201
208 public function getTitleForLanguage( $code ) {
209 return Title::makeTitle(
210 $this->title->getNamespace(),
211 $this->getKey() . "/$code"
212 );
213 }
214
220 public function getTitleForBase() {
221 return Title::makeTitle(
222 $this->title->getNamespace(),
223 $this->getKey()
224 );
225 }
226
233 public static function hasFuzzyString( $text ) {
234 return strpos( $text, TRANSLATE_FUZZY ) !== false;
235 }
236
241 public function isFuzzy() {
242 $dbr = wfGetDB( DB_REPLICA );
243
244 $tables = [ 'page', 'revtag' ];
245 $field = 'rt_type';
246 $conds = [
247 'page_namespace' => $this->title->getNamespace(),
248 'page_title' => $this->title->getDBkey(),
249 'rt_type' => RevTagStore::FUZZY_TAG,
250 'page_id=rt_page',
251 'page_latest=rt_revision'
252 ];
253
254 $res = $dbr->selectField( $tables, $field, $conds, __METHOD__ );
255
256 return $res !== false;
257 }
258
266 public function getInternalKey(): string {
267 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
268 $contentLanguage = MediaWikiServices::getInstance()->getContentLanguage();
269
270 $key = $this->getKey();
271 $group = $this->getGroup();
272 $groupKeys = $group->getKeys();
273
274 if ( in_array( $key, $groupKeys, true ) ) {
275 return $key;
276 }
277
278 $namespace = $this->title->getNamespace();
279 if ( $nsInfo->isCapitalized( $namespace ) ) {
280 $lowercaseKey = $contentLanguage->lcfirst( $key );
281 if ( in_array( $lowercaseKey, $groupKeys, true ) ) {
282 return $lowercaseKey;
283 }
284 }
285
286 // Brute force all the keys to find the one. This one should always find a match
287 // if there is one.
288 foreach ( $groupKeys as $haystackKey ) {
289 $normalizedHaystackKey = Title::makeTitleSafe( $namespace, $haystackKey )->getDBkey();
290 if ( $normalizedHaystackKey === $key ) {
291 return $haystackKey;
292 }
293 }
294
295 return "BUG:$key";
296 }
297}
Factory class for accessing message groups individually by id or all of them as a list.
Class to manage revision tags for translatable bundles.
Class for pointing to messages, like Title class is for titles.
isFuzzy()
Check if a title is marked as fuzzy.
isPageTranslation()
Determine whether the current handle is for page translation feature.
static hasFuzzyString( $text)
Check if a string contains the fuzzy string.
figureMessage()
Recommended to use getCode and getKey instead.
isDoc()
Determine whether the current handle is for message documentation.
getGroup()
Get the primary MessageGroup this message belongs to.
getTitleForLanguage( $code)
Get the original title.
isValid()
Checks if the handle corresponds to a known message.
getEffectiveLanguage()
Return the Language object for the assumed language of the content, which might be different from the...
getGroupIds()
Returns all message group ids this message belongs to.
getTitle()
Get the original title.
isMessageNamespace()
Check if this handle is in a message namespace.
getCode()
Returns the language code.
getTitleForBase()
Get the title for the page base.
getInternalKey()
This returns the key that can be used for showMessage parameter for Special:Translate for regular mes...
getKey()
Returns the identified or guessed message key.