/**
  * @see SpecialSetEntity::setValue()
  *
  * @since 0.4
  *
  * @param Entity $entity
  * @param string $languageCode
  * @param string $value
  *
  * @return Summary
  */
 protected function setValue($entity, $languageCode, $value)
 {
     $summary = new Summary('wbsetaliases');
     if ($value === '') {
         $aliases = $entity->getFingerprint()->getAliasGroup($languageCode)->getAliases();
         $changeOp = $this->termChangeOpFactory->newRemoveAliasesOp($languageCode, $aliases);
     } else {
         $changeOp = $this->termChangeOpFactory->newSetAliasesOp($languageCode, explode('|', $value));
     }
     $this->applyChangeOp($changeOp, $entity, $summary);
     return $summary;
 }
 /**
  * @param Entity $entity
  *
  * @return array
  */
 public function build(Entity $entity)
 {
     $entityId = $entity->getId();
     if (!$entityId) {
         $entityId = '';
         //XXX: should probably throw an exception
     } else {
         $entityId = $entityId->getSerialization();
     }
     $configVars = array('wbEntityId' => $entityId, 'wbEntity' => FormatJson::encode($this->getSerializedEntity($entity)));
     return $configVars;
 }
 /**
  * @see ChangeOp::apply()
  * - a new claim with $snak as mainsnak gets added when $claimGuid is empty and $snak is set
  * - the claim's mainsnak gets set to $snak when $claimGuid and $snak are set
  */
 public function apply(Entity $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $entity->getStatements();
     if (empty($this->statementGuid)) {
         $this->addStatement($statements, $entity->getId(), $summary);
     } else {
         $this->setStatement($statements, $summary);
     }
     $entity->setStatements($statements);
 }
 /**
  * @see ChangeOp::apply
  *
  * @param Entity $entity
  * @param Summary|null $summary
  *
  * @throws InvalidArgumentException
  * @throws ChangeOpException
  */
 public function apply(Entity $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $entity->getStatements();
     $statement = $statements->getFirstStatementWithGuid($this->guid);
     if ($statement === null) {
         throw new ChangeOpException("Entity does not have statement with GUID {$this->guid}");
     }
     $statements->removeStatementsWithGuid($this->guid);
     $entity->setStatements($statements);
     $removedSnak = $statement->getMainSnak();
     $this->updateSummary($summary, 'remove', '', $this->getSummaryArgs($removedSnak));
 }
 /**
  * @see ChangeOp::apply()
  */
 public function apply(Entity $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $entity->getStatements();
     $statement = $statements->getFirstStatementWithGuid($this->statementGuid);
     if ($statement === null) {
         throw new ChangeOpException("Entity does not have a statement with GUID {$this->statementGuid}");
     }
     $qualifiers = $statement->getQualifiers();
     $this->removeQualifier($qualifiers, $summary);
     $statement->setQualifiers($qualifiers);
     $entity->setStatements($statements);
 }
 /**
  * Watches or unwatches the entity.
  *
  * @note Keep in sync with logic in EditPage!
  * @todo: move to separate service
  *
  * @param bool $watch whether to watch or unwatch the page.
  *
  * @throws MWException
  */
 private function updateWatchlist($watch)
 {
     if ($this->getTitle() === null) {
         throw new MWException('Title not yet known!');
     }
     $this->entityStore->updateWatchlist($this->user, $this->newEntity->getId(), $watch);
 }
 public function importStatements(Entity $entity)
 {
     $statements = $entity->getStatements();
     $this->logger->info('Adding statements: ' . $entity->getId()->getSerialization());
     if (!$statements->isEmpty()) {
         $localId = $this->entityMappingStore->getLocalId($entity->getId()->getSerialization());
         if (!$localId) {
             $this->logger->error($entity->getId()->getSerialization() . ' not found');
         }
         try {
             $this->addStatementList($this->idParser->parse($localId), $statements);
         } catch (\Exception $ex) {
             $this->logger->error($ex->getMessage());
         }
     }
 }
 /**
  * @see ChangeOp::apply()
  */
 public function apply(Entity $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $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);
     $entity->setStatements($statements);
 }
 /**
  * @see ChangeOp::apply()
  */
 public function apply(Entity $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $entity->getStatements();
     $statement = $statements->getFirstStatementWithGuid($this->statementGuid);
     if ($statement === null) {
         throw new ChangeOpException("Entity does not have a statement with GUID {$this->statementGuid}");
     }
     $oldRank = $statement->getRank();
     $statement->setRank($this->rank);
     $this->updateSummary($summary, null, '', $this->getSnakSummaryArgs($statement->getMainSnak()));
     if ($summary !== null) {
         $statementRankSerializer = new StatementRankSerializer();
         $summary->addAutoCommentArgs(array($statementRankSerializer->serialize($oldRank), $statementRankSerializer->serialize($this->rank)));
     }
     $entity->setStatements($statements);
 }
