/
UpdateConstraintsTableJob.php
152 lines (133 loc) · 4.2 KB
/
UpdateConstraintsTableJob.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace WikibaseQuality\ConstraintReport;
use Config;
use Job;
use Serializers\Serializer;
use Title;
use MediaWiki\MediaWikiServices;
use Wikibase\DataModel\Entity\Property;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Services\Lookup\EntityLookup;
use Wikibase\DataModel\Snak\SnakList;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\Repo\WikibaseRepo;
use Wikimedia\Assert\Assert;
/**
* A job that updates the constraints table
* when changes were made on a property.
*
* @package WikibaseQuality\ConstraintReport;
* @author Lucas Werkmeister
* @license GNU GPL v2+
*/
class UpdateConstraintsTableJob extends Job {
const BATCH_SIZE = 10;
public static function newFromGlobalState( Title $title, array $params ) {
Assert::parameterType( 'string', $params['propertyId'], '$params["propertyId"]' );
$repo = WikibaseRepo::getDefaultInstance();
return new UpdateConstraintsTableJob(
$title,
$params,
$params['propertyId'],
MediaWikiServices::getInstance()->getMainConfig(),
ConstraintReportFactory::getDefaultInstance()->getConstraintRepository(),
$repo->getEntityLookup(),
$repo->getBaseDataModelSerializerFactory()->newSnakSerializer()
);
}
/**
* @var string
*/
private $propertyId;
/**
* @var Config
*/
private $config;
/**
* @var ConstraintRepository
*/
private $constraintRepo;
/**
* @var EntityLookup
*/
private $entityLookup;
/**
* @var Serializer
*/
private $snakSerializer;
/**
* @param Title $title
* @param string[] $params should contain 'propertyId' => 'P...'
* @param string $propertyId property ID of the property for this job (which has the constraint statements)
* @param Config $config
* @param ConstraintRepository $constraintRepo
* @param EntityLookup $entityLookup
* @param Serializer $snakSerializer
*/
public function __construct(
Title $title,
array $params,
$propertyId,
Config $config,
ConstraintRepository $constraintRepo,
EntityLookup $entityLookup,
Serializer $snakSerializer
) {
parent::__construct( 'constraintsTableUpdate', $title, $params );
$this->propertyId = $propertyId;
$this->config = $config;
$this->constraintRepo = $constraintRepo;
$this->entityLookup = $entityLookup;
$this->snakSerializer = $snakSerializer;
}
public function extractParametersFromQualifiers( SnakList $qualifiers ) {
$parameters = [];
foreach ( $qualifiers as $qualifier ) {
$qualifierId = $qualifier->getPropertyId()->getSerialization();
$paramSerialization = $this->snakSerializer->serialize( $qualifier );
$parameters[$qualifierId][] = $paramSerialization;
}
return $parameters;
}
public function extractConstraintFromStatement(
PropertyId $propertyId,
Statement $constraintStatement
) {
$constraintId = $constraintStatement->getGuid();
$constraintTypeQid = $constraintStatement->getMainSnak()->getDataValue()->getEntityId()->getSerialization();
$parameters = $this->extractParametersFromQualifiers( $constraintStatement->getQualifiers() );
return new Constraint(
$constraintId,
$propertyId,
$constraintTypeQid,
$parameters
);
}
public function importConstraintsForProperty(
Property $property,
ConstraintRepository $constraintRepo,
PropertyId $propertyConstraintPropertyId
) {
$constraintsStatements = $property->getStatements()->getByPropertyId( $propertyConstraintPropertyId );
$constraints = [];
foreach ( $constraintsStatements->getIterator() as $constraintStatement ) {
$constraints[] = $this->extractConstraintFromStatement( $property->getId(), $constraintStatement );
if ( count( $constraints ) >= self::BATCH_SIZE ) {
$constraintRepo->insertBatch( $constraints );
$constraints = [];
}
}
$constraintRepo->insertBatch( $constraints );
}
public function run() {
// TODO in the future: only touch constraints affected by the edit (requires T163465)
$propertyId = new PropertyId( $this->propertyId );
$this->constraintRepo->deleteForPropertyWhereConstraintIdIsStatementId( $propertyId );
$property = $this->entityLookup->getEntity( $propertyId );
$this->importConstraintsForProperty(
$property,
$this->constraintRepo,
new PropertyId( $this->config->get( 'WBQualityConstraintsPropertyConstraintId' ) )
);
}
}