1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
<?php
/**
* Class that enhances Title with stuff related to message groups
* @file
* @author Niklas Laxström
* @copyright Copyright © 2011-2013 Niklas Laxström
* @license GPL-2.0-or-later
*/
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
/**
* Class for pointing to messages, like Title class is for titles.
* @since 2011-03-13
*/
class MessageHandle {
/** @var LinkTarget */
protected $title;
/** @var string|null */
protected $key;
/** @var string|null Language code */
protected $code;
/** @var string[]|null */
protected $groupIds;
public function __construct( LinkTarget $title ) {
$this->title = $title;
}
/**
* Check if this handle is in a message namespace.
* @return bool
*/
public function isMessageNamespace() {
global $wgTranslateMessageNamespaces;
$namespace = $this->title->getNamespace();
return in_array( $namespace, $wgTranslateMessageNamespaces );
}
/**
* Recommended to use getCode and getKey instead.
* @return string[] Array of the message key and the language code
*/
public function figureMessage() {
if ( $this->key === null ) {
// Check if this is a valid message first
$this->key = $this->title->getDBkey();
$known = MessageIndex::singleton()->getGroupIds( $this ) !== [];
$pos = strrpos( $this->key, '/' );
if ( $known || $pos === false ) {
$this->code = '';
} else {
// For keys like Foo/, substr returns false instead of ''
$this->code = (string)( substr( $this->key, $pos + 1 ) );
$this->key = substr( $this->key, 0, $pos );
}
}
return [ $this->key, $this->code ];
}
/**
* Returns the identified or guessed message key.
* @return string
*/
public function getKey() {
$this->figureMessage();
return $this->key;
}
/**
* Returns the language code.
* For language codeless source messages will return empty string.
* @return string
*/
public function getCode() {
$this->figureMessage();
return $this->code;
}
/**
* Return the Language object for the assumed language of the content, which might
* be different from the subpage code (qqq, no subpage).
* @return Language
* @since 2016-01
*/
public function getEffectiveLanguage() {
$code = $this->getCode();
if ( $code === '' || $this->isDoc() ) {
return MediaWikiServices::getInstance()->getContentLanguage();
}
return wfGetLangObj( $code );
}
/**
* Determine whether the current handle is for message documentation.
* @return bool
*/
public function isDoc() {
global $wgTranslateDocumentationLanguageCode;
return $this->getCode() === $wgTranslateDocumentationLanguageCode;
}
/**
* Determine whether the current handle is for page translation feature.
* This does not consider whether the handle corresponds to any message.
* @return bool
*/
public function isPageTranslation() {
return $this->title->inNamespace( NS_TRANSLATIONS );
}
/**
* Returns all message group ids this message belongs to.
* The primary message group id is always the first one.
* If the handle does not correspond to any message, the returned array
* is empty.
* @return string[]
*/
public function getGroupIds() {
if ( $this->groupIds === null ) {
$this->groupIds = MessageIndex::singleton()->getGroupIds( $this );
}
return $this->groupIds;
}
/**
* Get the primary MessageGroup this message belongs to.
* You should check first that the handle is valid.
* @throws MWException
* @return MessageGroup
*/
public function getGroup() {
$ids = $this->getGroupIds();
if ( !isset( $ids[0] ) ) {
throw new MWException( 'called before isValid' );
}
return MessageGroups::getGroup( $ids[0] );
}
/**
* Checks if the handle corresponds to a known message.
* @since 2011-03-16
* @return bool
*/
public function isValid() {
if ( !$this->isMessageNamespace() ) {
return false;
}
$groups = $this->getGroupIds();
if ( !$groups ) {
return false;
}
// Do another check that the group actually exists
$group = $this->getGroup();
if ( !$group ) {
$logger = LoggerFactory::getInstance( 'Translate' );
$logger->warning(
'[MessageHandle] MessageIndex is out of date. Page {pagename} refers to ' .
'unknown group {messagegroup}',
[
'pagename' => $this->getTitle()->getPrefixedText(),
'messagegroup' => $groups[0],
]
);
// Schedule a job in the job queue (with deduplication)
$job = MessageIndexRebuildJob::newJob();
JobQueueGroup::singleton()->push( $job );
return false;
}
return true;
}
/**
* Get the original title.
* @return Title
*/
public function getTitle() {
return Title::newFromLinkTarget( $this->title );
}
/**
* Get the original title.
* @param string $code Language code.
* @return Title
* @since 2014.04
*/
public function getTitleForLanguage( $code ) {
return Title::makeTitle(
$this->title->getNamespace(),
$this->getKey() . "/$code"
);
}
/**
* Get the title for the page base.
* @return Title
* @since 2014.04
*/
public function getTitleForBase() {
return Title::makeTitle(
$this->title->getNamespace(),
$this->getKey()
);
}
/**
* Check if a string contains the fuzzy string.
*
* @param string $text Arbitrary text
* @return bool If string contains fuzzy string.
*/
public static function hasFuzzyString( $text ) {
return strpos( $text, TRANSLATE_FUZZY ) !== false;
}
/**
* Check if a title is marked as fuzzy.
* @return bool If title is marked fuzzy.
*/
public function isFuzzy() {
$dbr = wfGetDB( DB_REPLICA );
$tables = [ 'page', 'revtag' ];
$field = 'rt_type';
$conds = [
'page_namespace' => $this->title->getNamespace(),
'page_title' => $this->title->getDBkey(),
'rt_type' => RevTag::getType( 'fuzzy' ),
'page_id=rt_page',
'page_latest=rt_revision'
];
$res = $dbr->selectField( $tables, $field, $conds, __METHOD__ );
return $res !== false;
}
/**
* This returns the key that can be used for showMessage parameter for Special:Translate
* for regular message groups. It is not possible to automatically determine this key
* from the title alone.
* @return string
* @since 2017.10
*/
public function getInternalKey() {
$key = $this->getKey();
$nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
if ( !$nsInfo->isCapitalized( $this->title->getNamespace() ) ) {
return $key;
}
$group = $this->getGroup();
$keys = $group->getKeys();
// We cannot reliably map from the database key to the internal key if
// capital links setting is enabled for the namespace.
if ( in_array( $key, $keys, true ) ) {
return $key;
}
$lcKey = MediaWikiServices::getInstance()->getContentLanguage()
->lcfirst( $key );
if ( in_array( $lcKey, $keys, true ) ) {
return $lcKey;
}
return "BUG:$key";
}
}
|