1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Lookup; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\Property; |
6
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* PropertyDataTypeLookup that uses an EntityLookup to find |
10
|
|
|
* a property's data type ID. |
11
|
|
|
* |
12
|
|
|
* @since 1.1 |
13
|
|
|
* |
14
|
|
|
* @license GPL-2.0-or-later |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class EntityRetrievingDataTypeLookup implements PropertyDataTypeLookup { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var EntityLookup |
21
|
|
|
*/ |
22
|
|
|
private $entityLookup; |
23
|
|
|
private array $propertyIdsInProcess = []; |
24
|
4 |
|
|
25
|
4 |
|
public function __construct( EntityLookup $entityLookup ) { |
26
|
4 |
|
$this->entityLookup = $entityLookup; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @since 2.0 |
31
|
|
|
* |
32
|
|
|
* @param PropertyId $propertyId |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
* @throws PropertyDataTypeLookupException |
36
|
4 |
|
*/ |
37
|
4 |
|
public function getDataTypeIdForProperty( PropertyId $propertyId ) { |
38
|
|
|
if ( array_key_exists( $propertyId->getSerialization(), $this->propertyIdsInProcess ) ) { |
39
|
|
|
// avoid self-referencing loop for newly created properties (T374230) |
40
|
|
|
throw new PropertyDataTypeLookupException( $propertyId, 'loop detected' ); |
41
|
|
|
} |
42
|
|
|
$this->propertyIdsInProcess[ $propertyId->getSerialization() ] = true; |
43
|
|
|
try { |
44
|
|
|
return $this->getProperty( $propertyId )->getDataTypeId(); |
45
|
|
|
} finally { |
46
|
4 |
|
unset( $this->propertyIdsInProcess[ $propertyId->getSerialization() ] ); |
47
|
4 |
|
} |
48
|
|
|
} |
49
|
4 |
|
|
50
|
|
|
/** |
51
|
|
|
* @param PropertyId $propertyId |
52
|
|
|
* |
53
|
4 |
|
* @return Property |
54
|
|
|
* @throws PropertyDataTypeLookupException |
55
|
|
|
*/ |
56
|
|
|
private function getProperty( PropertyId $propertyId ) { |
57
|
|
|
$property = $this->entityLookup->getEntity( $propertyId ); |
58
|
|
|
|
59
|
|
|
if ( !( $property instanceof Property ) ) { |
60
|
|
|
throw new PropertyDataTypeLookupException( $propertyId ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $property; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|