Exemple #10
0
 /**
  * @param array $params
  * @param Statement $statement
  * @param Entity $entity
  *
  * @throws InvalidArgumentException
  * @return Summary
  *
  * @todo this summary builder is ugly and summary stuff needs to be refactored
  */
 private function getSummary(array $params, Statement $statement, Entity $entity)
 {
     if (!$entity instanceof StatementListProvider) {
         throw new InvalidArgumentException('$entity must be a StatementListProvider');
     }
     $claimSummaryBuilder = new ClaimSummaryBuilder($this->getModuleName(), new ClaimDiffer(new OrderedListDiffer(new ComparableComparer())));
     $summary = $claimSummaryBuilder->buildClaimSummary($entity->getStatements()->getFirstStatementWithGuid($statement->getGuid()), $statement);
     if (isset($params['summary'])) {
         $summary->setUserSummary($params['summary']);
     }
     return $summary;
 }
 /**
  * @dataProvider provideSaveEntityError
  */
 public function testSaveEntityError(Entity $entity, $flags, $baseRevId, $error)
 {
     /** @var WikiPageEntityStore $store */
     list($store, ) = $this->createStoreAndLookup();
     $user = $GLOBALS['wgUser'];
     // setup target item
     $one = new Item();
     $one->setLabel('en', 'one');
     $r1 = $store->saveEntity($one, 'create one', $user, EDIT_NEW);
     // inject ids
     if (is_int($baseRevId)) {
         // use target item's revision as an offset
         $baseRevId += $r1->getRevisionId();
     }
     if ($entity->getId() === null) {
         // use target item's id
         $entity->setId($r1->getEntity()->getId());
     }
     // check for error
     $this->setExpectedException($error);
     $store->saveEntity($entity, '', $GLOBALS['wgUser'], $flags, $baseRevId);
 }
 /**
  * @see ChangeOp::apply
  */
 public function apply(Entity $entity, Summary $summary = null)
 {
     if (!$entity instanceof Item) {
         throw new InvalidArgumentException('ChangeOpSiteLink can only be applied to Item instances');
     }
     $siteLinks = $entity->getSiteLinkList();
     if ($this->pageName === null && $this->badges === null || $this->pageName === '') {
         if ($siteLinks->hasLinkWithSiteId($this->siteId)) {
             $this->updateSummary($summary, 'remove', $this->siteId, $siteLinks->getBySiteId($this->siteId)->getPageName());
             $siteLinks->removeLinkWithSiteId($this->siteId);
         } else {
             //TODO: throw error, or ignore silently?
         }
     } else {
         $commentArgs = array();
         if ($this->pageName === null) {
             if (!$siteLinks->hasLinkWithSiteId($this->siteId)) {
                 throw new InvalidArgumentException('The sitelink does not exist');
             }
             // If page name is not set (but badges are) make sure that it remains intact
             $pageName = $siteLinks->getBySiteId($this->siteId)->getPageName();
         } else {
             $pageName = $this->pageName;
             $commentArgs[] = $pageName;
         }
         $action = $siteLinks->hasLinkWithSiteId($this->siteId) ? 'set' : 'add';
         $badges = $this->applyBadges($siteLinks, $action, $commentArgs);
         $this->updateSummary($summary, $action, $this->siteId, $commentArgs);
         // FIXME: Use SiteLinkList::setNewSiteLink.
         $siteLinks->removeLinkWithSiteId($this->siteId);
         $siteLinks->addNewSiteLink($this->siteId, $pageName, $badges);
     }
 }
 /**
  * @param Entity $entity
  * @param Summary|null $summary
  *
  * @throws InvalidArgumentException
  */
 private function applyClaimToEntity(Entity $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $this->removeStatement($entity->getStatements()->toArray(), $summary);
     $statements = $this->addStatement($statements);
     $entity->setStatements(new StatementList($statements));
 }
 private function getReferencedEntities(Entity $entity)
 {
     $snaks = $entity->getStatements()->getAllSnaks();
     $entities = array();
     foreach ($snaks as $snak) {
         $entities[] = $snak->getPropertyId()->getSerialization();
         if ($snak instanceof PropertyValueSnak) {
             $value = $snak->getDataValue();
             if ($value instanceof EntityIdValue) {
                 $entities[] = $value->getEntityId()->getSerialization();
             }
         }
     }
     return array_unique($entities);
 }
 public function detectLabelDescriptionConflictsForEntity(Entity $entity)
 {
     foreach ($entity->getFingerprint()->getLabels()->toTextArray() as $lang => $label) {
         if (!$entity->getFingerprint()->hasDescription($lang)) {
             continue;
         }
         $description = $entity->getFingerprint()->getDescription($lang)->getText();
         if ($label === 'DUPE' && $description === 'DUPE') {
             return Result::newError(array(Error::newError('found conflicting terms', 'label', 'label-with-description-conflict', array('label', $lang, $label, 'Q666'))));
         }
     }
     return Result::newSuccess();
 }
 /**
  * Validates this ChangeOp
  *
  * @see ChangeOp::validate()
  *
  * @since 0.5
  *
  * @param Entity $entity
  *
  * @throws ChangeOpException
  * @return Result
  */
 public function validate(Entity $entity)
 {
     $languageValidator = $this->termValidatorFactory->getLanguageValidator();
     $termValidator = $this->termValidatorFactory->getLabelValidator($entity->getType());
     // check that the language is valid
     $result = $languageValidator->validate($this->languageCode);
     if (!$result->isValid()) {
         return $result;
     }
     // It should be possible to remove invalid aliases, but not to add/set new invalid ones
     if ($this->action === 'set' || $this->action === '' || $this->action === 'add') {
         // Check that the new aliases are valid
         foreach ($this->aliases as $alias) {
             $result = $termValidator->validate($alias);
             if (!$result->isValid()) {
                 return $result;
             }
         }
     } elseif ($this->action !== 'remove') {
         throw new ChangeOpException('Bad action: ' . $this->action);
     }
     //XXX: Do we want to check the updated fingerprint, as we do for labels and descriptions?
     return $result;
 }
 /**
  * Prepares the given entity data for comparison with $entity.
  * That is, this method should add any extra data from $entity to $entityData.
  *
  * @param Entity $entity
  * @param array $entityData
  */
 protected function prepareEntityData(Entity $entity, array &$entityData)
 {
     /* @var Property $entity */
     $entityData['datatype'] = $entity->getDataTypeId();
 }
 /**
  * Validates this ChangeOp
  *
  * @see ChangeOp::validate()
  *
  * @since 0.5
  *
  * @param Entity $entity
  *
  * @return Result
  */
 public function validate(Entity $entity)
 {
     $languageValidator = $this->termValidatorFactory->getLanguageValidator();
     $termValidator = $this->termValidatorFactory->getLabelValidator($entity->getType());
     $fingerprintValidator = $this->termValidatorFactory->getFingerprintValidator($entity->getType());
     // check that the language is valid
     $result = $languageValidator->validate($this->languageCode);
     if ($result->isValid() && $this->label !== null) {
         // Check that the new label is valid
         $result = $termValidator->validate($this->label);
     }
     if (!$result->isValid()) {
         return $result;
     }
     // Check if the new fingerprint of the entity is valid (e.g. if the label is unique)
     $fingerprint = unserialize(serialize($entity->getFingerprint()));
     $this->updateFingerprint($fingerprint);
     $result = $fingerprintValidator->validateFingerprint($fingerprint, $entity->getId(), array($this->languageCode));
     return $result;
 }
 /**
  * @see SpecialModifyEntity::modifyEntity
  *
  * @param Entity $entity
  *
  * @return Summary|bool
  */
 protected function modifyEntity(Entity $entity)
 {
     $changeOps = $this->getChangeOps($entity->getFingerprint());
     $summary = false;
     $success = true;
     foreach ($changeOps as $module => $changeOp) {
         $summary = new Summary($module);
         try {
             $this->applyChangeOp($changeOp, $entity, $summary);
         } catch (ChangeOpException $ex) {
             $this->showErrorHTML($ex->getMessage());
             $success = false;
         }
     }
     if (!$success) {
         return false;
     } elseif (count($changeOps) === 1) {
         return $summary;
     }
     return $this->getSummaryForLabelDescriptionAliases();
 }
 public function doTestValidRequest(Entity $entity, $guid, $value, $expectedSummary)
 {
     $wikibaseRepo = WikibaseRepo::getDefaultInstance();
     $entityLookup = $wikibaseRepo->getEntityLookup();
     $obtainedEntity = $entityLookup->getEntity($entity->getId());
     $claimCount = count($obtainedEntity->getClaims());
     $params = array('action' => 'wbsetclaimvalue', 'claim' => $guid, 'value' => FormatJson::encode($value), 'snaktype' => 'value');
     list($resultArray, ) = $this->doApiRequestWithToken($params);
     $this->assertResultSuccess($resultArray);
     $this->assertInternalType('array', $resultArray, 'top level element is an array');
     $this->assertArrayHasKey('claim', $resultArray, 'top level element has a claim key');
     $claim = $resultArray['claim'];
     $this->assertEquals($value, $claim['mainsnak']['datavalue']['value']);
     /** @var StatementListProvider $obtainedEntity */
     $obtainedEntity = $entityLookup->getEntity($entity->getId());
     $page = new WikiPage($wikibaseRepo->getEntityTitleLookup()->getTitleForId($entity->getId()));
     $generatedSummary = $page->getRevision()->getComment(Revision::RAW);
     $this->assertEquals($expectedSummary, $generatedSummary, 'Summary mismatch');
     $statements = $obtainedEntity->getStatements();
     $this->assertEquals($claimCount, $statements->count(), 'Claim count should not change after doing a setclaimvalue request');
     $obtainedClaim = $statements->getFirstStatementWithGuid($guid);
     $this->assertNotNull($obtainedClaim);
     $dataValue = $wikibaseRepo->getDataValueFactory()->newFromArray($claim['mainsnak']['datavalue']);
     $this->assertTrue($obtainedClaim->getMainSnak()->getDataValue()->equals($dataValue));
 }
