summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/ReferenceValue.php
blob: cbcb4208c338532f7b2dfd5e58c2b82371a27405 (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
<?php

namespace SMW\DataValues;

use SMW\ApplicationFactory;
use SMW\DataModel\ContainerSemanticData;
use SMW\DataValueFactory;
use SMW\DataValues\ValueFormatters\DataValueFormatter;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\Message;
use SMWDataItem as DataItem;
use SMWDIContainer as DIContainer;
use SMWDITime as DITime;

/**
 * ReferenceValue allows to define additional DV to describe the state of a
 * SourceValue in terms of provenance or referential evidence. ReferenceValue is
 * stored as separate entity to the original subject in order to encapsulate the
 * SourceValue from the remaining annotations with reference to a subject.
 *
 * Defining which fields are required can vary and therefore is left to the user
 * to specify such requirements using the `'Has fields' property.
 *
 * For example, declaring `[[Has fields::SomeValue;Date;SomeUrl;...]]` on a
 * `SomeProperty` property page is to define:
 *
 * - a property called `SomeValue` with its own specification
 * - a date property with the Date type
 * - a property called `SomeUrl` with its own specification
 * - ... any other property the users expects to require when making a value
 *   annotation of this type
 *
 * An annotation like `[[SomeProperty::Foo;12-12-1212;http://example.org]]` is
 * expected to be a concatenated string and to be separated by ';' to indicate
 * the next value string and will corespondent to the index of the `Has fields`
 * declaration.
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class ReferenceValue extends AbstractMultiValue {

	/**
	 * DV identifier
	 */
	const TYPE_ID = '_ref_rec';

	/**
	 * @var DIProperty[]|null
	 */
	private $properties = null;

	/**
	 * @param string $typeid
	 */
	public function __construct( $typeid = '' ) {
		parent::__construct( self::TYPE_ID );
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function setFieldProperties( array $properties ) {
		foreach ( $properties as $property ) {
			if ( $property instanceof DIProperty ) {
				$this->properties[] = $property;
			}
		}
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getProperties() {
		return $this->properties;
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getValuesFromString( $value ) {
		// #664 / T17732
		$value = str_replace( "\;", "-3B", $value );

		// Bug 21926 / T23926
		// Values that use html entities are encoded with a semicolon
		$value = htmlspecialchars_decode( $value, ENT_QUOTES );
		$values = preg_split( '/[\s]*;[\s]*/u', trim( $value ) );

		return str_replace( "-3B", ";", $values );
	}

	/**
	 * @see DataValue::getShortWikiText
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getShortWikiText( $linker = null ) {
		return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::WIKI_SHORT, $linker );
	}

	/**
	 * @see DataValue::getShortHTMLText
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getShortHTMLText( $linker = null ) {
		return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::HTML_SHORT, $linker );
	}

	/**
	 * @see DataValue::getLongWikiText
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getLongWikiText( $linker = null ) {
		return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::WIKI_LONG, $linker );
	}

	/**
	 * @see DataValue::getLongHTMLText
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getLongHTMLText( $linker = null ) {
		return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::HTML_LONG, $linker );
	}

	/**
	 * @see DataValue::getWikiValue
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getWikiValue() {
		return $this->dataValueServiceFactory->getValueFormatter( $this )->format( DataValueFormatter::VALUE );
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getPropertyDataItems() {

		if ( $this->properties === null ) {
			$this->properties = $this->getFieldProperties( $this->getProperty() );

			if ( count( $this->properties ) == 0 ) {
				$this->addErrorMsg( [ 'smw-datavalue-reference-invalid-fields-definition' ], Message::PARSE );
			}
		}

		return $this->properties;
	}

	/**
	 * @since 2.5
	 *
	 * {@inheritDoc}
	 */
	public function getDataItems() {
		return parent::getDataItems();
	}

	/**
	 * @note called by DataValue::setUserValue
	 * @see DataValue::parseUserValue
	 *
	 * {@inheritDoc}
	 */
	protected function parseUserValue( $value ) {

		if ( $value === '' ) {
			$this->addErrorMsg( [ 'smw_novalues' ] );
			return;
		}

		$containerSemanticData = $this->newContainerSemanticData( $value );
		$sortKeys = [];

		$values = $this->getValuesFromString( $value );
		$index = 0; // index in value array

		$propertyIndex = 0; // index in property list
		$empty = true;

		foreach ( $this->getPropertyDataItems() as $property ) {

			if ( !array_key_exists( $index, $values ) || $this->getErrors() !== [] ) {
				break; // stop if there are no values left
			}

			// generating the DVs:
			if ( ( $values[$index] === '' ) || ( $values[$index] == '?' ) ) { // explicit omission
				$index++;
			} else {
				$dataValue = DataValueFactory::getInstance()->newDataValueByProperty(
					$property,
					$values[$index],
					false,
					$containerSemanticData->getSubject()
				);

				if ( $dataValue->isValid() ) { // valid DV: keep
					$dataItem = $dataValue->getDataItem();

					$containerSemanticData->addPropertyObjectValue(
						$property,
						$dataItem
					);

					// Chronological order determined first
					if ( $dataItem instanceof DITime ) {
						array_unshift( $sortKeys, $dataItem->getSortKey() );
					} else {
						$sortKeys[] = $dataItem->getSortKey();
					}

					$index++;
					$empty = false;
				} elseif ( $index == 0 || ( count( $values ) - $index ) == ( count( $this->properties ) - $propertyIndex ) ) {
					$containerSemanticData->addPropertyObjectValue( $property, $dataValue->getDataItem() );
					$this->addError( $dataValue->getErrors() );
					++$index;
				}
			}

			++$propertyIndex;
		}

		if ( $empty && $this->getErrors() === []  ) {
			$this->addErrorMsg( [ 'smw_novalues' ] );
		}

		// Remember the data to extend the sortkey
		$containerSemanticData->setExtensionData( 'sort.data', implode( ';', $sortKeys ) );

		$this->m_dataitem = new DIContainer( $containerSemanticData );
	}

	/**
	 * @see DataValue::loadDataItem
	 */
	protected function loadDataItem( DataItem $dataItem ) {

		if ( $dataItem->getDIType() === DataItem::TYPE_CONTAINER ) {
			$this->m_dataitem = $dataItem;
			return true;
		} elseif ( $dataItem->getDIType() === DataItem::TYPE_WIKIPAGE ) {
			$semanticData = new ContainerSemanticData( $dataItem );
			$semanticData->copyDataFrom( ApplicationFactory::getInstance()->getStore()->getSemanticData( $dataItem ) );
			$this->m_dataitem = new DIContainer( $semanticData );
			return true;
		}

		return false;
	}

	private function newContainerSemanticData( $value ) {

		if ( $this->m_contextPage === null ) {
			$containerSemanticData = ContainerSemanticData::makeAnonymousContainer();
			$containerSemanticData->skipAnonymousCheck();
		} else {
			$subobjectName = '_REF' . md5( $value );

			$subject = new DIWikiPage(
				$this->m_contextPage->getDBkey(),
				$this->m_contextPage->getNamespace(),
				$this->m_contextPage->getInterwiki(),
				$subobjectName
			);

			$containerSemanticData = new ContainerSemanticData( $subject );
		}

		return $containerSemanticData;
	}

}