/**
  * @see ApiBase::execute
  *
  * @since 0.3
  */
 public function execute()
 {
     $params = $this->extractRequestParams();
     $this->validateParameters($params);
     $guid = $params['claim'];
     $entityId = $this->guidParser->parse($guid)->getEntityId();
     if (isset($params['baserevid'])) {
         $entityRevision = $this->loadEntityRevision($entityId, (int) $params['baserevid']);
     } else {
         $entityRevision = $this->loadEntityRevision($entityId);
     }
     $entity = $entityRevision->getEntity();
     $summary = $this->modificationHelper->createSummary($params, $this);
     $claim = $this->modificationHelper->getStatementFromEntity($guid, $entity);
     $qualifierHashes = $this->getQualifierHashesFromParams($params, $claim);
     $changeOps = new ChangeOps();
     $changeOps->add($this->getChangeOps($guid, $qualifierHashes));
     try {
         $changeOps->apply($entity, $summary);
     } catch (ChangeOpException $e) {
         $this->errorReporter->dieException($e, 'failed-save');
     }
     $status = $this->saveChanges($entity, $summary);
     $this->getResultBuilder()->addRevisionIdFromStatusToResult($status, 'pageinfo');
     $this->getResultBuilder()->markSuccess();
 }
示例#2
0
 /**
  * @see ApiBase::execute
  *
  * @since 0.3
  */
 public function execute()
 {
     $params = $this->extractRequestParams();
     $entityId = $this->getEntityId($params);
     if (isset($params['baserevid'])) {
         $entityRevision = $this->loadEntityRevision($entityId, (int) $params['baserevid']);
     } else {
         $entityRevision = $this->loadEntityRevision($entityId);
     }
     $entity = $entityRevision->getEntity();
     if ($entity instanceof StatementListProvider) {
         $this->assertStatementListContainsGuids($entity->getStatements(), $params['claim']);
     }
     $summary = $this->modificationHelper->createSummary($params, $this);
     $changeOps = new ChangeOps();
     $changeOps->add($this->getChangeOps($params));
     try {
         $changeOps->apply($entity, $summary);
     } catch (ChangeOpException $e) {
         $this->errorReporter->dieException($e, 'failed-save');
     }
     $status = $this->saveChanges($entity, $summary);
     $this->getResultBuilder()->addRevisionIdFromStatusToResult($status, 'pageinfo');
     $this->getResultBuilder()->markSuccess();
     $this->getResultBuilder()->setList(null, 'claims', $params['claim'], 'claim');
 }
