/**
  * @return EntityId
  */
 public function getEntityId()
 {
     if (!$this->entityId && $this->hasField('object_id')) {
         // FIXME: this should be an injected EntityIdParser
         $idParser = new BasicEntityIdParser();
         $this->entityId = $idParser->parse($this->getObjectId());
     }
     return $this->entityId;
 }
 /**
  * @dataProvider provideTransformPageEntityUsages
  */
 public function testTransformPageEntityUsages($relevant, PageEntityUsages $usages, $expected)
 {
     $transformer = new UsageAspectTransformer();
     $idParser = new BasicEntityIdParser();
     foreach ($relevant as $id => $aspects) {
         $transformer->setRelevantAspects($idParser->parse($id), $aspects);
     }
     $transformed = $transformer->transformPageEntityUsages($usages);
     $this->assertEquals($usages->getPageId(), $transformed->getPageId());
     $this->assertEquals($expected, array_keys($transformed->getUsages()));
 }
 /**
  * @param array $ids
  *
  * @return GenericEntityInfoBuilder
  */
 protected function newEntityInfoBuilder(array $ids)
 {
     $idParser = new BasicEntityIdParser();
     $repo = new MockRepository();
     foreach ($this->getKnownEntities() as $entity) {
         $repo->putEntity($entity);
     }
     foreach ($this->getKnownRedirects() as $from => $toId) {
         $fromId = $idParser->parse($from);
         $repo->putRedirect(new EntityRedirect($fromId, $toId));
     }
     return new GenericEntityInfoBuilder($ids, $idParser, $repo);
 }
 /**
  * @param Title[] $titles
  *
  * @return EntityId[]
  */
 public function titlesToIds(array $titles)
 {
     $entityIds = array();
     $idParser = new BasicEntityIdParser();
     foreach ($titles as $title) {
         try {
             // Pretend the article ID is the numeric entity ID.
             $entityId = $idParser->parse($title->getText());
             $key = $entityId->getNumericId();
             $entityIds[$key] = $entityId;
         } catch (EntityIdParsingException $ex) {
             // skip
         }
     }
     return $entityIds;
 }
 /**
  * @see EntityRedirectLookup::getRedirectForEntityId
  */
 public function getRedirectForEntityId(EntityId $entityId, $forUpdate = '')
 {
     $entityIdSerialization = $entityId->getSerialization();
     $params = array('ids' => $entityIdSerialization);
     $result = $this->api->getRequest(new SimpleRequest('wbgetentities', $params));
     $entitiesData = $result['entities'];
     if (!array_key_exists($entityIdSerialization, $entitiesData)) {
         throw new EntityRedirectLookupException($entityId, "Failed to get {$entityIdSerialization}");
     }
     $entityData = $entitiesData[$entityIdSerialization];
     if (!array_key_exists('redirects', $entityData)) {
         throw new EntityRedirectLookupException($entityId, "{$entityIdSerialization} is not a redirect");
     }
     $entityIdParser = new BasicEntityIdParser();
     return $entityIdParser->parse($entityData['redirects']['to']);
 }
 /**
  * @since 0.4
  *
  * @return EntityIdParser
  */
 public function getEntityIdParser()
 {
     if ($this->entityIdParser === null) {
         //TODO: make the ID builders configurable
         $this->entityIdParser = new DispatchingEntityIdParser(BasicEntityIdParser::getBuilders());
     }
     return $this->entityIdParser;
 }
 private function newEntityIdParser()
 {
     return new DispatchingEntityIdParser(BasicEntityIdParser::getBuilders());
 }
 /**
  * @param string $idString
  *
  * @return ItemId|PropertyId
  */
 private function parseId($idString)
 {
     $parser = new BasicEntityIdParser();
     return $parser->parse($idString);
 }
 /**
  * @dataProvider invalidIdSerializationProvider
  */
 public function testCannotParseInvalidId($invalidIdSerialization)
 {
     $parser = new BasicEntityIdParser();
     $this->setExpectedException('Wikibase\\DataModel\\Entity\\EntityIdParsingException');
     $parser->parse($invalidIdSerialization);
 }
 /**
  * @dataProvider provideGetEntities
  */
 public function testGetEntities($ids, $expected, $expectedError = false)
 {
     $this->setupGetEntities();
     $idParser = new BasicEntityIdParser();
     // convert string IDs to EntityId objects
     foreach ($ids as $i => $id) {
         if (is_string($id)) {
             $ids[$i] = $idParser->parse($id);
         }
     }
     $entities = false;
     // do it!
     try {
         $entities = $this->repo->getEntities($ids);
         if ($expectedError !== false) {
             $this->fail("expected error: " . $expectedError);
         }
     } catch (MWException $ex) {
         if ($expectedError !== false) {
             $this->assertInstanceOf($expectedError, $ex);
         } else {
             $this->fail("error: " . $ex->getMessage());
         }
     }
     if (!is_array($expected)) {
         // expected some kind of special return value, e.g. false.
         $this->assertEquals($expected, $entities, "return value");
         return;
     } else {
         $this->assertType('array', $entities, "return value");
     }
     // extract map of entity IDs to label arrays.
     /* @var Entity $e  */
     $actual = array();
     foreach ($entities as $key => $e) {
         if (is_object($e)) {
             $actual[$e->getId()->getSerialization()] = $e->getFingerprint()->getLabels()->toTextArray();
         } else {
             $actual[$key] = $e;
         }
     }
     // check that we found the right number of entities
     $this->assertEquals(count($expected), count($actual), "number of entities found");
     foreach ($expected as $id => $labels) {
         // check that thew correct entity was found
         $this->assertArrayHasKey($id, $actual);
         if (is_array($labels)) {
             // check that the entity contains the expected labels
             $this->assertArrayEquals($labels, $actual[$id]);
         } else {
             // typically, $labels would be null here.
             // check that the entity/revision wasn't found
             $this->assertEquals($labels, $actual[$id]);
         }
     }
 }
 /**
  * @dataProvider typeFilterProvider
  */
 public function testTypeFilterDump(array $ids, $type, array $expectedIds)
 {
     $dumper = $this->newDumpGenerator($ids);
     $pager = $this->makeIdPager($ids);
     $dumper->setEntityTypeFilter($type);
     ob_start();
     $dumper->generateDump($pager);
     $json = ob_get_clean();
     // check that the resulting json contains all the ids we asked for.
     $data = json_decode($json, true);
     $this->assertTrue(is_array($data), 'decode failed: ' . $json);
     $actualIds = array_map(function ($entityData) {
         return $entityData['id'];
     }, $data);
     $this->assertEquals($expectedIds, $actualIds);
     $idParser = new BasicEntityIdParser();
     foreach ($data as $serialization) {
         $id = $idParser->parse($serialization['id']);
         $this->assertEntitySerialization($id, $serialization);
     }
 }