jenkins-bot has submitted this change and it was merged.

Change subject: Add snak validation to wbcreateclaim module.
......................................................................


Add snak validation to wbcreateclaim module.

Prevent invalid snaks to be created via the CreateClaim module.

Change-Id: I49216858fa1b077285a71e6fe206a773aa7c9de7
---
M repo/includes/api/CreateClaim.php
M repo/tests/phpunit/includes/api/CreateClaimTest.php
2 files changed, 35 insertions(+), 12 deletions(-)

Approvals:
  Aude: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/includes/api/CreateClaim.php 
b/repo/includes/api/CreateClaim.php
index 0beb963..9267bda 100644
--- a/repo/includes/api/CreateClaim.php
+++ b/repo/includes/api/CreateClaim.php
@@ -4,9 +4,9 @@
 
 use ApiBase, MWException;
 
+use ApiMain;
 use DataValues\IllegalValueException;
 use InvalidArgumentException;
-use UsageException;
 use ValueParsers\ParseException;
 use Wikibase\EntityId;
 use Wikibase\Entity;
@@ -14,13 +14,12 @@
 use Wikibase\EntityContentFactory;
 use Wikibase\Property;
 use Wikibase\Repo\WikibaseRepo;
-use Wikibase\SnakFactory;
 use Wikibase\LibRegistry;
 use Wikibase\Claim;
 use Wikibase\Autocomment;
-use Wikibase\Settings;
 use Wikibase\Summary;
 use Wikibase\Snak;
+use Wikibase\Validators\ValidatorErrorLocalizer;
 
 /**
  * API module for creating claims.
@@ -51,7 +50,29 @@
  */
 class CreateClaim extends ModifyClaim {
 
-       // TODO: rights
+
+       /**
+        * @var SnakValidationHelper
+        */
+       protected $snakValidation;
+
+       /**
+        * see ApiBase::__construct()
+        *
+        * @param ApiMain $mainModule
+        * @param string  $moduleName
+        * @param string  $modulePrefix
+        */
+       public function __construct( ApiMain $mainModule, $moduleName, 
$modulePrefix = '' ) {
+               parent::__construct( $mainModule, $moduleName, $modulePrefix );
+
+               $this->snakValidation = new SnakValidationHelper(
+                       $this,
+                       
WikibaseRepo::getDefaultInstance()->getPropertyDataTypeLookup(),
+                       
WikibaseRepo::getDefaultInstance()->getDataTypeFactory(),
+                       new ValidatorErrorLocalizer()
+               );
+       }
 
        /**
         * @see \ApiBase::execute
@@ -85,6 +106,8 @@
                        $this->dieUsage( $parseException->getMessage(), 
'claim-invalid-guid' );
                }
 
+               $this->snakValidation->validateSnak( $snak );
+
                $claim = $this->addClaim( $entityContent->getEntity(), $snak );
                $summary = $this->createSummary( $snak, 'create' );
 
diff --git a/repo/tests/phpunit/includes/api/CreateClaimTest.php 
b/repo/tests/phpunit/includes/api/CreateClaimTest.php
index f19fe58..ba4efcc 100644
--- a/repo/tests/phpunit/includes/api/CreateClaimTest.php
+++ b/repo/tests/phpunit/includes/api/CreateClaimTest.php
@@ -76,7 +76,7 @@
                        'entity' => $this->getFormattedIdForEntity( $entity ),
                        'snaktype' => 'value',
                        'property' => $this->getFormattedIdForEntity( $property 
),
-                       'value' => '"foo"',
+                       'value' => '"Foo.png"',
                        'token' => $GLOBALS['wgUser']->getEditToken()
                );
 
@@ -111,7 +111,7 @@
                        'entity' => 'q0',
                        'snaktype' => 'value',
                        'property' => '-',
-                       'value' => '"foo"',
+                       'value' => '"Foo.png"',
                );
 
                $argLists[] = array( 'cant-load-entity-content', $params );
@@ -121,7 +121,7 @@
                        'entity' => '-',
                        'snaktype' => 'value',
                        'property' => 'q0',
-                       'value' => '"foo"',
+                       'value' => '"Foo.png"',
                );
 
                $argLists[] = array( 'claim-invalid-snak', $params );
@@ -131,7 +131,7 @@
                        'entity' => '-',
                        'snaktype' => 'hax',
                        'property' => '-',
-                       'value' => '"foo"',
+                       'value' => '"Foo.png"',
                );
 
                $argLists[] = array( 'unknown_snaktype', $params );
@@ -142,7 +142,7 @@
                                'entity' => '-',
                                'snaktype' => 'value',
                                'property' => '-',
-                               'value' => '"foo"',
+                               'value' => '"Foo.png"',
                        );
 
                        unset( $params[$requiredParam] );
@@ -154,7 +154,7 @@
                        'action' => 'wbcreateclaim',
                        'entity' => '-',
                        'snaktype' => 'value',
-                       'value' => '"foo"',
+                       'value' => '"Foo.png"',
                );
 
                $argLists[] = array( 'claim-property-id-missing', $params );
@@ -244,7 +244,7 @@
                        'entity' => $this->getFormattedIdForEntity( $entity ),
                        'snaktype' => 'value',
                        'property' => $this->getFormattedIdForEntity( $property 
),
-                       'value' => '"foo"',
+                       'value' => '"Foo.png"',
                        'token' => $GLOBALS['wgUser']->getEditToken()
                );
 
@@ -261,7 +261,7 @@
                        'entity' => $this->getFormattedIdForEntity( $entity ),
                        'snaktype' => 'value',
                        'property' => $this->getFormattedIdForEntity( $property 
),
-                       'value' => '"bar"',
+                       'value' => '"Bar.jpg"',
                        'token' => $GLOBALS['wgUser']->getEditToken(),
                        'baserevid' => $revId
                );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I49216858fa1b077285a71e6fe206a773aa7c9de7
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de>
Gerrit-Reviewer: Henning Snater <henning.sna...@wikimedia.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to