Addshore has uploaded a new change for review.
https://gerrit.wikimedia.org/r/224633
Change subject: Covert spaces to tabs in SqlViolationRepo.php
......................................................................
Covert spaces to tabs in SqlViolationRepo.php
Change-Id: I627d086dde6c26e84d5ceccca05f5778c1961ad7
---
M includes/Violations/SqlViolationRepo.php
1 file changed, 275 insertions(+), 239 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQuality
refs/changes/33/224633/1
diff --git a/includes/Violations/SqlViolationRepo.php
b/includes/Violations/SqlViolationRepo.php
index 740fe2e..cc0f490 100755
--- a/includes/Violations/SqlViolationRepo.php
+++ b/includes/Violations/SqlViolationRepo.php
@@ -3,8 +3,8 @@
namespace WikibaseQuality\Violations;
use DatabaseBase;
-use ResultWrapper;
use InvalidArgumentException;
+use ResultWrapper;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\PropertyId;
@@ -15,102 +15,110 @@
*/
class SqlViolationRepo implements ViolationLookup, ViolationStore {
- const ORDER_ASCENDING = 'ASC';
- const ORDER_DESCENDING = 'DESC';
+ const ORDER_ASCENDING = 'ASC';
+ const ORDER_DESCENDING = 'DESC';
const TABLE_NAME = 'wbq_violations';
- /**
- * @var EntityIdParser
- */
- private $entityIdParser;
+ /**
+ * @var EntityIdParser
+ */
+ private $entityIdParser;
/**
* @param EntityIdParser $entityIdParser
*/
- public function __construct( EntityIdParser $entityIdParser ) {
- $this->entityIdParser = $entityIdParser;
- }
+ public function __construct( EntityIdParser $entityIdParser ) {
+ $this->entityIdParser = $entityIdParser;
+ }
- /**
- * @see ViolationLookup::get
- *
- * @param ViolationQuery $query
- * @return Violation[]
- */
- public function get( ViolationQuery $query ) {
- $db = wfGetDB( DB_SLAVE );
- $result = $db->select(
- self::TABLE_NAME,
- '*',
- $this->buildConditions( $db, $query )
- );
+ /**
+ * @see ViolationLookup::get
+ *
+ * @param ViolationQuery $query
+ *
+ * @return Violation[]
+ */
+ public function get( ViolationQuery $query ) {
+ $db = wfGetDB( DB_SLAVE );
+ $result = $db->select(
+ self::TABLE_NAME,
+ '*',
+ $this->buildConditions( $db, $query )
+ );
- $violations = array();
- foreach ( $result as $row ) {
- $violations[] = $this->getViolationFromRow( $row );
- }
+ $violations = array();
+ foreach ( $result as $row ) {
+ $violations[] = $this->getViolationFromRow( $row );
+ }
- return $violations;
- }
+ return $violations;
+ }
- /**
- * @see ViolationRepo::getForPage
- *
- * @param ViolationQuery $query
- * @param int $maxNumber
- * @param string $direction
- * @param string $claimGuid
- * @param string $constraintId
+ /**
+ * @see ViolationRepo::getForPage
+ *
+ * @param ViolationQuery $query
+ * @param int $maxNumber
+ * @param string $direction
+ * @param string $claimGuid
+ * @param string $constraintId
+ *
* @return array list( $violations, $prevPageAvailable,
$nextPageAvailable )
- */
- public function getForPage( ViolationQuery $query, $maxNumber, $direction,
$claimGuid, $constraintId ) {
- if ( !is_int( $maxNumber ) ) {
- throw new InvalidArgumentException( '$maxNumber must be int.' );
- }
- if ( $direction !== self::ORDER_ASCENDING && $direction !==
self::ORDER_DESCENDING ) {
- throw new InvalidArgumentException( '$direction must a valid
direction.' );
- }
- if ( $claimGuid && !is_string( $claimGuid ) ) {
- throw new InvalidArgumentException( '$claimGuid must be string.' );
- }
- if ( $constraintId && !is_string( $constraintId ) ) {
- throw new InvalidArgumentException( '$constraintId must be
string.' );
- }
- if ( $query->getClaimGuid() || $query->getConstraintId() ) {
- throw new InvalidArgumentException( 'Filtering by claim guid or
constraint id is not supported.' );
- }
+ */
+ public function getForPage( ViolationQuery $query,
+ $maxNumber,
+ $direction,
+ $claimGuid,
+ $constraintId ) {
+ if ( !is_int( $maxNumber ) ) {
+ throw new InvalidArgumentException( '$maxNumber must be
int.' );
+ }
+ if ( $direction !== self::ORDER_ASCENDING && $direction !==
self::ORDER_DESCENDING ) {
+ throw new InvalidArgumentException( '$direction must a
valid direction.' );
+ }
+ if ( $claimGuid && !is_string( $claimGuid ) ) {
+ throw new InvalidArgumentException( '$claimGuid must be
string.' );
+ }
+ if ( $constraintId && !is_string( $constraintId ) ) {
+ throw new InvalidArgumentException( '$constraintId must
be string.' );
+ }
+ if ( $query->getClaimGuid() || $query->getConstraintId() ) {
+ throw new InvalidArgumentException( 'Filtering by claim
guid or constraint id is not supported.' );
+ }
- $db = wfGetDB( DB_SLAVE );
- $queryConditions = $this->buildConditions( $db, $query );
- $pageConditions = $this->buildPaginationConditions( $db, $direction,
$claimGuid, $constraintId );
- $conditions = array_merge( $queryConditions, $pageConditions );
+ $db = wfGetDB( DB_SLAVE );
+ $queryConditions = $this->buildConditions( $db, $query );
+ $pageConditions =
+ $this->buildPaginationConditions( $db, $direction,
$claimGuid, $constraintId );
+ $conditions = array_merge( $queryConditions, $pageConditions );
- $firstKey = $this->getFirstKey( $db, $queryConditions, $direction );
- $result = $db->select(
- self::TABLE_NAME,
- '*',
- $conditions,
- __METHOD__,
- array(
- 'ORDER BY' => "claim_guid $direction, constraint_id
$direction",
- 'LIMIT' => $maxNumber + 1
- )
- );
+ $firstKey = $this->getFirstKey( $db, $queryConditions,
$direction );
+ $result = $db->select(
+ self::TABLE_NAME,
+ '*',
+ $conditions,
+ __METHOD__,
+ array(
+ 'ORDER BY' => "claim_guid $direction,
constraint_id $direction",
+ 'LIMIT' => $maxNumber + 1
+ )
+ );
- return $this->getViolationsForPageFromResult( $result, $firstKey,
$direction, $maxNumber );
- }
+ return $this->getViolationsForPageFromResult( $result,
$firstKey, $direction, $maxNumber );
+ }
/**
* @param Violation $violation
* @param bool $overwriteExisting
+ *
* @return bool
*/
public function insert( Violation $violation, $overwriteExisting =
false ) {
$db = wfGetDB( DB_MASTER );
- if( $this->violationExists( $db, $violation ) ) {
- if( $overwriteExisting ) {
+ if ( $this->violationExists( $db, $violation ) ) {
+ if ( $overwriteExisting ) {
return $this->update( $violation, false );
}
throw new InvalidArgumentException( 'Given violation
already exists in database.' );
@@ -125,18 +133,22 @@
/**
* @param Violation $violation
* @param bool $overwriteException
+ *
* @return bool
*/
public function update( Violation $violation, $overwriteException =
false ) {
$db = wfGetDB( DB_MASTER );
$violationRow = $this->violationExists( $db, $violation );
- if( $violationRow ) {
- if ( !$overwriteException &&
$this->getViolationFromRow( $violationRow )->getStatus() ===
Violation::STATUS_EXCEPTION ) {
- $violationRow = $this->getRowFromViolation(
$db, $violation, Violation::STATUS_EXCEPTION );
- }
- else {
- $violationRow = $this->getRowFromViolation(
$db, $violation);
+ if ( $violationRow ) {
+ if ( !$overwriteException &&
+ $this->getViolationFromRow( $violationRow
)->getStatus() ===
+ Violation::STATUS_EXCEPTION
+ ) {
+ $violationRow =
+ $this->getRowFromViolation( $db,
$violation, Violation::STATUS_EXCEPTION );
+ } else {
+ $violationRow = $this->getRowFromViolation(
$db, $violation );
}
return $db->update(
@@ -149,29 +161,33 @@
throw new InvalidArgumentException( 'Given violation does not
exist in database.' );
}
- /**
- * @see ViolationStore::delete
- *
- * @param Violation $violation
- * @return bool|\ResultWrapper
- * @throws \DBUnexpectedError
- */
- public function delete( Violation $violation ) {
- $db = wfGetDB( DB_MASTER );
- return $db->delete(
- self::TABLE_NAME,
- array(
- sprintf( 'claim_guid="%s"', $violation->getClaimGuid() ),
- sprintf( 'constraint_id="%s"', $violation->getConstraintId() )
- )
- );
- }
+ /**
+ * @see ViolationStore::delete
+ *
+ * @param Violation $violation
+ *
+ * @return bool|\ResultWrapper
+ * @throws \DBUnexpectedError
+ */
+ public function delete( Violation $violation ) {
+ $db = wfGetDB( DB_MASTER );
+
+ return $db->delete(
+ self::TABLE_NAME,
+ array(
+ sprintf( 'claim_guid="%s"',
$violation->getClaimGuid() ),
+ sprintf( 'constraint_id="%s"',
$violation->getConstraintId() )
+ )
+ );
+ }
/**
- * Checks, if violation exists in database. In this case, database row
of it is returned; otherwise false.
+ * Checks, if violation exists in database. In this case, database row
of it is returned;
+ * otherwise false.
*
* @param DatabaseBase $db
* @param Violation $violation
+ *
* @return bool|\stdClass
*/
private function violationExists( DatabaseBase $db, Violation
$violation ) {
@@ -186,6 +202,7 @@
* Builds conditions array for primary key of a violation.
*
* @param Violation $violation
+ *
* @return array
*/
private function buildKeyConditions( Violation $violation ) {
@@ -195,133 +212,150 @@
);
}
- /**
- * Builds array of query conditions for pagination.
- *
- * @param DatabaseBase $db
- * @param string $direction
- * @param string $claimGuid
- * @param string $constraintId
- * @return array
- */
- private function buildPaginationConditions( DatabaseBase $db, $direction,
$claimGuid, $constraintId ) {
- $conditions = array();
+ /**
+ * Builds array of query conditions for pagination.
+ *
+ * @param DatabaseBase $db
+ * @param string $direction
+ * @param string $claimGuid
+ * @param string $constraintId
+ *
+ * @return array
+ */
+ private function buildPaginationConditions( DatabaseBase $db,
+ $direction,
+ $claimGuid,
+ $constraintId ) {
+ $conditions = array();
- if ( $claimGuid && $constraintId ) {
- $operator = '>';
- $oppositeOperator = '<';
- if ( $direction === self::ORDER_DESCENDING ) {
- $operator = '<';
- $oppositeOperator = '>';
- }
+ if ( $claimGuid && $constraintId ) {
+ $operator = '>';
+ $oppositeOperator = '<';
+ if ( $direction === self::ORDER_DESCENDING ) {
+ $operator = '<';
+ $oppositeOperator = '>';
+ }
- $quotedClaimGuid = $db->addQuotes( $claimGuid );
- $quotedConstraintId = $db->addQuotes( $constraintId );
+ $quotedClaimGuid = $db->addQuotes( $claimGuid );
+ $quotedConstraintId = $db->addQuotes( $constraintId );
- $conditions[] = sprintf( 'claim_guid%s=%s', $operator,
$quotedClaimGuid );
- $conditions[] = sprintf( 'NOT (claim_guid=%s AND
constraint_id%s=%s)', $quotedClaimGuid, $oppositeOperator, $quotedConstraintId
);
- }
+ $conditions[] = sprintf( 'claim_guid%s=%s', $operator,
$quotedClaimGuid );
+ $conditions[] = sprintf(
+ 'NOT (claim_guid=%s AND constraint_id%s=%s)',
+ $quotedClaimGuid,
+ $oppositeOperator,
+ $quotedConstraintId
+ );
+ }
- return $conditions;
- }
-
- /**
- * Gets array with primary keys of the first (regarding specified
direction) violation
- * for the query in database.
- *
- * @param DatabaseBase $db
- * @param array $conditions
- * @param string $direction
- * @return array
- */
- private function getFirstKey( DatabaseBase $db, array $conditions,
$direction ) {
- $row = $db->selectRow(
- self::TABLE_NAME,
- array(
- 'claim_guid',
- 'constraint_id'
- ),
- $conditions,
- __METHOD__,
- array(
- 'LIMIT' => 1,
- 'ORDER BY' => "claim_guid $direction, constraint_id $direction"
- )
- );
-
- if ( $row ) {
- return $this->getKeyArray( $row );
- }
-
- return array();
- }
-
- /**
- * Gets array with primary keys of a given row.
- *
- * @param \stdClass $row
- * @return array
- */
- private function getKeyArray( $row ) {
- return array( $row->claim_guid, $row->constraint_id );
- }
-
- /**
- * Build array with query conditions from ViolationQuery object.
- *
- * @param DatabaseBase $db
- * @param ViolationQuery $query
- * @return array
- */
- private function buildConditions( DatabaseBase $db, ViolationQuery $query
) {
- $conditions = array();
- if ( $query->getEntityId() !== null ) {
- $conditions['entity_id'] =
$query->getEntityId()->getSerialization();
- }
- if ( $query->getPropertyId() !== null ) {
- $conditions['pid'] =
$query->getPropertyId()->getSerialization();
- }
- if ( $query->getClaimGuid() !== null ) {
- $conditions['claim_guid'] = $query->getClaimGuid();
- }
- if ( $query->getConstraintId() !== null ) {
- $conditions['constraint_id'] = $query->getConstraintId();
- }
- if ( $query->getConstraintGroup() !== null ) {
- $prefix = $query->getConstraintGroup() .
Violation::CONSTRAINT_ID_DELIMITER;
- $conditions[] = 'constraint_id' . $db->buildLike(
$prefix, $db->anyString() );
- }
- if ( $query->getConstraintTypeEntityId() !== null ) {
- $conditions['constraint_type_entity_id'] =
$query->getConstraintTypeEntityId();
- }
- if ( $query->getUpdatedAt() !== null ) {
- $conditions['updated_at'] = $db->timestamp( $query->getUpdatedAt()
);
- }
- if ( $query->getRevisionId() !== null ) {
- $conditions['revision_id'] = $query->getRevisionId();
- }
- if ( $query->getStatuses() !== null && count( $query->getStatuses() )
> 0 ) {
- $conditions['status'] = $query->getStatuses();
- }
-
- return $conditions;
- }
+ return $conditions;
+ }
/**
- * Creates violation objects from database result and checks, whether
previous or next page is available
+ * Gets array with primary keys of the first (regarding specified
direction) violation
+ * for the query in database.
+ *
+ * @param DatabaseBase $db
+ * @param array $conditions
+ * @param string $direction
+ *
+ * @return array
+ */
+ private function getFirstKey( DatabaseBase $db, array $conditions,
$direction ) {
+ $row = $db->selectRow(
+ self::TABLE_NAME,
+ array(
+ 'claim_guid',
+ 'constraint_id'
+ ),
+ $conditions,
+ __METHOD__,
+ array(
+ 'LIMIT' => 1,
+ 'ORDER BY' => "claim_guid $direction,
constraint_id $direction"
+ )
+ );
+
+ if ( $row ) {
+ return $this->getKeyArray( $row );
+ }
+
+ return array();
+ }
+
+ /**
+ * Gets array with primary keys of a given row.
+ *
+ * @param \stdClass $row
+ *
+ * @return array
+ */
+ private function getKeyArray( $row ) {
+ return array( $row->claim_guid, $row->constraint_id );
+ }
+
+ /**
+ * Build array with query conditions from ViolationQuery object.
+ *
+ * @param DatabaseBase $db
+ * @param ViolationQuery $query
+ *
+ * @return array
+ */
+ private function buildConditions( DatabaseBase $db, ViolationQuery
$query ) {
+ $conditions = array();
+ if ( $query->getEntityId() !== null ) {
+ $conditions['entity_id'] =
$query->getEntityId()->getSerialization();
+ }
+ if ( $query->getPropertyId() !== null ) {
+ $conditions['pid'] =
$query->getPropertyId()->getSerialization();
+ }
+ if ( $query->getClaimGuid() !== null ) {
+ $conditions['claim_guid'] = $query->getClaimGuid();
+ }
+ if ( $query->getConstraintId() !== null ) {
+ $conditions['constraint_id'] =
$query->getConstraintId();
+ }
+ if ( $query->getConstraintGroup() !== null ) {
+ $prefix = $query->getConstraintGroup() .
Violation::CONSTRAINT_ID_DELIMITER;
+ $conditions[] = 'constraint_id' . $db->buildLike(
$prefix, $db->anyString() );
+ }
+ if ( $query->getConstraintTypeEntityId() !== null ) {
+ $conditions['constraint_type_entity_id'] =
$query->getConstraintTypeEntityId();
+ }
+ if ( $query->getUpdatedAt() !== null ) {
+ $conditions['updated_at'] = $db->timestamp(
$query->getUpdatedAt() );
+ }
+ if ( $query->getRevisionId() !== null ) {
+ $conditions['revision_id'] = $query->getRevisionId();
+ }
+ if ( $query->getStatuses() !== null && count(
$query->getStatuses() ) > 0 ) {
+ $conditions['status'] = $query->getStatuses();
+ }
+
+ return $conditions;
+ }
+
+ /**
+ * Creates violation objects from database result and checks, whether
previous or next page is
+ * available
*
* @param \ResultWrapper $result
* @param array $firstKey
* @param string $direction
* @param int $maxNumber
+ *
* @return array list( $violations, $prevPageAvailable,
$nextPageAvailable )
*/
- private function getViolationsForPageFromResult( ResultWrapper $result,
array $firstKey, $direction, $maxNumber ) {
+ private function getViolationsForPageFromResult( ResultWrapper $result,
+ array $firstKey,
+ $direction,
+ $maxNumber ) {
$violations = array();
$prevPageAvailable = false;
$nextPageAvailable = false;
$resultCount = $result->numRows();
- if( $resultCount > 0 ) {
+ if ( $resultCount > 0 ) {
for ( $i = 0; $i < $resultCount; $i++ ) {
$row = $result->fetchObject();
$key = $this->getKeyArray( $row );
@@ -348,27 +382,28 @@
return array( $violations, $prevPageAvailable,
$nextPageAvailable );
}
- /**
- * Creates Violation object from single row of a database result.
- *
- * @param $row
- * @return Violation
- */
- private function getViolationFromRow( $row ) {
- $additionalInformation = json_decode( $row->additional_info, true ) ?:
array();
+ /**
+ * Creates Violation object from single row of a database result.
+ *
+ * @param $row
+ *
+ * @return Violation
+ */
+ private function getViolationFromRow( $row ) {
+ $additionalInformation = json_decode( $row->additional_info,
true ) ?: array();
- return new Violation(
- $this->entityIdParser->parse( $row->entity_id ),
- new PropertyId( $row->pid ),
- $row->claim_guid,
- $row->constraint_id,
- $row->constraint_type_entity_id,
- (int)$row->revision_id,
- $row->status,
- $additionalInformation,
+ return new Violation(
+ $this->entityIdParser->parse( $row->entity_id ),
+ new PropertyId( $row->pid ),
+ $row->claim_guid,
+ $row->constraint_id,
+ $row->constraint_type_entity_id,
+ (int)$row->revision_id,
+ $row->status,
+ $additionalInformation,
$row->updated_at
- );
- }
+ );
+ }
/**
* Gets array from Violation object for database insertions.
@@ -377,20 +412,21 @@
* @param DatabaseBase $db
* @param Violation $violation
* @param string|null $status
+ *
* @return array
*/
- private function getRowFromViolation( DatabaseBase $db, Violation
$violation, $status = null ) {
- return array(
- 'entity_id' => $violation->getEntityId()->getSerialization(),
- 'pid' => $violation->getPropertyId()->getSerialization(),
- 'claim_guid' => $violation->getClaimGuid(),
- 'constraint_id' => $violation->getConstraintId(),
- 'constraint_type_entity_id' =>
$violation->getConstraintTypeEntityId(),
- 'status' => $status ?: $violation->getStatus(),
- 'revision_id' => $violation->getRevisionId(),
- 'additional_info' => json_encode( $violation->getAdditionalInfo()
),
- 'updated_at' => $db->timestamp()
- );
- }
+ private function getRowFromViolation( DatabaseBase $db, Violation
$violation, $status = null ) {
+ return array(
+ 'entity_id' =>
$violation->getEntityId()->getSerialization(),
+ 'pid' =>
$violation->getPropertyId()->getSerialization(),
+ 'claim_guid' => $violation->getClaimGuid(),
+ 'constraint_id' => $violation->getConstraintId(),
+ 'constraint_type_entity_id' =>
$violation->getConstraintTypeEntityId(),
+ 'status' => $status ?: $violation->getStatus(),
+ 'revision_id' => $violation->getRevisionId(),
+ 'additional_info' => json_encode(
$violation->getAdditionalInfo() ),
+ 'updated_at' => $db->timestamp()
+ );
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/224633
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I627d086dde6c26e84d5ceccca05f5778c1961ad7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQuality
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits