summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Deserializers/SemanticDataDeserializer.php
blob: 2e95894a139f92ff98924d4ac5789b5374a71b6c (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
<?php

namespace SMW\Deserializers;

use Deserializers\Deserializer;
use OutOfBoundsException;
use RuntimeException;
use SMW\DataTypeRegistry;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\SemanticData;
use SMWContainerSemanticData;
use SMWDataItem as DataItem;
use SMWDIContainer as DIContainer;
use SMWErrorValue as ErrorValue;

/**
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class SemanticDataDeserializer implements Deserializer {

	/**
	 * @var array
	 */
	private $dataItemTypeIdCache = [];

	/**
	 * @see Deserializers::deserialize
	 *
	 * @since 1.9
	 *
	 * @return SemanticData
	 * @throws OutOfBoundsException
	 * @throws RuntimeException
	 */
	public function deserialize( $data ) {

		$semanticData = null;

		if ( isset( $data['version'] ) && $data['version'] !== 0.1 && $data['version'] !== 2 ) {
			throw new OutOfBoundsException( 'Serializer/Unserializer version does not match, please update your data' );
		}

		if ( isset( $data['subject'] ) ) {
			$semanticData = new SemanticData( DIWikiPage::doUnserialize( $data['subject'] ) );
		}

		if ( !$semanticData instanceof SemanticData ) {
			throw new RuntimeException( 'SemanticData could not be created probably due to a missing subject' );
		}

		$this->doDeserialize( $data, $semanticData );

		return $semanticData;
	}

	/**
	 * @return null
	 */
	private function doDeserialize( $data, &$semanticData ) {

		$property = null;

		if ( !isset( $data['data'] ) ) {
			return;
		}

		foreach ( $data['data'] as $values ) {

			if ( is_array( $values ) ) {

				foreach ( $values as $key => $value ) {

					/**
					 * @var DIProperty $property
					 */
					if ( $key === 'property' ) {
						$property = DIProperty::doUnserialize( $value );
					}

					/**
					 * @var DataItem
					 */
					if ( $key === 'dataitem' ) {
						foreach ( $value as $val ) {
							$this->doDeserializeDataItem( $property, $data, $val, $semanticData );
						}
					}
				}
			}
		}
	}

	/**
	 * @return DataItem
	 */
	private function doDeserializeDataItem( $property, $data, $value, $semanticData ) {

		$dataItem = null;

		if ( !is_array( $value ) ) {
			return;
		}

		$type = $this->getDataItemId( $property );

		// Verify that the current property type definition and the type of the
		// property during serialization do match, throw an error value to avoid any
		// exception during unserialization caused by the DataItem object due to a
		// mismatch of type definitions

		if ( $type === $value['type'] ) {
			$dataItem = DataItem::newFromSerialization( $value['type'], $value['item'] );
		} else {
			$dataItem = $property->getDiWikiPage();
			$property = new DIProperty( DIProperty::TYPE_ERROR );

			$semanticData->addError( [
				new ErrorValue( $type, 'type mismatch', $property->getLabel() )
			] );

		}

		// Check whether the current dataItem has a subobject reference
		if ( $dataItem->getDIType() === DataItem::TYPE_WIKIPAGE && $dataItem->getSubobjectName() !== '' ) {

			$dataItem = $this->doDeserializeSubSemanticData(
				$data,
				$value['item'],
				new SMWContainerSemanticData( $dataItem )
			);

		}

		// Ensure that errors are collected from a subobject level as well and
		// made available at the top
		if ( $dataItem instanceof DIContainer ) {
			$semanticData->addError( $dataItem->getSemanticData()->getErrors() );
		}

		if ( $property !== null && $dataItem !== null ) {
			$semanticData->addPropertyObjectValue( $property, $dataItem );
		}

	}

	/**
	 * Resolves properties and dataitems assigned to a subobject recursively
	 *
	 * @note The serializer has to make sure to provide a complete data set
	 * otherwise the subobject is neglected (of course one could set an error
	 * value to the DIContainer but as of now that seems unnecessary)
	 *
	 * @return DIContainer|null
	 */
	private function doDeserializeSubSemanticData( $data, $id, $semanticData ) {

		if ( !isset( $data['sobj'] ) ) {
			return new DIContainer( $semanticData );;
		}

		foreach ( $data['sobj'] as $subobject ) {
			if ( isset( $subobject['subject'] ) && $subobject['subject'] === $id && isset( $subobject['data'] ) ) {
				$this->doDeserialize( $subobject, $semanticData );
			}
		}

		return new DIContainer( $semanticData );
	}

	/**
	 * Returns DataItemId for a property
	 *
	 * @note findPropertyTypeID is calling the Store to find the
	 * typeId reference this is costly but at the moment there is no other
	 * way to determine the typeId
	 *
	 * This check is to ensure that during unserialization the correct item
	 * in terms of its definition is being sought otherwise inconsistencies
	 * can occur due to type changes of a property between the time of
	 * the serialization and the deserialization (e.g for when the
	 * serialization object is stored in cache, DB etc.)
	 *
	 * @return integer
	 */
	private function getDataItemId( DIProperty $property ) {

		if ( !isset( $this->dataItemTypeIdCache[$property->getKey()] ) ) {
			$this->dataItemTypeIdCache[$property->getKey()] = DataTypeRegistry::getInstance()->getDataItemId( $property->findPropertyTypeID() );
		}

		return $this->dataItemTypeIdCache[$property->getKey()];
	}

}