summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/Highlighter.php
blob: 6b6593468c4a35952e8421f47179f1244573de4b (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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php

namespace SMW;

use Html;
use SMWOutputs;

/**
 * Highlighter utility function for Semantic MediaWiki
 *
 * @license GNU GPL v2+
 * @since   1.9
 *
 * @author mwjames
 */
class Highlighter {

	/**
	 * Highlighter ID for no types
	 */
	const TYPE_NOTYPE = 0;

	/**
	 * Highlighter ID for properties
	 */
	const TYPE_PROPERTY = 1;

	/**
	 * Highlighter ID for text
	 */
	const TYPE_TEXT = 2;

	/**
	 * Highlighter ID for quantities
	 */
	const TYPE_QUANTITY = 3;

	/**
	 * Highlighter ID for warnings
	 */
	const TYPE_WARNING = 4;

	/**
	 * Highlighter ID for error
	 */
	const TYPE_ERROR = 5;

	/**
	 * Highlighter ID for information
	 */
	const TYPE_INFO = 6;

	/**
	 * Highlighter ID for help
	 */
	const TYPE_HELP = 7;

	/**
	 * Highlighter ID for notes
	 */
	const TYPE_NOTE = 8;

	/**
	 * Highlighter ID for service links
	 */
	const TYPE_SERVICE = 9;

	/**
	 * Highlighter ID for reference links
	 */
	const TYPE_REFERENCE = 10;

	/**
	 * @var array $options
	 */
	private $options;

	/**
	 * @var int $type
	 */
	private $type;

	/**
	 * @var string|null
	 */
	private $language = null;

	/**
	 * @since 1.9
	 *
	 * @param int $type
	 * @param string|null $language
	 */
	public function __construct( $type, $language = null ) {
		$this->type = $type;
		$this->language = $language;
	}

	/**
	 * @since 1.9
	 *
	 * @param string|int $type
	 * @param string|null $language
	 *
	 * @return Highlighter
	 */
	public static function factory( $type, $language = null ) {
		if ( $type === '' || !is_int( $type ) ) {
			$type = self::getTypeId( $type );
		}

		return new Highlighter( $type, $language );
	}

	/**
	 * @since 3.0
	 *
	 * @param string $text
	 * @param string|null $type
	 *
	 * @return booelan
	 */
	public static function hasHighlighterClass( $text, $type = null ) {

		if ( strpos( $text, 'smw-highlighter' ) === false ) {
			return false;
		}

		if ( $type !== null ) {
			return strpos( $text, 'data-type="' . self::getTypeId( $type ) . '"' ) !== false;
		}

		return true;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $text
	 *
	 * @return string
	 */
	public static function decode( $text ) {
		// #2347, '[' is handled by the MediaWiki parser/sanitizer itself
		return str_replace(
			[ '&amp;', '&lt;', '&gt;', '&#160;', '<nowiki>', '</nowiki>' ],
			[ '&', '<', '>', ' ', '', '' ],
			$text
		);
	}

	/**
	 * Returns html output
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	public function getHtml() {
		SMWOutputs::requireResource( 'ext.smw.tooltips' );
		return $this->getContainer();
	}

	/**
	 * Set content
	 *
	 * An external class that invokes content via setContent has to ensure
	 * that all data are appropriately escaped.
	 *
	 * @since 1.9
	 *
	 * @param array $content
	 *
	 * @return array
	 */
	public function setContent( array $content ) {
		/**
		 * @var $content
		 * $content['caption'] = a text or null
		 * $content['context'] = a text or null
		 */
		return $this->options = array_merge( $this->getTypeConfiguration( $this->type ), $content );
	}

	/**
	 * Returns type id
	 *
	 * @since 1.9
	 *
	 * @param string $type
	 *
	 * @return integer
	 */
	public static function getTypeId( $type ) {
		// TODO: why do we have a htmlspecialchars here?!
		switch ( strtolower ( htmlspecialchars ( $type ) ) ) {
			case 'property':
			return self::TYPE_PROPERTY;
			case 'text':
			return self::TYPE_TEXT;
			case 'quantity':
			return self::TYPE_QUANTITY;
			case 'warning':
			return self::TYPE_WARNING;
			case 'error':
			return self::TYPE_ERROR;
			case 'info':
			return self::TYPE_INFO;
			case 'help':
			return self::TYPE_HELP;
			case 'note':
			return self::TYPE_NOTE;
			case 'service':
			return self::TYPE_SERVICE;
			case 'reference':
			return self::TYPE_REFERENCE;
			default:
			return self::TYPE_NOTYPE;
		}
	}

	/**
	 * Builds Html container
	 *
	 * Content that is being invoked has to be escaped
	 * @see Highlighter::setContent
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	private function getContainer() {

		$captionclass = $this->options['captionclass'];

		// 2.4+ can display context for user-defined properties, here we ensure
		// to keep the style otherwise it displays italic which is by convention
		// reserved for predefined properties
		if ( $this->type === self::TYPE_PROPERTY && isset( $this->options['userDefined'] ) ) {
			$captionclass = $this->options['userDefined'] ? 'smwtext' : $captionclass;
		}

		$language = is_string( $this->language ) ? $this->language : Message::USER_LANGUAGE;
		$style = [];

		if ( isset( $this->options['style'] ) ) {
			$style = [ 'style' => $this->options['style'] ];
		}

		// #1875
		// title attribute contains stripped content to allow for a display in
		// no-js environments, the tooltip will remove the element once it is
		// loaded
		$title = $this->title( $this->options['content'], $language );

		$html = Html::rawElement(
			'span',
			[
				'class'        => 'smw-highlighter',
				'data-type'    => $this->options['type'],
				'data-content' => isset( $this->options['data-content'] ) ? $this->options['data-content'] : null,
				'data-state'   => $this->options['state'],
				'data-title'   => Message::get( $this->options['title'], Message::TEXT, $language ),
				'title'        => $title
			] + $style,
			Html::rawElement(
				'span',
				[
					'class' => $captionclass
				],
				$this->options['caption']
			) . Html::rawElement(
				'span',
				[
					'class' => 'smwttcontent'
				],
				htmlspecialchars_decode( $this->options['content'] )
			)
		);

		return $html;
	}

	/**
	 * Returns initial configuration settings
	 *
	 * @note You could create a class per entity type but does this
	 * really make sense just to get some configuration parameters?
	 *
	 * @since 1.9
	 *
	 * @param string $type
	 *
	 * @return array
	 */
	private function getTypeConfiguration( $type ) {
		$settings = [];
		$settings['type'] = $type;
		$settings['caption'] = '';
		$settings['content'] = '';

		switch ( $type ) {
			case self::TYPE_PROPERTY:
				$settings['state'] = 'inline';
				$settings['title'] = 'smw-ui-tooltip-title-property';
				$settings['captionclass'] = 'smwbuiltin';
				break;
			case self::TYPE_TEXT:
				$settings['state'] = 'persistent';
				$settings['title'] = 'smw-ui-tooltip-title-info';
				$settings['captionclass'] = 'smwtext';
				break;
			case self::TYPE_QUANTITY:
				$settings['state'] = 'inline';
				$settings['title'] = 'smw-ui-tooltip-title-quantity';
				$settings['captionclass'] = 'smwtext';
				break;
			case self::TYPE_NOTE:
				$settings['state'] = 'inline';
				$settings['title'] = 'smw-ui-tooltip-title-note';
				$settings['captionclass'] = 'smwtticon note';
				break;
			case self::TYPE_WARNING:
				$settings['state'] = 'inline';
				$settings['title'] = 'smw-ui-tooltip-title-warning';
				$settings['captionclass'] = 'smwtticon warning';
				break;
			case self::TYPE_ERROR:
				$settings['state'] = 'inline';
				$settings['title'] = 'smw-ui-tooltip-title-error';
				$settings['captionclass'] = 'smwtticon error';
				break;
			case self::TYPE_SERVICE:
				$settings['state'] = 'persistent';
				$settings['title'] = 'smw-ui-tooltip-title-service';
				$settings['captionclass'] = 'smwtticon service';
				break;
			case self::TYPE_REFERENCE:
				$settings['state'] = 'persistent';
				$settings['title'] = 'smw-ui-tooltip-title-reference';
				$settings['captionclass'] = 'smwtext';
				break;
			case self::TYPE_HELP:
			case self::TYPE_INFO:
				$settings['state'] = 'persistent';
				$settings['title'] = 'smw-ui-tooltip-title-info';
				$settings['captionclass'] = 'smwtticon info';
				break;
			case self::TYPE_NOTYPE:
			default:
				$settings['state'] = 'persistent';
				$settings['title'] = 'smw-ui-tooltip-title-info';
				$settings['captionclass'] = 'smwbuiltin';
		};

		return $settings;
	}

	private function title( $content, $language ) {

		// Pre-process the content when used as title to avoid breaking elements
		// (URLs etc.)
		if ( strpos( $content, '[' ) !== false || strpos( $content, '//' ) !== false ) {
			$content = Message::get( [ 'smw-parse', $content ], Message::PARSE, $language );
		}

		return strip_tags( htmlspecialchars_decode( str_replace( [ "[", '&#160;' ], [ "&#91;", ' ' ], $content ) ) );
	}

}