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());
         }
     }
 }
Example #2
0
 /**
  * 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);
 }
 /**
  * @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);
 }
 /**
  * @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;
 }
Example #6
0
 /**
  * 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;
 }
 /**
  * @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);
 }
Example #8
0
 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);
 }
 /**
  * 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));
 }
 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));
 }
 /**
  * @see SpecialModifyEntity::getFormElements
  *
  * @param Entity $entity
  *
  * @return string HTML
  */
 protected function getFormElements(Entity $entity = null)
 {
     if ($entity !== null && $this->languageCode !== null) {
         $languageName = Language::fetchLanguageName($this->languageCode, $this->getLanguage()->getCode());
         $intro = $this->msg('wikibase-setlabeldescriptionaliases-introfull', $this->getEntityTitle($entity->getId())->getPrefixedText(), $languageName);
         $html = Html::hidden('id', $entity->getId()->getSerialization()) . Html::hidden('language', $this->languageCode) . $this->getLabeledInputField('label', $this->label) . Html::element('br') . $this->getLabeledInputField('description', $this->description) . Html::element('br') . $this->getLabeledInputField('aliases', implode('|', $this->aliases));
     } else {
         $intro = $this->msg('wikibase-setlabeldescriptionaliases-intro');
         $fieldId = 'wikibase-setlabeldescriptionaliases-language';
         $languageCode = $this->languageCode ?: $this->getLanguage()->getCode();
         $html = parent::getFormElements($entity) . Html::element('br') . Html::label($this->msg('wikibase-modifyterm-language')->text(), $fieldId, array('class' => 'wb-label')) . Html::input('language', $languageCode, 'text', array('class' => 'wb-input', 'id' => $fieldId));
     }
     return Html::rawElement('p', array(), $intro->parse()) . $html . Html::element('br');
 }
 /**
  * @see ChangeOp::apply
  *
  * @param Entity $entity
  * @param Summary|null $summary
  *
  * @throws ChangeOpException
  */
 public function apply(Entity $entity, Summary $summary = null)
 {
     if ($this->statement->getGuid() === null) {
         $this->statement->setGuid($this->guidGenerator->newGuid($entity->getId()));
     }
     $guid = $this->guidParser->parse($this->statement->getGuid());
     if ($this->guidValidator->validate($guid->getSerialization()) === false) {
         throw new ChangeOpException("Claim does not have a valid GUID");
     } elseif (!$entity->getId()->equals($guid->getEntityId())) {
         throw new ChangeOpException("Claim GUID invalid for given entity");
     }
     $this->applyClaimToEntity($entity, $summary);
 }
Example #13
0
 /**
  * @dataProvider instanceProvider
  * @param Entity $entity
  */
 public function testCopy(Entity $entity)
 {
     $copy = $entity->copy();
     // The equality method alone is not enough since it does not check the IDs.
     $this->assertTrue($entity->equals($copy));
     $this->assertEquals($entity->getId(), $copy->getId());
     $this->assertNotSame($entity, $copy);
 }