/** * @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(); }
/** * @see ApiBase::execute * * @since 0.4 */ public function execute() { $params = $this->extractRequestParams(); $claim = $this->getClaimFromParams($params); $guid = $claim->getGuid(); if ($guid === null) { $this->errorReporter->dieError('GUID must be set when setting a claim', 'invalid-claim'); } try { $claimGuid = $this->guidParser->parse($guid); } catch (StatementGuidParsingException $ex) { $this->errorReporter->dieException($ex, 'invalid-claim'); } $entityId = $claimGuid->getEntityId(); if (isset($params['baserevid'])) { $entityRevision = $this->loadEntityRevision($entityId, (int) $params['baserevid']); } else { $entityRevision = $this->loadEntityRevision($entityId); } $entity = $entityRevision->getEntity(); $summary = $this->getSummary($params, $claim, $entity); $changeop = $this->statementChangeOpFactory->newSetStatementOp($claim, isset($params['index']) ? $params['index'] : null); $this->modificationHelper->applyChangeOp($changeop, $entity, $summary); $status = $this->saveChanges($entity, $summary); $this->getResultBuilder()->addRevisionIdFromStatusToResult($status, 'pageinfo'); $this->getResultBuilder()->markSuccess(); $this->getResultBuilder()->addStatement($claim); }
/** * @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'); }
private function propertyMatchesFilter(EntityId $propertyId) { if (isset($this->requestParams['property'])) { try { $parsedProperty = $this->idParser->parse($this->requestParams['property']); } catch (EntityIdParsingException $e) { $this->errorReporter->dieException($e, 'param-invalid'); } /** @var EntityId $parsedProperty */ return $propertyId->equals($parsedProperty); } return true; }
/** * @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(); }
/** * @see ApiBase::execute * * @since 0.3 */ public function execute() { $params = $this->extractRequestParams(); $this->validateParameters($params); list($idString, $guid) = $this->getIdentifiers($params); try { $entityId = $this->idParser->parse($idString); } catch (EntityIdParsingException $e) { $this->errorReporter->dieException($e, 'param-invalid'); } /** @var EntityId $entityId */ $entityRevision = $this->entityLoadingHelper->loadEntityRevision($entityId, EntityRevisionLookup::LATEST_FROM_SLAVE); $entity = $entityRevision->getEntity(); $claims = $this->getClaims($entity, $guid); $this->resultBuilder->addStatements($claims, null, $params['props']); }
/** * Load the entity content of the given revision. * * Will fail by calling dieException() $this->errorReporter if the revision * cannot be found or cannot be loaded. * * @since 0.5 * * @param EntityId $entityId EntityId of the page to load the revision for * @param int|string $revId revision to load. If not given, the current revision will be loaded. * * @throws UsageException * @throws LogicException * * @return EntityRevision */ public function loadEntityRevision(EntityId $entityId, $revId = EntityRevisionLookup::LATEST_FROM_MASTER) { try { $revision = $this->entityRevisionLookup->getEntityRevision($entityId, $revId); if (!$revision) { $this->errorReporter->dieError('Entity ' . $entityId->getSerialization() . ' not found', 'cant-load-entity-content'); } return $revision; } catch (RevisionedUnresolvedRedirectException $ex) { $this->errorReporter->dieException($ex, 'unresolved-redirect'); } catch (BadRevisionException $ex) { $this->errorReporter->dieException($ex, 'nosuchrevid'); } catch (StorageException $ex) { $this->errorReporter->dieException($ex, 'cant-load-entity-content'); } throw new LogicException('ApiErrorReporter::dieException did not throw a UsageException'); }
/** * @param ItemMergeException|RedirectCreationException $ex * * @throws UsageException always */ private function handleException(Exception $ex) { $cause = $ex->getPrevious(); if ($cause) { $this->errorReporter->dieException($cause, $ex->getErrorCode()); } else { $this->errorReporter->dieError($ex->getMessage(), $ex->getErrorCode()); } }
/** * @dataProvider exceptionProvider */ public function testDieException($exception, $code, $httpStatusCode, array $extradata = null, $infoPattern, array $expectedDataFields) { $api = new ApiMain(); $localizer = $this->getExceptionLocalizer(); $reporter = new ApiErrorReporter($api, $localizer, Language::factory('de')); try { $reporter->dieException($exception, $code, $httpStatusCode, $extradata); $this->fail('UsageException was not thrown!'); } catch (UsageException $ex) { $this->assertUsageException($infoPattern, $code, $httpStatusCode, $expectedDataFields, $ex); } }
/** * Applies the given ChangeOp to the given Entity. * Any ChangeOpException is converted into a UsageException with the code 'modification-failed'. * * @since 0.5 * * @param ChangeOp $changeOp * @param Entity $entity * @param Summary $summary The summary object to update with information about the change. * * @throws UsageException */ protected function applyChangeOp(ChangeOp $changeOp, Entity $entity, Summary $summary = null) { try { $result = $changeOp->validate($entity); if (!$result->isValid()) { throw new ChangeOpValidationException($result); } $changeOp->apply($entity, $summary); } catch (ChangeOpException $ex) { $this->errorReporter->dieException($ex, 'modification-failed'); } }
/** * @param string $json A JSON-encoded DataValue * * @throws UsageException * @throws LogicException * @return DataValue */ private function decodeDataValue($json) { $data = json_decode($json, true); if (!is_array($data)) { $this->errorReporter->dieError('Failed to decode datavalue', 'baddatavalue'); } try { $value = $this->dataValueFactory->newFromArray($data); return $value; } catch (IllegalValueException $ex) { $this->errorReporter->dieException($ex, 'baddatavalue'); } throw new LogicException('ApiErrorReporter::dieException did not throw a UsageException'); }
/** * @param array[] $statements array of serialized statements * * @return ChangeOp[] */ private function getModifyStatementChangeOps(array $statements) { $opsToReturn = array(); foreach ($statements as $statementArray) { if (!array_key_exists('remove', $statementArray)) { try { $statement = $this->statementDeserializer->deserialize($statementArray); if (!$statement instanceof Statement) { throw new IllegalValueException('Statement serialization did not contained a Statement.'); } $opsToReturn[] = $this->statementChangeOpFactory->newSetStatementOp($statement); } catch (IllegalValueException $ex) { $this->errorReporter->dieException($ex, 'invalid-claim'); } catch (MWException $ex) { $this->errorReporter->dieException($ex, 'invalid-claim'); } } } return $opsToReturn; }