summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/EntityStore/SemanticDataLookup.php
blob: ca1d95fa345be08080fc115585ea2671ce869e0a (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php

namespace SMW\SQLStore\EntityStore;

use Psr\Log\LoggerAwareTrait;
use RuntimeException;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\RequestOptions;
use SMW\SemanticData;
use SMW\SQLStore\PropertyTableDefinition;
use SMW\SQLStore\SQLStore;
use SMW\SQLStore\TableBuilder\FieldType;
use SMWDataItem as DataItem;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class SemanticDataLookup {

	use LoggerAwareTrait;

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

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

	/**
	 * @since 3.0
	 *
	 * @param PropertyTableDefinition $propertyTableDef
	 * @param RequestOptions|null $requestOptions
	 *
	 * @return RequestOptions|null
	 */
	public function newRequestOptions( PropertyTableDefinition $propertyTableDef, DIProperty $property, RequestOptions $requestOptions = null ) {

		if ( $requestOptions === null || !isset( $requestOptions->conditionConstraint ) ) {
			return null;
		}

		$ropts = new RequestOptions();

		$ropts->setLimit( $requestOptions->getLimit() );
		$ropts->setOffset( $requestOptions->getOffset() );

		if ( $propertyTableDef->isFixedPropertyTable() ) {
			return $ropts;
		}

		$pid = $this->store->getObjectIds()->getSMWPropertyID(
			$property
		);

		if ( $pid > 0 ) {
			$ropts->addExtraCondition( [ 'p_id' => $pid ] );
		}

		return $ropts;
	}

	/**
	 * @since 3.0
	 *
	 * @param DIWikiPage|SemanticData $object
	 *
	 * @return StubSemanticData
	 * @throws RuntimeException
	 */
	public function newStubSemanticData( $object ) {

		if ( $object instanceof DIWikiPage ) {
			return new StubSemanticData( $object, $this->store, false );
		}

		if ( $object instanceof SemanticData ) {
			return StubSemanticData::newFromSemanticData( $object, $this->store );
		}

		throw new RuntimeException( 'Expectd either a DIWikiPage or SemanticData object!' );
	}

	/**
	 * @since 3.0
	 *
	 * @param SemanticData $semanticData
	 *
	 * @return array
	 */
	public function getTableUsageInfo( SemanticData $semanticData ) {
		$state = [];

		foreach ( $semanticData->getProperties() as $property ) {
			$state[$this->store->findPropertyTableID( $property )] = true;
		}

		return $state;
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $id
	 * @param DataItem $dataItem
	 * @param PropertyTableDefinition $propTable
	 * @param RequestOptions $requestOptions
	 *
	 * @return SemanticData
	 */
	public function getSemanticData( $id, DataItem $dataItem = null, PropertyTableDefinition $propTable, RequestOptions $requestOptions = null ) {

		if ( !$dataItem instanceof DIWikiPage ) {
			throw new RuntimeException( 'Expected a DIWikiPage instance' );
		}

		$stubSemanticData = $this->newStubSemanticData( $dataItem );

		$data = $this->fetchSemanticData(
			$id,
			$dataItem,
			$propTable,
			$requestOptions
		);

		foreach ( $data as $d ) {
			$stubSemanticData->addPropertyStubValue( reset( $d ), end( $d ) );
		}

		return $stubSemanticData;
	}

	/**
	 * Helper function for reading all data for from a given property table
	 * (specified by an SMWSQLStore3Table dataItem), based on certain
	 * restrictions. The function can filter data based on the subject (1)
	 * or on the property it belongs to (2) -- but one of those must be
	 * done. The Boolean $issubject is true for (1) and false for (2).
	 *
	 * In case (1), the first two parameters are taken to refer to a
	 * subject; in case (2) they are taken to refer to a property. In any
	 * case, the retrieval is limited to the specified $proptable. The
	 * parameters are an internal $id (of a subject or property), and an
	 * $dataItem (being an DIWikiPage or SMWDIProperty). Moreover, when
	 * filtering by property, it is assumed that the given $proptable
	 * belongs to the property: if it is a table with fixed property, it
	 * will not be checked that this is the same property as the one that
	 * was given in $dataItem.
	 *
	 * In case (1), the result in general is an array of pairs (arrays of
	 * size 2) consisting of a property key (string), and DB keys (array if
	 * many, string if one) from which a datvalue dataItem for this value can
	 * be built. It is possible that some of the DB keys are based on
	 * internal dataItems; these will be represented by similar result arrays
	 * of (recursive calls of) fetchSemanticData().
	 *
	 * In case (2), the result is simply an array of DB keys (array)
	 * without the property keys. Container dataItems will be encoded with
	 * nested arrays like in case (1).
	 *
	 * @param integer $id
	 * @param DataItem $dataItem
	 * @param PropertyTableDefinition $propTable
	 * @param RequestOptions $requestOptions
	 *
	 * @return array
	 */
	public function fetchSemanticData( $id, DataItem $dataItem = null, PropertyTableDefinition $propTable, RequestOptions $requestOptions = null ) {

		$isSubject = $dataItem instanceof DIWikiPage || $dataItem === null;

		// stop if there is not enough data:
		// properties always need to be given as dataItem,
		// subjects at least if !$proptable->idsubject
		if ( ( $id == 0 ) ||
			( $dataItem === null && ( !$isSubject || !$propTable->usesIdSubject() ) ) ||
			( $propTable->getDIType() === null ) ) {
			return [];
		}

		$result = [];
		$connection = $this->store->getConnection( 'mw.db' );

		// Build something like:
		//
		// SELECT o_id AS id0,o0.smw_title AS v0,o0.smw_namespace AS v1,o0.smw_iw
		// AS v2,o0.smw_sortkey AS v3,o0.smw_subobject AS v4
		// FROM `smw_fpt_sobj`
		// INNER JOIN `smw_object_ids` AS o0 ON o_id=o0.smw_id
		// WHERE s_id='852'
		// LIMIT 4
		//
		// or
		//
		// SELECT p.smw_title as prop,o_blob AS v0,o_hash AS v1 FROM `smw_di_blob`
		// INNER JOIN `smw_object_ids` AS p ON p_id=p.smw_id
		// WHERE s_id='80' AND p.smw_iw!=':smw' AND p.smw_iw!=':smw-delete'

		$query = $this->newQuery(
			$propTable,
			$id,
			$isSubject,
			$dataItem
		);

		if ( $requestOptions !== null ) {
			foreach ( $requestOptions->getExtraConditions() as $extraCondition ) {
				if ( isset( $extraCondition['p_id'] ) ) {
					$query->condition( $query->eq( 'p_id', $extraCondition['p_id'] ) );
				}
			}
		} else {
			$requestOptions = new RequestOptions();
		}

		$valueCount = 0;
		$fieldname = '';

		$diHandler = $this->store->getDataItemHandlerForDIType(
			$propTable->getDiType()
		);

		$valueField = $diHandler->getIndexField();
		$labelField = $diHandler->getLabelField();

		$fields = $diHandler->getFetchFields();

		$this->addFields(
			$query,
			$fields,
			$valueField,
			$labelField,
			$valueCount,
			$fieldname
		);

		// Don't use DISTINCT for subject related value match but make sure
		// (#3531) it is used when requesting other values in order to retrieve
		// all available unique values within the range of the limit
		if ( !$isSubject ) {
			$requestOptions->setOption( 'DISTINCT', true );

			// Don't sort, this avoids a SQL `filesort`/`temporary table` usage
			// in combination with DISTINCT, values will be listed as-is instead
			// of a lexical representation but can be compensated by selecting a
			// wider range in case this is used as retrieving "all" values
			// for a property

			// SELECT DISTINCT o_id AS id0, o0.smw_title AS v0, o0.smw_namespace
			// AS v1, o0.smw_iw AS v2, o0.smw_sortkey AS v3, o0.smw_subobject AS
			// v4 FROM `smw_di_wikipage` INNER JOIN `smw_object_ids` AS o0 ON
			// o_id=o0.smw_id WHERE (p_id='x') LIMIT 51
			//
			// 8.6281ms
			//
			// vs.
			//
			// SELECT DISTINCT o_id AS id0, o0.smw_title AS v0, o0.smw_namespace
			// AS v1, o0.smw_iw AS v2, o0.smw_sortkey AS v3, o0.smw_subobject AS
			// v4 FROM `smw_di_wikipage` INNER JOIN `smw_object_ids` AS o0 ON
			// o_id=o0.smw_id WHERE (p_id='x') ORDER BY o_id LIMIT 51
			//
			// 24189.0128ms
			//
			// PS: In case of a `TYPE_WIKIPAGE` entity, sorting by `o_id`
			// wouldn't make much sense as it does not guarantee any lexical order
			$requestOptions->setOption( 'ORDER BY', false );
		}

 		// Apply sorting/string matching; only with given property
		if ( !$isSubject ) {
			$conds = $this->store->getSQLConditions(
				$requestOptions,
				$valueField,
				$labelField,
				$query->hasCondition()
			);

			$query->condition( $conds );
		} else {
			$valueField = '';
		}

		$query->options(
			$this->store->getSQLOptions( $requestOptions, $valueField )
		);

		$res = $connection->query(
			$query,
			__METHOD__
		);

		foreach ( $res as $row ) {
			$propertykey = '';

			// use joined or predefined property name
			if ( $isSubject ) {
				$propertykey = $propTable->isFixedPropertyTable() ? $propTable->getFixedProperty() : $row->prop;
			}

			$this->resultFromRow(
				$result,
				$row,
				$fields,
				$fieldname,
				$valueCount,
				$isSubject,
				$propertykey
			);
		}

		$connection->freeResult( $res );

		// Sorting via PHP for an explicit disabled `ORDER BY` to ensure that
		// the result set has at least a lexical order applied for the range of
		// retrieved values
		if ( $requestOptions->getOption( 'ORDER BY' ) === false ) {
			sort( $result );
		}

		return $result;
	}

	private function newQuery( $propTable, $id, $isSubject, $dataItem ) {

		$connection = $this->store->getConnection( 'mw.db' );
		$query = $connection->newQuery();

		$query->type( 'select' );
		$query->table( $propTable->getName() );

		// Restrict property only
		if ( !$isSubject && !$propTable->isFixedPropertyTable() ) {
			$query->condition( $query->eq( 'p_id', $id ) );
		}

		// Restrict subject, select property
		if ( $isSubject && $propTable->usesIdSubject() ) {
			$query->condition( $query->eq( 's_id', $id ) );
		} elseif ( $isSubject ) {
			$query->condition( $query->eq( 's_title', $dataItem->getDBkey() ) );
			$query->condition( $query->eq( 's_namespace', $dataItem->getNamespace() ) );
		}

		// Select property name
		// In case of a fixed property, no select needed
		if ( $isSubject && !$propTable->isFixedPropertyTable() ) {
			$query->join(
				'INNER JOIN',
				[ SQLStore::ID_TABLE => 'p ON p_id=p.smw_id' ]
			);

			$query->field( 'p.smw_title', 'prop' );

			// Avoid displaying any property that has been marked deleted or outdated
			$query->condition( $query->neq( "p.smw_iw", SMW_SQL3_SMWIW_OUTDATED ) );
			$query->condition( $query->neq( "p.smw_iw", SMW_SQL3_SMWDELETEIW ) );
		}

		return $query;
	}

	private function addFields( &$query, $fields, $valueField, $labelField, &$valueCount, &$fieldname ) {

		// Select dataItem column(s)
		foreach ( $fields as $fieldname => $fieldType ) {

			 // Get data from ID table
			if ( $fieldType === FieldType::FIELD_ID ) {
				$query->join(
					'INNER JOIN',
					[ SQLStore::ID_TABLE => "o$valueCount ON $fieldname=o$valueCount.smw_id" ]
				);

				$query->field( "$fieldname AS id$valueCount" );
				$query->field( "o$valueCount.smw_title AS v$valueCount" );
				$query->field( "o$valueCount.smw_namespace AS v" . ( $valueCount + 1 ) );
				$query->field( "o$valueCount.smw_iw AS v" . ( $valueCount + 2 ) );
				$query->field( "o$valueCount.smw_sortkey AS v" . ( $valueCount + 3 ) );
				$query->field( "o$valueCount.smw_subobject AS v" . ( $valueCount + 4 ) );

				if ( $valueField == $fieldname ) {
					$valueField = "o$valueCount.smw_sortkey";
				}
				if ( $labelField == $fieldname ) {
					$labelField = "o$valueCount.smw_sortkey";
				}

				$valueCount += 4;
			} else {
				$query->field( $fieldname, "v$valueCount" );
			}

			$valueCount += 1;
		}

		// Postgres
		// Function: SMWSQLStore3Readers::fetchSemanticData
		// Error: 42P10 ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
		if ( !$query->hasField( $valueField ) ) {
			$query->field( $valueField, "v" . ( $valueCount + 1 ) );
		}
	}

	private function resultFromRow( &$result, $row, $fields, $fieldname, $valueCount, $isSubject, $propertykey ) {

		$hash = '';

		if ( $isSubject ) { // use joined or predefined property name
			$hash = $propertykey;
		}

		// Use enclosing array only for results with many values:
		if ( $valueCount > 1 ) {
			$valueKeys = [];
			for ( $i = 0; $i < $valueCount; $i += 1 ) { // read the value fields from the current row
				$fieldname = "v$i";
				$valueKeys[] = $row->$fieldname;
			}
		} else {
			$valueKeys = $row->v0;
		}

		// #Issue 615
		// If the iw field contains a redirect marker then remove it
		if ( isset( $valueKeys[2] ) && ( $valueKeys[2] === SMW_SQL3_SMWREDIIW || $valueKeys[2] === SMW_SQL3_SMWDELETEIW ) ) {
			$valueKeys[2] = '';
		}

		// The hash prevents from inserting duplicate entries of the same content
		if ( $valueCount > 1 ) {
			$hash = md5( $hash . implode( '#', $valueKeys ) );
		} else {
			$hash = md5( $hash . $valueKeys );
		}

		// Filter out any accidentally retrieved internal things (interwiki starts with ":"):
		if ( $valueCount < 3 ||
			implode( '', $fields ) !== FieldType::FIELD_ID ||
			$valueKeys[2] === '' ||
			$valueKeys[2]{0} != ':' ) {

			if ( isset( $result[$hash] ) ) {
				$this->reportDuplicate( $propertykey, $valueKeys );
			}

			if ( $isSubject ) {
				$result[$hash] = [ $propertykey, $valueKeys ];
			} else{
				$result[$hash] = $valueKeys;
			}
		}
	}

	private function reportDuplicate( $propertykey, $valueKeys ) {
		$this->logger->info(
			"Found duplicate entry for {propertykey} with {valueKeys}",
			[
				'method' => __METHOD__,
				'role' => 'user',
				'propertykey' => $propertykey,
				'valueKeys' => ( is_array( $valueKeys ) ? implode( ',', $valueKeys ) : $valueKeys )
			]
		);
	}

}