summaryrefslogtreecommitdiff
blob: c98e789a1dfd6a6bc3b3fea5cb37103852e4d0a5 (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
<?php

namespace SMW;

use SMWOutputs;

use IContextSource;
use ContextSource;
use Html;

use MWException;

/**
 * Highlighter utility function for Semantic MediaWiki
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * @file
 *
 * @license GNU GPL v2+
 * @since   1.9
 *
 * @author mwjames
 */

/**
 * Highlighter utility function for Semantic MediaWiki
 *
 * @ingroup SMW
 */
class Highlighter extends ContextSource {

	// 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 informations
	const TYPE_INFO      = 5;
	//  Highlighter ID for help
	const TYPE_HELP      = 6;
	//  Highlighter ID for notes
	const TYPE_NOTE      = 7;
	//  Highlighter ID for service links
	const TYPE_SERVICE   = 8;

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

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

	/**
	 * Constructor
	 *
	 * @since 1.9
	 *
	 * @param int $type
	 * @param \IContextSource|null $context
	 */
	public function __construct( $type, IContextSource $context = null ) {
		if ( !$context ) {
			$context = \RequestContext::getMain();
		}
		$this->setContext( $context );
		$this->type = $type;
	}

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

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

	/**
	 * Returns html output
	 *
	 * @since 1.9
	 *
	 * @return string
	 */
	public function getHtml() {
		//@todo Introduce temporary fix, for more information see bug 43205
		SMWOutputs::requireResource( 'ext.smw.tooltips' );
		// $this->getOutput()->addModules( '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 'info'    : return self::TYPE_INFO;
			case 'help'    : return self::TYPE_HELP;
			case 'note'    : return self::TYPE_NOTE;
			case 'service' : return self::TYPE_SERVICE;
			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() {
		return Html::rawElement(
			'span',
			array(
				'class'      => 'smw-highlighter',
				'data-type'  => $this->options['type'],
				'data-state' => $this->options['state'],
				'data-title' => $this->msg( $this->options['title'] )->text(),
			), Html::rawElement(
					'span',
					array(
						'class' => $this->options['captionclass']
					), $this->options['caption']
				) . Html::rawElement(
					'span',
					array(
						'class' => 'smwttcontent'
					), $this->options['content']
				)
			);
	}

	/**
	 * Returns initial configuation 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 = array();
		$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_SERVICE:
				$settings['state'] = 'persistent';
				$settings['title'] = 'smw-ui-tooltip-title-service';
				$settings['captionclass'] = 'smwtticon service';
				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;
	}
}