summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValueFactory.php
blob: b8ed09e7fc035a44bead86b698561906552243f4 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php

namespace SMW;

use SMW\DataValues\PropertyValue;
use SMW\Services\DataValueServiceFactory;
use SMWDataItem as DataItem;
use SMWDataValue as DataValue;
use SMWDIError;
use SMWErrorValue as ErrorValue;

/**
 * Factory class for creating SMWDataValue objects for supplied types or
 * properties and data values.
 *
 * The class has the main entry point newTypeIdValue(), which creates a new
 * datavalue object, possibly with preset user values, captions and
 * property names. To create suitable datavalues for a given property, the
 * method newDataValueByProperty() can be used.
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author Markus Krötzsch
 * @author Jeroen De Dauw
 * @author mwjames
 */
class DataValueFactory {

	/**
	 * @var DataTypeRegistry
	 */
	private static $instance = null;

	/**
	 * @var DataTypeRegistry
	 */
	private $dataTypeRegistry = null;

	/**
	 * @var DataValueServiceFactory
	 */
	private $dataValueServiceFactory;

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

	/**
	 * @since 1.9
	 *
	 * @param DataTypeRegistry $dataTypeRegistry
	 * @param DataValueServiceFactory $dataValueServiceFactory
	 */
	protected function __construct( DataTypeRegistry $dataTypeRegistry, DataValueServiceFactory $dataValueServiceFactory ) {
		$this->dataTypeRegistry = $dataTypeRegistry;
		$this->dataValueServiceFactory = $dataValueServiceFactory;
	}

	/**
	 * @since 1.9
	 *
	 * @return DataValueFactory
	 */
	public static function getInstance() {

		if ( self::$instance !== null ) {
			return self::$instance;
		}

		$applicationFactory = ApplicationFactory::getInstance();
		$dataValueServiceFactory = $applicationFactory->create( 'DataValueServiceFactory' );
		$dataTypeRegistry = DataTypeRegistry::getInstance();

		$dataValueServiceFactory->importExtraneousFunctions(
			$dataTypeRegistry->getExtraneousFunctions()
		);

		self::$instance = new self(
			$dataTypeRegistry,
			$dataValueServiceFactory
		);

		self::$instance->setDefaultOutputFormatters(
			$applicationFactory->getSettings()->get( 'smwgDefaultOutputFormatters' )
		);

		return self::$instance;
	}

	/**
	 * @since 2.4
	 */
	public function clear() {
		$this->dataTypeRegistry->clear();
		self::$instance = null;
	}

	/**
	 * @since 3.0
	 *
	 * @param array $defaultOutputFormatters
	 */
	public function setDefaultOutputFormatters( array $defaultOutputFormatters ) {

		$this->defaultOutputFormatters = [];

		foreach ( $defaultOutputFormatters as $type => $formatter ) {

			$type = str_replace( ' ' , '_', $type );

			if ( $type{0} !== '_' && ( $dType = $this->dataTypeRegistry->findTypeByLabel( $type ) ) !== '' ) {
				$type = $dType;
			}

			$this->defaultOutputFormatters[$type] = $formatter;
		}
	}

