summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/Message.php
blob: 9bc0ba4b19e1278e331a694d911ad2f53e603155 (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
<?php
/**
 * Classes for message objects TMessage and ThinMessage.
 *
 * @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|null
	 */
	abstract public function translation();

	/**
	 * Set the committed translation.
	 * @param string $text
	 */
	public function setInfile( $text ) {
		$this->infile = $text;
	}

	/**
	 * Returns the committed translation.
	 * @return string|null
	 */
	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 array of strings
	 */
	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;

	/**
	 * Set the database row this message is based on.
	 * @param array $row Database Result Row
	 */
	public function setRow( $row ) {
		$this->row = $row;
	}

	public function translation() {
		if ( !isset( $this->row ) ) {
			return $this->infile();
		}

		return Revision::getRevisionText( $this->row );
	}

	// Re-implemented
	public function getProperty( $key ) {
		if ( !isset( self::$propertyMap[$key] ) ) {
			return parent::getProperty( $key );
		}

		$field = self::$propertyMap[$key];
		if ( !isset( $this->row->$field ) ) {
			return null;
		}

		return $this->row->$field;
	}

	// 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;
	}
}