Completed
Push — master ( 458634...d47244 )
by
unknown
02:02 queued 11s
created

ConstraintRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace WikibaseQuality\ConstraintReport;
4
5
use MediaWiki\Logger\LoggerFactory;
6
use Wikimedia\Rdbms\DBUnexpectedError;
7
use Wikimedia\Rdbms\ILoadBalancer;
8
use Wikimedia\Rdbms\IResultWrapper;
9
use Wikibase\DataModel\Entity\PropertyId;
10
11
/**
12
 * @author BP2014N1
13
 * @license GPL-2.0-or-later
14
 */
15
class ConstraintRepository implements ConstraintLookup {
16
17
	/** @var ILoadBalancer */
18
	private $lb;
19
20
	public function __construct( ILoadBalancer $lb ) {
21
		$this->lb = $lb;
22
	}
23
24
	/**
25
	 * @param PropertyId $propertyId
26
	 *
27
	 * @return Constraint[]
28
	 */
29 View Code Duplication
	public function queryConstraintsForProperty( PropertyId $propertyId ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
30
		$dbr = $this->lb->getConnection( ILoadBalancer::DB_REPLICA );
31
32
		$results = $dbr->select(
33
			'wbqc_constraints',
34
			'*',
35
			[ 'pid' => $propertyId->getNumericId() ]
36
		);
37
38
		return $this->convertToConstraints( $results );
39
	}
40
41
	private function encodeConstraintParameters( array $constraintParameters ) {
42
		$json = json_encode( $constraintParameters, JSON_FORCE_OBJECT );
43
44
		if ( strlen( $json ) > 50000 ) {
45
			$json = json_encode( [ '@error' => [ 'toolong' => true ] ] );
46
		}
47
48
		return $json;
49
	}
50
51
	/**
52
	 * @param Constraint[] $constraints
53
	 *
54
	 * @throws DBUnexpectedError
55
	 * @return bool
56
	 */
57
	public function insertBatch( array $constraints ) {
58
		$accumulator = array_map(
59
			function ( Constraint $constraint ) {
60
				return [
61
					'constraint_guid' => $constraint->getConstraintId(),
62
					'pid' => $constraint->getPropertyId()->getNumericId(),
63
					'constraint_type_qid' => $constraint->getConstraintTypeItemId(),
64
					'constraint_parameters' => $this->encodeConstraintParameters( $constraint->getConstraintParameters() )
65
				];
66
			},
67
			$constraints
68
		);
69
70
		$dbw = $this->lb->getConnection( ILoadBalancer::DB_MASTER );
71
		return $dbw->insert( 'wbqc_constraints', $accumulator );
72
	}
73
74
	/**
75
	 * Delete all constraints for the property ID.
76
	 *
77
	 * @param PropertyId $propertyId
78
	 *
79
	 * @throws DBUnexpectedError
80
	 */
81 View Code Duplication
	public function deleteForProperty( PropertyId $propertyId ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
82
		$dbw = $this->lb->getConnection( ILoadBalancer::DB_MASTER );
83
		$dbw->delete(
84
			'wbqc_constraints',
85
			[
86
				'pid' => $propertyId->getNumericId(),
87
			]
88
		);
89
	}
90
91
	/**
92
	 * @param IResultWrapper $results
93
	 *
94
	 * @return Constraint[]
95
	 */
96
	private function convertToConstraints( IResultWrapper $results ) {
97
		$constraints = [];
98
		foreach ( $results as $result ) {
99
			$constraintTypeItemId = $result->constraint_type_qid;
100
			$constraintParameters = json_decode( $result->constraint_parameters, true );
101
102
			if ( $constraintParameters === null ) {
103
				// T171295
104
				LoggerFactory::getInstance( 'WikibaseQualityConstraints' )
105
					->warning( 'Constraint {constraintId} has invalid constraint parameters.', [
106
						'method' => __METHOD__,
107
						'constraintId' => $result->constraint_guid,
108
						'constraintParameters' => $result->constraint_parameters,
109
					] );
110
				$constraintParameters = [ '@error' => [ /* unknown */ ] ];
111
			}
112
113
			$constraints[] = new Constraint(
114
				$result->constraint_guid,
115
				PropertyId::newFromNumber( $result->pid ),
116
				$constraintTypeItemId,
117
				$constraintParameters
118
			);
119
		}
120
		return $constraints;
121
	}
122
123
}
124