summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataModel/ContainerSemanticData.php
blob: 38288940d534a8a44ba606fb9895228a36c2ed96 (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
<?php

namespace SMW\DataModel;

use SMW\DIWikiPage;
use SMW\Exception\DataItemException;
use SMW\SemanticData;

/**
 * Subclass of SemanticData that is used to store the data in SMWDIContainer
 * objects. It is special since the subject that the stored property-value pairs
 * refer may or may not be specified explicitly. This can be tested with
 * hasAnonymousSubject(). When trying to access the subject in anonymous state,
 * an Exception will be thrown.
 *
 * Anonymous container data items are used when no
 * page context is available, e.g. when specifying such a value in a search form
 * where the parent page is not known.
 *
 * Besides this change, the subclass mainly is needed to restore the disabled
 * serialization of SemanticData.
 *
 * See also the documentation of SMWDIContainer.
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class ContainerSemanticData extends SemanticData {

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

	/**
	 * Construct a data container that refers to an anonymous subject. See
	 * the documentation of the class for details.
	 *
	 * @since 1.7
	 *
	 * @param boolean $noDuplicates stating if duplicate data should be avoided
	 */
	public static function makeAnonymousContainer( $noDuplicates = true, $skipAnonymousCheck = false ) {

		$containerSemanticData = new ContainerSemanticData(
			new DIWikiPage( 'SMWInternalObject', NS_SPECIAL, '', 'int' ),
			$noDuplicates
		);

		if ( $skipAnonymousCheck ) {
			$containerSemanticData->skipAnonymousCheck();
		}

		return $containerSemanticData;
	}

	/**
	 * Restore complete serialization which is disabled in SemanticData.
	 */
	public function __sleep() {
		return [
			'mSubject',
			'mProperties',
			'mPropVals',
			'mHasVisibleProps',
			'mHasVisibleSpecs',
			'mNoDuplicates',
			'skipAnonymousCheck',
			'subSemanticData',
			'options',
			'extensionData'
		];
	}

	/**
	 * Skip the check as it is required for some "search pattern match" activity
	 * to temporarily to access the container without raising an exception.
	 *
	 * @since 2.4
	 */
	public function skipAnonymousCheck() {
		$this->skipAnonymousCheck = true;
	}

	/**
	 * Check if the subject of this container is an anonymous object.
	 * See the documenation of the class for details.
	 *
	 * @return boolean
	 */
	public function hasAnonymousSubject() {

		if ( $this->mSubject->getNamespace() == NS_SPECIAL &&
		     $this->mSubject->getDBkey() == 'SMWInternalObject' &&
		     $this->mSubject->getInterwiki() === '' &&
		     $this->mSubject->getSubobjectName() === 'int' ) {
			return true;
		}

		return false;
	}

	/**
	 * Return subject to which the stored semantic annotation refer to, or
	 * throw an exception if the subject is anonymous (if the data has not
	 * been contextualized with setMasterPage() yet).
	 *
	 * @return DIWikiPage subject
	 * @throws DataItemException
	 */
	public function getSubject() {

		$error = "This container has been classified as anonymous and by trying to access" .
		" its subject (that has not been given any) an exception is raised to inform about" .
		" the incorrect usage. An anonymous container can only be used for a search pattern match.";

		if ( !$this->skipAnonymousCheck && $this->hasAnonymousSubject() ) {
			throw new DataItemException( $error );
		}

		return $this->mSubject;
	}

	/**
	 * Change the object to become an exact copy of the given
	 * SemanticData object. This is used to make other types of
	 * SemanticData into an SMWContainerSemanticData. To copy objects of
	 * the same type, PHP clone() should be used.
	 *
	 * @since 1.7
	 *
	 * @param SemanticData|null $semanticData
	 */
	public function copyDataFrom( SemanticData $semanticData = null ) {

		if ( $semanticData === null ) {
			return;
		}		
		
		$this->mSubject = $semanticData->getSubject();
		$this->mProperties = $semanticData->getProperties();
		$this->mPropVals = [];

		foreach ( $this->mProperties as $property ) {
			$this->mPropVals[$property->getKey()] = $semanticData->getPropertyValues( $property );
		}

		$this->mHasVisibleProps = $semanticData->hasVisibleProperties();
		$this->mHasVisibleSpecs = $semanticData->hasVisibleSpecialProperties();
		$this->mNoDuplicates = $semanticData->mNoDuplicates;
	}

}