1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\SPARQLStore\QueryEngine\DescriptionInterpreters; |
4
|
|
|
|
5
|
|
|
use SMW\ApplicationFactory; |
6
|
|
|
use SMW\DIProperty; |
7
|
|
|
use SMW\DIWikiPage; |
8
|
|
|
use SMW\Query\Language\ConceptDescription; |
9
|
|
|
use SMW\Query\Language\Description; |
10
|
|
|
use SMW\Query\Language\Conjunction; |
11
|
|
|
use SMW\Query\Language\Disjunction; |
12
|
|
|
use SMW\SPARQLStore\QueryEngine\CompoundConditionBuilder; |
13
|
|
|
use SMW\SPARQLStore\QueryEngine\Condition\FalseCondition; |
14
|
|
|
use SMW\SPARQLStore\QueryEngine\DescriptionInterpreter; |
15
|
|
|
use SMWExporter as Exporter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @license GNU GPL v2+ |
19
|
|
|
* @since 2.1 |
20
|
|
|
* |
21
|
|
|
* @author Markus Krötzsch |
22
|
|
|
* @author mwjames |
23
|
|
|
*/ |
24
|
|
|
class ConceptDescriptionInterpreter implements DescriptionInterpreter { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CompoundConditionBuilder |
28
|
|
|
*/ |
29
|
|
|
private $compoundConditionBuilder; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Exporter |
33
|
|
|
*/ |
34
|
|
|
private $exporter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @since 2.1 |
38
|
|
|
* |
39
|
9 |
|
* @param CompoundConditionBuilder|null $compoundConditionBuilder |
40
|
9 |
|
*/ |
41
|
9 |
|
public function __construct( CompoundConditionBuilder $compoundConditionBuilder = null ) { |
42
|
9 |
|
$this->compoundConditionBuilder = $compoundConditionBuilder; |
43
|
|
|
$this->exporter = Exporter::getInstance(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @since 2.2 |
48
|
|
|
* |
49
|
3 |
|
* {@inheritDoc} |
50
|
3 |
|
*/ |
51
|
|
|
public function canInterpretDescription( Description $description ) { |
52
|
|
|
return $description instanceof ConceptDescription; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @since 2.2 |
57
|
|
|
* |
58
|
2 |
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function interpretDescription( Description $description ) { |
61
|
2 |
|
|
62
|
|
|
$joinVariable = $this->compoundConditionBuilder->getJoinVariable(); |
63
|
2 |
|
$orderByProperty = $this->compoundConditionBuilder->getOrderByProperty(); |
64
|
2 |
|
|
65
|
|
|
$conceptDescription = $this->getConceptDescription( |
66
|
|
|
$description->getConcept() |
|
|
|
|
67
|
2 |
|
); |
68
|
1 |
|
|
69
|
|
|
if ( $conceptDescription === '' ) { |
70
|
|
|
return new FalseCondition(); |
|
|
|
|
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
$hash = 'concept-' . $conceptDescription->getQueryString(); |
74
|
|
|
|
75
|
1 |
|
$this->compoundConditionBuilder->getCircularReferenceGuard()->mark( $hash ); |
76
|
|
|
|
77
|
|
|
if ( $this->compoundConditionBuilder->getCircularReferenceGuard()->isCircularByRecursionFor( $hash ) ) { |
78
|
|
|
|
79
|
|
|
$this->compoundConditionBuilder->addError( |
80
|
|
|
array( 'smw-query-condition-circular', $description->getQueryString() ) |
|
|
|
|
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return new FalseCondition(); |
|
|
|
|
84
|
1 |
|
} |
85
|
1 |
|
|
86
|
|
|
$this->compoundConditionBuilder->setJoinVariable( $joinVariable ); |
87
|
1 |
|
$this->compoundConditionBuilder->setOrderByProperty( $orderByProperty ); |
88
|
|
|
|
89
|
|
|
$condition = $this->compoundConditionBuilder->mapDescriptionToCondition( |
90
|
|
|
$conceptDescription |
|
|
|
|
91
|
1 |
|
); |
92
|
|
|
|
93
|
1 |
|
$this->compoundConditionBuilder->getCircularReferenceGuard()->unmark( $hash ); |
94
|
|
|
|
95
|
|
|
return $condition; |
|
|
|
|
96
|
2 |
|
} |
97
|
|
|
|
98
|
2 |
|
private function getConceptDescription( DIWikiPage $concept ) { |
99
|
|
|
|
100
|
2 |
|
$applicationFactory = ApplicationFactory::getInstance(); |
101
|
2 |
|
|
102
|
|
|
$value = $applicationFactory->getStore()->getSemanticData( $concept )->getPropertyValues( |
103
|
|
|
new DIProperty( '_CONC' ) |
104
|
2 |
|
); |
105
|
1 |
|
|
106
|
|
|
if ( $value === null || $value === array() ) { |
107
|
|
|
return ''; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
1 |
|
$value = end( $value ); |
111
|
1 |
|
|
112
|
|
|
$description = $applicationFactory->newQueryParser()->getQueryDescription( |
|
|
|
|
113
|
|
|
$value->getConceptQuery() |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->findCircularDescription( $concept, $description ); |
117
|
|
|
|
118
|
|
|
return $description; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function findCircularDescription( $concept, $description ) { |
122
|
|
|
|
123
|
|
|
if ( $description instanceof ConceptDescription ) { |
124
|
|
|
if ( $description->getConcept()->equals( $concept ) ) { |
125
|
|
|
$this->compoundConditionBuilder->addError( |
126
|
|
|
array( 'smw-query-condition-circular', $description->getQueryString() ) |
|
|
|
|
127
|
|
|
); |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ( $description instanceof Conjunction || $description instanceof Disjunction ) { |
133
|
|
|
foreach ( $description->getDescriptions() as $desc ) { |
134
|
|
|
$this->findCircularDescription( $concept, $desc ); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: