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

namespace SMW\SQLStore\QueryEngine;

use SMW\MediaWiki\Database;
use SMW\SQLStore\SQLStore;
use SMWQuery as Query;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class QuerySegmentListBuildManager {

	/**
	 * @var QuerySegment[]
	 */
	private $querySegmentList = [];

	/**
	 * @var string[]
	 */
	private $errors = [];

	/**
	 * @var string[]
	 */
	private $sortKeys;

	/**
	 * @var QuerySegmentListBuilder
	 */
	private $querySegmentListBuilder;

	/**
	 * @var OrderCondition
	 */
	private $orderCondition;

	/**
	 * @since 2.5
	 *
	 * @param Database $connection
	 * @param QuerySegmentListBuilder $querySegmentListBuilder
	 * @param OrderCondition $orderCondition
	 */
	public function __construct( Database $connection, QuerySegmentListBuilder $querySegmentListBuilder, OrderCondition $orderCondition ) {
		$this->connection = $connection;
		$this->querySegmentListBuilder = $querySegmentListBuilder;
		$this->orderCondition = $orderCondition;
	}

	/**
	 * @since 2.2
	 *
	 * @return string[]
	 */
	public function getSortKeys() {
		return $this->sortKeys;
	}

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

	/**
	 * @since 2.5
	 *
	 * @return array
	 */
	public function getQuerySegmentList() {
		return $this->querySegmentList;
	}

	/**
	 * Compute abstract representation of the query (compilation)
	 *
	 * @param Query $query
	 *
	 * @return integer
	 */
	public function getQuerySegmentFrom( Query $query ) {

		$this->sortKeys = $query->sortkeys;

		// Anchor IT_TABLE as root element
		$rootSegmentNumber = QuerySegment::$qnum;
		$rootSegment = new QuerySegment();
		$rootSegment->joinTable = SQLStore::ID_TABLE;
		$rootSegment->joinfield = "$rootSegment->alias.smw_id";

		$this->querySegmentListBuilder->addQuerySegment(
			$rootSegment
		);

		$this->querySegmentListBuilder->setSortKeys(
			$this->sortKeys
		);

		// compile query, build query "plan"
		$this->querySegmentListBuilder->getQuerySegmentFrom(
			$query->getDescription()
		);

		$qid = $this->querySegmentListBuilder->getLastQuerySegmentId();
		$this->querySegmentList = $this->querySegmentListBuilder->getQuerySegmentList();
		$this->errors = $this->querySegmentListBuilder->getErrors();

		// no valid/supported condition; ensure that at least only proper pages
		// are delivered
		if ( $qid < 0 ) {
			$qid = $rootSegmentNumber;
			$qobj = $this->querySegmentList[$rootSegmentNumber];
			$qobj->where = "$qobj->alias.smw_iw!=" . $this->connection->addQuotes( SMW_SQL3_SMWIW_OUTDATED ) .
				" AND $qobj->alias.smw_iw!=" . $this->connection->addQuotes( SMW_SQL3_SMWREDIIW ) .
				" AND $qobj->alias.smw_iw!=" . $this->connection->addQuotes( SMW_SQL3_SMWBORDERIW ) .
				" AND $qobj->alias.smw_iw!=" . $this->connection->addQuotes( SMW_SQL3_SMWINTDEFIW );
			$this->querySegmentListBuilder->addQuerySegment( $qobj );
		}

		if ( isset( $this->querySegmentList[$qid]->joinTable ) && $this->querySegmentList[$qid]->joinTable != SQLStore::ID_TABLE ) {
			// manually make final root query (to retrieve namespace,title):
			$rootid = $rootSegmentNumber;
			$qobj = $this->querySegmentList[$rootSegmentNumber];
			$qobj->components = [ $qid => "$qobj->alias.smw_id" ];
			$qobj->sortfields = $this->querySegmentList[$qid]->sortfields;
			$this->querySegmentListBuilder->addQuerySegment( $qobj );
		} else { // not such a common case, but worth avoiding the additional inner join:
			$rootid = $qid;
		}

		$this->orderCondition->setSortKeys(
			$this->sortKeys
		);

		// Include order conditions (may extend query if needed for sorting):
		$this->querySegmentList = $this->orderCondition->apply(
			$rootid
		);

		$this->sortKeys = $this->orderCondition->getSortKeys();
		$this->errors = $this->orderCondition->getErrors();

		return $rootid;
	}

}