summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Elastic/QueryEngine/DescriptionInterpreters/ConceptDescriptionInterpreter.php
blob: 76b05b36619f37bf9f832c5f1faef626ddb06824 (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
<?php

namespace SMW\Elastic\QueryEngine\DescriptionInterpreters;

use SMW\Elastic\QueryEngine\ConditionBuilder;
use SMW\Query\Language\ConceptDescription;
use SMW\Query\Language\Conjunction;
use SMW\Query\Language\Disjunction;
use SMW\Query\Parser as QueryParser;
use SMW\ApplicationFactory;
use SMW\DIWikiPage;
use SMW\DIProperty;
use SMW\Options;

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

	/**
	 * @var ConditionBuilder
	 */
	private $conditionBuilder;

	/**
	 * @var QueryParser
	 */
	private $queryParser;

	/**
	 * @since 3.0
	 *
	 * @param ConditionBuilder $conditionBuilder
	 * @param QueryParser $queryParser
	 */
	public function __construct( ConditionBuilder $conditionBuilder, QueryParser $queryParser ) {
		$this->conditionBuilder = $conditionBuilder;
		$this->queryParser = $queryParser;
	}

	/**
	 * @since 3.0
	 *
	 * @param ConceptDescription $description
	 *
	 * @return Condition|[]
	 */
	public function interpretDescription( ConceptDescription $description, $isConjunction = false ) {

		$concept = $description->getConcept();

		$value = $this->conditionBuilder->getStore()->getPropertyValues(
			$concept,
			new DIProperty( '_CONC' )
		);

		if ( $value === null || $value === [] ) {
			return [];
		}

		$value = end( $value );

		$description = $this->queryParser->getQueryDescription(
			$value->getConceptQuery()
		);

		if ( $this->hasCircularConceptDescription( $description, $concept ) ) {
			return [];
		}

		$params = $this->conditionBuilder->interpretDescription(
			$description,
			$isConjunction
		);

		$params = $this->terms_lookup( $description, $concept, $params );

		$condition = $this->conditionBuilder->newCondition( $params );
		$condition->type( '' );

		$condition->log( [ 'ConceptDescription' => $description->getQueryString() ] );

		return $condition;
	}

	private function terms_lookup( $description, $concept, $params ) {

		$concept->setId(
			$this->conditionBuilder->getID( $concept )
		);

		$termsLookup = $this->conditionBuilder->getTermsLookup();

		$parameters = $termsLookup->newParameters(
			[
				'id' => $concept->getId(),
				'hash' => $concept->getHash(),
				'query.string' => $description->getQueryString(),
				'fingerprint' => $description->getFingerprint(),
				'params' => $params,
			]
		);

		// Using the terms lookup to prefetch IDs from the lookup index
		if ( $this->conditionBuilder->getOption( 'concept.terms.lookup' ) ) {
			$params = $termsLookup->lookup( 'concept', $parameters );
		}

		if ( $parameters->has( 'query.info' ) ) {
			$this->conditionBuilder->addQueryInfo( $parameters->get( 'query.info' ) );
		}

		return $params;
	}

	private function hasCircularConceptDescription( $description, $concept ) {

		if ( $description instanceof ConceptDescription ) {
			if ( $description->getConcept()->equals( $concept ) ) {
				$this->conditionBuilder->addError( [ 'smw-query-condition-circular', $description->getQueryString() ] );
				return true;
			}
		}

		if ( $description instanceof Conjunction || $description instanceof Disjunction ) {
			foreach ( $description->getDescriptions() as $desc ) {
				if ( $this->hasCircularConceptDescription( $desc, $concept ) ) {
					return true;
				}
			}
		}

		return false;
	}

}