Exemple #21
0
 /**
  * @param Entity $entity
  */
 private function buildResult(Entity $entity)
 {
     $builder = $this->getResultBuilder();
     if ($entity instanceof FingerprintProvider) {
         $fingerprint = $entity->getFingerprint();
         $builder->addLabels($fingerprint->getLabels(), 'entity');
         $builder->addDescriptions($fingerprint->getDescriptions(), 'entity');
         $builder->addAliasGroupList($fingerprint->getAliasGroups(), 'entity');
     }
     if ($entity instanceof Item) {
         $builder->addSiteLinkList($entity->getSiteLinkList(), 'entity');
     }
     $builder->addStatements($entity->getClaims(), 'entity');
 }
 /**
  * Returns the form elements.
  *
  * @since 0.5
  *
  * @param Entity $entity
  *
  * @return string HTML
  */
 protected function getFormElements(Entity $entity = null)
 {
     $id = 'wb-modifyentity-id';
     return Html::label($this->msg('wikibase-modifyentity-id')->text(), $id, array('class' => 'wb-label')) . Html::input('id', $entity === null ? '' : $entity->getId(), 'text', array('class' => 'wb-input', 'id' => $id));
 }
 /**
  * Attempt to modify entity
  *
  * @since 0.1
  *
  * @param Entity &$entity
  *
  * @return Status
  */
 protected function modifyEntity(Entity &$entity)
 {
     $contentLanguageCode = $this->contentLanguage->getCode();
     if ($this->label !== '') {
         $entity->setLabel($contentLanguageCode, $this->label);
     }
     if ($this->description !== '') {
         $entity->setDescription($contentLanguageCode, $this->description);
     }
     if (count($this->aliases) !== 0) {
         $entity->setAliases($contentLanguageCode, $this->aliases);
     }
     return Status::newGood();
 }
 protected function addToOutput(Entity $entity, Status $status, $oldRevId = null)
 {
     $this->getResultBuilder()->addBasicEntityInformation($entity->getId(), 'entity');
     $this->getResultBuilder()->addRevisionIdFromStatusToResult($status, 'entity', $oldRevId);
     $params = $this->extractRequestParams();
     if (isset($params['site']) && isset($params['title'])) {
         $normTitle = $this->stringNormalizer->trimToNFC($params['title']);
         if ($normTitle !== $params['title']) {
             $this->getResultBuilder()->addNormalizedTitle($params['title'], $normTitle, 'normalized');
         }
     }
     $this->getResultBuilder()->markSuccess(1);
 }
 /**
  * @param Entity $entity
  * @param Snak $mainSnak
  *
  * @return Statement
  */
 private function makeStatement(Entity $entity, Snak $mainSnak)
 {
     $statement = new Statement($mainSnak);
     $guidGenerator = new GuidGenerator();
     $statement->setGuid($guidGenerator->newGuid($entity->getId()));
     return $statement;
 }
Exemple #26
0
 private function assertHasNoTerms(Entity $entity)
 {
     $this->assertEquals(array(), $entity->getLabels());
     $this->assertEquals(array(), $entity->getDescriptions());
     $this->assertEquals(array(), $entity->getAllAliases());
 }