Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ConstraintRepositoryLookup | |
100.00% |
34 / 34 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
queryConstraintsForProperty | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
convertToConstraints | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport; |
4 | |
5 | use MediaWiki\Logger\LoggerFactory; |
6 | use Wikibase\DataModel\Entity\NumericPropertyId; |
7 | use Wikimedia\Rdbms\ILoadBalancer; |
8 | use Wikimedia\Rdbms\IResultWrapper; |
9 | |
10 | /** |
11 | * @author BP2014N1 |
12 | * @license GPL-2.0-or-later |
13 | */ |
14 | class ConstraintRepositoryLookup implements ConstraintLookup { |
15 | |
16 | /** @var ILoadBalancer */ |
17 | private $lb; |
18 | |
19 | /** @var string|false */ |
20 | private $dbName; |
21 | |
22 | /** |
23 | * @param ILoadBalancer $lb Load balancer for database connections. |
24 | * Must match $dbName, i.e. if $dbName is not false, |
25 | * then using the main DBLoadBalancer service may be incorrect. |
26 | * @param string|false $dbName Database name ($domain for ILoadBalancer methods). |
27 | */ |
28 | public function __construct( ILoadBalancer $lb, $dbName ) { |
29 | $this->lb = $lb; |
30 | $this->dbName = $dbName; |
31 | } |
32 | |
33 | /** |
34 | * @param NumericPropertyId $propertyId |
35 | * |
36 | * @return Constraint[] |
37 | */ |
38 | public function queryConstraintsForProperty( NumericPropertyId $propertyId ) { |
39 | $dbr = $this->lb->getConnection( ILoadBalancer::DB_REPLICA, [], $this->dbName ); |
40 | |
41 | $results = $dbr->newSelectQueryBuilder() |
42 | ->select( [ |
43 | 'constraint_type_qid', |
44 | 'constraint_parameters', |
45 | 'constraint_guid', |
46 | 'pid', |
47 | ] ) |
48 | ->from( 'wbqc_constraints' ) |
49 | ->where( [ 'pid' => $propertyId->getNumericId() ] ) |
50 | ->caller( __METHOD__ ) |
51 | ->fetchResultSet(); |
52 | |
53 | return $this->convertToConstraints( $results ); |
54 | } |
55 | |
56 | /** |
57 | * @param IResultWrapper $results |
58 | * |
59 | * @return Constraint[] |
60 | */ |
61 | private function convertToConstraints( IResultWrapper $results ) { |
62 | $constraints = []; |
63 | $logger = LoggerFactory::getInstance( 'WikibaseQualityConstraints' ); |
64 | foreach ( $results as $result ) { |
65 | $constraintTypeItemId = $result->constraint_type_qid; |
66 | $constraintParameters = json_decode( $result->constraint_parameters, true ); |
67 | |
68 | if ( $constraintParameters === null ) { |
69 | // T171295 |
70 | $logger->warning( 'Constraint {constraintId} has invalid constraint parameters.', [ |
71 | 'method' => __METHOD__, |
72 | 'constraintId' => $result->constraint_guid, |
73 | 'constraintParameters' => $result->constraint_parameters, |
74 | ] ); |
75 | $constraintParameters = [ '@error' => [ /* unknown */ ] ]; |
76 | } |
77 | |
78 | $constraints[] = new Constraint( |
79 | $result->constraint_guid, |
80 | NumericPropertyId::newFromNumber( $result->pid ), |
81 | $constraintTypeItemId, |
82 | $constraintParameters |
83 | ); |
84 | } |
85 | return $constraints; |
86 | } |
87 | |
88 | } |