summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/PrintRequest.php
blob: 24542c925a05956cada083605274968b29504171 (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
<?php

namespace SMW\Query;

use InvalidArgumentException;
use SMW\DataValues\PropertyChainValue;
use SMW\Localizer;
use SMW\Query\PrintRequest\Deserializer;
use SMW\Query\PrintRequest\Formatter;
use SMW\Query\PrintRequest\Serializer;
use SMWDataValue;
use SMWPropertyValue as PropertyValue;
use Title;

/**
 * Container class for request for printout, as used in queries to
 * obtain additional information for the retrieved results.
 *
 * @ingroup SMWQuery
 * @author Markus Krötzsch
 */
class PrintRequest {

	/**
	 * Query mode to print all direct categories of the current element.
	 */
	const PRINT_CATS = 0;

	/**
	 * Query mode to print all property values of a certain attribute of the
	 * current element.
	 */
	const PRINT_PROP = 1;

	/**
	 * Query mode to print the current element (page in result set).
	 */
	const PRINT_THIS = 2;

	/**
	 * Query mode to print whether current element is in given category
	 * (Boolean printout).
	 */
	const PRINT_CCAT = 3;

	/**
	 * Query mode indicating a chainable property value entity, with the last
	 * element to represent the printable output
	 */
	const PRINT_CHAIN = 4;

	protected $m_mode; // type of print request

	protected $m_label; // string for labelling results, contains no markup

	protected $m_data; // data entries specifyin gwhat was requested (mixed type)

	protected $m_typeid = false; // id of the datatype of the printed objects, if applicable

	protected $m_outputformat; // output format string for formatting results, if applicable

	protected $m_hash = false; // cache your hash (currently useful since SMWQueryResult accesses the hash many times, might be dropped at some point)

	protected $m_params = [];

	/**
	 * Identifies whether this instance was used/added and is diconnected to
	 * the original query where it was added.
	 *
	 * Mostly used in cases where QueryProcessor::addThisPrintout was executed.
	 */
	private $isDisconnected = false;

	/**
	 * Whether the label was marked with an extra `#` identifier.
	 */
	private $labelMarker = false;

	/**
	 * Create a print request.
	 *
	 * @param integer $mode a constant defining what to printout
	 * @param string $label the string label to describe this printout
	 * @param mixed $data optional data for specifying some request, might be a property object, title, or something else; interpretation depends on $mode
	 * @param mixed $outputformat optional string for specifying an output format, e.g. an output unit
	 * @param array|null $params optional array of further, named parameters for the print request
	 */
	public function __construct( $mode, $label, $data = null, $outputformat = false, array $params = null ) {
		if ( ( ( $mode == self::PRINT_CATS || $mode == self::PRINT_THIS ) &&
				!is_null( $data ) ) ||
			( $mode == self::PRINT_PROP &&
				( !( $data instanceof PropertyValue ) || !$data->isValid() ) ) ||
			( $mode == self::PRINT_CHAIN &&
				( !( $data instanceof PropertyChainValue ) || !$data->isValid() ) ) ||
			( $mode == self::PRINT_CCAT &&
				!( $data instanceof Title ) )
		) {
			throw new InvalidArgumentException( 'Data provided for print request does not fit the type of printout.' );
		}

		$this->m_mode = $mode;
		$this->m_data = $data;
		$this->m_outputformat = $outputformat;

		if ( $mode == self::PRINT_CCAT && !$outputformat ) {
			$this->m_outputformat = 'x'; // changed default for Boolean case
		}

		$this->setLabel( $label );

		if ( $params !== null ) {
			$this->m_params = $params;
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $isDisconnected
	 */
	public function isDisconnected( $isDisconnected ) {
		$this->isDisconnected = (bool)$isDisconnected;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $text
	 */
	public function markThisLabel( $text ) {

		if ( $this->m_mode !== self::PRINT_THIS ) {
			return;
		}

		$this->labelMarker = $text !== '' && $text{0} === '#';
	}

	/**
	 * @since 3.0
	 *
	 * @return boolean
	 */
	public function hasLabelMarker() {
		return $this->labelMarker;
	}

	/**
	 * @since 2.5
	 *
	 * @param integer $mode
	 *
	 * @return boolean
	 */
	public function isMode( $mode ) {
		return $this->m_mode === $mode;
	}

	public function getMode() {
		return $this->m_mode;
	}

	public function getLabel() {
		return $this->m_label;
	}

	/**
	 * @since 3.0
	 *
	 * @return string
	 */
	public function getCanonicalLabel() {

		if ( $this->m_mode === self::PRINT_PROP ) {
			return $this->m_data->getDataItem()->getCanonicalLabel();
		} elseif ( $this->m_mode === self::PRINT_CHAIN ) {
			return $this->m_data->getDataItem()->getString();
		} elseif ( $this->m_mode === self::PRINT_CATS ) {
			return Localizer::getInstance()->getNamespaceTextById( NS_CATEGORY );
		} elseif ( $this->m_mode === self::PRINT_CCAT ) {
			return $this->m_data->getPrefixedText();
		}

		return $this->m_label;
	}

	/**
	 * Obtain an HTML-formatted representation of the label.
	 * The $linker is a Linker object used for generating hyperlinks.
	 * If it is NULL, no links will be created.
	 */
	public function getHTMLText( $linker = null ) {
		return Formatter::format( $this, $linker, Formatter::FORMAT_HTML );
	}

	/**
	 * Obtain a Wiki-formatted representation of the label.
	 */
	public function getWikiText( $linker = false ) {
		return Formatter::format( $this, $linker, Formatter::FORMAT_WIKI );
	}

	/**
	 * Convenience method for accessing the text in either HTML or Wiki format.
	 */
	public function getText( $outputMode, $linker = null ) {
		return Formatter::format( $this, $linker, $outputMode );
	}

	/**
	 * Return additional data related to the print request. The result might be
	 * an object of class PropertyValue or Title, or simply NULL if no data
	 * is required for the given type of printout.
	 */
	public function getData() {
		return $this->m_data;
	}

	public function getOutputFormat() {
		return $this->m_outputformat;
	}

	/**
	 * If this print request refers to some property, return the type id of this property.
	 * Otherwise return '_wpg' since all other types of print request return wiki pages.
	 *
	 * @return string
	 */
	public function getTypeID() {

		if ( $this->m_typeid !== false ) {
			return $this->m_typeid;
		}

		if ( $this->m_mode == self::PRINT_PROP ) {
			$this->m_typeid = $this->m_data->getDataItem()->findPropertyTypeID();
		} elseif ( $this->m_mode == self::PRINT_CHAIN ) {
			$this->m_typeid = $this->m_data->getLastPropertyChainValue()->getDataItem()->findPropertyTypeID();
		} else {
			$this->m_typeid = '_wpg';
		}

		return $this->m_typeid;
	}

	/**
	 * Return a hash string that is used to eliminate duplicate
	 * print requests. The hash also includes the chosen label,
	 * so it is possible to print the same date with different
	 * labels.
	 *
	 * @return string
	 */
	public function getHash() {

		if ( $this->m_hash !== false ) {
			return $this->m_hash;
		}

		$this->m_hash = $this->m_mode . ':' . $this->m_label . ':';

		if ( $this->m_data instanceof Title ) {
			$this->m_hash .= $this->m_data->getPrefixedText() . ':';
		}
		elseif ( $this->m_data instanceof SMWDataValue ) {
			$this->m_hash .= $this->m_data->getHash() . ':';
		}

		$this->m_hash .= $this->m_outputformat . ':' . implode( '|', $this->m_params );

		return $this->m_hash;
	}

	/**
	 * Serialise this object like print requests given in \#ask.
	 *
	 * @param $params boolean that sets if the serialization should
	 *                include the extra print request parameters
	 */
	public function getSerialisation( $showparams = false ) {

		// In case of  disconnected instance (QueryProcessor::addThisPrintout as
		// part of a post-processing) return an empty serialization when the
		// mainLabel is available to avoid an extra `?...`
		if ( $this->isMode( self::PRINT_THIS ) && $this->isDisconnected ) {
			return '';
		}

		return Serializer::serialize( $this, $showparams );
	}

	/**
	 * Returns the value of a named parameter.
	 *
	 * @param $key string the name of the parameter key
	 *
	 * @return string Value of the paramer, if set (else FALSE)
	 */
	public function getParameter( $key ) {
		return array_key_exists( $key, $this->m_params ) ? $this->m_params[$key] : false;
	}

	/**
	 * Returns the array of parameters, where a string is mapped to a string.
	 *
	 * @return array Map of parameter names to values.
	 */
	public function getParameters() {
		return $this->m_params;
	}

	/**
	 * Sets a print request parameter.
	 *
	 * @param $key string Name of the parameter
	 * @param $value string Value for the parameter
	 */
	public function setParameter( $key, $value ) {
		$this->m_params[$key] = $value;
	}

	/**
	 * Removes a request parameter
	 *
	 * @since 3.0
	 *
	 * @param string $key
	 */
	public function removeParameter( $key ) {
		unset( $this->m_params[$key] );
	}

	/**
	 * @since  2.1
	 *
	 * @note $this->m_data = clone $data; // we assume that the caller denotes
	 * the object ot us; else he needs provide us with a clone
	 *
	 * @param string $label
	 */
	public function setLabel( $label ) {
		$this->m_label = $label;

		if ( $this->m_data instanceof SMWDataValue ) {
			$this->m_data->setCaption( $label );
		}
	}

	/**
	 * @see Deserializer::deserialize
	 * @since 2.4
	 *
	 * @param string $text
	 * @param $showMode = false
	 *
	 * @return PrintRequest|null
	 */
	public static function newFromText( $text, $showMode = false ) {
		return Deserializer::deserialize( $text, $showMode );
	}

}