summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/TableSchemaManager.php
blob: 16be15ca0a0294b7d2b8ba4cd36398166c6cd1a5 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php

namespace SMW\SQLStore;

use SMW\SQLStore\TableBuilder\FieldType;
use SMW\SQLStore\TableBuilder\Table;
use SMWDataItem as DataItem;

/**
 * @private
 *
 * Database type agnostic table/schema definition manager
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class TableSchemaManager {

	/**
	 * @var SQLStore
	 */
	private $store;

	/**
	 * @var MessageReporter
	 */
	private $messageReporter;

	/**
	 * @var Table[]
	 */
	private $tables = [];

	/**
	 * @var integer
	 */
	private $featureFlags = false;

	/**
	 * @since 2.5
	 *
	 * @param SQLStore $store
	 */
	public function __construct( SQLStore $store ) {
		$this->store = $store;
	}

	/**
	 * @since 2.5
	 *
	 * @return string
	 */
	public function getHash() {

		$hash = [];

		foreach ( $this->getTables() as $table ) {
			$hash[$table->getName()] = $table->getHash();
		}

		// Avoid by-chance sorting with an eventual differing hash
		sort( $hash );

		return md5( json_encode( $hash ) );
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $featureFlags
	 */
	public function setFeatureFlags( $featureFlags ) {
		$this->featureFlags = $featureFlags;
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $feature
	 *
	 * @return boolean
	 */
	public function hasFeatureFlag( $feature ) {
		return ( (int)$this->featureFlags & $feature ) != 0;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $tableName
	 *
	 * @return Table|null
	 */
	public function findTable( $tableName ) {

		foreach ( $this->getTables() as $table ) {
			if ( $table->getName() === $tableName ) {
				return $table;
			}
		}

		return null;
	}

	/**
	 * @since 2.5
	 *
	 * @return Table[]
	 */
	public function getTables() {

		if ( $this->tables !== [] ) {
			return $this->tables;
		}

		$this->addTable( $this->newEntityIdTable() );
		$this->addTable( $this->newConceptCacheTable() );
		$this->addTable( $this->newQueryLinksTable() );
		$this->addTable( $this->newFulltextSearchTable() );
		$this->addTable( $this->newPropertyStatisticsTable() );

		foreach ( $this->store->getPropertyTables() as $propertyTable ) {

			// Only extensions that aren't setup correctly can force an exception
			// and to avoid a failure during setup, ensure that standard tables
			// are correctly initialized otherwise SMW can't recover
			try {
				$diHandler = $this->store->getDataItemHandlerForDIType( $propertyTable->getDiType() );
			} catch ( \Exception $e ) {
				continue;
			}

			$this->addTable( $this->newPropertyTable( $propertyTable, $diHandler ) );
		}

		return $this->tables;
	}

	private function newEntityIdTable() {

		// ID_TABLE
		$table = new Table( SQLStore::ID_TABLE );

		$table->addColumn( 'smw_id', FieldType::FIELD_ID_PRIMARY );
		$table->addColumn( 'smw_namespace', [ FieldType::FIELD_NAMESPACE, 'NOT NULL' ] );
		$table->addColumn( 'smw_title', [ FieldType::FIELD_TITLE, 'NOT NULL' ] );
		$table->addColumn( 'smw_iw', [ FieldType::FIELD_INTERWIKI, 'NOT NULL' ] );
		$table->addColumn( 'smw_subobject', [ FieldType::FIELD_TITLE, 'NOT NULL' ] );

		$table->addColumn( 'smw_sortkey', [
			$this->hasFeatureFlag( SMW_FIELDT_CHAR_NOCASE ) ? FieldType::TYPE_CHAR_NOCASE : FieldType::FIELD_TITLE,
			'NOT NULL'
		] );

		$table->addColumn( 'smw_sort', [ FieldType::FIELD_TITLE ] );
		$table->addColumn( 'smw_proptable_hash', FieldType::TYPE_BLOB );
		$table->addColumn( 'smw_hash', FieldType::FIELD_HASH );
		$table->addColumn( 'smw_rev', FieldType::FIELD_ID_UNSIGNED );

		$table->addIndex( 'smw_id' );
		$table->addIndex( 'smw_id,smw_sortkey' );
		$table->addIndex( 'smw_hash,smw_id' );

		// IW match lookup
		$table->addIndex( 'smw_iw' );
		$table->addIndex( 'smw_iw,smw_id' );

		// ID lookup
		$table->addIndex( 'smw_title,smw_namespace,smw_iw,smw_subobject' );

		// InProperty lookup
		// $table->addIndex( 'smw_iw,smw_id,smw_title,smw_sortkey,smw_sort' );

		// Select by sortkey (range queries)
		$table->addIndex( 'smw_sortkey' );

		// Sort related indices, Store::getPropertySubjects (GROUP BY)
		// $table->addIndex( 'smw_sort' );
		$table->addIndex( 'smw_sort,smw_id' );

		// API smwbrowse primary lookup
		// SMW\MediaWiki\Api\Browse\ListLookup::fetchFromTable
		$table->addIndex( 'smw_namespace,smw_sortkey' );

		// Interfered with the API lookup index, couldn't find a use case
		// that would require the this index
		// $table->addIndex( 'smw_sort,smw_id,smw_iw' );

		$table->addIndex( 'smw_rev,smw_id' );

		return $table;
	}

	private function newConceptCacheTable() {

		// CONCEPT_CACHE_TABLE (member elements (s)->concepts (o) )
		$table = new Table( SQLStore::CONCEPT_CACHE_TABLE );

		$table->addColumn( 's_id', [ FieldType::FIELD_ID, 'NOT NULL' ] );
		$table->addColumn( 'o_id', [ FieldType::FIELD_ID, 'NOT NULL' ] );

		$table->addIndex( 'o_id' );

		return $table;
	}

	private function newQueryLinksTable() {

		// QUERY_LINKS_TABLE
		$table = new Table( SQLStore::QUERY_LINKS_TABLE );

		$table->addColumn( 's_id', [ FieldType::FIELD_ID, 'NOT NULL' ] );
		$table->addColumn( 'o_id', [ FieldType::FIELD_ID, 'NOT NULL' ] );

		$table->addIndex( 's_id' );
		$table->addIndex( 'o_id' );
		$table->addIndex( 's_id,o_id' );

		return $table;
	}

	private function newFulltextSearchTable() {

		// FT_SEARCH_TABLE
		// TEXT and BLOB is stored off the table with the table just having a pointer
		// VARCHAR is stored inline with the table
		$table = new Table( SQLStore::FT_SEARCH_TABLE );

		$table->addColumn( 's_id', [ FieldType::FIELD_ID, 'NOT NULL' ] );
		$table->addColumn( 'p_id', [ FieldType::FIELD_ID, 'NOT NULL' ] );
		$table->addColumn( 'o_text', FieldType::TYPE_TEXT );
		$table->addColumn( 'o_sort', FieldType::FIELD_TITLE );

		$table->addIndex( 's_id' );
		$table->addIndex( 'p_id' );
		$table->addIndex( 'o_sort' );
		$table->addIndex( [ 'o_text', 'FULLTEXT' ] );

		$table->addOption(
			'fulltextSearchTableOptions',
			$GLOBALS['smwgFulltextSearchTableOptions']
		);

		return $table;
	}

	private function newPropertyStatisticsTable() {

		// PROPERTY_STATISTICS_TABLE
		$table = new Table( SQLStore::PROPERTY_STATISTICS_TABLE );

		$table->addColumn( 'p_id', FieldType::FIELD_ID );
		$table->addColumn( 'usage_count', FieldType::FIELD_USAGE_COUNT );
		$table->addColumn( 'null_count', FieldType::FIELD_USAGE_COUNT );

		$table->addDefault( 'usage_count', 0 );
		$table->addDefault( 'null_count', 0 );

		$table->addIndex( [ 'p_id', 'UNIQUE INDEX' ] );
		$table->addIndex( 'usage_count' );
		$table->addIndex( 'null_count' );

		return $table;
	}

	private function newPropertyTable( $propertyTable, $diHandler ) {

		// Prepare indexes. By default, property-value tables
		// have the following indexes:
		//
		// sp: getPropertyValues(), getSemanticData(), getProperties()
		// po: ask, getPropertySubjects()
		//
		// The "p" component is omitted for tables with fixed property.
		$indexes = [];
		if ( $propertyTable->usesIdSubject() ) {
			$fieldarray = [
				's_id' => [ FieldType::FIELD_ID, 'NOT NULL' ]
			];

			$indexes['sp'] = 's_id';
		} else {
			$fieldarray = [
				's_title' => [ FieldType::FIELD_TITLE, 'NOT NULL' ],
				's_namespace' => [ FieldType::FIELD_NAMESPACE, 'NOT NULL' ]
			];

			$indexes['sp'] = 's_title,s_namespace';
		}

		$indexes['po'] = $diHandler->getIndexField();

		if ( !$propertyTable->isFixedPropertyTable() ) {
			$fieldarray['p_id'] = [ FieldType::FIELD_ID, 'NOT NULL' ];
			$indexes['sp'] = $indexes['sp'] . ',p_id';
		}

		// TODO Special handling; concepts should be handled differently
		// in the future. See comments in SMW_DIHandler_Concept.php.
		if ( $propertyTable->getDiType() === DataItem::TYPE_CONCEPT ) {
			unset( $indexes['po'] );
		}

		foreach ( $diHandler->getTableIndexes() as $value ) {

			if ( strpos( $value, 'p_id' ) !== false && $propertyTable->isFixedPropertyTable() ) {
				continue;
			}

			if ( strpos( $value, 'o_id' ) !== false && !$propertyTable->usesIdSubject() ) {
				continue;
			}

			if ( strpos( $value, 's_id' ) !== false && !$propertyTable->usesIdSubject() ) {
				continue;
			}

			$indexes = array_merge( $indexes, [ $value ] );
		}

		$indexes = array_unique( $indexes );

		foreach ( $diHandler->getTableFields() as $fieldname => $fieldType ) {
			$fieldarray[$fieldname] = $fieldType;
		}

		$table = new Table( $propertyTable->getName() );

		foreach ( $fieldarray as $fieldName => $fieldType ) {
			$table->addColumn( $fieldName, $fieldType );
		}

		foreach ( $indexes as $key => $index ) {
			$table->addIndex( $index, $key );
		}

		return $table;
	}

	private function addTable( Table $table ) {
		$this->tables[] = $table;
	}

}