	/**
	 * Create a value from a type id. If no $value is given, an empty
	 * container is created, the value of which can be set later on.
	 *
	 * @param $typeId string id string for the given type
	 * @param $valueString mixed user value string, or false if unknown
	 * @param $caption mixed user-defined caption, or false if none given
	 * @param $property SMWDIProperty property object for which this value is made, or null
	 * @param $contextPage SMWDIWikiPage that provides a context for parsing the value string, or null
	 *
	 * @return DataValue
	 */
	public function newDataValueByType( $typeId, $valueString = false, $caption = false, DIProperty $property = null, $contextPage = null ) {

		if ( !$this->dataTypeRegistry->hasDataTypeClassById( $typeId ) ) {
			return new ErrorValue(
				$typeId,
				[ 'smw_unknowntype', $typeId ],
				$valueString,
				$caption
			);
		}

		$dataValue = $this->dataValueServiceFactory->newDataValueByType(
			$typeId,
			$this->dataTypeRegistry->getDataTypeClassById( $typeId )
		);

		$dataValue->setDataValueServiceFactory(
			$this->dataValueServiceFactory
		);

		$dataValue->copyOptions(
			$this->dataTypeRegistry->getOptions()
		);

		foreach ( $this->dataTypeRegistry->getExtensionData( $typeId ) as $key => $value ) {

			if ( !is_string( $key ) ) {
				continue;
			}

			$dataValue->setExtensionData( $key, $value );
		}

		$localizer = Localizer::getInstance();

		$dataValue->setOption(
			DataValue::OPT_USER_LANGUAGE,
			$localizer->getUserLanguage()->getCode()
		);

		$dataValue->setOption(
			DataValue::OPT_CONTENT_LANGUAGE,
			$localizer->getContentLanguage()->getCode()
		);

		$dataValue->setOption(
			DataValue::OPT_COMPACT_INFOLINKS,
			$GLOBALS['smwgCompactLinkSupport']
		);

		if ( isset( $this->defaultOutputFormatters[$typeId] ) ) {
			$dataValue->setOutputFormat( $this->defaultOutputFormatters[$typeId] );
		}

		if ( $property !== null ) {
			$dataValue->setProperty( $property );

			if ( isset( $this->defaultOutputFormatters[$property->getKey()] ) ) {
				$dataValue->setOutputFormat( $this->defaultOutputFormatters[$property->getKey()] );
			}
		}

		if ( !is_null( $contextPage ) ) {
			$dataValue->setContextPage( $contextPage );
		}

		if ( $valueString !== false ) {
			$dataValue->setUserValue( $valueString, $caption );
		}

		return $dataValue;
	}

	/**
	 * Create a value for a data item.
	 *
	 * @param $dataItem DataItem
	 * @param $property mixed null or SMWDIProperty property object for which this value is made
	 * @param $caption mixed user-defined caption, or false if none given
	 *
	 * @return DataValue
	 */
	public function newDataValueByItem( DataItem $dataItem, DIProperty $property = null, $caption = false ) {

		if ( $property !== null ) {
			$typeId = $property->findPropertyTypeID();
		} else {
			$typeId = $this->dataTypeRegistry->getDefaultDataItemByType( $dataItem->getDiType() );
		}

		$dataValue = $this->newDataValueByType( $typeId, false, $caption, $property );
		$dataValue->setDataItem( $dataItem );

		if ( $caption !== false ) {
			$dataValue->setCaption( $caption );
		}

		return $dataValue;
	}

	/**
	 * Create a value for the given property, provided as an SMWDIProperty
	 * object. If no value is given, an empty container is created, the
	 * value of which can be set later on.
	 *
	 * @param $property SMWDIProperty property object for which this value is made
	 * @param $valueString mixed user value string, or false if unknown
	 * @param $caption mixed user-defined caption, or false if none given
	 * @param $contextPage SMWDIWikiPage that provides a context for parsing the value string, or null
	 *
	 * @return DataValue
	 */
	public function newDataValueByProperty( DIProperty $property, $valueString = false, $caption = false, $contextPage = null ) {

		$typeId = $property->isInverse() ? '_wpg' : $property->findPropertyTypeID();

		return $this->newDataValueByType( $typeId, $valueString, $caption, $property, $contextPage );
	}

