1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Lookup; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
6
|
|
|
use Wikibase\DataModel\Term\Term; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @since 1.1 |
10
|
|
|
* |
11
|
|
|
* @license GPL-2.0-or-later |
12
|
|
|
* @author Katie Filbert < [email protected] > |
13
|
|
|
* @author Marius Hoch < [email protected] > |
14
|
|
|
* @author Addshore |
15
|
|
|
*/ |
16
|
|
|
class LanguageLabelDescriptionLookup implements LabelDescriptionLookup { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var TermLookup |
20
|
|
|
*/ |
21
|
|
|
private $termLookup; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $languageCode; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param TermLookup $termLookup |
30
|
|
|
* @param string $languageCode |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct( TermLookup $termLookup, $languageCode ) { |
33
|
4 |
|
$this->termLookup = $termLookup; |
34
|
4 |
|
$this->languageCode = $languageCode; |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param EntityId $entityId |
39
|
|
|
* |
40
|
|
|
* @throws LabelDescriptionLookupException |
41
|
|
|
* @return Term|null |
42
|
|
|
*/ |
43
|
2 |
|
public function getLabel( EntityId $entityId ) { |
44
|
|
|
try { |
45
|
2 |
|
$text = $this->termLookup->getLabel( $entityId, $this->languageCode ); |
46
|
2 |
|
} catch ( TermLookupException $ex ) { |
47
|
|
|
throw new LabelDescriptionLookupException( $entityId, 'Failed to lookup label', $ex ); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
if ( $text === null ) { |
51
|
1 |
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
return new Term( $this->languageCode, $text ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param EntityId $entityId |
59
|
|
|
* |
60
|
|
|
* @throws LabelDescriptionLookupException |
61
|
|
|
* @return Term|null |
62
|
|
|
*/ |
63
|
2 |
|
public function getDescription( EntityId $entityId ) { |
64
|
|
|
try { |
65
|
2 |
|
$text = $this->termLookup->getDescription( $entityId, $this->languageCode ); |
66
|
2 |
|
} catch ( TermLookupException $ex ) { |
67
|
|
|
throw new LabelDescriptionLookupException( $entityId, 'Failed to lookup description', $ex ); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
if ( $text === null ) { |
71
|
1 |
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return new Term( $this->languageCode, $text ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|