jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/369417 )
Change subject: Add Context interface and StatementContext implementation
......................................................................
Add Context interface and StatementContext implementation
A Context is a container for all the information that a
ConstraintChecker needs to check a constraint, apart from the constraint
itself. A context also knows where to store constraint check results in
an API response.
The StatementContext implementation matches the current API output
format. A later commit will add another implementation with a more
extensible API output format, which is required for T168532. It will
then be possible to switch between the API output formats by changing
the Context implementation, which should be possible to do via a
configuration change, or perhaps even via an API parameter.
The StatementContext implementation derives from an AbstractContext base
class, which contains some common functionality that other context
implementations can also use. Possible Context implementations include:
- contexts for qualifier and reference snaks, for T168532
- contexts for snaks that have not yet been saved, for T168626
- contexts for whole entities and no snak, for T163683
Bug: T171743
Change-Id: Ifec159629b6b07d0a1fd6e79c593c34989429142
---
A includes/ConstraintCheck/Context/AbstractContext.php
A includes/ConstraintCheck/Context/Context.php
A includes/ConstraintCheck/Context/StatementContext.php
3 files changed, 181 insertions(+), 0 deletions(-)
Approvals:
Jonas Kress (WMDE): Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/ConstraintCheck/Context/AbstractContext.php
b/includes/ConstraintCheck/Context/AbstractContext.php
new file mode 100644
index 0000000..5f0f39a
--- /dev/null
+++ b/includes/ConstraintCheck/Context/AbstractContext.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Context;
+
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Snak\Snak;
+use Wikibase\DataModel\Statement\Statement;
+
+/**
+ * Base implementation of some Context functions,
+ * given a snak and an entity.
+ */
+abstract class AbstractContext implements Context {
+
+ /**
+ * @type EntityDocument
+ */
+ protected $entity;
+
+ /**
+ * @type Snak
+ */
+ protected $snak;
+
+ public function __construct(
+ EntityDocument $entity,
+ Snak $snak
+ ) {
+ $this->entity = $entity;
+ $this->snak = $snak;
+ }
+
+ public function getSnak() {
+ return $this->snak;
+ }
+
+ public function getEntity() {
+ return $this->entity;
+ }
+
+ // unimplemented: getType
+
+ public function getSnakRank() {
+ return null;
+ }
+
+ public function getSnakStatement() {
+ return null;
+ }
+
+ // unimplemented: storeCheckResultInArray
+
+}
diff --git a/includes/ConstraintCheck/Context/Context.php
b/includes/ConstraintCheck/Context/Context.php
new file mode 100644
index 0000000..e38bb82
--- /dev/null
+++ b/includes/ConstraintCheck/Context/Context.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Context;
+
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Snak\Snak;
+use Wikibase\DataModel\Statement\Statement;
+
+/**
+ * A context in which a constraint check can run.
+ */
+interface Context {
+
+ /**
+ * The snak that is being checked.
+ *
+ * @return Snak
+ */
+ public function getSnak();
+
+ /**
+ * The entity that is being checked.
+ *
+ * @return EntityDocument
+ */
+ public function getEntity();
+
+ /**
+ * The type / role of the snak that is being checked within this
context.
+ * Not to be confused with the snak’s own type
(value/somevalue/novalue).
+ *
+ * @return string 'statement', 'qualifier', 'reference', or something
else
+ */
+ public function getType();
+
+ /**
+ * The rank of the snak that is being checked.
+ * Only the main snak of a statement has a rank.
+ *
+ * @return integer|null One of the Statement::RANK_* constants
+ * if this is a 'statement' context,
+ * or null if it’s any other type of context.
+ */
+ public function getSnakRank();
+
+ /**
+ * The statement that this snak is the main snak of.
+ * Only the snak of a 'statement' context has a statement.
+ *
+ * @return Statement|null The statement if this is a 'statement'
context,
+ * or null if it’s any other type of context.
+ */
+ public function getSnakStatement();
+
+ /**
+ * Store the check result serialization $result
+ * at the appropriate location for this context in $container.
+ *
+ * Mainly used in the API, where $container is part of the API response.
+ *
+ * @param array $result
+ * @param array &$container
+ */
+ public function storeCheckResultInArray( array $result, array
&$container );
+
+}
diff --git a/includes/ConstraintCheck/Context/StatementContext.php
b/includes/ConstraintCheck/Context/StatementContext.php
new file mode 100644
index 0000000..b18b750
--- /dev/null
+++ b/includes/ConstraintCheck/Context/StatementContext.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Context;
+
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Statement\Statement;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
+
+/**
+ * A constraint check context for the main snak of a statement.
+ *
+ * The result format used by storeCheckResultInArray() is only suitable
+ * if no other kinds of contexts are to be stored in the same container.
+ */
+class StatementContext extends AbstractContext {
+
+ /**
+ * @type Statement
+ */
+ private $statement;
+
+ /**
+ * @param EntityDocument $entity
+ * @param Statement $statement
+ */
+ public function __construct(
+ EntityDocument $entity,
+ Statement $statement
+ ) {
+ parent::__construct( $entity, $statement->getMainSnak() );
+ $this->statement = $statement;
+ }
+
+ public function getType() {
+ return 'statement';
+ }
+
+ public function getSnakRank() {
+ return $this->statement->getRank();
+ }
+
+ public function getSnakStatement() {
+ return $this->statement;
+ }
+
+ /**
+ * The $container is keyed by entity ID, then by property ID,
+ * then by claim ID, and then contains the $result in a list:
+ * { "Q1": { "P1": { "Q1$1a2b...": [ { "status": "compliance", ... } ]
} } }
+ *
+ * @param array $result
+ * @param array &$container
+ */
+ public function storeCheckResultInArray( array $result, array
&$container ) {
+ $entityId = $this->entity->getId()->getSerialization();
+ $propertyId =
$this->statement->getPropertyId()->getSerialization();
+ $statementId = $this->statement->getGuid();
+
+ $container[$entityId][$propertyId][$statementId][] = $result;
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/369417
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ifec159629b6b07d0a1fd6e79c593c34989429142
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits