1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WikibaseQuality\ConstraintReport; |
4
|
|
|
|
5
|
|
|
use Config; |
6
|
|
|
use IBufferingStatsdDataFactory; |
7
|
|
|
use MediaWiki\Logger\LoggerFactory; |
8
|
|
|
use MediaWiki\MediaWikiServices; |
9
|
|
|
use TitleParser; |
10
|
|
|
use Wikibase\DataModel\Entity\EntityIdParser; |
11
|
|
|
use Wikibase\DataModel\Services\Lookup\EntityLookup; |
12
|
|
|
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup; |
13
|
|
|
use Wikibase\Lib\Units\UnitConverter; |
14
|
|
|
use Wikibase\Rdf\RdfVocabulary; |
15
|
|
|
use Wikibase\Repo\WikibaseRepo; |
16
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\EntityTypeChecker; |
17
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\NoBoundsChecker; |
18
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\AllowedUnitsChecker; |
19
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\NoneOfChecker; |
20
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ReferenceChecker; |
21
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\SingleBestValueChecker; |
22
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker; |
23
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser; |
24
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\CommonsLinkChecker; |
25
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\FormatChecker; |
26
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\OneOfChecker; |
27
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\QualifierChecker; |
28
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\RangeChecker; |
29
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\TypeChecker; |
30
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ConflictsWithChecker; |
31
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\QualifiersChecker; |
32
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\TargetRequiredClaimChecker; |
33
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ItemChecker; |
34
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\MandatoryQualifiersChecker; |
35
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ValueTypeChecker; |
36
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\SymmetricChecker; |
37
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\InverseChecker; |
38
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\DiffWithinRangeChecker; |
39
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\SingleValueChecker; |
40
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\MultiValueChecker; |
41
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\UniqueValueChecker; |
42
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\ValueOnlyChecker; |
43
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
44
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConnectionCheckerHelper; |
45
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\LoggingHelper; |
46
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\RangeCheckerHelper; |
47
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\SparqlHelper; |
48
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\TypeCheckerHelper; |
49
|
|
|
use Wikibase\DataModel\Services\Statement\StatementGuidParser; |
50
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessageDeserializer; |
51
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessageSerializer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Factory for {@link DelegatingConstraintChecker} |
55
|
|
|
* and {@link ConstraintRepository}. |
56
|
|
|
* |
57
|
|
|
* @license GPL-2.0-or-later |
58
|
|
|
*/ |
59
|
|
|
class ConstraintReportFactory { |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var ConstraintRepository|null |
63
|
|
|
*/ |
64
|
|
|
private $constraintRepository; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var ConstraintChecker[]|null |
68
|
|
|
*/ |
69
|
|
|
private $constraintCheckerMap; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var DelegatingConstraintChecker|null |
73
|
|
|
*/ |
74
|
|
|
private $delegatingConstraintChecker; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var EntityLookup |
78
|
|
|
*/ |
79
|
|
|
private $lookup; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var PropertyDataTypeLookup |
83
|
|
|
*/ |
84
|
|
|
private $propertyDataTypeLookup; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var array[]|null |
88
|
|
|
*/ |
89
|
|
|
private $constraintParameterMap; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var StatementGuidParser |
93
|
|
|
*/ |
94
|
|
|
private $statementGuidParser; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var Config |
98
|
|
|
*/ |
99
|
|
|
private $config; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var ConstraintParameterParser |
103
|
|
|
*/ |
104
|
|
|
private $constraintParameterParser; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var ViolationMessageSerializer |
108
|
|
|
*/ |
109
|
|
|
private $violationMessageSerializer; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var ViolationMessageDeserializer |
113
|
|
|
*/ |
114
|
|
|
private $violationMessageDeserializer; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @var RdfVocabulary |
118
|
|
|
*/ |
119
|
|
|
private $rdfVocabulary; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @var EntityIdParser |
123
|
|
|
*/ |
124
|
|
|
private $entityIdParser; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @var TitleParser |
128
|
|
|
*/ |
129
|
|
|
private $titleParser; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @var UnitConverter|null |
133
|
|
|
*/ |
134
|
|
|
private $unitConverter; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var IBufferingStatsdDataFactory |
138
|
|
|
*/ |
139
|
|
|
private $dataFactory; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the default instance. |
143
|
|
|
* IMPORTANT: Use only when it is not feasible to inject an instance properly. |
144
|
|
|
* |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
|
|
public static function getDefaultInstance() { |
148
|
|
|
static $instance = null; |
149
|
|
|
|
150
|
|
|
if ( $instance === null ) { |
151
|
|
|
$wikibaseRepo = WikibaseRepo::getDefaultInstance(); |
152
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig(); |
153
|
|
|
$titleParser = MediaWikiServices::getInstance()->getTitleParser(); |
154
|
|
|
$violationMessageDeserializer = new ViolationMessageDeserializer( |
155
|
|
|
$wikibaseRepo->getEntityIdParser(), |
156
|
|
|
$wikibaseRepo->getDataValueFactory() |
157
|
|
|
); |
158
|
|
|
$instance = new self( |
159
|
|
|
$wikibaseRepo->getEntityLookup(), |
160
|
|
|
$wikibaseRepo->getPropertyDataTypeLookup(), |
161
|
|
|
$wikibaseRepo->getStatementGuidParser(), |
162
|
|
|
$config, |
163
|
|
|
new ConstraintParameterParser( |
164
|
|
|
$config, |
165
|
|
|
$wikibaseRepo->getBaseDataModelDeserializerFactory(), |
166
|
|
|
$wikibaseRepo->getConceptBaseUris() |
167
|
|
|
), |
168
|
|
|
new ViolationMessageSerializer(), |
169
|
|
|
$violationMessageDeserializer, |
170
|
|
|
$wikibaseRepo->getRdfVocabulary(), |
171
|
|
|
$wikibaseRepo->getEntityIdParser(), |
172
|
|
|
$titleParser, |
173
|
|
|
$wikibaseRepo->getUnitConverter(), |
174
|
|
|
MediaWikiServices::getInstance()->getStatsdDataFactory() |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $instance; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function __construct( |
182
|
|
|
EntityLookup $lookup, |
183
|
|
|
PropertyDataTypeLookup $propertyDataTypeLookup, |
184
|
|
|
StatementGuidParser $statementGuidParser, |
185
|
|
|
Config $config, |
186
|
|
|
ConstraintParameterParser $constraintParameterParser, |
187
|
|
|
ViolationMessageSerializer $violationMessageSerializer, |
188
|
|
|
ViolationMessageDeserializer $violationMessageDeserializer, |
189
|
|
|
RdfVocabulary $rdfVocabulary, |
190
|
|
|
EntityIdParser $entityIdParser, |
191
|
|
|
TitleParser $titleParser, |
192
|
|
|
UnitConverter $unitConverter = null, |
193
|
|
|
IBufferingStatsdDataFactory $dataFactory |
194
|
|
|
) { |
195
|
|
|
$this->lookup = $lookup; |
196
|
|
|
$this->propertyDataTypeLookup = $propertyDataTypeLookup; |
197
|
|
|
$this->statementGuidParser = $statementGuidParser; |
198
|
|
|
$this->config = $config; |
199
|
|
|
$this->constraintParameterParser = $constraintParameterParser; |
200
|
|
|
$this->violationMessageSerializer = $violationMessageSerializer; |
201
|
|
|
$this->violationMessageDeserializer = $violationMessageDeserializer; |
202
|
|
|
$this->rdfVocabulary = $rdfVocabulary; |
203
|
|
|
$this->entityIdParser = $entityIdParser; |
204
|
|
|
$this->titleParser = $titleParser; |
205
|
|
|
$this->unitConverter = $unitConverter; |
206
|
|
|
$this->dataFactory = $dataFactory; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return DelegatingConstraintChecker |
211
|
|
|
*/ |
212
|
|
|
public function getConstraintChecker() { |
213
|
|
|
if ( $this->delegatingConstraintChecker === null ) { |
214
|
|
|
$this->delegatingConstraintChecker = new DelegatingConstraintChecker( |
215
|
|
|
$this->lookup, |
216
|
|
|
$this->getConstraintCheckerMap(), |
217
|
|
|
new CachingConstraintLookup( $this->getConstraintRepository() ), |
218
|
|
|
$this->constraintParameterParser, |
219
|
|
|
$this->statementGuidParser, |
220
|
|
|
new LoggingHelper( |
221
|
|
|
$this->dataFactory, |
222
|
|
|
LoggerFactory::getInstance( 'WikibaseQualityConstraints' ), |
223
|
|
|
$this->config |
224
|
|
|
), |
225
|
|
|
$this->config->get( 'WBQualityConstraintsCheckQualifiers' ), |
226
|
|
|
$this->config->get( 'WBQualityConstraintsCheckReferences' ), |
227
|
|
|
$this->config->get( 'WBQualityConstraintsPropertiesWithViolatingQualifiers' ) |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this->delegatingConstraintChecker; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return ConstraintChecker[] |
236
|
|
|
*/ |
237
|
|
|
private function getConstraintCheckerMap() { |
238
|
|
|
if ( $this->constraintCheckerMap === null ) { |
239
|
|
|
$connectionCheckerHelper = new ConnectionCheckerHelper(); |
240
|
|
|
$rangeCheckerHelper = new RangeCheckerHelper( $this->config, $this->unitConverter ); |
241
|
|
|
if ( $this->config->get( 'WBQualityConstraintsSparqlEndpoint' ) !== '' ) { |
242
|
|
|
$sparqlHelper = new SparqlHelper( |
243
|
|
|
$this->config, |
244
|
|
|
$this->rdfVocabulary, |
245
|
|
|
$this->entityIdParser, |
246
|
|
|
$this->propertyDataTypeLookup, |
247
|
|
|
MediaWikiServices::getInstance()->getMainWANObjectCache(), |
248
|
|
|
$this->violationMessageSerializer, |
249
|
|
|
$this->violationMessageDeserializer, |
250
|
|
|
$this->dataFactory |
251
|
|
|
); |
252
|
|
|
} else { |
253
|
|
|
$sparqlHelper = null; |
254
|
|
|
} |
255
|
|
|
$typeCheckerHelper = new TypeCheckerHelper( |
256
|
|
|
$this->lookup, |
257
|
|
|
$this->config, |
258
|
|
|
$sparqlHelper, |
259
|
|
|
$this->dataFactory |
260
|
|
|
); |
261
|
|
|
|
262
|
|
|
$this->constraintCheckerMap = [ |
263
|
|
|
$this->config->get( 'WBQualityConstraintsConflictsWithConstraintId' ) |
264
|
|
|
=> new ConflictsWithChecker( |
265
|
|
|
$this->lookup, |
266
|
|
|
$this->constraintParameterParser, |
267
|
|
|
$connectionCheckerHelper |
268
|
|
|
), |
269
|
|
|
$this->config->get( 'WBQualityConstraintsItemRequiresClaimConstraintId' ) |
270
|
|
|
=> new ItemChecker( |
271
|
|
|
$this->lookup, |
272
|
|
|
$this->constraintParameterParser, |
273
|
|
|
$connectionCheckerHelper |
274
|
|
|
), |
275
|
|
|
$this->config->get( 'WBQualityConstraintsValueRequiresClaimConstraintId' ) |
276
|
|
|
=> new TargetRequiredClaimChecker( |
277
|
|
|
$this->lookup, |
278
|
|
|
$this->constraintParameterParser, |
279
|
|
|
$connectionCheckerHelper |
280
|
|
|
), |
281
|
|
|
$this->config->get( 'WBQualityConstraintsSymmetricConstraintId' ) |
282
|
|
|
=> new SymmetricChecker( |
283
|
|
|
$this->lookup, |
284
|
|
|
$connectionCheckerHelper |
285
|
|
|
), |
286
|
|
|
$this->config->get( 'WBQualityConstraintsInverseConstraintId' ) |
287
|
|
|
=> new InverseChecker( |
288
|
|
|
$this->lookup, |
289
|
|
|
$this->constraintParameterParser, |
290
|
|
|
$connectionCheckerHelper |
291
|
|
|
), |
292
|
|
|
$this->config->get( 'WBQualityConstraintsUsedAsQualifierConstraintId' ) |
293
|
|
|
=> new QualifierChecker(), |
294
|
|
|
$this->config->get( 'WBQualityConstraintsAllowedQualifiersConstraintId' ) |
295
|
|
|
=> new QualifiersChecker( |
296
|
|
|
$this->constraintParameterParser |
297
|
|
|
), |
298
|
|
|
$this->config->get( 'WBQualityConstraintsMandatoryQualifierConstraintId' ) |
299
|
|
|
=> new MandatoryQualifiersChecker( |
300
|
|
|
$this->constraintParameterParser |
301
|
|
|
), |
302
|
|
|
$this->config->get( 'WBQualityConstraintsRangeConstraintId' ) |
303
|
|
|
=> new RangeChecker( |
304
|
|
|
$this->propertyDataTypeLookup, |
305
|
|
|
$this->constraintParameterParser, |
306
|
|
|
$rangeCheckerHelper |
307
|
|
|
), |
308
|
|
|
$this->config->get( 'WBQualityConstraintsDifferenceWithinRangeConstraintId' ) |
309
|
|
|
=> new DiffWithinRangeChecker( |
310
|
|
|
$this->constraintParameterParser, |
311
|
|
|
$rangeCheckerHelper, |
312
|
|
|
$this->config |
313
|
|
|
), |
314
|
|
|
$this->config->get( 'WBQualityConstraintsTypeConstraintId' ) |
315
|
|
|
=> new TypeChecker( |
316
|
|
|
$this->lookup, |
317
|
|
|
$this->constraintParameterParser, |
318
|
|
|
$typeCheckerHelper, |
319
|
|
|
$this->config |
320
|
|
|
), |
321
|
|
|
$this->config->get( 'WBQualityConstraintsValueTypeConstraintId' ) |
322
|
|
|
=> new ValueTypeChecker( |
323
|
|
|
$this->lookup, |
324
|
|
|
$this->constraintParameterParser, |
325
|
|
|
$typeCheckerHelper, |
326
|
|
|
$this->config |
327
|
|
|
), |
328
|
|
|
$this->config->get( 'WBQualityConstraintsSingleValueConstraintId' ) |
329
|
|
|
=> new SingleValueChecker( $this->constraintParameterParser ), |
330
|
|
|
$this->config->get( 'WBQualityConstraintsMultiValueConstraintId' ) |
331
|
|
|
=> new MultiValueChecker( $this->constraintParameterParser ), |
332
|
|
|
$this->config->get( 'WBQualityConstraintsDistinctValuesConstraintId' ) |
333
|
|
|
=> new UniqueValueChecker( |
334
|
|
|
$sparqlHelper |
335
|
|
|
), |
336
|
|
|
$this->config->get( 'WBQualityConstraintsFormatConstraintId' ) |
337
|
|
|
=> new FormatChecker( |
338
|
|
|
$this->constraintParameterParser, |
339
|
|
|
$this->config, |
340
|
|
|
$sparqlHelper |
341
|
|
|
), |
342
|
|
|
$this->config->get( 'WBQualityConstraintsCommonsLinkConstraintId' ) |
343
|
|
|
=> new CommonsLinkChecker( |
344
|
|
|
$this->constraintParameterParser, |
345
|
|
|
$this->titleParser |
346
|
|
|
), |
347
|
|
|
$this->config->get( 'WBQualityConstraintsOneOfConstraintId' ) |
348
|
|
|
=> new OneOfChecker( |
349
|
|
|
$this->constraintParameterParser |
350
|
|
|
), |
351
|
|
|
$this->config->get( 'WBQualityConstraintsUsedForValuesOnlyConstraintId' ) |
352
|
|
|
=> new ValueOnlyChecker(), |
353
|
|
|
$this->config->get( 'WBQualityConstraintsUsedAsReferenceConstraintId' ) |
354
|
|
|
=> new ReferenceChecker(), |
355
|
|
|
$this->config->get( 'WBQualityConstraintsNoBoundsConstraintId' ) |
356
|
|
|
=> new NoBoundsChecker(), |
357
|
|
|
$this->config->get( 'WBQualityConstraintsAllowedUnitsConstraintId' ) |
358
|
|
|
=> new AllowedUnitsChecker( |
359
|
|
|
$this->constraintParameterParser, |
360
|
|
|
$this->unitConverter |
361
|
|
|
), |
362
|
|
|
$this->config->get( 'WBQualityConstraintsSingleBestValueConstraintId' ) |
363
|
|
|
=> new SingleBestValueChecker( $this->constraintParameterParser ), |
364
|
|
|
$this->config->get( 'WBQualityConstraintsAllowedEntityTypesConstraintId' ) |
365
|
|
|
=> new EntityTypeChecker( |
366
|
|
|
$this->constraintParameterParser |
367
|
|
|
), |
368
|
|
|
$this->config->get( 'WBQualityConstraintsNoneOfConstraintId' ) |
369
|
|
|
=> new NoneOfChecker( |
370
|
|
|
$this->constraintParameterParser |
371
|
|
|
), |
372
|
|
|
]; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
return $this->constraintCheckerMap; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @return array[] |
380
|
|
|
*/ |
381
|
|
|
public function getConstraintParameterMap() { |
382
|
|
|
if ( $this->constraintParameterMap === null ) { |
383
|
|
|
$this->constraintParameterMap = [ |
|
|
|
|
384
|
|
|
'Commons link' => [ 'namespace' ], |
385
|
|
|
'Conflicts with' => [ 'property', 'item' ], |
386
|
|
|
'Diff within range' => [ 'property', 'minimum_quantity', 'maximum_quantity' ], |
387
|
|
|
'Format' => [ 'pattern' ], |
388
|
|
|
'Inverse' => [ 'property' ], |
389
|
|
|
'Item' => [ 'property', 'item' ], |
390
|
|
|
'Mandatory qualifiers' => [ 'property' ], |
391
|
|
|
'Multi value' => [], |
392
|
|
|
'One of' => [ 'item' ], |
393
|
|
|
'Qualifier' => [], |
394
|
|
|
'Qualifiers' => [ 'property' ], |
395
|
|
|
'Range' => [ 'minimum_quantity', 'maximum_quantity', 'minimum_date', 'maximum_date' ], |
396
|
|
|
'Single value' => [], |
397
|
|
|
'Symmetric' => [], |
398
|
|
|
'Target required claim' => [ 'property', 'item' ], |
399
|
|
|
'Type' => [ 'class', 'relation' ], |
400
|
|
|
'Unique value' => [], |
401
|
|
|
'Value type' => [ 'class', 'relation' ] |
402
|
|
|
]; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
return $this->constraintParameterMap; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @return ConstraintRepository |
410
|
|
|
*/ |
411
|
|
|
public function getConstraintRepository() { |
412
|
|
|
if ( $this->constraintRepository === null ) { |
413
|
|
|
$this->constraintRepository = new ConstraintRepository(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
return $this->constraintRepository; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
} |
420
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..