summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/Subobject.php
blob: f1d129a6e385eb7972e7b9640e0a85ad591be11e (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
<?php

namespace SMW;

use InvalidArgumentException;
use SMW\Exception\SubSemanticDataException;
use SMWContainerSemanticData;
use SMWDataValue;
use SMWDIContainer;
use Title;

/**
 * @see http://www.semantic-mediawiki.org/wiki/Help:Subobject
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class Subobject {

	/**
	 * @var Title
	 */
	 protected $title;

	/**
	 * @var SMWContainerSemanticData
	 */
	 protected $semanticData;

	/**
	 * @var array
	 */
	protected $errors = [];

	/**
	 * @since 1.9
	 *
	 * @param Title $title
	 */
	public function __construct( Title $title ) {
		$this->title = $title;
	}

	/**
	 * Returns the Title object
	 *
	 * @since 1.9
	 *
	 * @return Title
	 */
	public function getTitle() {
		return $this->title;
	}

	/**
	 * @since 2.1
	 *
	 * @return DIWikiPage
	 */
	public function getSubject() {
		return $this->getSemanticData()->getSubject();
	}

	/**
	 * @since 2.1
	 *
	 * @return string
	 */
	public function getSubobjectId() {
		return $this->getSemanticData()->getSubject()->getSubobjectName();
	}

	/**
	 * @since 1.9
	 *
	 * @return array
	 */
	public function getErrors() {
		return $this->errors;
	}

	/**
	 * @since 1.9
	 *
	 * @param array|string $error
	 */
	public function addError( $error ) {

		if ( is_string( $error ) ) {
			$error = [ md5( $error ) => $error ];
		}

		// Preserve the keys, avoid using array_merge to avert a possible
		// Fatal error: Allowed memory size of ... bytes exhausted ... Subobject.php on line 89
		$this->errors += $error;
	}

	/**
	 * @since 2.0
	 *
	 * @param string $identifier
	 *
	 * @return self
	 * @throws InvalidArgumentException
	 */
	public function setEmptyContainerForId( $identifier ) {

		if ( $identifier === '' ) {
			throw new InvalidArgumentException( 'Expected a valid (non-empty) indentifier' );
		}

		$subWikiPage = new DIWikiPage(
			$this->title->getDBkey(),
			$this->title->getNamespace(),
			$this->title->getInterwiki(),
			$identifier
		);

		$this->semanticData = new SMWContainerSemanticData( $subWikiPage );

		return $this;
	}

	/**
	 * @deprecated since 2.0
	 */
	public function setSemanticData( $identifier ) {
		$this->setEmptyContainerForId( $identifier );
	}

	/**
	 * Returns semantic data container for a subobject
	 *
	 * @since 1.9
	 *
	 * @return SMWContainerSemanticData
	 */
	public function getSemanticData() {

		if ( !( $this->semanticData instanceof SMWContainerSemanticData ) ) {
			throw new SubSemanticDataException( 'The semantic data container is not initialized' );
		}

		return $this->semanticData;
	}

	/**
	 * Returns the property data item for the subobject
	 *
	 * @since 1.9
	 *
	 * @return DIProperty
	 */
	public function getProperty() {
		return new DIProperty( DIProperty::TYPE_SUBOBJECT );
	}

	/**
	 * Returns the container data item for the subobject
	 *
	 * @since 1.9
	 *
	 * @return SMWDIContainer
	 */
	public function getContainer() {
		return new SMWDIContainer( $this->getSemanticData() );
	}

	/**
	 * @since 1.9
	 *
	 * @param DataValue $dataValue
	 *
	 * @throws SubSemanticDataException
	 */
	public function addDataValue( SMWDataValue $dataValue ) {

		if ( !( $this->semanticData instanceof SMWContainerSemanticData ) ) {
			throw new SubSemanticDataException( 'The semantic data container is not initialized' );
		}

		$this->semanticData->addDataValue( $dataValue );
		$this->addError( $this->semanticData->getErrors() );
	}

}