Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
50 / 50 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
CheckResultsRenderer | |
100.00% |
50 / 50 |
|
100.00% |
4 / 4 |
13 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
checkResultToArray | |
100.00% |
33 / 33 |
|
100.00% |
1 / 1 |
7 | |||
renderConstraintClarification | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace WikibaseQuality\ConstraintReport\Api; |
6 | |
7 | use Wikibase\DataModel\Entity\ItemId; |
8 | use Wikibase\DataModel\Entity\NumericPropertyId; |
9 | use Wikibase\DataModel\Services\EntityId\EntityIdFormatter; |
10 | use Wikibase\Lib\Store\EntityTitleLookup; |
11 | use Wikibase\Lib\TermLanguageFallbackChain; |
12 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedCheckConstraintsResponse; |
13 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedCheckResults; |
14 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
15 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessageRenderer; |
16 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
17 | use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\NullResult; |
18 | |
19 | /** |
20 | * Converts check results into arrays and stores them inside one big response array. |
21 | * |
22 | * @author Lucas Werkmeister |
23 | * @license GPL-2.0-or-later |
24 | */ |
25 | class CheckResultsRenderer { |
26 | |
27 | private EntityTitleLookup $entityTitleLookup; |
28 | private EntityIdFormatter $entityIdLabelFormatter; |
29 | private TermLanguageFallbackChain $languageFallbackChain; |
30 | private ViolationMessageRenderer $violationMessageRenderer; |
31 | |
32 | public function __construct( |
33 | EntityTitleLookup $entityTitleLookup, |
34 | EntityIdFormatter $entityIdLabelFormatter, |
35 | TermLanguageFallbackChain $languageFallbackChain, |
36 | ViolationMessageRenderer $violationMessageRenderer |
37 | ) { |
38 | $this->entityTitleLookup = $entityTitleLookup; |
39 | $this->entityIdLabelFormatter = $entityIdLabelFormatter; |
40 | $this->languageFallbackChain = $languageFallbackChain; |
41 | $this->violationMessageRenderer = $violationMessageRenderer; |
42 | } |
43 | |
44 | public function render( CachedCheckResults $checkResults ): CachedCheckConstraintsResponse { |
45 | $response = []; |
46 | foreach ( $checkResults->getArray() as $checkResult ) { |
47 | $resultArray = $this->checkResultToArray( $checkResult ); |
48 | $checkResult->getContextCursor()->storeCheckResultInArray( $resultArray, $response ); |
49 | } |
50 | return new CachedCheckConstraintsResponse( |
51 | $response, |
52 | $checkResults->getMetadata() |
53 | ); |
54 | } |
55 | |
56 | public function checkResultToArray( CheckResult $checkResult ): ?array { |
57 | if ( $checkResult instanceof NullResult ) { |
58 | return null; |
59 | } |
60 | |
61 | $constraintId = $checkResult->getConstraint()->getConstraintId(); |
62 | $typeItemId = $checkResult->getConstraint()->getConstraintTypeItemId(); |
63 | $constraintPropertyId = new NumericPropertyId( $checkResult->getContextCursor()->getSnakPropertyId() ); |
64 | |
65 | $title = $this->entityTitleLookup->getTitleForId( $constraintPropertyId ); |
66 | $talkTitle = $title->getTalkPageIfDefined(); |
67 | $typeLabel = $this->entityIdLabelFormatter->formatEntityId( new ItemId( $typeItemId ) ); |
68 | $link = $title->getFullURL() . '#' . $constraintId; |
69 | |
70 | $constraint = [ |
71 | 'id' => $constraintId, |
72 | 'type' => $typeItemId, |
73 | 'typeLabel' => $typeLabel, |
74 | 'link' => $link, |
75 | 'discussLink' => $talkTitle ? $talkTitle->getFullURL() : $title->getFullURL(), |
76 | ]; |
77 | |
78 | $result = [ |
79 | 'status' => $checkResult->getStatus(), |
80 | 'property' => $constraintPropertyId->getSerialization(), |
81 | 'constraint' => $constraint, |
82 | ]; |
83 | $message = $checkResult->getMessage(); |
84 | if ( $message ) { |
85 | $result['message-html'] = $this->violationMessageRenderer->render( $message ); |
86 | } |
87 | $constraintClarification = $this->renderConstraintClarification( $checkResult ); |
88 | if ( $constraintClarification !== null ) { |
89 | $result['constraint-clarification'] = $constraintClarification; |
90 | } |
91 | if ( $checkResult->getContextCursor()->getType() === Context::TYPE_STATEMENT ) { |
92 | $result['claim'] = $checkResult->getContextCursor()->getStatementGuid(); |
93 | } |
94 | $cachingMetadataArray = $checkResult->getMetadata()->getCachingMetadata()->toArray(); |
95 | if ( $cachingMetadataArray !== null ) { |
96 | $result['cached'] = $cachingMetadataArray; |
97 | } |
98 | |
99 | return $result; |
100 | } |
101 | |
102 | private function renderConstraintClarification( CheckResult $result ): ?string { |
103 | $texts = $result->getConstraintClarification()->getTexts(); |
104 | foreach ( $this->languageFallbackChain->getFetchLanguageCodes() as $languageCode ) { |
105 | if ( array_key_exists( $languageCode, $texts ) ) { |
106 | return $texts[$languageCode]->getText(); |
107 | } |
108 | } |
109 | |
110 | return null; |
111 | } |
112 | |
113 | } |