| Current File : /home/jvzmxxx/wiki1/extensions/Wikibase/repo/includes/Api/SetClaimValue.php |
<?php
namespace Wikibase\Repo\Api;
use ApiBase;
use ApiMain;
use Wikibase\ChangeOp\StatementChangeOpFactory;
use Wikibase\DataModel\Services\Statement\StatementGuidParser;
use Wikibase\Repo\WikibaseRepo;
/**
* API module for setting the DataValue contained by the main snak of a claim.
*
* @since 0.3
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
* @author Tobias Gritschacher < tobias.gritschacher@wikimedia.de >
*/
class SetClaimValue extends ApiBase {
/**
* @var StatementChangeOpFactory
*/
private $statementChangeOpFactory;
/**
* @var ApiErrorReporter
*/
private $errorReporter;
/**
* @var StatementModificationHelper
*/
private $modificationHelper;
/**
* @var StatementGuidParser
*/
private $guidParser;
/**
* @var ResultBuilder
*/
private $resultBuilder;
/**
* @var EntityLoadingHelper
*/
private $entityLoadingHelper;
/**
* @var EntitySavingHelper
*/
private $entitySavingHelper;
/**
* @param ApiMain $mainModule
* @param string $moduleName
* @param string $modulePrefix
*/
public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) {
parent::__construct( $mainModule, $moduleName, $modulePrefix );
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
$apiHelperFactory = $wikibaseRepo->getApiHelperFactory( $this->getContext() );
$changeOpFactoryProvider = $wikibaseRepo->getChangeOpFactoryProvider();
$this->errorReporter = $apiHelperFactory->getErrorReporter( $this );
$this->statementChangeOpFactory = $changeOpFactoryProvider->getStatementChangeOpFactory();
$this->modificationHelper = new StatementModificationHelper(
$wikibaseRepo->getSnakFactory(),
$wikibaseRepo->getEntityIdParser(),
$wikibaseRepo->getStatementGuidValidator(),
$apiHelperFactory->getErrorReporter( $this )
);
$this->guidParser = $wikibaseRepo->getStatementGuidParser();
$this->resultBuilder = $apiHelperFactory->getResultBuilder( $this );
$this->entityLoadingHelper = $apiHelperFactory->getEntityLoadingHelper( $this );
$this->entitySavingHelper = $apiHelperFactory->getEntitySavingHelper( $this );
}
/**
* @see ApiBase::execute
*
* @since 0.3
*/
public function execute() {
$params = $this->extractRequestParams();
$this->validateParameters( $params );
$this->logFeatureUsage( 'action=wbsetclaimvalue' );
$guid = $params['claim'];
$entityId = $this->guidParser->parse( $guid )->getEntityId();
if ( isset( $params['baserevid'] ) ) {
$entityRevision = $this->entityLoadingHelper->loadEntityRevision(
$entityId,
(int)$params['baserevid']
);
} else {
$entityRevision = $this->entityLoadingHelper->loadEntityRevision( $entityId );
}
$entity = $entityRevision->getEntity();
$claim = $this->modificationHelper->getStatementFromEntity( $guid, $entity );
$snak = $this->modificationHelper->getSnakInstance( $params, $claim->getMainSnak()->getPropertyId() );
$summary = $this->modificationHelper->createSummary( $params, $this );
$changeOp = $this->statementChangeOpFactory->newSetMainSnakOp( $guid, $snak );
$this->modificationHelper->applyChangeOp( $changeOp, $entity, $summary );
$status = $this->entitySavingHelper->attemptSaveEntity( $entity, $summary, EDIT_UPDATE );
$this->resultBuilder->addRevisionIdFromStatusToResult( $status, 'pageinfo' );
$this->resultBuilder->markSuccess();
$this->resultBuilder->addStatement( $claim );
}
/**
* @param array $params
*/
private function validateParameters( array $params ) {
if ( !( $this->modificationHelper->validateStatementGuid( $params['claim'] ) ) ) {
$this->errorReporter->dieError( 'Invalid claim guid', 'invalid-guid' );
}
}
/**
* @see ApiBase::isWriteMode
*/
public function isWriteMode() {
return true;
}
/**
* @see ApiBase::needsToken
*
* @return string
*/
public function needsToken() {
return 'csrf';
}
/**
* @see ApiBase::getAllowedParams
*/
protected function getAllowedParams() {
return array_merge(
array(
'claim' => array(
self::PARAM_TYPE => 'string',
self::PARAM_REQUIRED => true,
),
'value' => array(
self::PARAM_TYPE => 'text',
self::PARAM_REQUIRED => false,
),
'snaktype' => array(
self::PARAM_TYPE => array( 'value', 'novalue', 'somevalue' ),
self::PARAM_REQUIRED => true,
),
'summary' => array(
self::PARAM_TYPE => 'string',
),
'token' => null,
'baserevid' => array(
self::PARAM_TYPE => 'integer',
),
'bot' => false,
),
parent::getAllowedParams()
);
}
/**
* @see ApiBase::getExamplesMessages
*/
protected function getExamplesMessages() {
return array(
'action=wbsetclaimvalue&claim=Q42$D8404CDA-25E4-4334-AF13-A3290BCD9C0F&snaktype=value'
. '&value={"entity-type":"item","numeric-id":1}&token=foobar&baserevid=7201010'
=> 'apihelp-wbsetclaimvalue-example-1',
);
}
}