summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/EntityStore/PropertiesLookup.php
blob: 87d0d7484e29df2b00671e4d7edc91b52135a0e7 (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
<?php

namespace SMW\SQLStore\EntityStore;

use SMW\SQLStore\SQLStore;
use SMW\SQLStore\PropertyTableDefinition as TableDefinition;
use SMWDataItem as DataItem;
use SMW\DIWikiPage;
use SMW\RequestOptions;
use RuntimeException;

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

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

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

	/**
	 * @since 3.0
	 *
	 * @return RequestOptions|null
	 */
	public function newRequestOptions( RequestOptions $requestOptions = null ) {

		if ( $requestOptions !== null ) {
			$clone = clone $requestOptions;
			$clone->limit = $requestOptions->limit + $requestOptions->offset;
			$clone->offset = 0;
		} else {
			$clone = null;
		}

		return $clone;
	}

	/**
	 * @see Store::getProperties
	 *
	 * @since 3.0
	 *
	 * {@inheritDoc}
	 */
	public function fetchFromTable( DIWikiPage $subject, TableDefinition $propertyTable, RequestOptions $requestOptions = null ) {

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

		$query->type( 'SELECT' );
		$query->table( $propertyTable->getName() );

		if ( $propertyTable->usesIdSubject() ) {
			$query->condition( $query->eq( 's_id', $subject->getId() ) );
		} elseif ( $subject->getInterwiki() === '' ) {
			$query->condition( $query->eq( 's_title', $subject->getDBkey() ) );
			$query->condition( $query->eq( 's_namespace', $subject->getNamespace() ) );
		} else {
			// subjects with non-empty interwiki cannot have properties
			return [];
		}

		if ( $propertyTable->isFixedPropertyTable() ) {
			return $this->fetchFromFixedTable( $query, $propertyTable->getFixedProperty() );
		}

		$query->join(
			'INNER JOIN',
			[ SQLStore::ID_TABLE => "ON smw_id=p_id" ]
		);

		$query->fields( [ 'smw_title', 'smw_sortkey' ] );

		// (select sortkey since it might be used in ordering (needed by Postgres))
		$query->condition( $this->store->getSQLConditions(
			$requestOptions,
			'smw_sortkey',
			'smw_sortkey'
		) );

		$opt =  $this->store->getSQLOptions(
			$requestOptions,
			'smw_sortkey'
		);

		$query->options( $opt + [ 'DISTINCT' => true ] );

		return $query->execute( __METHOD__ );
	}

	private function fetchFromFixedTable( $query, $title ) {

		// just check if subject occurs in table
		$query->options(
			[ 'LIMIT' => 1 ]
		);

		$query->field( '*' );
		$res = $query->execute( __METHOD__ );

		if ( $res->numRows() > 0 ) {
			return [ $title ];
		}

		return [];
	}

}