blob: 32b13b1b0fedd07c3a4deadec86193560a93c9aa (
plain)
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
|
<?php
/**
* Classes for message objects TMessage, ThinMessage and FatMessage.
*
* @file
* @author Niklas Laxström
* @copyright Copyright © 2008-2010, Niklas Laxström
* @license GPL-2.0-or-later
*/
/**
* Interface for message objects used by MessageCollection.
*/
abstract class TMessage {
/** @var string Message display key. */
protected $key;
/** @var string Message definition. */
protected $definition;
/** @var string Committed in-file translation. */
protected $infile;
/** @var string[] Message tags. */
protected $tags = [];
/** @var array Message properties. */
protected $props = [];
/** @var string[] Message reviewers. */
protected $reviewers = [];
/**
* Creates new message object.
*
* @param string $key Unique key identifying this message.
* @param string $definition The authoritave definition of this message.
*/
public function __construct( $key, $definition ) {
$this->key = $key;
$this->definition = $definition;
}
/**
* Get the message key.
* @return string
*/
public function key() {
return $this->key;
}
/**
* Get the message definition.
* @return string
*/
public function definition() {
return $this->definition;
}
/**
* Get the message translation.
* @return ?string
*/
abstract public function translation();
/**
* Set the committed translation.
* @param string $text
*/
public function setInfile( $text ) {
$this->infile = $text;
}
/**
* Returns the committed translation.
* @return ?string
*/
public function infile() {
return $this->infile;
}
/**
* Add a tag for this message.
* @param string $tag
*/
public function addTag( $tag ) {
$this->tags[] = $tag;
}
/**
* Check if this message has a given tag.
* @param string $tag
* @return bool
*/
public function hasTag( $tag ) {
return in_array( $tag, $this->tags, true );
}
/**
* Return all tags for this message;
* @return string[]
*/
public function getTags() {
return $this->tags;
}
public function setProperty( $key, $value ) {
$this->props[$key] = $value;
}
public function appendProperty( $key, $value ) {
if ( !isset( $this->props[$key] ) ) {
$this->props[$key] = [];
}
$this->props[$key][] = $value;
}
public function getProperty( $key ) {
return $this->props[$key] ?? null;
}
/**
* Get all the available property names.
* @return array
* @since 2013-01-17
*/
public function getPropertyNames() {
return array_keys( $this->props );
}
}
/**
* %Message object which is based on database result row. Hence the name thin.
* Needs fields rev_user_text and those that are needed for loading revision
* text.
*/
class ThinMessage extends TMessage {
// This maps properties to fields in the database result row
protected static $propertyMap = [
'last-translator-text' => 'rev_user_text',
'last-translator-id' => 'rev_user',
];
/** @var stdClass Database Result Row */
protected $row;
/** @var string Stored translation. */
protected $translation;
/**
* Set the database row this message is based on.
* @param stdClass $row Database Result Row
*/
public function setRow( $row ) {
$this->row = $row;
}
/**
* Set the current translation of this message.
* @param string $text
*/
public function setTranslation( $text ) {
$this->translation = $text;
}
/** @inheritDoc */
public function translation() {
if ( !isset( $this->row ) ) {
return $this->infile();
}
if ( $this->translation === null ) {
// Should only happen with MW < 1.34. Slow if the text table hasn't been joined in!
$text = Revision::getRevisionText( $this->row );
if ( $text !== false ) {
$this->translation = $text;
}
}
return $this->translation;
}
// Re-implemented
public function getProperty( $key ) {
if ( !isset( self::$propertyMap[$key] ) ) {
return parent::getProperty( $key );
}
$field = self::$propertyMap[$key];
return $this->row->$field ?? null;
}
// Re-implemented
public function getPropertyNames() {
return array_merge( parent::getPropertyNames(), array_keys( self::$propertyMap ) );
}
}
/**
* %Message object where you can directly set the translation.
* Hence the name fat. Authors are not supported.
*/
class FatMessage extends TMessage {
/** @var string Stored translation. */
protected $translation;
/**
* Set the current translation of this message.
* @param string $text
*/
public function setTranslation( $text ) {
$this->translation = $text;
}
public function translation() {
if ( $this->translation === null ) {
return $this->infile;
}
return $this->translation;
}
}
|