1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Queryr\TermStore; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\DBALException; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
8
|
|
|
use Wikibase\DataModel\Term\Fingerprint; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Package public |
12
|
|
|
* @since 0.2 |
13
|
|
|
* |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class TermStore implements LabelLookup, EntityIdLookup { |
18
|
|
|
|
19
|
|
|
private $storeWriter; |
20
|
|
|
private $idLookup; |
21
|
|
|
|
22
|
|
|
private $labelTable; |
23
|
|
|
private $aliasesTable; |
24
|
|
|
|
25
|
17 |
|
public function __construct( Connection $connection, TermStoreConfig $config ) { |
26
|
17 |
|
$this->labelTable = new TableQueryExecutor( $connection, $config->getLabelTableName() ); |
27
|
17 |
|
$this->aliasesTable = new TableQueryExecutor( $connection, $config->getAliasesTableName() ); |
28
|
|
|
|
29
|
17 |
|
$this->storeWriter = new TermStoreWriter( $connection, $config ); |
30
|
17 |
|
$this->idLookup = new IdLookup( $this->labelTable, $this->aliasesTable ); |
31
|
17 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param EntityId $id |
35
|
|
|
* @param Fingerprint $fingerprint |
36
|
|
|
* |
37
|
|
|
* @throws TermStoreException |
38
|
|
|
*/ |
39
|
15 |
|
public function storeEntityFingerprint( EntityId $id, Fingerprint $fingerprint ) { |
40
|
15 |
|
$this->storeWriter->storeEntityFingerprint( $id, $fingerprint ); |
41
|
14 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param EntityId $id |
45
|
|
|
* |
46
|
|
|
* @throws TermStoreException |
47
|
|
|
*/ |
48
|
|
|
public function dropTermsForId( EntityId $id ) { |
49
|
|
|
$this->storeWriter->dropTermsForId( $id ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @throws TermStoreException |
54
|
|
|
*/ |
55
|
3 |
|
public function getLabelByIdAndLanguage( EntityId $id, string $languageCode ): ?string { |
56
|
|
|
try { |
57
|
3 |
|
return $this->labelTable->selectOneField( |
58
|
3 |
|
'text', |
59
|
|
|
[ |
60
|
3 |
|
'entity_id' => $id->getSerialization(), |
61
|
3 |
|
'language' => $languageCode |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
catch ( DBALException $ex ) { |
66
|
|
|
throw new TermStoreException( $ex->getMessage(), $ex ); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param EntityId $id |
72
|
|
|
* @param string $languageCode |
73
|
|
|
* |
74
|
|
|
* @return string[] |
75
|
|
|
* @throws TermStoreException |
76
|
|
|
*/ |
77
|
3 |
|
public function getAliasesByIdAndLanguage( EntityId $id, string $languageCode ): array { |
78
|
|
|
try { |
79
|
3 |
|
return $this->aliasesTable->selectField( |
80
|
3 |
|
'text', |
81
|
|
|
[ |
82
|
3 |
|
'entity_id' => $id->getSerialization(), |
83
|
3 |
|
'language' => $languageCode |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
catch ( DBALException $ex ) { |
88
|
|
|
throw new TermStoreException( $ex->getMessage(), $ex ); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the first matching entity id. Case insensitive. |
94
|
|
|
* |
95
|
|
|
* @throws TermStoreException |
96
|
|
|
*/ |
97
|
2 |
|
public function getIdByLabel( string $labelLanguageCode, string $labelText ): ?string { |
98
|
2 |
|
return $this->idLookup->getIdByLabel( $labelLanguageCode, $labelText ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the first matching item id. Case insensitive. |
103
|
|
|
* |
104
|
|
|
* @throws TermStoreException |
105
|
|
|
*/ |
106
|
2 |
|
public function getItemIdByLabel( string $labelLanguageCode, string $labelText ): ?string { |
107
|
2 |
|
return $this->idLookup->getItemIdByLabel( $labelLanguageCode, $labelText ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the first matching property id. Case insensitive. |
112
|
|
|
* |
113
|
|
|
* @throws TermStoreException |
114
|
|
|
*/ |
115
|
2 |
|
public function getPropertyIdByLabel( string $labelLanguageCode, string $labelText ): ?string { |
116
|
2 |
|
return $this->idLookup->getPropertyIdByLabel( $labelLanguageCode, $labelText ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the first matching entity id. Case insensitive. |
121
|
|
|
* |
122
|
|
|
* @throws TermStoreException |
123
|
|
|
*/ |
124
|
2 |
|
public function getIdByText( string $languageCode, string $termText ): ?string { |
125
|
2 |
|
return $this->idLookup->getIdByText( $languageCode, $termText ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the first matching item id. Case insensitive. |
130
|
|
|
* |
131
|
|
|
* @throws TermStoreException |
132
|
|
|
*/ |
133
|
2 |
|
public function getItemIdByText( string $languageCode, string $termText ): ?string { |
134
|
2 |
|
return $this->idLookup->getItemIdByText( $languageCode, $termText ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns the first matching property id. Case insensitive. |
139
|
|
|
* |
140
|
|
|
* @throws TermStoreException |
141
|
|
|
*/ |
142
|
2 |
|
public function getPropertyIdByText( string $languageCode, string $termText ): ?string { |
143
|
2 |
|
return $this->idLookup->getPropertyIdByText( $languageCode, $termText ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|