1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\TermStore\MediaWiki\PackagePrivate; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use LogicException; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
use stdClass; |
10
|
|
|
use Wikimedia\Rdbms\IDatabase; |
11
|
|
|
use Wikimedia\Rdbms\ILoadBalancer; |
12
|
|
|
use Wikimedia\Rdbms\IResultWrapper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Term ID resolver using the normalized database schema. |
16
|
|
|
* |
17
|
|
|
* @license GPL-2.0-or-later |
18
|
|
|
*/ |
19
|
|
|
class DatabaseTermIdsResolver implements TermIdsResolver { |
20
|
|
|
|
21
|
|
|
/** @var TypeIdsResolver */ |
22
|
|
|
private $typeIdsResolver; |
23
|
|
|
|
24
|
|
|
/** @var ILoadBalancer */ |
25
|
|
|
private $lb; |
26
|
|
|
|
27
|
|
|
/** @var bool */ |
28
|
|
|
private $allowMasterFallback; |
29
|
|
|
|
30
|
|
|
/** @var LoggerInterface */ |
31
|
|
|
private $logger; |
32
|
|
|
|
33
|
|
|
/** @var IDatabase */ |
34
|
|
|
private $dbr = null; |
35
|
|
|
|
36
|
|
|
/** @var IDatabase */ |
37
|
|
|
private $dbw = null; |
38
|
|
|
|
39
|
|
|
/** @var string[] stash of data returned from the {@link TypeIdsResolver} */ |
40
|
|
|
private $typeNames = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param TypeIdsResolver $typeIdsResolver |
44
|
|
|
* @param ILoadBalancer $lb |
45
|
|
|
* @param bool $allowMasterFallback Whether to fall back to the master database if the data from |
46
|
|
|
* the replica database is detected to be stale. |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
TypeIdsResolver $typeIdsResolver, |
50
|
|
|
ILoadBalancer $lb, |
51
|
|
|
$allowMasterFallback = false, |
52
|
|
|
LoggerInterface $logger = null |
53
|
|
|
) { |
54
|
|
|
$this->typeIdsResolver = $typeIdsResolver; |
55
|
|
|
$this->lb = $lb; |
56
|
|
|
$this->allowMasterFallback = $allowMasterFallback; |
57
|
|
|
$this->logger = $logger ?: new NullLogger(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/* |
61
|
|
|
* Term data is first read from the replica; if that returns less rows than we asked for, then |
62
|
|
|
* there are some new rows in the master that were not yet replicated, and we fall back to the |
63
|
|
|
* master if allowed. As the internal relations of the term store never change (for example, a |
64
|
|
|
* term_in_lang row will never suddenly point to a different text_in_lang), a master fallback |
65
|
|
|
* should never be necessary in any other case. However, callers need to consider where they |
66
|
|
|
* got the list of term IDs they pass into this method from: if it’s from a replica, they may |
67
|
|
|
* still see outdated data overall. |
68
|
|
|
*/ |
69
|
|
|
public function resolveTermIds( array $termIds ): array { |
70
|
|
|
$terms = []; |
71
|
|
|
|
72
|
|
|
$this->logger->debug( |
73
|
|
|
'{method}: getting {termCount} rows from replica', |
74
|
|
|
[ |
75
|
|
|
'method' => __METHOD__, |
76
|
|
|
'termCount' => count( $termIds ), |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
$replicaResult = $this->selectTerms( $this->getDbr(), $termIds ); |
80
|
|
|
$this->preloadTypes( $replicaResult ); |
81
|
|
|
$replicaTermIds = []; |
82
|
|
|
|
83
|
|
|
foreach ( $replicaResult as $row ) { |
84
|
|
|
$replicaTermIds[] = $row->wbtl_id; |
85
|
|
|
$this->addResultTerms( $terms, $row ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ( $this->allowMasterFallback && count( $replicaTermIds ) !== count( $termIds ) ) { |
89
|
|
|
$this->logger->info( |
90
|
|
|
'{method}: replica only returned {replicaCount} out of {termCount} rows, ' . |
91
|
|
|
'falling back to master', |
92
|
|
|
[ |
93
|
|
|
'method' => __METHOD__, |
94
|
|
|
'replicaCount' => count( $replicaTermIds ), |
95
|
|
|
'termCount' => count( $termIds ), |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
$masterTermIds = array_values( array_diff( $termIds, $replicaTermIds ) ); |
99
|
|
|
$masterResult = $this->selectTerms( $this->getDbw(), $masterTermIds ); |
100
|
|
|
$this->preloadTypes( $masterResult ); |
101
|
|
|
foreach ( $masterResult as $row ) { |
102
|
|
|
$this->addResultTerms( $terms, $row ); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $terms; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function selectTerms( IDatabase $db, array $termIds ): IResultWrapper { |
110
|
|
|
return $db->select( |
111
|
|
|
[ 'wbt_term_in_lang', 'wbt_text_in_lang', 'wbt_text' ], |
112
|
|
|
[ 'wbtl_id', 'wbtl_type_id', 'wbxl_language', 'wbx_text' ], |
113
|
|
|
[ |
114
|
|
|
'wbtl_id' => $termIds, |
115
|
|
|
// join conditions |
116
|
|
|
'wbtl_text_in_lang_id=wbxl_id', |
117
|
|
|
'wbxl_text_id=wbx_id', |
118
|
|
|
], |
119
|
|
|
__METHOD__ |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function preloadTypes( IResultWrapper $result ) { |
124
|
|
|
$typeIds = []; |
125
|
|
|
foreach ( $result as $row ) { |
126
|
|
|
$typeId = $row->wbtl_type_id; |
127
|
|
|
if ( !array_key_exists( $typeId, $this->typeNames ) ) { |
128
|
|
|
$typeIds[$typeId] = true; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
$this->typeNames += $this->typeIdsResolver->resolveTypeIds( array_keys( $typeIds ) ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function addResultTerms( array &$terms, stdClass $row ) { |
135
|
|
|
$type = $this->lookupType( $row->wbtl_type_id ); |
136
|
|
|
$lang = $row->wbxl_language; |
137
|
|
|
$text = $row->wbx_text; |
138
|
|
|
$terms[$type][$lang][] = $text; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function lookupType( $typeId ) { |
142
|
|
|
$typeName = $this->typeNames[$typeId] ?? null; |
143
|
|
|
if ( $typeName === null ) { |
144
|
|
|
throw new InvalidArgumentException( |
145
|
|
|
'Type ID ' . $typeId . ' was requested but not preloaded!' ); |
146
|
|
|
} |
147
|
|
|
return $typeName; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function getDbr() { |
151
|
|
|
if ( $this->dbr === null ) { |
152
|
|
|
$this->dbr = $this->lb->getConnection( ILoadBalancer::DB_REPLICA ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->dbr; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function getDbw() { |
159
|
|
|
if ( !$this->allowMasterFallback ) { |
160
|
|
|
throw new LogicException( 'Master fallback not allowed!' ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ( $this->dbw === null ) { |
164
|
|
|
$this->dbw = $this->lb->getConnection( ILoadBalancer::DB_MASTER ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->dbw; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.