1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @ingroup SMWDataItems |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
use SMW\Exception\DataItemException; |
7
|
|
|
use SMW\DIProperty; |
8
|
|
|
use SMWDIBlob as DIBlob; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class implements container data items that can store SMWSemanticData |
12
|
|
|
* objects. Containers are not dataitems in the proper sense: they do not |
13
|
|
|
* represent a single, opaque value that can be assigned to a property. Rather, |
14
|
|
|
* a container represents a "subobject" with a number of property-value |
15
|
|
|
* assignments. When a container is stored, these individual data assignments |
16
|
|
|
* are stored -- the data managed by SMW never contains any "container", just |
17
|
|
|
* individual property assignments for the subobject. Likewise, when a container |
18
|
|
|
* is used in search, it is interpreted as a patterns of possible property |
19
|
|
|
* assignments, and this pattern is searched for. |
20
|
|
|
* |
21
|
|
|
* The data encapsulated in a container data item is essentially an |
22
|
|
|
* SMWSemanticData object of class SMWContainerSemanticData. This class allows |
23
|
|
|
* the subject to be kept anonymous if not known (if no context page is |
24
|
|
|
* available for finding a suitable subobject name). See the repsective |
25
|
|
|
* documentation for details. |
26
|
|
|
* |
27
|
|
|
* Being a mere placeholder/template for other data, an SMWDIContainer is not |
28
|
|
|
* immutable as the other basic data items. New property-value pairs can always |
29
|
|
|
* be added to the internal SMWContainerSemanticData. |
30
|
|
|
* |
31
|
|
|
* @since 1.6 |
32
|
|
|
* |
33
|
|
|
* @author Markus Krötzsch |
34
|
|
|
* @ingroup SMWDataItems |
35
|
|
|
*/ |
36
|
|
|
class SMWDIContainer extends SMWDataItem { |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Internal value. |
40
|
|
|
* |
41
|
|
|
* @var SMWSemanticData |
42
|
|
|
*/ |
43
|
|
|
protected $m_semanticData; |
44
|
3 |
|
|
45
|
3 |
|
/** |
46
|
3 |
|
* Constructor. The given SMWContainerSemanticData object will be owned |
47
|
|
|
* by the constructed object afterwards, and in particular will not |
48
|
3 |
|
* allow further changes. |
49
|
|
|
* |
50
|
|
|
* @param $semanticData SMWContainerSemanticData |
51
|
|
|
*/ |
52
|
3 |
|
public function __construct( SMWContainerSemanticData $semanticData ) { |
53
|
|
|
$this->m_semanticData = $semanticData; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getDIType() { |
57
|
|
|
return SMWDataItem::TYPE_CONTAINER; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getSemanticData() { |
61
|
|
|
return $this->m_semanticData; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getSortKey() { |
65
|
|
|
return ''; |
66
|
3 |
|
} |
67
|
3 |
|
|
68
|
3 |
|
/** |
69
|
|
|
* @since 2.5 |
70
|
|
|
* |
71
|
|
|
* @param string $sortKey |
72
|
|
|
*/ |
73
|
|
|
public function addCompositeSortKey( $sortKey ) { |
74
|
|
|
$this->m_semanticData->addPropertyObjectValue( |
75
|
|
|
new DIProperty( '_SKEY' ), |
76
|
164 |
|
new DIBlob( $this->m_semanticData->getSubject()->getSortKey() . '#' . $sortKey ) |
77
|
164 |
|
); |
78
|
164 |
|
} |
79
|
164 |
|
|
80
|
164 |
|
public function getSerialization() { |
81
|
|
|
return serialize( $this->m_semanticData ); |
82
|
|
|
} |
83
|
164 |
|
|
84
|
|
|
/** |
85
|
|
|
* Get a hash string for this data item. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getHash() { |
90
|
|
|
|
91
|
|
|
$hash = $this->getValueHash( $this->m_semanticData ); |
92
|
|
|
sort( $hash ); |
93
|
|
|
|
94
|
164 |
|
return md5( implode( '#', $hash ) ); |
95
|
|
|
|
96
|
164 |
|
// We want a value hash, not an entity hash!! |
97
|
|
|
// return $this->m_semanticData->getHash(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
164 |
|
private function getValueHash( $semanticData ) { |
101
|
|
|
|
102
|
|
|
$hash = array(); |
103
|
|
|
|
104
|
|
|
foreach ( $semanticData->getProperties() as $property ) { |
105
|
|
|
$hash[] = $property->getKey(); |
106
|
|
|
|
107
|
|
|
foreach ( $semanticData->getPropertyValues( $property ) as $di ) { |
108
|
|
|
$hash[] = $di->getHash(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
foreach ( $semanticData->getSubSemanticData() as $data ) { |
113
|
21 |
|
$hash[] = $this->getValueHash( $data ); |
114
|
21 |
|
} |
115
|
21 |
|
|
116
|
21 |
|
return $hash; |
117
|
21 |
|
} |
118
|
21 |
|
|
119
|
|
|
/** |
120
|
21 |
|
* Create a data item from the provided serialization string and type |
121
|
21 |
|
* ID. |
122
|
21 |
|
* |
123
|
21 |
|
* @return SMWDIContainer |
124
|
|
|
*/ |
125
|
|
|
public static function doUnserialize( $serialization ) { |
126
|
|
|
/// TODO May issue an E_NOTICE when problems occur; catch this |
127
|
|
|
$data = unserialize( $serialization ); |
128
|
|
|
if ( !( $data instanceof SMWContainerSemanticData ) ) { |
|
|
|
|
129
|
|
|
throw new DataItemException( "Could not unserialize SMWDIContainer from the given string." ); |
130
|
|
|
} |
131
|
|
|
return new SMWDIContainer( $data ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function equals( SMWDataItem $di ) { |
135
|
|
|
if ( $di->getDIType() !== SMWDataItem::TYPE_CONTAINER ) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $di->getSerialization() === $this->getSerialization(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.