	/**
	 * This factory method returns a data value object from a given property,
	 * value string. It is intended to be used on user input to allow to
	 * turn a property and value string into a data value object.
	 *
	 * @since 1.9
	 *
	 * @param string $propertyName property string
	 * @param string $valueString user value string
	 * @param mixed $caption user-defined caption
	 * @param SMWDIWikiPage|null $contextPage context for parsing the value string
	 *
	 * @return DataValue
	 */
	public function newDataValueByText( $propertyName, $valueString, $caption = false, DIWikiPage $contextPage = null ) {

		$propertyDV = $this->newPropertyValueByLabel( $propertyName, $caption, $contextPage );

		if ( !$propertyDV->isValid() ) {
			return $propertyDV;
		}

		if ( $propertyDV->isRestricted() ) {
			$dataValue = new ErrorValue(
				$propertyDV->getPropertyTypeID(),
				$propertyDV->getRestrictionError(),
				$valueString,
				$caption
			);

			if ( $propertyDV->getDataItem() instanceof DIProperty ) {
				$dataValue->setProperty( $propertyDV->getDataItem() );
			}

			return $dataValue;
		}

		$propertyDI = $propertyDV->getDataItem();

		if ( $propertyDI instanceof SMWDIError ) {
			return $propertyDV;
		}

		if ( $propertyDI instanceof DIProperty && !$propertyDI->isInverse() ) {
			$dataValue = $this->newDataValueByProperty(
				$propertyDI,
				$valueString,
				$caption,
				$contextPage
			);

			$dataValue->setProperty( $propertyDV->getDataItem() );

		} elseif ( $propertyDI instanceof DIProperty && $propertyDI->isInverse() ) {
			$dataValue = new ErrorValue( $propertyDV->getPropertyTypeID(),
				[ 'smw_noinvannot' ],
				$valueString,
				$caption
			);

			$dataValue->setProperty( $propertyDV->getDataItem() );
		} else {
			$dataValue = new ErrorValue(
				$propertyDV->getPropertyTypeID(),
				[ 'smw-property-name-invalid', $propertyName ],
				$valueString,
				$caption
			);

			$dataValue->setProperty( $propertyDV->getDataItem() );
		}

		if ( $dataValue->isValid() && !$dataValue->canUse() ) {
			$dataValue = new ErrorValue(
				$propertyDV->getPropertyTypeID(),
				[ 'smw-datavalue-restricted-use', implode( ',', $dataValue->getErrors() ) ],
				$valueString,
				$caption
			);

			$dataValue->setProperty( $propertyDV->getDataItem() );
		}

		return $dataValue;
	}

	/**
	 * @since 2.4
	 *
	 * @param string $propertyLabel
	 * @param string|false $caption
	 * @param DIWikiPage|null $contextPage
	 *
	 * @return DataValue
	 */
	public function newPropertyValueByLabel( $propertyLabel, $caption = false, DIWikiPage $contextPage = null ) {
		return $this->newDataValueByType( PropertyValue::TYPE_ID, $propertyLabel, $caption, null, $contextPage );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $typeid
	 * @param string|array $errormsg
	 * @param string $uservalue
	 * @param string $caption
	 *
	 * @return ErrorValue
	 */
	public function newErrorValue( $typeid, $errormsg = '', $uservalue = '', $caption = false ) {
		return new ErrorValue( $typeid, $errormsg, $uservalue, $caption );
	}

/// Deprecated methods

	/**
	 * @deprecated since 2.4, use DataValueFactory::newDataValueByItem
	 *
	 * @return DataValue
	 */
	public static function newDataItemValue( DataItem $dataItem, DIProperty $property = null, $caption = false ) {
		return self::getInstance()->newDataValueByItem( $dataItem, $property, $caption );
	}

	/**
	 * @deprecated since 2.4, use DataValueFactory::newDataValueByProperty
	 *
	 * @return DataValue
	 */
	public static function newPropertyObjectValue( DIProperty $property, $valueString = false, $caption = false, $contextPage = null ) {
		return self::getInstance()->newDataValueByProperty( $property, $valueString, $caption, $contextPage );
	}

	/**
	 * @deprecated since 2.4, use DataValueFactory::newDataValueByType
	 *
	 * @return DataValue
	 */
	public static function newTypeIdValue( $typeId, $valueString = false, $caption = false, DIProperty $property = null, $contextPage = null ) {
		return self::getInstance()->newDataValueByType( $typeId, $valueString, $caption, $property, $contextPage );
	}

	/**
	 * @deprecated since 2.4, use DataTypeRegistry::newDataValueByText
	 *
	 * @return DataValue
	 */
	public function newPropertyValue( $propertyName, $valueString, $caption = false, DIWikiPage $contextPage = null ) {
		return $this->newDataValueByText( $propertyName, $valueString, $caption, $contextPage );
	}

}