|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\Lexeme; |
|
4
|
|
|
|
|
5
|
|
|
use ExtensionRegistry; |
|
6
|
|
|
use Wikibase\DataModel\Statement\Statement; |
|
7
|
|
|
use Wikibase\Lexeme\Domain\Model\Lexeme; |
|
8
|
|
|
use WikibaseQuality\ConstraintReport\Constraint; |
|
9
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\ConstraintChecker; |
|
10
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context; |
|
11
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException; |
|
12
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser; |
|
13
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Message\ViolationMessage; |
|
14
|
|
|
use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult; |
|
15
|
|
|
use WikibaseQuality\ConstraintReport\Role; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @license GPL-2.0-or-later |
|
19
|
|
|
*/ |
|
20
|
|
|
class LanguageChecker implements ConstraintChecker { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ConstraintParameterParser |
|
24
|
|
|
*/ |
|
25
|
|
|
private $constraintParameterParser; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
ConstraintParameterParser $constraintParameterParser |
|
29
|
|
|
) { |
|
30
|
|
|
$this->constraintParameterParser = $constraintParameterParser; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @codeCoverageIgnore This method is purely declarative. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getSupportedContextTypes() { |
|
37
|
|
|
return [ |
|
38
|
|
|
Context::TYPE_STATEMENT => CheckResult::STATUS_COMPLIANCE, |
|
39
|
|
|
Context::TYPE_QUALIFIER => CheckResult::STATUS_COMPLIANCE, |
|
40
|
|
|
Context::TYPE_REFERENCE => CheckResult::STATUS_COMPLIANCE, |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @codeCoverageIgnore This method is purely declarative. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getDefaultContextTypes() { |
|
48
|
|
|
return [ |
|
49
|
|
|
Context::TYPE_STATEMENT, |
|
50
|
|
|
Context::TYPE_QUALIFIER, |
|
51
|
|
|
Context::TYPE_REFERENCE, |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Checks 'Language' constraint. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Context $context |
|
59
|
|
|
* @param Constraint $constraint |
|
60
|
|
|
* |
|
61
|
|
|
* @throws ConstraintParameterException |
|
62
|
|
|
* @return CheckResult |
|
63
|
|
|
*/ |
|
64
|
|
|
public function checkConstraint( Context $context, Constraint $constraint ) { |
|
65
|
|
|
if ( !ExtensionRegistry::getInstance()->isLoaded( 'WikibaseLexeme' ) ) { |
|
66
|
|
|
return new CheckResult( $context, $constraint, [], CheckResult::STATUS_NOT_IN_SCOPE ); |
|
67
|
|
|
} |
|
68
|
|
|
$entityType = $context->getEntity()->getType(); |
|
69
|
|
|
if ( $entityType !== Lexeme::ENTITY_TYPE ) { |
|
70
|
|
|
return new CheckResult( $context, $constraint, [], CheckResult::STATUS_NOT_IN_SCOPE ); |
|
71
|
|
|
} |
|
72
|
|
|
if ( $context->getSnakRank() === Statement::RANK_DEPRECATED ) { |
|
73
|
|
|
return new CheckResult( $context, $constraint, [], CheckResult::STATUS_DEPRECATED ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$parameters = []; |
|
77
|
|
|
$constraintParameters = $constraint->getConstraintParameters(); |
|
78
|
|
|
$constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
|
79
|
|
|
|
|
80
|
|
|
$languages = $this->constraintParameterParser->parseItemsParameter( |
|
81
|
|
|
$constraintParameters, |
|
82
|
|
|
$constraintTypeItemId, |
|
83
|
|
|
true |
|
84
|
|
|
); |
|
85
|
|
|
$parameters['languages'] = $languages; |
|
86
|
|
|
|
|
87
|
|
|
$message = ( new ViolationMessage( 'wbqc-violation-message-language' ) ) |
|
88
|
|
|
->withEntityId( $context->getSnak()->getPropertyId(), Role::PREDICATE ) |
|
89
|
|
|
->withItemIdSnakValueList( $languages, Role::OBJECT ); |
|
90
|
|
|
$status = CheckResult::STATUS_VIOLATION; |
|
91
|
|
|
/** @var Lexeme $lexeme */ |
|
92
|
|
|
$lexeme = $context->getEntity(); |
|
93
|
|
|
'@phan-var Lexeme $lexeme'; |
|
94
|
|
|
|
|
95
|
|
|
foreach ( $languages as $language ) { |
|
96
|
|
|
if ( $language->isNoValue() || $language->isSomeValue() ) { |
|
97
|
|
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
if ( $lexeme->getLanguage()->equals( $language->getItemId() ) ) { |
|
100
|
|
|
$message = null; |
|
101
|
|
|
$status = CheckResult::STATUS_COMPLIANCE; |
|
102
|
|
|
break; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return new CheckResult( $context, $constraint, $parameters, $status, $message ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function checkConstraintParameters( Constraint $constraint ): array { |
|
110
|
|
|
$constraintParameters = $constraint->getConstraintParameters(); |
|
111
|
|
|
$constraintTypeItemId = $constraint->getConstraintTypeItemId(); |
|
112
|
|
|
$exceptions = []; |
|
113
|
|
|
try { |
|
114
|
|
|
$this->constraintParameterParser->parseItemsParameter( |
|
115
|
|
|
$constraintParameters, |
|
116
|
|
|
$constraintTypeItemId, |
|
117
|
|
|
true |
|
118
|
|
|
); |
|
119
|
|
|
} catch ( ConstraintParameterException $e ) { |
|
120
|
|
|
$exceptions[] = $e; |
|
121
|
|
|
} |
|
122
|
|
|
return $exceptions; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|