Completed
Push — master ( f2f6e8...470987 )
by mw
33:59
created

ContainerSemanticData::skipAnonymousCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\DataModel;
4
5
use RuntimeException;
6
use SMW\SemanticData;
7
use SMW\DIWikiPage;
8
use SMW\Exception\DataItemException;
9
10
/**
11
 * Subclass of SemanticData that is used to store the data in SMWDIContainer
12
 * objects. It is special since the subject that the stored property-value pairs
13
 * refer may or may not be specified explicitly. This can be tested with
14
 * hasAnonymousSubject(). When trying to access the subject in anonymous state,
15
 * an Exception will be thrown.
16
 *
17
 * Anonymous container data items are used when no
18
 * page context is available, e.g. when specifying such a value in a search form
19
 * where the parent page is not known.
20
 *
21
 * Besides this change, the subclass mainly is needed to restore the disabled
22
 * serialization of SemanticData.
23
 *
24
 * See also the documentation of SMWDIContainer.
25
 *
26
 * @license GNU GPL v2+
27
 * @since 1.6
28
 *
29
 * @author Markus Krötzsch
30
 * @author mwjames
31
 */
32
class ContainerSemanticData extends SemanticData {
33
34
	/**
35
	 * @var boolean
36
	 */
37
	private $skipAnonymousCheck = false;
38
39
	/**
40
	 * Construct a data container that refers to an anonymous subject. See
41
	 * the documentation of the class for details.
42
	 *
43
	 * @since 1.7
44
	 *
45
	 * @param boolean $noDuplicates stating if duplicate data should be avoided
46
	 */
47
	public static function makeAnonymousContainer( $noDuplicates = true, $skipAnonymousCheck = false ) {
48
49
		$containerSemanticData = new ContainerSemanticData(
50
			new DIWikiPage( 'SMWInternalObject', NS_SPECIAL, '', 'int' ),
51
			$noDuplicates
52
		);
53
54
		if ( $skipAnonymousCheck ) {
55
			$containerSemanticData->skipAnonymousCheck();
56
		}
57
58
		return $containerSemanticData;
59
	}
60
61
	/**
62
	 * Restore complete serialization which is disabled in SemanticData.
63
	 */
64
	public function __sleep() {
65
		return array(
66
			'mSubject',
67
			'mProperties',
68
			'mPropVals',
69
			'mHasVisibleProps',
70
			'mHasVisibleSpecs',
71
			'mNoDuplicates',
72
			'skipAnonymousCheck'
73
		);
74
	}
75
76
	/**
77
	 * Skip the check as it is required for some "search pattern match" activity
78
	 * to temporarily to access the container without raising an exception.
79
	 *
80
	 * @since 2.4
81
	 */
82
	public function skipAnonymousCheck() {
83
		$this->skipAnonymousCheck = true;
84
	}
85
86
	/**
87
	 * Check if the subject of this container is an anonymous object.
88
	 * See the documenation of the class for details.
89
	 *
90
	 * @return boolean
91
	 */
92
	public function hasAnonymousSubject() {
93
94
		if ( $this->mSubject->getNamespace() == NS_SPECIAL &&
95
		     $this->mSubject->getDBkey() == 'SMWInternalObject' &&
96
		     $this->mSubject->getInterwiki() === '' &&
97
		     $this->mSubject->getSubobjectName() === 'int' ) {
98
			return true;
99
		}
100
101
		return false;
102
	}
103
104
	/**
105
	 * Return subject to which the stored semantic annotation refer to, or
106
	 * throw an exception if the subject is anonymous (if the data has not
107
	 * been contextualized with setMasterPage() yet).
108
	 *
109
	 * @return DIWikiPage subject
110
	 * @throws DataItemException
111
	 */
112
	public function getSubject() {
113
114
		$error = "This container has been classified as anonymous and by trying to access" .
115
		" its subject (that has not been given any) an exception is raised to inform about" .
116
		" the incorrect usage. An anonymous container can only be used for a search pattern match.";
117
118
		if ( !$this->skipAnonymousCheck && $this->hasAnonymousSubject() ) {
119
			throw new DataItemException( $error );
120
		}
121
122
		return $this->mSubject;
123
	}
124
125
	/**
126
	 * Change the object to become an exact copy of the given
127
	 * SemanticData object. This is used to make other types of
128
	 * SemanticData into an SMWContainerSemanticData. To copy objects of
129
	 * the same type, PHP clone() should be used.
130
	 *
131
	 * @since 1.7
132
	 *
133
	 * @param $semanticData SemanticData object to copy from
134
	 */
135
	public function copyDataFrom( SemanticData $semanticData ) {
136
		$this->mSubject = $semanticData->getSubject();
137
		$this->mProperties = $semanticData->getProperties();
138
		$this->mPropVals = array();
139
140
		foreach ( $this->mProperties as $property ) {
141
			$this->mPropVals[$property->getKey()] = $semanticData->getPropertyValues( $property );
142
		}
143
144
		$this->mHasVisibleProps = $semanticData->hasVisibleProperties();
145
		$this->mHasVisibleSpecs = $semanticData->hasVisibleSpecialProperties();
146
		$this->mNoDuplicates = $semanticData->mNoDuplicates;
147
	}
148
149
}
150