repo/includes/ChangeOp/ChangeOpReferenceRemove.php
<?php
namespace Wikibase\Repo\ChangeOp;
use InvalidArgumentException;
use ValueValidators\Result;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\ReferenceList;
use Wikibase\DataModel\Snak\Snak;
use Wikibase\DataModel\Statement\StatementListProvider;
use Wikibase\Lib\Summary;
/**
* Class for reference removal change operation
*
* @license GPL-2.0-or-later
* @author Addshore
*/
class ChangeOpReferenceRemove extends ChangeOpBase {
/**
* @var string
*/
private $statementGuid;
/**
* @var string
*/
private $referenceHash;
/**
* Constructs a new reference removal change operation
*
* @param string $statementGuid
* @param string $referenceHash
*
* @throws InvalidArgumentException
*/
public function __construct( $statementGuid, $referenceHash ) {
if ( !is_string( $statementGuid ) || $statementGuid === '' ) {
throw new InvalidArgumentException( '$statementGuid needs to be a string and must not be empty' );
}
if ( !is_string( $referenceHash ) || $referenceHash === '' ) {
throw new InvalidArgumentException( '$referenceHash needs to be a string and must not be empty' );
}
$this->statementGuid = $statementGuid;
$this->referenceHash = $referenceHash;
}
/** @inheritDoc */
public function apply( EntityDocument $entity, Summary $summary = null ) {
if ( !( $entity instanceof StatementListProvider ) ) {
throw new InvalidArgumentException( '$entity must be a StatementListProvider' );
}
$statements = $entity->getStatements();
$statement = $statements->getFirstStatementWithGuid( $this->statementGuid );
if ( $statement === null ) {
throw new ChangeOpException( "Entity does not have claim with GUID $this->statementGuid" );
}
$references = $statement->getReferences();
$this->removeReference( $references, $summary );
if ( $summary !== null ) {
$summary->addAutoSummaryArgs( $this->getSnakSummaryArgs( $statement->getMainSnak() ) );
}
$statement->setReferences( $references );
return new GenericChangeOpResult( $entity->getId(), true );
}
/**
* @param ReferenceList $references
* @param Summary|null $summary
*
* @throws ChangeOpException
*/
protected function removeReference( ReferenceList $references, Summary $summary = null ) {
if ( !$references->hasReferenceHash( $this->referenceHash ) ) {
throw new ChangeOpException( "Reference with hash $this->referenceHash does not exist" );
}
$references->removeReferenceHash( $this->referenceHash );
$this->updateSummary( $summary, 'remove' );
if ( $summary !== null ) {
$summary->addAutoCommentArgs( 1 ); //atomic edit, only one reference changed
}
}
/**
* @param Snak $snak
* @return array
*/
protected function getSnakSummaryArgs( Snak $snak ) {
$propertyId = $snak->getPropertyId();
return [ [ $propertyId->getSerialization() => $snak ] ];
}
/**
* @see ChangeOp::validate
*
* @param EntityDocument $entity
*
* @return Result Always successful.
*/
public function validate( EntityDocument $entity ) {
//TODO: move validation logic from apply() here.
return Result::newSuccess();
}
}