/** * @param PropertyId $propertyId * @param User $user User to attribute the changes made to. * @param string $dataTypeId * * @throws InvalidArgumentException * @throws StorageException */ public function changeDataType(PropertyId $propertyId, User $user, $dataTypeId) { $entityRevision = $this->entityRevisionLookup->getEntityRevision($propertyId, EntityRevisionLookup::LATEST_FROM_MASTER); if ($entityRevision === null) { throw new StorageException("Could not load property: " . $propertyId->getSerialization()); } /* @var $property Property */ $property = $entityRevision->getEntity(); $oldDataTypeId = $property->getDataTypeId(); $this->assertDataTypesCompatible($oldDataTypeId, $dataTypeId); $property->setDataTypeId($dataTypeId); $this->entityStore->saveEntity($property, 'Changed data type from ' . $oldDataTypeId . ' to ' . $dataTypeId, $user, EDIT_UPDATE, $entityRevision->getRevisionId()); }
/** * 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); }
/** * Save the new version of the given item. * * @param Item $item * @param User $user * * @return bool */ private function saveChanges(Item $item, User $user) { $summary = $this->getSummary(); $itemId = $item->getId(); $summaryString = $this->summaryFormatter->formatSummary($summary); $editEntity = $this->editEntityFactory->newEditEntity($user, $item, true); $status = $editEntity->attemptSave($summaryString, EDIT_UPDATE, false, $this->entityStore->isWatching($user, $itemId)); if (!$status->isOK()) { wfDebugLog('UpdateRepo', __FUNCTION__ . ': attemptSave for ' . $itemId->getSerialization() . ' failed: ' . $status->getMessage()->text()); } return $status->isOK(); }
/** * @param string[] $labels Associative array, mapping language codes to labels. * * @return bool true if the item was created, false otherwise */ private function createProperty(array $labels) { $property = Property::newFromType('wikibase-item'); $fingerprint = $property->getFingerprint(); foreach ($labels as $languageCode => $label) { $fingerprint->setLabel($languageCode, $label); } try { $this->store->saveEntity($property, 'imported', $this->user, EDIT_NEW); return true; } catch (Exception $ex) { $this->doPrint('ERROR: ' . str_replace("\n", ' ', $ex->getMessage())); } return false; }
private function saveEntity(Entity $entity, Summary $summary, $bot) { // Given we already check all constraints in ChangeOpsMerge, it's // fine to ignore them here. This is also needed to not run into // the constraints we're supposed to ignore (see ChangeOpsMerge::removeConflictsWithEntity // for reference) $flags = EDIT_UPDATE | EntityContent::EDIT_IGNORE_CONSTRAINTS; if ($bot && $this->user->isAllowed('bot')) { $flags |= EDIT_FORCE_BOT; } try { return $this->entityStore->saveEntity($entity, $this->summaryFormatter->formatSummary($summary), $this->user, $flags); } catch (StorageException $ex) { throw new ItemMergeException($ex->getMessage(), 'failed-save', $ex); } }
private function createTitleForEntity(Entity $entity) { // NOTE: needs database access $this->entityStore->assignFreshId($entity); $titleLookup = WikibaseRepo::getDefaultInstance()->getEntityTitleLookup(); $title = $titleLookup->getTitleForId($entity->getId()); if (!$title->exists()) { $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); $store->saveEntity($entity, 'test', $GLOBALS['wgUser']); // $title lies, make a new one $title = Title::makeTitleSafe($title->getNamespace(), $title->getText()); } // sanity check - page must exist now $this->assertGreaterThan(0, $title->getArticleID(), 'sanity check: getArticleID()'); $this->assertTrue($title->exists(), 'sanity check: exists()'); return $title; }
/** * @param EntityRedirect $redirect * @param Summary $summary * @param bool $bot Whether the edit should be marked as bot * * @throws RedirectCreationException */ private function saveRedirect(EntityRedirect $redirect, Summary $summary, $bot) { $summary = $this->summaryFormatter->formatSummary($summary); $flags = EDIT_UPDATE; if ($bot) { $flags = $flags | EDIT_FORCE_BOT; } $hookStatus = $this->editFilterHookRunner->run($redirect, $this->user, $summary); if (!$hookStatus->isOK()) { throw new RedirectCreationException('EditFilterHook stopped redirect creation', 'cant-redirect'); } try { $this->entityStore->saveRedirect($redirect, $summary, $this->user, $flags); } catch (StorageException $ex) { throw new RedirectCreationException($ex->getMessage(), 'cant-redirect', $ex); } }
/** * @see ApiBase::execute() * * @since 0.1 */ public function execute() { $params = $this->extractRequestParams(); $user = $this->getUser(); $this->flags = 0; $this->validateParameters($params); // Try to find the entity or fail and create it, or die in the process $entityRev = $this->getEntityRevisionFromApiParams($params); if (is_null($entityRev)) { $entity = $this->createEntity($params['new']); $entityRevId = 0; // HACK: We need to assign an ID early, for things like the ClaimIdGenerator. if ($entity->getId() === null) { $this->entityStore->assignFreshId($entity); } } else { $entity = $entityRev->getEntity(); $entityRevId = $entityRev->getRevisionId(); } if ($entity->getId() === null) { throw new LogicException('The Entity should have an ID at this point!'); } // At this point only change/edit rights should be checked $status = $this->checkPermissions($entity, $user); if (!$status->isOK()) { $this->errorReporter->dieError('You do not have sufficient permissions', 'permissiondenied'); } $summary = $this->modifyEntity($entity, $params, $entityRevId); if (!$summary) { //XXX: This could rather be used for "silent" failure, i.e. in cases where // there was simply nothing to do. $this->errorReporter->dieError('Attempted modification of the item failed', 'failed-modify'); } $this->addFlags($entity->getId() === null); //NOTE: EDIT_NEW will not be set automatically. If the entity doesn't exist, and EDIT_NEW was // not added to $this->flags explicitly, the save will fail. $status = $this->attemptSaveEntity($entity, $summary, $this->flags); $this->addToOutput($entity, $status, $entityRevId); }