示例#3
0
 /**
  * @param Statement $fromStatement statement to take references from
  * @param Statement $toStatement statement to add references to
  */
 private function generateReferencesChangeOps(Statement $fromStatement, Statement $toStatement)
 {
     /** @var Reference $reference */
     foreach ($fromStatement->getReferences() as $reference) {
         if (!$toStatement->getReferences()->hasReferenceHash($reference->getHash())) {
             $this->toChangeOps->add($this->getStatementChangeOpFactory()->newSetReferenceOp($toStatement->getGuid(), $reference, ''));
         }
     }
 }
 /**
  * @param array $data
  * @param EntityDocument $entity
  *
  * @return ChangeOps
  */
 private function getChangeOps(array $data, EntityDocument $entity)
 {
     $changeOps = new ChangeOps();
     //FIXME: Use a ChangeOpBuilder so we can batch fingerprint ops etc,
     //       for more efficient validation!
     if (array_key_exists('labels', $data)) {
         $this->assertArray($data['labels'], 'List of labels must be an array');
         $changeOps->add($this->getLabelChangeOps($data['labels']));
     }
     if (array_key_exists('descriptions', $data)) {
         $this->assertArray($data['descriptions'], 'List of descriptions must be an array');
         $changeOps->add($this->getDescriptionChangeOps($data['descriptions']));
     }
     if (array_key_exists('aliases', $data)) {
         $this->assertArray($data['aliases'], 'List of aliases must be an array');
         $changeOps->add($this->getAliasesChangeOps($data['aliases']));
     }
     if (array_key_exists('sitelinks', $data)) {
         if (!$entity instanceof Item) {
             $this->errorReporter->dieError('Non Items cannot have sitelinks', 'not-recognized');
         }
         $this->assertArray($data['sitelinks'], 'List of sitelinks must be an array');
         $changeOps->add($this->getSiteLinksChangeOps($data['sitelinks'], $entity));
     }
     if (array_key_exists('claims', $data)) {
         $this->assertArray($data['claims'], 'List of claims must be an array');
         $changeOps->add($this->getClaimsChangeOps($data['claims']));
     }
     return $changeOps;
 }
 /**
  * @see ApiBase::execute
  *
  * @since 0.3
  */
 public function execute()
 {
     $params = $this->extractRequestParams();
     $this->validateParameters($params);
     $guid = $params['statement'];
     $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();
     $summary = $this->modificationHelper->createSummary($params, $this);
     $claim = $this->modificationHelper->getStatementFromEntity($guid, $entity);
     if (!$claim instanceof Statement) {
         $this->errorReporter->dieError('The referenced claim is not a statement and thus cannot have references', 'not-statement');
     }
     $referenceHashes = $this->getReferenceHashesFromParams($params, $claim);
     $changeOps = new ChangeOps();
     $changeOps->add($this->getChangeOps($guid, $referenceHashes));
     try {
         $changeOps->apply($entity, $summary);
     } catch (ChangeOpException $e) {
         $this->errorReporter->dieException($e, 'failed-save');
     }
     $status = $this->entitySavingHelper->attemptSaveEntity($entity, $summary, EDIT_UPDATE);
     $this->resultBuilder->addRevisionIdFromStatusToResult($status, 'pageinfo');
     $this->resultBuilder->markSuccess();
 }
示例#6
0
 public function testValidate_()
 {
     $item = new Item();
     $changeOp = $this->getMockBuilder('\\Wikibase\\ChangeOp\\ChangeOp')->disableOriginalConstructor()->getMock();
     $changeOp->expects($this->any())->method('validate')->will($this->returnCallback(function (Item $item) {
         // Fail when the label is already set (by a previous apply call).
         return $item->getFingerprint()->hasLabel('en') ? Result::newError(array()) : Result::newSuccess();
     }));
     $changeOp->expects($this->any())->method('apply')->will($this->returnCallback(function (Item $item) {
         $item->setLabel('en', 'Label');
     }));
     $changeOps = new ChangeOps();
     $changeOps->add($changeOp);
     $changeOps->add($changeOp);
     $result = $changeOps->validate($item);
     $this->assertFalse($result->isValid(), 'Validate must fail with this mock');
     $this->assertTrue($item->isEmpty(), 'Item must still be empty');
 }
示例#7
0
 /**
  * @see ModifyEntity::modifyEntity
  */
 protected function modifyEntity(Entity &$entity, array $params, $baseRevId)
 {
     $summary = $this->createSummary($params);
     $language = $params['language'];
     /** @var ChangeOp[] $aliasesChangeOps */
     $aliasesChangeOps = $this->getChangeOps($params);
     if (count($aliasesChangeOps) == 1) {
         $this->applyChangeOp($aliasesChangeOps[0], $entity, $summary);
     } else {
         $changeOps = new ChangeOps();
         $changeOps->add($aliasesChangeOps);
         $this->applyChangeOp($changeOps, $entity);
         // Set the action to 'set' in case we add and remove aliases in a single edit
         $summary->setAction('set');
         $summary->setLanguage($language);
         // Get the full list of current aliases
         $fingerprint = $entity->getFingerprint();
         $aliases = $fingerprint->hasAliasGroup($language) ? $fingerprint->getAliasGroup($language)->getAliases() : array();
         $summary->addAutoSummaryArgs($aliases);
     }
     $fingerprint = $entity->getFingerprint();
     if ($fingerprint->hasAliasGroup($language)) {
         $aliasGroupList = $fingerprint->getAliasGroups()->getWithLanguages(array($language));
         $this->getResultBuilder()->addAliasGroupList($aliasGroupList, 'entity');
     }
     return $summary;
 }