summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Message.php
blob: 7548a6733cb00c58a30d47f077465e8d3f2e0ed9 (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
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
<?php

namespace SMW;

use Closure;
use Language;

/**
 * @private
 *
 * Object agnostic handler class that encapsulates a foreign Message object
 * (e.g MW's Message class). It is expected that a registered handler returns a
 * simple string representation for the parameters, type, and language given.
 *
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class Message {

	/**
	 * @var array
	 */
	private static $messageCache = null;

	/**
	 * PoolCache ID
	 */
	const POOLCACHE_ID = 'message.cache';

	/**
	 * MW processing mode
	 */
	const TEXT = 0x2;
	const ESCAPED = 0x4;
	const PARSE = 0x8;

	/**
	 * Predefined language mode
	 */
	const CONTENT_LANGUAGE = 0x32;
	const USER_LANGUAGE = 0x64;

	/**
	 * @var array
	 */
	private static $messageHandler = [];

	/**
	 * @since 2.4
	 *
	 * @param $type
	 * @param Closure $handler
	 */
	public static function registerCallbackHandler( $type, Closure $handler ) {
		self::$messageHandler[$type] = $handler;
	}

	/**
	 * @since 2.4
	 *
	 * @param $type
	 */
	public static function deregisterHandlerFor( $type ) {
		unset( self::$messageHandler[$type] );
	}

	/**
	 * @since 2.4
	 */
	public static function clear() {
		self::$messageCache = null;
	}

	/**
	 * @since 2.4
	 *
	 * @return FixedInMemoryLruCache
	 */
	public static function getCache() {

		if ( self::$messageCache === null ) {
			self::$messageCache = InMemoryPoolCache::getInstance()->getPoolCacheById( self::POOLCACHE_ID, 1000 );
		}

		return self::$messageCache;
	}

	/**
	 * Encodes a message into a JSON representation that can transferred,
	 * transformed, and stored while allowing to add an infinite amount of
	 * arguments.
	 *
	 * '[2,"Foo", "Bar"]' => Preferred output type, Message ID, Argument $1 ... $
	 *
	 * @since 2.5
	 *
	 * @param string|array $parameters
	 * @param integer|null $type
	 *
	 * @return string
	 */
	public static function encode( $message, $type = null ) {

		if ( is_string( $message ) && json_decode( $message ) && json_last_error() === JSON_ERROR_NONE ) {
			return $message;
		}

		if ( $type === null ) {
			$type = self::TEXT;
		}

		if ( $message === [] ) {
			return '';
		}

		$message = (array)$message;
		$encode = [];
		$encode[] = $type;

		foreach ( $message as $value ) {
			// Check if the value is already encoded, and if decode to keep the
			// structure intact
			if ( substr( $value, 0, 1 ) === '[' && ( $dc = json_decode( $value, true ) ) && json_last_error() === JSON_ERROR_NONE ) {
				$encode += $dc;
			} else {
				// Normalize arguments like "<strong>Expression error:
				// Unrecognized word "yyyy".</strong>"
				$value = strip_tags( htmlspecialchars_decode( $value, ENT_QUOTES ) );

				// - Internally encoded to circumvent the strip_tags which would
				//   remove <, > from values that represent a range
				// - Encode `::` to prevent the annotation parser to pick the
				//   message value
				$value = str_replace( [ '%3C', '%3E', "::" ], [ '>', '<', "&#58;&#58;" ], $value );

				$encode[] = $value;
			}
		}

		return json_encode( $encode );
	}

	/**
	 * @FIXME Needs to be MW agnostic !
	 *
	 * @since 2.5
	 *
	 * @param string $messageId
	 *
	 * @return boolean
	 */
	public static function exists( $message ) {
		return wfMessage( $message )->exists();
	}

	/**
	 * @since 2.5
	 *
	 * @param string $json
	 * @param integer|null $type
	 * @param integer|null $language
	 *
	 * @return string|boolean
	 */
	public static function decode( $message, $type = null, $language = null ) {

		$message = json_decode( $message );
		$asType = null;

		if ( json_last_error() !== JSON_ERROR_NONE || $message === '' || $message === null ) {
			return false;
		}

		// If the first element is numeric then its signals the expected message
		// formatter type
		if ( isset( $message[0] ) && is_numeric( $message[0] ) ) {
			$asType = array_shift( $message );
		}

		// Is it a msgKey or a simple text?
		if ( isset( $message[0] ) && !self::exists( $message[0] ) ) {
			return $message[0];
		}

		return self::get( $message, ( $type !== null ? $type: $asType ), $language );
	}

	/**
	 * @since 2.4
	 *
	 * @param string|array $parameters
	 * @param integer|null $type
	 * @param integer|null $language
	 *
	 * @return string
	 */
	public static function get( $parameters, $type = null, $language = null ) {

		$handler = null;
		$parameters = (array)$parameters;

		if ( $type === null ) {
			$type = self::TEXT;
		}

		if ( $language === null || !$language ) {
			$language = self::CONTENT_LANGUAGE;
		}

		$hash = self::getHash( $parameters, $type, $language );

		if ( $content = self::getCache()->fetch( $hash ) ) {
			return $content;
		}

		if ( isset( self::$messageHandler[$type] ) && is_callable( self::$messageHandler[$type] ) ) {
			$handler = self::$messageHandler[$type];
		}

		if ( $handler === null ) {
			return '';
		}

		$message = call_user_func_array(
			$handler,
			[ $parameters, $language ]
		);

		self::getCache()->save( $hash, $message );

		return $message;
	}

	/**
	 * @since 2.4
	 *
	 * @param array $parameters
	 * @param integer $type
	 * @param integer|string|Language $language
	 *
	 * @return string
	 */
	public static function getHash( $parameters, $type = null, $language = null ) {

		if ( $language instanceof Language ) {
			$language = $language->getCode();
		}

		return md5( json_encode( $parameters ) . '#' . $type . '#' . $language );
	}

}