summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/PropertyTableDefinition.php
blob: 9cb77c8460b90707b33413fb10f680d5968649a1 (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
<?php

namespace SMW\SQLStore;

use OutOfBoundsException;

/**
 * Simple data container for storing information about property tables. A
 * property table is a DB table that is used to store subject-property-value
 * records about data in SMW. Tables mostly differ in the composition of the
 * value, but also in whether the property is explicitly named (or fixed),
 * and in the way subject pages are referred to.
 *
 *
 * @license GNU GPL v2+
 * @since 1.8
 *
 * @author Nischay Nahata
 * @author Markus Krötzsch
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class PropertyTableDefinition {

	/**
	 * Name of the table in the DB.
	 *
	 * @since 1.8
	 * @var string
	 */
	protected $name;

	/**
	 * DIType of this table.
	 *
	 * @since 1.8
	 * @var integer
	 */
	protected $diType;

	/**
	 * If the table is only for one property, this field holds its key.
	 * Empty otherwise. Tables without a fixed property have a column "p_id"
	 * for storing the SMW page id of the property.
	 *
	 * @note It is important that this is the DB key form or special
	 * property key, not the label. This is not checked eagerly in SMW but
	 * can lead to spurious errors when properties are compared to each
	 * other or to the contents of the store.
	 *
	 * @since 1.8
	 * @var string|boolean false
	 */
	protected $fixedProperty;

	/**
	 * Boolean that states how subjects are stored. If true, a column "s_id"
	 * with an SMW page id is used. If false, two columns "s_title" and
	 * "s_namespace" are used. The latter de-normalized form cannot store
	 * sortkeys and interwiki prefixes, and is used only for the redirect
	 * table. New tables should really keep the default "true" here.
	 *
	 * @since 1.8
	 * @var boolean
	 */
	protected $idSubject = true;

	/**
	* Factory method to create an instance for a given
	* DI type and the given table name.
	*
	* @since 1.8
	*
	* @param integer $DIType constant
	* @param string $tableName logocal table name (not the DB version)
	* @param string|false $fixedProperty property key if any
	*/
	public function __construct( $DIType, $tableName, $fixedProperty = false ) {
		$this->name = $tableName;
		$this->fixedProperty = $fixedProperty;
		$this->diType = $DIType;
	}

	/**
	* Method to return the fields for this table
	*
	* @since 1.8
	*
	* @param SQLStore $store
	*
	* @return array
	*/
	public function getFields( SQLStore $store ) {
		$diHandler = $store->getDataItemHandlerForDIType( $this->diType );
		return $diHandler->getTableFields();
	}

	/**
	 * @see $idSubject
	 *
	 * @since 1.8
	 *
	 * @return boolean
	 */
	public function usesIdSubject() {
		return $this->idSubject;
	}

	/**
	 * @see $idSubject
	 *
	 * @param $usesIdSubject
	 *
	 * @since 1.8
	 */
	public function setUsesIdSubject( $usesIdSubject ) {
		$this->idSubject = $usesIdSubject;
	}

	/**
	 * Returns the name of the fixed property which this table is for.
	 * Throws an exception when called on a table not for any fixed
	 * property, so call @see isFixedPropertyTable first when appropriate.
	 *
	 * @see $fixedProperty
	 *
	 * @since 1.8
	 *
	 * @return string
	 * @throws OutOfBoundsException
	 */
	public function getFixedProperty() {

		if ( $this->fixedProperty === false ) {
			throw new OutOfBoundsException( 'Attempt to get the fixed property from a table that does not hold one' );
		}

		return $this->fixedProperty;
	}

	/**
	 * Returns if the table holds a fixed property or is a general table.
	 *
	 * @see $fixedProperty
	 *
	 * @since 1.8
	 *
	 * @return boolean
	 */
	public function isFixedPropertyTable() {
		return $this->fixedProperty !== false;
	}

	/**
	 * Returns the name of the table in the database.
	 *
	 * @since 1.8
	 *
	 * @return string
	 */
	public function getName() {
		return $this->name;
	}

	/**
	 * Returns @see $diType
	 *
	 * @since 1.8
	 *
	 * @return integer
	 */
	public function getDiType() {
		return $this->diType;
	}

}