1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace WikibaseQuality\ConstraintReport\Api; |
5
|
|
|
|
6
|
|
|
use Config; |
7
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
8
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
9
|
|
|
use Wikibase\DataModel\Services\EntityId\EntityIdFormatter; |
10
|
|
|
use Wikibase\Lib\Store\EntityTitleLookup; |
11
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedCheckConstraintsResponse; |
12
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedCheckResults; |
13
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
14
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessageRenderer; |
15
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
16
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\NullResult; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Converts check results into arrays and stores them inside one big response array. |
20
|
|
|
* |
21
|
|
|
* @author Lucas Werkmeister |
22
|
|
|
* @license GPL-2.0-or-later |
23
|
|
|
*/ |
24
|
|
|
class CheckResultsRenderer { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var EntityTitleLookup |
28
|
|
|
*/ |
29
|
|
|
private $entityTitleLookup; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EntityIdFormatter |
33
|
|
|
*/ |
34
|
|
|
private $entityIdLabelFormatter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ViolationMessageRenderer |
38
|
|
|
*/ |
39
|
|
|
private $violationMessageRenderer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Config |
43
|
|
|
*/ |
44
|
|
|
private $config; |
45
|
|
|
|
46
|
|
|
public function __construct( |
47
|
|
|
EntityTitleLookup $entityTitleLookup, |
48
|
|
|
EntityIdFormatter $entityIdLabelFormatter, |
49
|
|
|
ViolationMessageRenderer $violationMessageRenderer, |
50
|
|
|
Config $config |
51
|
|
|
) { |
52
|
|
|
$this->entityTitleLookup = $entityTitleLookup; |
53
|
|
|
$this->entityIdLabelFormatter = $entityIdLabelFormatter; |
54
|
|
|
$this->violationMessageRenderer = $violationMessageRenderer; |
55
|
|
|
$this->config = $config; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param CachedCheckResults $checkResults |
60
|
|
|
* @return CachedCheckConstraintsResponse |
61
|
|
|
*/ |
62
|
|
|
public function render( CachedCheckResults $checkResults ) { |
63
|
|
|
$response = []; |
64
|
|
|
foreach ( $checkResults->getArray() as $checkResult ) { |
65
|
|
|
$resultArray = $this->checkResultToArray( $checkResult ); |
66
|
|
|
$checkResult->getContextCursor()->storeCheckResultInArray( $resultArray, $response ); |
67
|
|
|
} |
68
|
|
|
return new CachedCheckConstraintsResponse( |
69
|
|
|
$response, |
70
|
|
|
$checkResults->getMetadata() |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function checkResultToArray( CheckResult $checkResult ) { |
|
|
|
|
75
|
|
|
if ( $checkResult instanceof NullResult ) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$constraintId = $checkResult->getConstraint()->getConstraintId(); |
80
|
|
|
$typeItemId = $checkResult->getConstraint()->getConstraintTypeItemId(); |
81
|
|
|
$constraintPropertyId = new PropertyId( $checkResult->getContextCursor()->getSnakPropertyId() ); |
82
|
|
|
|
83
|
|
|
$title = $this->entityTitleLookup->getTitleForId( $constraintPropertyId ); |
84
|
|
|
$typeLabel = $this->entityIdLabelFormatter->formatEntityId( new ItemId( $typeItemId ) ); |
85
|
|
|
// TODO link to the statement when possible (T169224) |
86
|
|
|
$link = $title->getFullURL() . '#' . $this->config->get( 'WBQualityConstraintsPropertyConstraintId' ); |
87
|
|
|
|
88
|
|
|
$constraint = [ |
89
|
|
|
'id' => $constraintId, |
90
|
|
|
'type' => $typeItemId, |
91
|
|
|
'typeLabel' => $typeLabel, |
92
|
|
|
'link' => $link, |
93
|
|
|
'discussLink' => $title->getTalkPage()->getFullURL(), |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$result = [ |
97
|
|
|
'status' => $checkResult->getStatus(), |
98
|
|
|
'property' => $constraintPropertyId->getSerialization(), |
99
|
|
|
'constraint' => $constraint |
100
|
|
|
]; |
101
|
|
|
$message = $checkResult->getMessage(); |
102
|
|
|
if ( $message ) { |
103
|
|
|
$result['message-html'] = $this->violationMessageRenderer->render( $message ); |
104
|
|
|
} |
105
|
|
|
if ( $checkResult->getContextCursor()->getType() === Context::TYPE_STATEMENT ) { |
106
|
|
|
$result['claim'] = $checkResult->getContextCursor()->getStatementGuid(); |
107
|
|
|
} |
108
|
|
|
$cachingMetadataArray = $checkResult->getMetadata()->getCachingMetadata()->toArray(); |
109
|
|
|
if ( $cachingMetadataArray !== null ) { |
110
|
|
|
$result['cached'] = $cachingMetadataArray; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $result; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.