Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
72.73% |
24 / 33 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
ConstraintRepositoryStore | |
72.73% |
24 / 33 |
|
50.00% |
2 / 4 |
6.73 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
encodeConstraintParameters | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
insertBatch | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
2.00 | |||
deleteForProperty | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace WikibaseQuality\ConstraintReport; |
4 | |
5 | use Wikibase\DataModel\Entity\NumericPropertyId; |
6 | use Wikimedia\Rdbms\DBUnexpectedError; |
7 | use Wikimedia\Rdbms\ILoadBalancer; |
8 | |
9 | /** |
10 | * @author BP2014N1 |
11 | * @license GPL-2.0-or-later |
12 | */ |
13 | class ConstraintRepositoryStore implements ConstraintStore { |
14 | |
15 | /** @var ILoadBalancer */ |
16 | private $lb; |
17 | |
18 | /** @var string|false */ |
19 | private $dbName; |
20 | |
21 | /** |
22 | * @param ILoadBalancer $lb Load balancer for database connections. |
23 | * Must match $dbName, i.e. if $dbName is not false, |
24 | * then using the main DBLoadBalancer service may be incorrect. |
25 | * @param string|false $dbName Database name ($domain for ILoadBalancer methods). |
26 | */ |
27 | public function __construct( ILoadBalancer $lb, $dbName ) { |
28 | $this->lb = $lb; |
29 | $this->dbName = $dbName; |
30 | } |
31 | |
32 | private function encodeConstraintParameters( array $constraintParameters ) { |
33 | $json = json_encode( $constraintParameters, JSON_FORCE_OBJECT ); |
34 | |
35 | if ( strlen( $json ) > 50000 ) { |
36 | $json = json_encode( [ '@error' => [ 'toolong' => true ] ] ); |
37 | } |
38 | |
39 | return $json; |
40 | } |
41 | |
42 | /** |
43 | * @param Constraint[] $constraints |
44 | * |
45 | * @throws DBUnexpectedError |
46 | */ |
47 | public function insertBatch( array $constraints ) { |
48 | if ( !$constraints ) { |
49 | return; |
50 | } |
51 | |
52 | $accumulator = array_map( |
53 | function ( Constraint $constraint ) { |
54 | return [ |
55 | 'constraint_guid' => $constraint->getConstraintId(), |
56 | 'pid' => $constraint->getPropertyId()->getNumericId(), |
57 | 'constraint_type_qid' => $constraint->getConstraintTypeItemId(), |
58 | 'constraint_parameters' => $this->encodeConstraintParameters( $constraint->getConstraintParameters() ), |
59 | ]; |
60 | }, |
61 | $constraints |
62 | ); |
63 | |
64 | $dbw = $this->lb->getConnection( ILoadBalancer::DB_PRIMARY, [], $this->dbName ); |
65 | $dbw->newInsertQueryBuilder() |
66 | ->insertInto( 'wbqc_constraints' ) |
67 | ->rows( $accumulator ) |
68 | ->caller( __METHOD__ ) |
69 | ->execute(); |
70 | } |
71 | |
72 | /** |
73 | * Delete all constraints for the property ID. |
74 | * |
75 | * @param NumericPropertyId $propertyId |
76 | * |
77 | * @throws DBUnexpectedError |
78 | */ |
79 | public function deleteForProperty( NumericPropertyId $propertyId ) { |
80 | $dbw = $this->lb->getConnection( ILoadBalancer::DB_PRIMARY, [], $this->dbName ); |
81 | $dbw->newDeleteQueryBuilder() |
82 | ->deleteFrom( 'wbqc_constraints' ) |
83 | ->where( [ |
84 | 'pid' => $propertyId->getNumericId(), |
85 | ] ) |
86 | ->caller( __METHOD__ ) |
87 | ->execute(); |
88 | } |
89 | |
90 | } |