/** * @param EntityDocument $entity * * @throws InvalidArgumentException */ public function addEntity(EntityDocument $entity) { if ($entity->getId() === null) { throw new InvalidArgumentException('The entity needs to have an ID'); } $this->entities[$entity->getId()->getSerialization()] = $entity; }
/** * 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); }
/** * @see EntityValidator::validate() * * @param EntityDocument $entity * * @return Result */ public function validateEntity(EntityDocument $entity) { if ($entity instanceof FingerprintProvider) { return $this->duplicateDetector->detectLabelConflicts($entity->getType(), $entity->getFingerprint()->getLabels()->toTextArray(), null, $entity->getId()); } return Result::newSuccess(); }
/** * @see Iterator::key */ public function key() { if ($this->currentEntity === null) { return null; } return $this->currentEntity->getId()->getSerialization(); }
/** * @param EntityDocument $entity */ private function save(EntityDocument $entity) { $flags = $entity->getId() ? EDIT_UPDATE : EDIT_NEW; $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); $rev = $store->saveEntity($entity, '', $GLOBALS['wgUser'], $flags); $entity->setId($rev->getEntity()->getId()); }
/** * @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(EntityDocument $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 EntityHolder::getEntityId * * This implements lazy deserialization of the blob passed to the constructor. * * @param string $expectedClass The class with which the result is expected to be compatible. * Defaults to EntityDocument. * * @throws RuntimeException If the entity held by this EntityHolder is not compatible with $expectedClass. * @return EntityDocument */ public function getEntity($expectedClass = 'Wikibase\\DataModel\\Entity\\EntityDocument') { if (!$this->entity) { $this->entity = $this->codec->decodeEntity($this->blob, $this->contentFormat); if (!$this->entity instanceof EntityDocument) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected EntityDocument, got ' . gettype($this->entity)); } elseif ($this->entity->getType() !== $this->entityType) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $this->entityType . ', got ' . $this->entity->getType()); } elseif ($this->entity->getId() === null) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected an entity id to be set, got null'); } elseif ($this->entityId && !$this->entity->getId()->equals($this->entityId)) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $this->entityId . ', got ' . $this->entity->getId()); } } if (!$this->entity instanceof $expectedClass) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $expectedClass . ', got ' . get_class($this->entity)); } return $this->entity; }
/** * Add the entity's terms to the RDF graph. * * @param EntityDocument $entity the entity to output. */ public function addEntity(EntityDocument $entity) { if ($entity instanceof FingerprintProvider) { $fingerprint = $entity->getFingerprint(); /** @var EntityDocument $entity */ $entityLName = $this->vocabulary->getEntityLName($entity->getId()); $this->addLabels($entityLName, $fingerprint->getLabels()); $this->addDescriptions($entityLName, $fingerprint->getDescriptions()); $this->addAliases($entityLName, $fingerprint->getAliasGroups()); } }
/** * @see ChangeOp::apply * * @param EntityDocument $entity * @param Summary|null $summary * * @throws ChangeOpException */ public function apply(EntityDocument $entity, Summary $summary = null) { if (!$entity instanceof StatementListHolder) { throw new InvalidArgumentException('$entity must be a StatementListHolder'); } 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->applyStatementToEntity($entity, $summary); }
/** * @see EntityStore::saveEntity * @see WikiPage::doEditContent * * @param EntityDocument $entity * @param string $summary * @param User $user * @param int $flags * @param int|bool $baseRevId * * @throws StorageException * @return EntityRevision */ public function saveEntity(EntityDocument $entity, $summary, User $user, $flags = 0, $baseRevId = false) { if ($entity->getId() === null) { if (($flags & EDIT_NEW) !== EDIT_NEW) { throw new StorageException(Status::newFatal('edit-gone-missing')); } $this->assignFreshId($entity); } $content = $this->contentFactory->newFromEntity($entity); $revision = $this->saveEntityContent($content, $summary, $user, $flags, $baseRevId); $entityRevision = new EntityRevision($entity, $revision->getId(), $revision->getTimestamp()); $this->dispatcher->dispatch('entityUpdated', $entityRevision); return $entityRevision; }
/** * @see EntityDocumentSaver::saveEntityDocument */ public function saveEntityDocument(EntityDocument $entityDocument) { $this->database->selectCollection($entityDocument->getType())->upsert($this->buildGetEntityForIdQuery($entityDocument->getId()), $this->documentBuilder->buildDocumentForEntity($entityDocument)); }
/** * Stores the given Entity. * * @param EntityDocument $entity the entity to save. * @param string $summary ignored * @param User $user ignored * @param int $flags EDIT_XXX flags, as defined for WikiPage::doEditContent. * @param int|bool $baseRevisionId the revision ID $entity is based on. Saving should fail if * $baseRevId is no longer the current revision. * * @see WikiPage::doEditContent * * @return EntityRevision * @throws StorageException */ public function saveEntity(EntityDocument $entity, $summary, User $user, $flags = 0, $baseRevisionId = false) { $entityId = $entity->getId(); $status = Status::newGood(); if (($flags & EDIT_NEW) > 0 && $entityId && $this->hasEntity($entityId)) { $status->fatal('edit-already-exists'); } if (($flags & EDIT_UPDATE) > 0 && !$this->hasEntity($entityId)) { $status->fatal('edit-gone-missing'); } if ($baseRevisionId !== false && !$this->hasEntity($entityId)) { //TODO: find correct message key to use with status?? throw new StorageException('No base revision found for ' . $entityId->getSerialization()); } if ($baseRevisionId !== false && $this->getEntityRevision($entityId)->getRevisionId() !== $baseRevisionId) { $status->fatal('edit-conflict'); } if (!$status->isOK()) { throw new StorageException($status); } $revision = $this->putEntity($entity, 0, 0, $user); $this->putLog($revision->getRevisionId(), $entity->getId(), $summary, $user->getName()); return $revision; }
/** * Add truthy statements for the given entity to the RDF graph. * * @param EntityDocument $entity the entity to output. */ public function addEntity(EntityDocument $entity) { $entityId = $entity->getId(); if ($entity instanceof StatementListProvider) { $this->addStatements($entityId, $entity->getStatements()); } }
/** * @param EntityDocument $fromEntity * @param EntityDocument $toEntity * * @throws ItemMergeException */ private function validateEntities(EntityDocument $fromEntity, EntityDocument $toEntity) { if (!($fromEntity instanceof Item && $toEntity instanceof Item)) { throw new ItemMergeException('One or more of the entities are not items', 'not-item'); } if ($toEntity->getId()->equals($fromEntity->getId())) { throw new ItemMergeException('You must provide unique ids', 'cant-merge-self'); } }
/** * @param ParserOutput $parserOutput * @param EntityDocument $entity */ private function addTitleTextToParserOutput(ParserOutput $parserOutput, EntityDocument $entity) { $titleText = null; if ($entity instanceof FingerprintProvider) { $labels = $entity->getFingerprint()->getLabels()->toTextArray(); $preferred = $this->languageFallbackChain->extractPreferredValue($labels); if (is_array($preferred)) { $titleText = $preferred['value']; } } if (!is_string($titleText)) { $entityId = $entity->getId(); if ($entityId instanceof EntityId) { $titleText = $entityId->getSerialization(); } } $parserOutput->setExtensionData('wikibase-titletext', $titleText); }
/** * Adds meta-information about an entity (such as the ID and type) to the RDF graph. * * @todo: extract into MetaDataRdfBuilder * * @param EntityDocument $entity */ private function addEntityMetaData(EntityDocument $entity) { $entityLName = $this->vocabulary->getEntityLName($entity->getId()); $this->writer->about(RdfVocabulary::NS_ENTITY, $entityLName)->a(RdfVocabulary::NS_ONTOLOGY, $this->vocabulary->getEntityTypeName($entity->getType())); if ($entity instanceof Property) { $this->writer->say(RdfVocabulary::NS_ONTOLOGY, 'propertyType')->is(RdfVocabulary::NS_ONTOLOGY, $this->vocabulary->getDataTypeName($entity)); $id = $entity->getId()->getSerialization(); $this->writePropertyPredicates($id, $this->propertyIsLink($entity)); $this->writeNovalueClass($id); } }
/** * Validates this ChangeOp * * @see ChangeOp::validate() * * @since 0.5 * * @param EntityDocument $entity * * @return Result */ public function validate(EntityDocument $entity) { if (!$entity instanceof FingerprintHolder) { throw new InvalidArgumentException('$entity must be a FingerprintHolder'); } $languageValidator = $this->termValidatorFactory->getLanguageValidator(); $termValidator = $this->termValidatorFactory->getDescriptionValidator(); $fingerprintValidator = $this->termValidatorFactory->getFingerprintValidator($entity->getType()); // check that the language is valid $result = $languageValidator->validate($this->languageCode); if ($result->isValid() && $this->description !== null) { // Check that the new description is valid $result = $termValidator->validate($this->description); } if (!$result->isValid()) { return $result; } // Check if the new fingerprint of the entity is valid (e.g. if the combination // of label and description is still unique) $fingerprint = unserialize(serialize($entity->getFingerprint())); $this->updateFingerprint($fingerprint); $result = $fingerprintValidator->validateFingerprint($fingerprint, $entity->getId(), array($this->languageCode)); return $result; }
/** * @see EntityPermissionChecker::getPermissionStatusForEntity * * @note When checking for the 'edit' permission, this will check the 'createpage' * permission first in case the entity does not yet exist (i.e. if $entity->getId() * returns null). * * @param User $user * @param string $permission * @param EntityDocument $entity * @param string $quick * * @return Status a status object representing the check's result. * * @todo Move to a separate service (merge into WikiPageEntityStore?) */ public function getPermissionStatusForEntity(User $user, $permission, EntityDocument $entity, $quick = '') { $id = $entity->getId(); $status = null; if (!$id) { $entityType = $entity->getType(); if ($permission === 'edit') { // for editing a non-existing page, check the createpage permission $status = $this->getPermissionStatusForEntityType($user, 'createpage', $entityType, $quick); } if (!$status || $status->isOK()) { $status = $this->getPermissionStatusForEntityType($user, $permission, $entityType, $quick); } } else { $status = $this->getPermissionStatusForEntityId($user, $permission, $id, $quick); } return $status; }
/** * @see SpecialModifyEntity::getFormElements * * @param EntityDocument|null $entity * * @return string HTML */ protected function getFormElements(EntityDocument $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 EntityHolder::getEntityId * * @return EntityId|null */ public function getEntityId() { return $this->entity->getId(); }
private function addToOutput(EntityDocument $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); }
private function getSerialization(EntityDocument $entity, PropertyId $propertyId) { return array('id' => $entity->getId()->getSerialization(), 'type' => $entity->getType(), 'labels' => array('de' => array('language' => 'de', 'value' => 'Kuchen'), 'en' => array('language' => 'en', 'value' => 'Cake')), 'claims' => array($propertyId->getSerialization() => array(array('id' => $this->makeGuid($entity->getId()), 'mainsnak' => array('snaktype' => 'value', 'property' => $propertyId->getSerialization(), 'datavalue' => array('value' => 'kittens!', 'type' => 'string')), 'type' => 'statement', 'rank' => 'normal')))); }
/** * @param EntityDocument $entity * * @return TermIndexEntry[] */ public function getEntityTerms(EntityDocument $entity) { // FIXME: OCP violation. No support for new types of entities can be registered if ($entity instanceof FingerprintProvider) { $fingerprint = $entity->getFingerprint(); /** @var EntityDocument $entity */ $extraFields = array('entityType' => $entity->getType()); $entityId = $entity->getId(); if ($entityId !== null) { $extraFields['entityId'] = $entityId->getNumericId(); } return $this->getFingerprintTerms($fingerprint, $extraFields); } return array(); }
/** * @param mixed $data * @param EntityDocument $entity * @param int $revisionId */ private function validateDataProperties($data, EntityDocument $entity, $revisionId = 0) { $entityId = $entity->getId(); $title = $entityId === null ? null : $this->getTitleLookup()->getTitleForId($entityId); $allowedProps = array('length', 'count', 'touched', 'id', 'type', 'pageid', 'ns', 'title', 'lastrevid', 'labels', 'descriptions', 'aliases', 'sitelinks', 'claims', 'datatype'); $this->checkValidJson($data, $allowedProps); $this->checkEntityId($data, $entityId); $this->checkEntityType($data, $entity); $this->checkPageIdProp($data, $title); $this->checkNamespaceProp($data, $title); $this->checkTitleProp($data, $title); $this->checkRevisionProp($data, $revisionId); }
/** * Returns the form elements. * * @since 0.5 * * @param EntityDocument|null $entity * * @return string HTML */ protected function getFormElements(EntityDocument $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)); }
/** * Constructs an EntityChange from the given old and new Entity. * * @since 0.5 * * @param string $action The action name * @param EntityDocument|null $oldEntity * @param EntityDocument|null $newEntity * @param array $fields additional fields to set * * @return EntityChange * @throws MWException */ public function newFromUpdate($action, EntityDocument $oldEntity = null, EntityDocument $newEntity = null, array $fields = array()) { if ($oldEntity === null && $newEntity === null) { throw new MWException('Either $oldEntity or $newEntity must be given'); } if ($oldEntity === null) { $oldEntity = $this->entityFactory->newEmpty($newEntity->getType()); $id = $newEntity->getId(); } elseif ($newEntity === null) { $newEntity = $this->entityFactory->newEmpty($oldEntity->getType()); $id = $oldEntity->getId(); } elseif ($oldEntity->getType() !== $newEntity->getType()) { throw new MWException('Entity type mismatch'); } else { $id = $newEntity->getId(); } // HACK: don't include statements diff, since those are unused and not helpful // performance-wise to the dispatcher and change handling. // For a better solution, see T113468. if ($oldEntity instanceof StatementListHolder) { $oldEntity->setStatements(new StatementList()); $newEntity->setStatements(new StatementList()); } $diff = $this->entityDiffer->diffEntities($oldEntity, $newEntity); /** @var EntityChange $instance */ $instance = self::newForEntity($action, $id, $fields); $instance->setDiff($diff); return $instance; }