Andreasburmeister has submitted this change and it was merged.

Change subject: move Job and test to this extension for now
......................................................................


move Job and test to this extension for now

Change-Id: I2699c288752a4532cecad4bd76dba7652d451346
---
A includes/CheckForViolationsJob.php
A tests/phpunit/CheckForViolationsJobTest.php
2 files changed, 215 insertions(+), 0 deletions(-)

Approvals:
  Andreasburmeister: Verified; Looks good to me, approved



diff --git a/includes/CheckForViolationsJob.php 
b/includes/CheckForViolationsJob.php
new file mode 100644
index 0000000..2ff8602
--- /dev/null
+++ b/includes/CheckForViolationsJob.php
@@ -0,0 +1,131 @@
+<?php
+
+namespace WikidataQuality\ConstraintReport;
+
+use Job;
+use Title;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\Repo\WikibaseRepo;
+use WikidataQuality\ConstraintReport\ConstraintCheck\ConstraintChecker;
+
+class CheckForViolationsJob extends Job {
+
+       private $db;
+
+       public static function newInsertNow(
+               $specialPage,
+               EntityDocument $entity,
+               $checkTimestamp,
+               $results ) {
+               // The Job class wants a Title object for some reason. Supply a 
dummy.
+               $dummyTitle = Title::newFromText( "CheckForViolationsJob", 
NS_SPECIAL );
+
+               $params = array();
+
+               $params['specialPage'] = $specialPage;
+               $params['entity'] = $entity;
+               $params['results'] = $results;
+               $params['checkTimestamp'] = $checkTimestamp;
+               $params['referenceTimestamp'] = null;
+
+               return new CheckForViolationsJob( $dummyTitle, $params );
+       }
+
+       public static function newInsertDeferred(
+               $specialPage,
+               EntityDocument $entity,
+               $referenceTimestamp = null,
+               $releaseTimestamp = 0 ) {
+               // The Job class wants a Title object for some reason. Supply a 
dummy.
+               $dummyTitle = Title::newFromText( "CheckForViolationsJob", 
NS_SPECIAL );
+
+               $params = array();
+
+               $params['specialPage'] = $specialPage;
+               $params['entity'] = $entity;
+               $params['results'] = null;
+               $params['referenceTimestamp'] = $referenceTimestamp;
+               $params['releaseTimestamp'] = wfTimestamp( TS_MW ) + 
$releaseTimestamp;
+
+               return new CheckForViolationsJob( $dummyTitle, $params );
+       }
+
+       public function __construct( Title $title, $params ) {
+               parent::__construct( 'CheckForViolations', $title, $params );
+               wfWaitForSlaves();
+               $loadBalancer = wfGetLB();
+               $this->db = $loadBalancer->getConnection( DB_MASTER );
+       }
+
+       public function run() {
+               $checkTimestamp = array_key_exists( 'checkTimestamp', 
$this->params ) ? $this->params['checkTimestamp'] : wfTimestamp( TS_MW );
+
+               if( $this->params['results'] === null ) {
+                       $constraintChecker = new ConstraintChecker( 
WikibaseRepo::getDefaultInstance()->getEntityLookup() );
+                       $results = $constraintChecker->execute( 
$this->params['entity'] );
+               } else {
+                       $results = $this->params['results'];
+               }
+
+               $accumulator = array (
+                       'special_page_id' => $this->params['specialPage'],
+                       'entity_id' => 
$this->params['entity']->getId()->getSerialization(),
+                       'insertion_timestamp' => $checkTimestamp,
+                       'reference_timestamp' => 
$this->params['referenceTimestamp'],
+                       'result_string' => $this->getResultSerialization( 
$results )
+               );
+               $success = $this->db->insert( EVALUATION_TABLE, $accumulator );
+
+               return $success;
+       }
+
+       private function getResultSerialization( $results ) {
+               $serialization = '';
+               $compliances = $violations = $exceptions = array();
+               foreach( $results as $result ) {
+                       switch( $result->getStatus() ) {
+                               case 'compliance':
+                                       if( array_key_exists( 
$result->getConstraintName(), $compliances ) ) {
+                                               
$compliances[$result->getConstraintName()] += 1;
+                                       } else {
+                                               
$compliances[$result->getConstraintName()] = 1;
+                                       }
+                                       break;
+                               case 'violation':
+                                       if( array_key_exists( 
$result->getConstraintName(), $violations ) ) {
+                                               
$violations[$result->getConstraintName()] += 1;
+                                       } else {
+                                               
$violations[$result->getConstraintName()] = 1;
+                                       }
+                                       break;
+                               case 'exception':
+                                       if( array_key_exists( 
$result->getConstraintName(), $exceptions ) ) {
+                                               
$exceptions[$result->getConstraintName()] += 1;
+                                       } else {
+                                               
$exceptions[$result->getConstraintName()] = 1;
+                                       }
+                       }
+               }
+
+               $serialization .= '{Compliances: {';
+               foreach( array_keys($compliances) as $key ) {
+                       $serialization .= $key . ': ' . $compliances[ $key ] . 
', ';
+               }
+               $serialization .= '}, ';
+
+               $serialization .= '{Violations: {';
+               foreach( array_keys($violations) as $key ) {
+                       $serialization .= $key . ': ' . $violations[ $key ] . 
', ';
+               }
+               $serialization .= '}, ';
+
+               $serialization .= '{Exceptions: {';
+               foreach( array_keys($compliances) as $key ) {
+                       $serialization .= $key . ': ' . $compliances[ $key ] . 
', ';
+               }
+               $serialization .= '}, ';
+
+               return $serialization;
+       }
+
+}
\ No newline at end of file
diff --git a/tests/phpunit/CheckForViolationsJobTest.php 
b/tests/phpunit/CheckForViolationsJobTest.php
new file mode 100644
index 0000000..1fbcd71
--- /dev/null
+++ b/tests/phpunit/CheckForViolationsJobTest.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace WikidataQuality\Tests;
+
+use Wikibase\DataModel\Entity\Item;
+use WikidataQuality\ConstraintReport\ConstraintCheck\Result\CheckResult;
+use WikidataQuality\ConstraintReport\CheckForViolationsJob;
+use Wikibase\DataModel\Statement\Statement;
+use Wikibase\DataModel\Claim\Claim;
+use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Entity\PropertyId;
+use DataValues\StringValue;
+use Wikibase\Repo\WikibaseRepo;
+
+
+/**
+ * @covers WikidataQuality\ConstraintReport\CheckForViolationsJob
+ *
+ * @group Database
+ *
+ * @uses WikidataQuality\ConstraintReport\ConstraintCheck\Result\CheckResult
+ * @uses WikidataQuality\ConstraintReport\ConstraintCheck\ConstraintChecker
+ *
+ * @author BP2014N1
+ * @license GNU GPL v2+
+ */
+class CheckForViolationsJobTest extends \MediaWikiTestCase {
+
+       private $results;
+       private $entity;
+       private $checkTimestamp;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $statement = new Statement( new Claim( new PropertyValueSnak( 
new PropertyId( 'P188' ), new StringValue( 'foo' ) ) ) );
+               $constraintName = 'Single value';
+               $results = array();
+               $results[] = new CheckResult( $statement, $constraintName, 
array(), CheckResult::STATUS_VIOLATION );
+               $results[] = new CheckResult( $statement, $constraintName, 
array(), CheckResult::STATUS_COMPLIANCE );
+               $results[] = new CheckResult( $statement, $constraintName, 
array(), CheckResult::STATUS_EXCEPTION );
+               $this->results = $results;
+
+               $this->checkTimestamp = wfTimestamp( TS_MW );
+
+               // specify database tables used by this test
+               $this->tablesUsed[ ] = EVALUATION_TABLE;
+       }
+
+       protected function tearDown() {
+               unset( $this->results );
+               unset( $this->entity );
+               unset( $this->checkTimestamp );
+               parent::tearDown();
+       }
+
+       public function addDBData() {
+               $this->db->delete(
+                       EVALUATION_TABLE,
+                       '*'
+               );
+
+               $this->entity = new Item();
+               $store = WikibaseRepo::getDefaultInstance()->getEntityStore();
+               $store->saveEntity( $this->entity, 'TestEntityQ1', $GLOBALS[ 
'wgUser' ], EDIT_NEW );
+       }
+
+       public function testCheckForViolationJobNow() {
+               $job = CheckForViolationsJob::newInsertNow( 1, $this->entity, 
$this->checkTimestamp, $this->results );
+               $job->run();
+               $count = $this->db->select(EVALUATION_TABLE, array( 
'special_page_id' ), array ( 'special_page_id=1' ) )->numRows();
+               $result = $this->db->selectRow(EVALUATION_TABLE, array( 
'result_string' ), array ( 'special_page_id=1' ) );
+               $this->assertEquals( 1, $count );
+               $this->assertEquals( '{Compliances: {Single value: 1, }, 
{Violations: {Single value: 1, }, {Exceptions: {Single value: 1, }, ', 
$result->result_string );
+       }
+
+       public function testCheckForViolationJobDeferred() {
+               $job = CheckForViolationsJob::newInsertDeferred( 2, 
$this->entity, $this->checkTimestamp, 10 );
+               $job->run();
+               $count = $this->db->select(EVALUATION_TABLE, array( 
'special_page_id' ), array ( 'special_page_id=2' ) )->numRows();
+               $this->assertEquals( 1, $count );
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I2699c288752a4532cecad4bd76dba7652d451346
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikidataQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Jonaskeutel <[email protected]>
Gerrit-Reviewer: Andreasburmeister <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to