Tamslo has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/235245

Change subject: [DNM] Began to implement API for constraints.
......................................................................

[DNM] Began to implement API for constraints.

[Cherry-picked from v1] This should be a foundation to work upon.
* defined API module in WikibaseQualityConstraints.php
* added namespace in composer.json
* added class RunConstraintCheck
* RunConstraintCheck does not write results yet -> need to implement serializers

Bug: T102757
Change-Id: I18a73be3da8539766364ac739ae6a976106ccc24
---
M WikibaseQualityConstraints.php
A api/RunConstraintCheck.php
M composer.json
3 files changed, 151 insertions(+), 1 deletion(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
 refs/changes/45/235245/1

diff --git a/WikibaseQualityConstraints.php b/WikibaseQualityConstraints.php
index 4a3e0f7..6530231 100755
--- a/WikibaseQualityConstraints.php
+++ b/WikibaseQualityConstraints.php
@@ -44,4 +44,11 @@
 
        // Jobs
        $GLOBALS['wgJobClasses']['evaluateConstraintReportJob'] = 
'WikibaseQuality\ConstraintReport\EvaluateConstraintReportJob';
+
+       // Define API modules
+       $GLOBALS['wgAPIModules']['wbqcconstraintcheck'] = array(
+               'class' => 
'WikibaseQuality\ConstraintReport\Api\RunConstraintCheck',
+               'factory' => 
'WikibaseQuality\ConstraintReport\Api\RunConstraintCheck::newFromGlobalState'
+       );
+
 } );
\ No newline at end of file
diff --git a/api/RunConstraintCheck.php b/api/RunConstraintCheck.php
new file mode 100644
index 0000000..e04084b
--- /dev/null
+++ b/api/RunConstraintCheck.php
@@ -0,0 +1,142 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\Api;
+
+use ApiBase;
+use ApiMain;
+use Wikibase\Repo\WikibaseRepo;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Services\EntityId\EntityIdParser;
+use Wikibase\DataModel\Services\Lookup\EntityLookup;
+use 
WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
+use WikibaseQuality\ConstraintReport\ConstraintReportFactory;
+
+
+/**
+ * API module that performs constraint-checks of entities or claims.
+ *
+ * @package WikibaseQuality\ExternalValidation\Api
+ * @author BP2014N1
+ * @license GNU GPL v2+
+ */
+class RunConstraintCheck extends ApiBase {
+
+       /**
+        * @var EntityIdParser
+        */
+       private $idParser;
+
+       /**
+        * @var EntityLookup
+        */
+       private $entityLookup;
+
+       /**
+        * Creates new instance from global state.
+        *
+        * @param ApiMain $main
+        * @param $name
+        * @param string $prefix
+        * @return RunConstraintCheck
+        */
+       public static function newFromGlobalState( ApiMain $main, $name, 
$prefix = '' ) {
+               $repo = WikibaseRepo::getDefaultInstance();
+               $constraintReportFactory = 
ConstraintReportFactory::getDefaultInstance();
+
+               return new self(
+                       $main,
+                       $name,
+                       $prefix,
+                       $repo->getEntityIdParser(),
+                       $constraintReportFactory->getConstraintChecker(),
+                       $repo->getEntityLookup()
+               );
+       }
+
+       /**
+        * @var DelegatingConstraintChecker
+        */
+       private $constraintChecker;
+
+       public function __construct( ApiMain $main, $name, $prefix = '', 
EntityIdParser $idParser,
+                                                                
DelegatingConstraintChecker $constraintChecker, EntityLookup $entityLookup) {
+               parent::__construct($main, $name, $prefix);
+
+               $this->$idParser = $idParser;
+               $this->$entityLookup = $entityLookup;
+               $this->constraintChecker = $constraintChecker;
+       }
+
+       /**
+        * Evaluates the parameters, runs the requested constraint-check, and 
sets up the result
+        */
+       public function execute() {
+               $params = $this->extractRequestParams();
+               $entityIds = $this->extractEntityIds( $params );
+
+               $checkResults = array();
+
+               foreach ( $entityIds as $entityId ) {
+                       $entity = $this->entityLookup->getEntity( $entityId );
+
+                       if ( $entity ) {
+                               $checkResult = 
$this->constraintChecker->checkAgainstConstraints( $entity );
+
+                               if ( $checkResult ) {
+                                       $checkResults[] = $checkResult;
+                               }
+                       }
+               }
+
+               $this->writeResultOutput( $checkResults );
+       }
+
+       private function writeResultOutput( array $checkResults ) {
+               // TODO: example how to do it in 
ExternalValidation\Api\RunCrossCheck
+               // TODO: Serializer needed (examples also in ExternalValidation)
+       }
+
+       /**
+        * @param array $params
+        *
+        * @return array
+        */
+       private function extractEntityIds( array $params ) {
+               $entityIds = array_map(
+                       array( $this->idParser, 'parse' ),
+                       $params['entities']
+               );
+
+               return $entityIds;
+       }
+
+       /**
+        * Returns an array of allowed parameters
+        *
+        * @return array
+        * @codeCoverageIgnore
+        */
+       public function getAllowedParams() {
+               return array(
+                       'entities' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_REQUIRED => true
+                       )
+               );
+       }
+
+       /**
+        * Returns usage examples for this module
+        *
+        * @return array
+        * @codeCoverageIgnore
+        */
+       public function getExamplesMessages() {
+               return array(
+                       'action=wbqcconstraintcheck&entities=Q76' => 
'apihelp-wbcconstraintcheck-examples-1',
+                       'action=wbqcconstraintcheck&entities=Q76|Q42' => 
'apihelp-wbcconstraintcheck-examples-2'
+               );
+       }
+
+}
\ No newline at end of file
diff --git a/composer.json b/composer.json
index d02b18a..b3e99d2 100644
--- a/composer.json
+++ b/composer.json
@@ -28,7 +28,8 @@
                        "WikibaseQuality\\ConstraintReport\\": "includes/",
                        "WikibaseQuality\\ConstraintReport\\Specials\\": 
"specials/",
                        "WikibaseQuality\\ConstraintReport\\Tests\\": 
"tests/phpunit/",
-                       "WikibaseQuality\\ConstraintReport\\Maintenance\\": 
"maintenance/"
+                       "WikibaseQuality\\ConstraintReport\\Maintenance\\": 
"maintenance/",
+                       "WikibaseQuality\\ConstraintReport\\Api\\": "api/"
                },
                "classmap": [
                        "WikibaseQualityConstraintsHooks.php",

-- 
To view, visit https://gerrit.wikimedia.org/r/235245
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I18a73be3da8539766364ac739ae6a976106ccc24
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Tamslo <tamaraslosa...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to