Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageHandle.php
Go to the documentation of this file.
1<?php
11use MediaWiki\Linker\LinkTarget;
12use MediaWiki\Logger\LoggerFactory;
13use MediaWiki\MediaWikiServices;
14
21 protected $title;
23 protected $key;
25 protected $code;
27 protected $groupIds;
28
29 public function __construct( LinkTarget $title ) {
30 $this->title = $title;
31 }
32
37 public function isMessageNamespace() {
38 global $wgTranslateMessageNamespaces;
39 $namespace = $this->title->getNamespace();
40
41 return in_array( $namespace, $wgTranslateMessageNamespaces );
42 }
43
48 public function figureMessage() {
49 if ( $this->key === null ) {
50 // Check if this is a valid message first
51 $this->key = $this->title->getDBkey();
52 $known = MessageIndex::singleton()->getGroupIds( $this ) !== [];
53
54 $pos = strrpos( $this->key, '/' );
55 if ( $known || $pos === false ) {
56 $this->code = '';
57 } else {
58 // For keys like Foo/, substr returns false instead of ''
59 $this->code = (string)( substr( $this->key, $pos + 1 ) );
60 $this->key = substr( $this->key, 0, $pos );
61 }
62 }
63
64 return [ $this->key, $this->code ];
65 }
66
71 public function getKey() {
72 $this->figureMessage();
73
74 return $this->key;
75 }
76
82 public function getCode() {
83 $this->figureMessage();
84
85 return $this->code;
86 }
87
94 public function getEffectiveLanguage() {
95 $code = $this->getCode();
96 if ( $code === '' || $this->isDoc() ) {
97 return MediaWikiServices::getInstance()->getContentLanguage();
98 }
99
100 return wfGetLangObj( $code );
101 }
102
107 public function isDoc() {
108 global $wgTranslateDocumentationLanguageCode;
109
110 return $this->getCode() === $wgTranslateDocumentationLanguageCode;
111 }
112
118 public function isPageTranslation() {
119 return $this->title->inNamespace( NS_TRANSLATIONS );
120 }
121
129 public function getGroupIds() {
130 if ( $this->groupIds === null ) {
131 $this->groupIds = MessageIndex::singleton()->getGroupIds( $this );
132 }
133
134 return $this->groupIds;
135 }
136
143 public function getGroup() {
144 $ids = $this->getGroupIds();
145 if ( !isset( $ids[0] ) ) {
146 throw new MWException( 'called before isValid' );
147 }
148
149 return MessageGroups::getGroup( $ids[0] );
150 }
151
157 public function isValid() {
158 if ( !$this->isMessageNamespace() ) {
159 return false;
160 }
161
162 $groups = $this->getGroupIds();
163 if ( !$groups ) {
164 return false;
165 }
166
167 // Do another check that the group actually exists
168 $group = $this->getGroup();
169 if ( !$group ) {
170 $logger = LoggerFactory::getInstance( 'Translate' );
171 $logger->warning(
172 '[MessageHandle] MessageIndex is out of date. Page {pagename} refers to ' .
173 'unknown group {messagegroup}',
174 [
175 'pagename' => $this->getTitle()->getPrefixedText(),
176 'messagegroup' => $groups[0],
177 ]
178 );
179
180 // Schedule a job in the job queue (with deduplication)
181 $job = MessageIndexRebuildJob::newJob();
182 MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
183
184 return false;
185 }
186
187 return true;
188 }
189
194 public function getTitle() {
195 return Title::newFromLinkTarget( $this->title );
196 }
197
204 public function getTitleForLanguage( $code ) {
205 return Title::makeTitle(
206 $this->title->getNamespace(),
207 $this->getKey() . "/$code"
208 );
209 }
210
216 public function getTitleForBase() {
217 return Title::makeTitle(
218 $this->title->getNamespace(),
219 $this->getKey()
220 );
221 }
222
229 public static function hasFuzzyString( $text ) {
230 return strpos( $text, TRANSLATE_FUZZY ) !== false;
231 }
232
237 public function isFuzzy() {
238 $dbr = wfGetDB( DB_REPLICA );
239
240 $tables = [ 'page', 'revtag' ];
241 $field = 'rt_type';
242 $conds = [
243 'page_namespace' => $this->title->getNamespace(),
244 'page_title' => $this->title->getDBkey(),
245 'rt_type' => RevTagStore::FUZZY_TAG,
246 'page_id=rt_page',
247 'page_latest=rt_revision'
248 ];
249
250 $res = $dbr->selectField( $tables, $field, $conds, __METHOD__ );
251
252 return $res !== false;
253 }
254
262 public function getInternalKey(): string {
263 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
264 $contentLanguage = MediaWikiServices::getInstance()->getContentLanguage();
265
266 $key = $this->getKey();
267 $group = $this->getGroup();
268 $groupKeys = $group->getKeys();
269
270 if ( in_array( $key, $groupKeys, true ) ) {
271 return $key;
272 }
273
274 $namespace = $this->title->getNamespace();
275 if ( $nsInfo->isCapitalized( $namespace ) ) {
276 $lowercaseKey = $contentLanguage->lcfirst( $key );
277 if ( in_array( $lowercaseKey, $groupKeys, true ) ) {
278 return $lowercaseKey;
279 }
280 }
281
282 // Brute force all the keys to find the one. This one should always find a match
283 // if there is one.
284 foreach ( $groupKeys as $haystackKey ) {
285 $normalizedHaystackKey = Title::makeTitleSafe( $namespace, $haystackKey )->getDBkey();
286 if ( $normalizedHaystackKey === $key ) {
287 return $haystackKey;
288 }
289 }
290
291 return "BUG:$key";
292 }
293}
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.