summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/ParserFunctions/SubobjectParserFunction.php
blob: c2bf1cd9942c399323d18ccb05dda25d417a930c (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
<?php

namespace SMW\ParserFunctions;

use Parser;
use SMW\DataValueFactory;
use SMW\DIProperty;
use SMW\HashBuilder;
use SMW\MediaWiki\StripMarkerDecoder;
use SMW\Message;
use SMW\MessageFormatter;
use SMW\ParserData;
use SMW\ParserParameterProcessor;
use SMW\SemanticData;
use SMW\Subobject;

/**
 * @private This class should not be instantiated directly, please use
 * ParserFunctionFactory::newSubobjectParserFunction
 *
 * Provides the {{#subobject}} parser function
 *
 * @see http://www.semantic-mediawiki.org/wiki/Help:ParserFunction
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class SubobjectParserFunction {

	/**
	 * Fixed identifier that describes the sortkey annotation parameter
	 */
	const PARAM_SORTKEY = '@sortkey';

	/**
	 * Fixed identifier that describes a category parameter
	 *
	 * Those will not be visible by the "standard" category list as the handling
	 * of assigned categories is SMW specific for subobjects.
	 */
	const PARAM_CATEGORY = '@category';

	/**
	 * Fixed identifier that describes a property that can auto-linked the
	 * embeddedding subject
	 */
	const PARAM_LINKWITH = '@linkWith';

	/**
	 * @var ParserData
	 */
	protected $parserData;

	/**
	 * @var Subobject
	 */
	protected $subobject;

	/**
	 * @var MessageFormatter
	 */
	protected $messageFormatter;

	/**
	 * @var StripMarkerDecoder
	 */
	private $stripMarkerDecoder;

	/**
	 * @var boolean
	 */
	private $useFirstElementAsPropertyLabel = false;

	/**
	 * @var boolean
	 */
	private $isCapitalLinks = true;

	/**
	 * @var boolean
	 */
	private $isComparableContent = false;

	/**
	 * @since 1.9
	 *
	 * @param ParserData $parserData
	 * @param Subobject $subobject
	 * @param MessageFormatter $messageFormatter
	 */
	public function __construct( ParserData $parserData, Subobject $subobject, MessageFormatter $messageFormatter ) {
		$this->parserData = $parserData;
		$this->subobject = $subobject;
		$this->messageFormatter = $messageFormatter;
	}

	/**
	 * @since 3.0
	 *
	 * @param StripMarkerDecoder $stripMarkerDecoder
	 */
	public function setStripMarkerDecoder( StripMarkerDecoder $stripMarkerDecoder ) {
		$this->stripMarkerDecoder = $stripMarkerDecoder;
	}

	/**
	 * @see $wgCapitalLinks
	 *
	 * @since 2.5
	 *
	 * @param boolean $isCapitalLinks
	 */
	public function isCapitalLinks( $isCapitalLinks ) {
		$this->isCapitalLinks = $isCapitalLinks;
	}

	/**
	 * Ensures that unordered parameters and property names are normalized and
	 * sorted to produce the same hash even if elements of the same literal
	 * representation are placed differently.
	 *
	 * @since 3.0
	 *
	 * @param boolean $isComparableContent
	 */
	public function isComparableContent( $isComparableContent = true ) {
		$this->isComparableContent = (bool)$isComparableContent;
	}

	/**
	 * @since 1.9
	 *
	 * @param boolean $useFirstElementAsPropertyLabel
	 *
	 * @return SubobjectParserFunction
	 */
	public function useFirstElementAsPropertyLabel( $useFirstElementAsPropertyLabel = true ) {
		$this->useFirstElementAsPropertyLabel = (bool)$useFirstElementAsPropertyLabel;
		return $this;
	}

	/**
	 * @since 1.9
	 *
	 * @param ParserParameterProcessor $params
	 *
	 * @return string|null
	 */
	public function parse( ParserParameterProcessor $parameters ) {

		if (
			$this->parserData->canUse() &&
			$this->addDataValuesToSubobject( $parameters ) &&
			$this->subobject->getSemanticData()->isEmpty() === false ) {
			$this->parserData->getSemanticData()->addSubobject( $this->subobject );
		}

		$this->parserData->pushSemanticDataToParserOutput();

		$html = $this->messageFormatter->addFromArray( $this->subobject->getErrors() )
			->addFromArray( $this->parserData->getErrors() )
			->addFromArray( $parameters->getErrors() )
			->getHtml();

		// An empty output in MW forces an extra <br> element.
		//if ( $html == '' ) {
		//	$html = '<p></p>';
		//}

		return $html;
	}

	protected function addDataValuesToSubobject( ParserParameterProcessor $parserParameterProcessor ) {

		// Named subobjects containing a "." in the first five characters are
		// reserved to be used by extensions only in order to separate them from
		// user land and avoid having them accidentally to refer to the same
		// named ID (i.e. different access restrictions etc.)
		if ( strpos( mb_substr( $parserParameterProcessor->getFirst(), 0, 5 ), '.' ) !== false ) {
			return $this->parserData->addError(
				Message::encode( [ 'smw-subobject-parser-invalid-naming-scheme', $parserParameterProcessor->getFirst() ] )
			);
		}

		list( $parameters, $id ) = $this->getParameters(
			$parserParameterProcessor
		);

		$this->subobject->setEmptyContainerForId(
			$id
		);

		$subject = $this->subobject->getSubject();

		foreach ( $parameters as $property => $values ) {

			if ( $property === self::PARAM_SORTKEY ) {
				$property = DIProperty::TYPE_SORTKEY;
			}

			if ( $property === self::PARAM_CATEGORY ) {
				$property = DIProperty::TYPE_CATEGORY;
			}

			foreach ( $values as $value ) {

				$dataValue = DataValueFactory::getInstance()->newDataValueByText(
						$property,
						$value,
						false,
						$subject
					);

				$this->subobject->addDataValue( $dataValue );
			}
		}

		$this->augment( $this->subobject->getSemanticData() );

		return true;
	}

	private function getParameters( ParserParameterProcessor $parserParameterProcessor ) {

		$id = $parserParameterProcessor->getFirst();
		$isAnonymous = in_array( $id, [ null, '' ,'-' ] );

		$useFirst = $this->useFirstElementAsPropertyLabel && !$isAnonymous;

		$parameters = $this->preprocess(
			$parserParameterProcessor,
			$useFirst
		);

		// FIXME remove the check with 3.1, should be standard by then!
		if ( !$this->isComparableContent ) {
			$p = $parameters;
		} else {
			$p = $parameters;
			// Sort the copy not the parameters itself
			$parserParameterProcessor->sort( $p );
		}

		// Reclaim the ID to be content hash based
		if ( $useFirst || $isAnonymous ) {
			$id = HashBuilder::createFromContent( $p, '_' );
		}

		return [ $parameters, $id ];
	}

	private function preprocess( ParserParameterProcessor $parserParameterProcessor, $useFirst ) {

		if ( $parserParameterProcessor->hasParameter( self::PARAM_LINKWITH ) ) {
			$val = $parserParameterProcessor->getParameterValuesByKey( self::PARAM_LINKWITH );
			$parserParameterProcessor->addParameter(
				end( $val ),
				$this->parserData->getTitle()->getPrefixedText()
			);

			$parserParameterProcessor->removeParameterByKey( self::PARAM_LINKWITH );
		}

		if ( $useFirst ) {
			$parserParameterProcessor->addParameter(
				$parserParameterProcessor->getFirst(),
				$this->parserData->getTitle()->getPrefixedText()
			);
		}

		$parameters = $this->decode(
			$parserParameterProcessor->toArray()
		);

		foreach ( $parameters as $property => $values ) {

			$prop = $property;

			// Normalize property names to generate the same hash for when
			// CapitalLinks is enabled (has foo === Has foo)
			if ( $property !== '' && $property{0} !== '@' && $this->isCapitalLinks ) {
				$property = mb_strtoupper( mb_substr( $property, 0, 1 ) ) . mb_substr( $property, 1 );
			}

			unset( $parameters[$prop] );
			$parameters[$property] = $values;
		}

		return $parameters;
	}

	private function decode( $parameters ) {

		if ( $this->stripMarkerDecoder === null || !$this->stripMarkerDecoder->canUse() ) {
			return $parameters;
		}

		// Any decoding has to happen before the subject ID is generated otherwise
		// the value would contain something like `UNIQ--nowiki-00000011-QINU`
		// and be part of the hash. `UNIQ--nowiki-00000011-QINU` isn't stable
		// and changes to text will create new marker positions therefore it
		// cannot be part of the hash computation
		foreach ( $parameters as $property => &$values ) {
			foreach ( $values as &$value ) {
				$value = $this->stripMarkerDecoder->decode( $value );
			}
		}

		return $parameters;
	}

	private function augment( $semanticData ) {

		// Data block created by a user
		$semanticData->setOption( SemanticData::PROC_USER, true );

		$sortkey = new DIProperty( DIProperty::TYPE_SORTKEY );
		$displayTitle = new DIProperty( DIProperty::TYPE_DISPLAYTITLE );

		if ( $semanticData->hasProperty( $sortkey ) || !$semanticData->hasProperty( $displayTitle ) ) {
			return null;
		}

		$pv = $semanticData->getPropertyValues(
			$displayTitle
		);

		$semanticData->addPropertyObjectValue(
			$sortkey,
			end( $pv )
		);
	}

}