Ejemplo n.º 1
0
 /**
  * Get serialized entity for the EntityRevision and add it to the result
  *
  * @param string|null $sourceEntityIdSerialization EntityId used to retreive $entityRevision
  *        Used as the key for the entity in the 'entities' structure and for adding redirect
  *     info Will default to the entity's serialized ID if null. If given this must be the
  *     entity id before any redirects were resolved.
  * @param EntityRevision $entityRevision
  * @param string[]|string $props a list of fields to include, or "all"
  * @param string[]|null $filterSiteIds A list of site IDs to filter by
  * @param string[] $filterLangCodes A list of language codes to filter by
  * @param LanguageFallbackChain[] $fallbackChains with keys of the origional language
  *
  * @since 0.5
  */
 public function addEntityRevision($sourceEntityIdSerialization, EntityRevision $entityRevision, $props = 'all', array $filterSiteIds = null, array $filterLangCodes = array(), array $fallbackChains = array())
 {
     $entity = $entityRevision->getEntity();
     $entityId = $entity->getId();
     if ($sourceEntityIdSerialization === null) {
         $sourceEntityIdSerialization = $entityId->getSerialization();
     }
     $record = array();
     //if there are no props defined only return type and id..
     if ($props === array()) {
         $record['id'] = $entityId->getSerialization();
         $record['type'] = $entityId->getEntityType();
     } else {
         if ($props == 'all' || in_array('info', $props)) {
             $title = $this->entityTitleLookup->getTitleForId($entityId);
             $record['pageid'] = $title->getArticleID();
             $record['ns'] = $title->getNamespace();
             $record['title'] = $title->getPrefixedText();
             $record['lastrevid'] = $entityRevision->getRevisionId();
             $record['modified'] = wfTimestamp(TS_ISO_8601, $entityRevision->getTimestamp());
         }
         if ($sourceEntityIdSerialization !== $entityId->getSerialization()) {
             $record['redirects'] = array('from' => $sourceEntityIdSerialization, 'to' => $entityId->getSerialization());
         }
         $entitySerialization = $this->getEntityArray($entity, $props, $filterSiteIds, $filterLangCodes, $fallbackChains);
         $record = array_merge($record, $entitySerialization);
     }
     $this->appendValue(array('entities'), $sourceEntityIdSerialization, $record, 'entity');
     if ($this->addMetaData) {
         $this->result->addArrayType(array('entities'), 'kvp', 'id');
         $this->result->addValue(array('entities'), ApiResult::META_KVP_MERGE, true, ApiResult::OVERRIDE);
     }
 }
 /**
  * @since 0.4
  * @see EntityRevisionLookup::getEntityRevision
  *
  * @param EntityId $entityId
  * @param int|string $revisionId The desired revision id, or LATEST_FROM_SLAVE or LATEST_FROM_MASTER.
  *
  * @throws RevisionedUnresolvedRedirectException
  * @throws StorageException
  * @return EntityRevision|null
  */
 public function getEntityRevision(EntityId $entityId, $revisionId = self::LATEST_FROM_SLAVE)
 {
     $key = $entityId->getSerialization();
     if (isset($this->redirects[$key])) {
         $redirRev = $this->redirects[$key];
         throw new RevisionedUnresolvedRedirectException($entityId, $redirRev->getRedirect()->getTargetId(), $redirRev->getRevisionId(), $redirRev->getTimestamp());
     }
     if (empty($this->entities[$key])) {
         return null;
     }
     // default changed from false to 0 and then to LATEST_FROM_SLAVE
     if ($revisionId === false || $revisionId === 0) {
         wfWarn('getEntityRevision() called with $revisionId = false or 0, ' . 'use EntityRevisionLookup::LATEST_FROM_SLAVE or EntityRevisionLookup::LATEST_FROM_MASTER instead.');
         $revisionId = self::LATEST_FROM_SLAVE;
     }
     /** @var EntityRevision[] $revisions */
     $revisions = $this->entities[$key];
     if (!is_int($revisionId)) {
         $revisionIds = array_keys($revisions);
         $revisionId = end($revisionIds);
     } elseif (!isset($revisions[$revisionId])) {
         throw new StorageException("no such revision for entity {$key}: {$revisionId}");
     }
     $revision = $revisions[$revisionId];
     $revision = new EntityRevision(unserialize(serialize($revision->getEntity())), $revision->getRevisionId(), $revision->getTimestamp());
     return $revision;
 }
 /**
  * @param EntityRevision $entityRevision
  * @param RedirectRevision|null $followedRedirect a redirect leading to the entity for use in the output
  * @param EntityId[] $incomingRedirects Incoming redirects to include in the output
  * @param RdfBuilder $rdfBuilder
  * @param string|null $flavor The type of the output provided by serializer
  *
  * @return string RDF
  */
 private function rdfSerialize(EntityRevision $entityRevision, RedirectRevision $followedRedirect = null, array $incomingRedirects, RdfBuilder $rdfBuilder, $flavor = null)
 {
     $rdfBuilder->startDocument();
     $redir = null;
     if ($followedRedirect) {
         $redir = $followedRedirect->getRedirect();
         $rdfBuilder->addEntityRedirect($redir->getEntityId(), $redir->getTargetId());
         if ($followedRedirect->getRevisionId() > 0) {
             $rdfBuilder->addEntityRevisionInfo($redir->getEntityId(), $followedRedirect->getRevisionId(), $followedRedirect->getTimestamp());
         }
     }
     if ($followedRedirect && $flavor === 'dump') {
         // For redirects, don't output the target entity data if the "dump" flavor is requested.
         // @todo: In this case, avoid loading the Entity all together.
         // However we want to output the revisions for redirects
     } else {
         $rdfBuilder->addEntityRevisionInfo($entityRevision->getEntity()->getId(), $entityRevision->getRevisionId(), $entityRevision->getTimestamp());
         $rdfBuilder->addEntity($entityRevision->getEntity());
         $rdfBuilder->resolveMentionedEntities($this->entityLookup);
     }
     if ($flavor !== 'dump') {
         // For $flavor === 'dump' we don't need to output incoming redirects.
         $targetId = $entityRevision->getEntity()->getId();
         $this->addIncomingRedirects($targetId, $redir, $incomingRedirects, $rdfBuilder);
     }
     $rdfBuilder->finishDocument();
     return $rdfBuilder->getRDF();
 }