/** * Do not use to construct new stuff from outside of this class, * use the static newFoobar methods. * * In other words: treat as protected (which it was, but now * cannot be since we derive from Content). * * @protected * * @param EntityHolder $mediumHolder * @throws InvalidArgumentException */ public function __construct(EntityHolder $mediumHolder) { parent::__construct(CONTENT_MODEL_WIKIBASE_MEDIAINFO); if ($mediumHolder->getEntityType() !== MediaInfo::ENTITY_TYPE) { throw new InvalidArgumentException('$mediumHolder must contain a Medium entity!'); } $this->mediumHolder = $mediumHolder; }
/** * @dataProvider entityRedirectProvider */ public function testGetEntityRedirect(EntityContent $content, EntityRedirect $redirect = null) { $this->assertEquals($content->getEntityRedirect(), $redirect); if ($redirect === null) { $this->assertNull($content->getRedirectTarget()); } else { $this->assertNotNull($content->getRedirectTarget()); } }
/** * Returns modification updates for the given EntityContent. * * @see Content::getSecondaryDataUpdates * * @since 0.5 * * @param EntityContent $content * @param Title $title * * @return DataUpdate[] */ public function getEntityModificationUpdates(EntityContent $content, Title $title) { $updates = array(); $entityId = $content->getEntityId(); //FIXME: we should not need this! if ($entityId === null) { $entityId = $this->getIdForTitle($title); } if ($content->isRedirect()) { // Remove the entity from the terms table since it's now a redirect. $updates[] = new DataUpdateAdapter(array($this->termIndex, 'deleteTermsOfEntity'), $entityId); // Register the redirect from the EntityPerPage table. $updates[] = new DataUpdateAdapter(array($this->entityPerPage, 'addRedirectPage'), $entityId, $title->getArticleID(), $content->getEntityRedirect()->getTargetId()); } else { // Register the entity in the EntityPerPage table. $updates[] = new DataUpdateAdapter(array($this->entityPerPage, 'addEntityPage'), $entityId, $title->getArticleID()); // Register the entity in the terms table. $updates[] = new DataUpdateAdapter(array($this->termIndex, 'saveTermsOfEntity'), $content->getEntity()); } // Call the WikibaseEntityModificationUpdate hook. // Do this after doing all well-known updates. $updates[] = new DataUpdateAdapter('wfRunHooks', 'WikibaseEntityModificationUpdate', array($content, $title)); return $updates; }
/** * Returns a EntityChange based on the old and new content object, taking * redirects into consideration. * * @todo: Notify the client about changes to redirects explicitly. * * @param EntityContent $oldContent * @param EntityContent $newContent * * @return EntityChange|null */ private function getChangeForModification(EntityContent $oldContent, EntityContent $newContent) { $oldEntity = $oldContent->isRedirect() ? null : $oldContent->getEntity(); $newEntity = $newContent->isRedirect() ? null : $newContent->getEntity(); if ($oldEntity === null && $newEntity === null) { // Old and new versions are redirects. Nothing to do. return null; } elseif ($newEntity === null) { // The new version is a redirect. For now, treat that as a deletion. $action = EntityChange::REMOVE; } elseif ($oldEntity === null) { // The old version is a redirect. For now, treat that like restoring the entity. $action = EntityChange::RESTORE; } else { // No redirects involved $action = EntityChange::UPDATE; } $change = $this->changeFactory->newFromUpdate($action, $oldEntity, $newEntity); return $change; }
/** * @dataProvider provideGetUndoContent * * @param Revision $latestRevision * @param Revision $newerRevision * @param Revision $olderRevision * @param EntityContent|null $expected * @param string $message */ public function testGetUndoContent(Revision $latestRevision, Revision $newerRevision, Revision $olderRevision, EntityContent $expected = null, $message) { $handler = $this->getHandler(); $undo = $handler->getUndoContent($latestRevision, $newerRevision, $olderRevision); if ($expected) { $this->assertInstanceOf('Wikibase\\EntityContent', $undo, $message); $this->assertTrue($expected->equals($undo), $message); } else { $this->assertFalse($undo, $message); } }
/** * Saves the entity. If the corresponding page does not exist yet, it will be created * (ie a new ID will be determined and a new page in the data NS created). * * @note: this method should not be overloaded, and should not be extended to save additional * information to the database. Such things should be done in a way that will also be * triggered when the save is performed by calling WikiPage::doEditContent. * * @see WikiPage::doEditContent * * @param EntityContent $entityContent the entity to save. * @param string $summary * @param null|User $user * @param int $flags Flags as used by WikiPage::doEditContent, use EDIT_XXX constants. * @param int|bool $baseRevId * * @throws StorageException * @return Revision The new revision (or the latest one, in case of a null edit). */ private function saveEntityContent(EntityContent $entityContent, $summary = '', User $user = null, $flags = 0, $baseRevId = false) { $page = $this->getWikiPageForEntity($entityContent->getEntityId()); if (($flags & EDIT_NEW) === EDIT_NEW) { $title = $page->getTitle(); if ($title->exists()) { throw new StorageException(Status::newFatal('edit-already-exists')); } } /** * @note Make sure we start saving from a clean slate. Calling WikiPage::clearPreparedEdit * may cause the old content to be loaded from the database again. This may be necessary, * because EntityContent is mutable, so the cached object might have changed. * * @todo Might be able to further optimize handling of prepared edit in WikiPage. */ $page->clear(); $page->clearPreparedEdit(); $status = $page->doEditContent($entityContent, $summary, $flags | EDIT_AUTOSUMMARY, $baseRevId, $user); if (!$status->isOK()) { throw new StorageException($status); } // As per convention defined by WikiPage, the new revision is in the status value: if (isset($status->value['revision'])) { $revision = $status->value['revision']; } else { // NOTE: No new revision was created (content didn't change). Report the old one. // There *might* be a race condition here, but since $page already loaded the // latest revision, it should still be cached, and should always be the correct one. $revision = $page->getRevision(); } return $revision; }
/** * @dataProvider getTextForSearchIndexProvider * * @param EntityContent $itemContent * @param string $pattern */ public function testGetTextForSearchIndex(EntityContent $itemContent, $pattern) { $text = $itemContent->getTextForSearchIndex(); $this->assertRegExp($pattern . 'm', $text); }
/** * @see EntityContent::getEntityStatus() * * An item is considered a stub if it has terms but no statements or sitelinks. * If an item has sitelinks but no statements, it is considered a "linkstub". * If an item has statements, it's not empty nor a stub. * * @see STATUS_LINKSTUB * * @note Will fail of this ItemContent is a redirect. * * @return int */ public function getEntityStatus() { $status = parent::getEntityStatus(); if (!$this->isRedirect()) { $hasSiteLinks = !$this->getItem()->getSiteLinkList()->isEmpty(); if ($status === self::STATUS_EMPTY && $hasSiteLinks) { $status = self::STATUS_LINKSTUB; } elseif ($status === self::STATUS_STUB && $hasSiteLinks) { $status = self::STATUS_LINKSTUB; } } return $status; }
/** * Checks if this PropertyContent is valid for saving. * * Returns false if the entity does not have a DataType set. * * @see Content::isValid() */ public function isValid() { //TODO: provide a way to get the data type from the holder directly! return parent::isValid() && $this->getProperty()->getDataTypeId() !== null; }
/** * Returns a diff between this EntityContent and the given EntityContent. * * @param EntityContent $toContent * * @return EntityContentDiff */ public function getDiff(EntityContent $toContent) { $fromContent = $this; $differ = new MapDiffer(); $redirectDiffOps = $differ->doDiff($fromContent->getRedirectData(), $toContent->getRedirectData()); $redirectDiff = new Diff($redirectDiffOps, true); $fromEntity = $fromContent->isRedirect() ? $this->makeEmptyEntity() : $fromContent->getEntity(); $toEntity = $toContent->isRedirect() ? $this->makeEmptyEntity() : $toContent->getEntity(); $entityDiffer = new EntityDiffer(); $entityDiff = $entityDiffer->diffEntities($fromEntity, $toEntity); return new EntityContentDiff($entityDiff, $redirectDiff); }
/** * Returns modification updates for the given EntityContent. * * @see EntityHandler::getEntityModificationUpdates * * @since 0.5 * * @param EntityContent $content * @param Title $title * * @return DataUpdate[] */ public function getEntityModificationUpdates(EntityContent $content, Title $title) { $updates = array(); if ($content->isRedirect()) { $updates[] = new DataUpdateAdapter(array($this->siteLinkStore, 'deleteLinksOfItem'), $content->getEntityId()); } else { $updates[] = new DataUpdateAdapter(array($this->siteLinkStore, 'saveLinksOfItem'), $content->getEntity()); } return array_merge($updates, parent::getEntityModificationUpdates($content, $title)); }