/**
  * Adds the given Statement's main Snak to the RDF graph.
  *
  * @todo share more of this code with FullStatementRdfBuilder
  *
  * @param EntityId $entityId
  * @param Statement $statement
  *
  * @throws InvalidArgumentException
  */
 private function addMainSnak(EntityId $entityId, Statement $statement)
 {
     $snak = $statement->getMainSnak();
     $entityLName = $this->vocabulary->getEntityLName($entityId);
     $this->writer->about(RdfVocabulary::NS_ENTITY, $entityLName);
     $this->snakBuilder->addSnak($this->writer, $snak, RdfVocabulary::NSP_DIRECT_CLAIM);
 }
 /**
  * If the provided statement has a GUID not yet in the map, it will be appended to the map.
  * If the GUID is already in the map, the statement with this guid will be replaced.
  *
  * @throws InvalidArgumentException
  * @param Statement $statement
  */
 public function addStatement(Statement $statement)
 {
     if ($statement->getGuid() === null) {
         throw new InvalidArgumentException('Can only add statements that have a non-null GUID');
     }
     $this->statements[$statement->getGuid()] = $statement;
 }
 public function testReturnsTrueForMinimalStatementWithReferences()
 {
     $statement = new Statement(new PropertyValueSnak(42, new StringValue('\\o/')));
     $statement->setReferences(new ReferenceList(array(new Reference(array(new PropertyValueSnak(1, new StringValue('wee')))), new Reference(array(new PropertyValueSnak(2, new StringValue('woo')), new PropertyValueSnak(3, new StringValue('waa')))))));
     $filter = new ReferencedStatementFilter();
     $this->assertTrue($filter->statementMatches($statement));
 }
Example #4
0
 /**
  * @param Snak $mainSnak
  * @param Snak[]|SnakList|null $qualifiers
  * @param Reference[]|ReferenceList|null $references
  * @param string|null $guid
  */
 public function addNewStatement(Snak $mainSnak, $qualifiers = null, $references = null, $guid = null)
 {
     $qualifiers = is_array($qualifiers) ? new SnakList($qualifiers) : $qualifiers;
     $references = is_array($references) ? new ReferenceList($references) : $references;
     $statement = new Statement($mainSnak, $qualifiers, $references);
     $statement->setGuid($guid);
     $this->addStatement($statement);
 }
 public function snaksProvider()
 {
     $statement = new Statement(new PropertyNoValueSnak(42));
     $statement->setGuid('test');
     $statement2 = new Statement(new PropertyNoValueSnak(42));
     $statement2->setGuid('test2');
     return array(array(new StatementList()), array(new StatementList(array($statement))), array(new StatementList(array($statement, $statement2))));
 }
 /**
  * @param Snak $mainSnak
  *
  * @return Statement
  */
 private function makeStatement(Snak $mainSnak)
 {
     static $guidCounter = 0;
     $guidCounter++;
     $statement = new Statement($mainSnak);
     $statement->setGuid('EntityViewTest$' . $guidCounter);
     return $statement;
 }
Example #7
0
 /**
  * @param Statement $statement
  *
  * @throws InvalidArgumentException
  * @return string
  */
 private function getClaimKey(Statement $statement)
 {
     $guid = $statement->getGuid();
     if ($guid === null) {
         throw new InvalidArgumentException('Can\'t handle statements with no GUID set');
     }
     $key = $this->getGuidKey($guid);
     return $key;
 }
 /**
  * @since 0.5
  *
  * @param Statement $statement
  * @param EditInfo|null $editInfo
  *
  * @throws InvalidArgumentException
  * @return bool
  *
  * @todo allow setting of indexes
  */
 public function set(Statement $statement, EditInfo $editInfo = null)
 {
     if ($statement->getGuid() === null) {
         throw new InvalidArgumentException('Can not set a statement that does not have a GUID');
     }
     $params = array('claim' => $this->statementSerializer->serialize($statement));
     $this->api->postRequest('wbsetclaim', $params, $editInfo);
     return true;
 }
 /**
  * @param Statement $statement
  *
  * @return Statement
  */
 public function copy(Statement $statement)
 {
     $mainSnak = $this->copySnak($statement->getMainSnak());
     $qualifiers = $this->copyQualifiers($statement->getQualifiers());
     $references = $this->copyReferences($statement->getReferences());
     $newStatement = new Statement($mainSnak, $qualifiers, $references);
     $newStatement->setRank($statement->getRank());
     return $newStatement;
 }
 private function addStatements(Item $item)
 {
     $statement = new Statement(new PropertyValueSnak(42, new StringValue('kittens')));
     $statement->setGuid('first guid');
     $item->getStatements()->addStatement($statement);
     $statement = new Statement(new PropertyNoValueSnak(23));
     $statement->setGuid('second guid');
     $item->getStatements()->addStatement($statement);
 }
 /**
  * @param Statement $statement
  *
  * @return string|null
  */
 private function getEntityType(Statement $statement)
 {
     try {
         $guid = $this->guidParser->parse($statement->getGuid());
     } catch (StatementGuidParsingException $ex) {
         // FIXME: Fail when there is a statement with no GUID?
         return null;
     }
     return $guid->getEntityId()->getEntityType();
 }
 private function checkDiff(Statement $lhs, Statement $rhs)
 {
     $lhsSnak = $lhs->getMainSnak();
     $rhsSnak = $rhs->getMainSnak();
     if (!$lhsSnak instanceof PropertyValueSnak || !$rhsSnak instanceof PropertyValueSnak) {
         return true;
     }
     $diff = abs($lhsSnak->getDataValue()->getSortKey() - $rhsSnak->getDataValue()->getSortKey());
     return $diff <= $this->diff->getSortKey();
 }
Example #13
0
 private function makeStatement(Snak $mainSnak, $guid = null)
 {
     if ($guid === null) {
         $this->guidCounter++;
         $guid = 'TEST$statement-' . $this->guidCounter;
     }
     $claim = new Statement($mainSnak);
     $claim->setGuid($guid);
     return $claim;
 }
 /**
  * @see StatementFilter::statementMatches
  *
  * @param Statement $statement
  *
  * @return bool
  */
 public function statementMatches(Statement $statement)
 {
     $id = $statement->getPropertyId();
     try {
         $dataType = $this->dataTypeLookup->getDataTypeIdForProperty($id);
     } catch (PropertyDataTypeLookupException $ex) {
         return false;
     }
     return in_array($dataType, $this->dataTypes);
 }
 /**
  * @dataProvider rankProvider
  */
 public function testRankSerialization($rank)
 {
     $statement = new Statement(new PropertyNoValueSnak(new PropertyId('P42')));
     $statement->setRank($rank);
     $factory = new SerializerFactory(new DataValueSerializer());
     $statementSerializer = $factory->newStatementSerializer();
     $serialization = $statementSerializer->serialize($statement);
     $rankSerializer = new StatementRankSerializer();
     $this->assertEquals($rank, $rankSerializer->deserialize($serialization['rank']), 'Roundtrip between rank serialization and unserialization 1');
     $this->assertEquals($serialization['rank'], $rankSerializer->serialize($rank), 'Roundtrip between rank serialization and unserialization 2');
 }
 public function testLabelLookupFallsBackToId()
 {
     $labelLookup = $this->getMock(LabelLookup::class);
     $labelLookup->expects($this->any())->method('getLabelByIdAndLanguage')->will($this->returnValue(null));
     $statement = new Statement(new PropertyValueSnak(42, new EntityIdValue(new ItemId('Q1337'))));
     $statement->setGuid('first guid');
     $builder = new SimpleStatementsBuilder('en', $labelLookup, new UrlBuilder('http://test'));
     $simpleStatements = $builder->buildFromStatements(new StatementList([$statement]));
     $expected = SimpleStatement::newInstance()->withPropertyName('P42')->withPropertyId(new PropertyId('P42'))->withPropertyUrl('http://test/properties/P42')->withType('queryr-entity-identity')->withValues([new EntityIdentityValue(new ItemId('Q1337'), 'Q1337', 'http://test/items/Q1337')]);
     $this->assertEquals([$expected], $simpleStatements);
 }
 public function testLabelLookupFallsBackToId()
 {
     $labelLookup = $this->getMock('Queryr\\Resources\\Builders\\ResourceLabelLookup');
     $labelLookup->expects($this->any())->method('getLabelByIdAndLanguage')->will($this->returnValue(null));
     $statement = new Statement(new PropertyValueSnak(42, new EntityIdValue(new ItemId('Q1337'))));
     $statement->setGuid('first guid');
     $builder = new SimpleStatementsBuilder('en', $labelLookup);
     $simpleStatements = $builder->buildFromStatements(new StatementList([$statement]));
     $expected = SimpleStatement::newInstance()->withPropertyName('P42')->withPropertyId(new PropertyId('P42'))->withType('string')->withValues([new StringValue('Q1337')]);
     $this->assertEquals([$expected], $simpleStatements);
 }
 public function testStatementListSerializerWithOptionObjectsForMaps()
 {
     $statement = new Statement(new PropertyNoValueSnak(42));
     $statement->setGuid('test');
     $statementSerializerMock = $this->getMock('\\Serializers\\Serializer');
     $statementSerializerMock->expects($this->any())->method('serialize')->with($this->equalTo($statement))->will($this->returnValue(array('mockedsuff' => array(), 'type' => 'statement')));
     $serializer = new StatementListSerializer($statementSerializerMock, true);
     $statementList = new StatementList(array($statement));
     $serial = new stdClass();
     $serial->P42 = array(array('mockedsuff' => array(), 'type' => 'statement'));
     $this->assertEquals($serial, $serializer->serialize($statementList));
 }
 public function testQualifiersOrderSerialization()
 {
     $snakSerializerMock = $this->getMock('\\Serializers\\Serializer');
     $snakSerializerMock->expects($this->any())->method('serialize')->will($this->returnValue(array('snaktype' => 'novalue', 'property' => 'P42')));
     $snaksSerializerMock = $this->getMock('\\Serializers\\Serializer');
     $snaksSerializerMock->expects($this->any())->method('serialize')->will($this->returnValue(array()));
     $referencesSerializerMock = $this->getMock('\\Serializers\\Serializer');
     $statementSerializer = new StatementSerializer($snakSerializerMock, $snaksSerializerMock, $referencesSerializerMock);
     $statement = new Statement(new PropertyNoValueSnak(42));
     $statement->setQualifiers(new SnakList(array(new PropertyNoValueSnak(42), new PropertySomeValueSnak(24), new PropertyNoValueSnak(24))));
     $this->assertEquals(array('mainsnak' => array('snaktype' => 'novalue', 'property' => 'P42'), 'qualifiers' => array(), 'qualifiers-order' => array('P42', 'P24'), 'type' => 'statement', 'rank' => 'normal'), $statementSerializer->serialize($statement));
 }
 public function testQualifiersOrderDeserialization()
 {
     $snakDeserializerMock = $this->getMock('\\Deserializers\\Deserializer');
     $snakDeserializerMock->expects($this->any())->method('deserialize')->with($this->equalTo(array('snaktype' => 'novalue', 'property' => 'P42')))->will($this->returnValue(new PropertyNoValueSnak(42)));
     $snaksDeserializerMock = $this->getMock('\\Deserializers\\Deserializer');
     $snaksDeserializerMock->expects($this->any())->method('deserialize')->with($this->equalTo(array('P24' => array(array('snaktype' => 'novalue', 'property' => 'P24')), 'P42' => array(array('snaktype' => 'somevalue', 'property' => 'P42'), array('snaktype' => 'novalue', 'property' => 'P42')))))->will($this->returnValue(new SnakList(array(new PropertyNoValueSnak(24), new PropertySomeValueSnak(42), new PropertyNoValueSnak(42)))));
     $referencesDeserializerMock = $this->getMock('\\Deserializers\\Deserializer');
     $statementDeserializer = new StatementDeserializer($snakDeserializerMock, $snaksDeserializerMock, $referencesDeserializerMock);
     $statement = new Statement(new PropertyNoValueSnak(42));
     $statement->setQualifiers(new SnakList(array(new PropertySomeValueSnak(42), new PropertyNoValueSnak(42), new PropertyNoValueSnak(24))));
     $serialization = array('mainsnak' => array('snaktype' => 'novalue', 'property' => 'P42'), 'qualifiers' => array('P24' => array(array('snaktype' => 'novalue', 'property' => 'P24')), 'P42' => array(array('snaktype' => 'somevalue', 'property' => 'P42'), array('snaktype' => 'novalue', 'property' => 'P42'))), 'qualifiers-order' => array('P42', 'P24'), 'type' => 'claim');
     $this->assertEquals($statement->getHash(), $statementDeserializer->deserialize($serialization)->getHash());
 }
 public function testGetStatementFromEntity()
 {
     $helper = $this->getNewInstance();
     $item = new Item(new ItemId('Q42'));
     $snak = new PropertyValueSnak(2754236, new StringValue('test'));
     $statement = new Statement($snak);
     $statement->setGuid('q42$D8404CDA-25E4-4334-AF13-A3290BCD9C0F');
     $item->getStatements()->addStatement($statement);
     $guid = $statement->getGuid();
     $this->assertEquals($statement, $helper->getStatementFromEntity($guid, $item));
     $this->setExpectedException('\\UsageException');
     $helper->getStatementFromEntity('q42$D8404CDA-25E4-4334-AF13-A3290BCD9C0N', $item);
 }
 public function buildDeserializer()
 {
     $entityIdDeserializerMock = $this->getMock('\\Deserializers\\Deserializer');
     $entityIdDeserializerMock->expects($this->any())->method('deserialize')->with($this->equalTo('P42'))->will($this->returnValue(new PropertyId('P42')));
     $termListDeserializerMock = $this->getMock('\\Deserializers\\Deserializer');
     $termListDeserializerMock->expects($this->any())->method('deserialize')->with($this->equalTo(array('en' => array('lang' => 'en', 'value' => 'foo'))))->will($this->returnValue(new TermList(array(new Term('en', 'foo')))));
     $aliasGroupListDeserializerMock = $this->getMock('\\Deserializers\\Deserializer');
     $aliasGroupListDeserializerMock->expects($this->any())->method('deserialize')->with($this->equalTo(array('en' => array('lang' => 'en', 'values' => array('foo', 'bar')))))->will($this->returnValue(new AliasGroupList(array(new AliasGroup('en', array('foo', 'bar'))))));
     $statement = new Statement(new PropertyNoValueSnak(42));
     $statement->setGuid('test');
     $statementListDeserializerMock = $this->getMock('\\Deserializers\\Deserializer');
     $statementListDeserializerMock->expects($this->any())->method('deserialize')->with($this->equalTo(array('P42' => array(array('mainsnak' => array('snaktype' => 'novalue', 'property' => 'P42'), 'type' => 'statement', 'rank' => 'normal')))))->will($this->returnValue(new StatementList(array($statement))));
     return new PropertyDeserializer($entityIdDeserializerMock, $termListDeserializerMock, $aliasGroupListDeserializerMock, $statementListDeserializerMock);
 }
 public function testArrayObjectNotConstructedFromObject()
 {
     $statement1 = new Statement(new PropertyNoValueSnak(1));
     $statement1->setGuid('1');
     $statement2 = new Statement(new PropertyNoValueSnak(2));
     $statement2->setGuid('2');
     $object = new ArrayObject();
     $object->append($statement1);
     $byPropertyIdArray = new ByPropertyIdArray($object);
     // According to the documentation append() "cannot be called when the ArrayObject was
     // constructed from an object." This test makes sure it was not constructed from an object.
     $byPropertyIdArray->append($statement2);
     $this->assertCount(2, $byPropertyIdArray);
 }
 public function testStatementsArePatched()
 {
     $s1337 = new Statement(new PropertyNoValueSnak(1337));
     $s1337->setGuid('s1337');
     $s23 = new Statement(new PropertyNoValueSnak(23));
     $s23->setGuid('s23');
     $s42 = new Statement(new PropertyNoValueSnak(42));
     $s42->setGuid('s42');
     $patch = new EntityDiff(array('claim' => new Diff(array('s42' => new DiffOpRemove($s42), 's23' => new DiffOpAdd($s23)))));
     $property = Property::newFromType('kittens');
     $property->getStatements()->addStatement($s1337);
     $property->getStatements()->addStatement($s42);
     $expectedProperty = Property::newFromType('kittens');
     $expectedProperty->getStatements()->addStatement($s1337);
     $expectedProperty->getStatements()->addStatement($s23);
     $this->assertEquals($expectedProperty, $this->getPatchedProperty($property, $patch));
 }
 public function findSnaksProvider()
 {
     $propertyId = new PropertyId('P1337');
     $statement1 = new Statement(new PropertyValueSnak($propertyId, new StringValue('a kitten!')));
     $statement1->setGuid('Q42$1');
     $statement2 = new Statement(new PropertyValueSnak($propertyId, new StringValue('two kittens!!')));
     $statement2->setGuid('Q42$2');
     // A Statement with a lower rank which should not affect the output
     $statement3 = new Statement(new PropertyValueSnak($propertyId, new StringValue('three kittens!!!')));
     $statement3->setGuid('Q42$3');
     $statement3->setRank(Statement::RANK_DEPRECATED);
     $item = new Item(new ItemId('Q42'));
     $item->getStatements()->addStatement($statement1);
     $item->getStatements()->addStatement($statement2);
     $item->getStatements()->addStatement($statement3);
     $snaksNormal = array(new PropertyValueSnak($propertyId, new StringValue('a kitten!')), new PropertyValueSnak($propertyId, new StringValue('two kittens!!')));
     $snakDeprecated = array(new PropertyValueSnak($propertyId, new StringValue('three kittens!!!')));
     return array(array($snaksNormal, $item, new PropertyId('P1337')), array(array(), $item, new PropertyId('P90001')), array($snakDeprecated, $item, new PropertyId('P1337'), array(Statement::RANK_DEPRECATED)));
 }
Example #26
0
 /**
  * Applies validation to the given Claim.
  * This is done by validating all snaks contained in the claim, notably:
  * the main snak, the qualifiers, and all snaks of all references,
  * in case the claim is a Statement.
  *
  * @param Statement $statement The value to validate
  *
  * @return Result
  */
 public function validateClaimSnaks(Statement $statement)
 {
     $snak = $statement->getMainSnak();
     $result = $this->validate($snak);
     if (!$result->isValid()) {
         return $result;
     }
     foreach ($statement->getQualifiers() as $snak) {
         $result = $this->validate($snak);
         if (!$result->isValid()) {
             return $result;
         }
     }
     $result = $this->validateReferences($statement->getReferences());
     if (!$result->isValid()) {
         return $result;
     }
     return Result::newSuccess();
 }
 public function testFoo()
 {
     $statement0 = new Statement(new PropertyNoValueSnak(42));
     $statement0->setGuid('s0');
     $statement1 = new Statement(new PropertySomeValueSnak(42));
     $statement1->setGuid('s1');
     $statement2 = new Statement(new PropertyValueSnak(42, new StringValue('ohi')));
     $statement2->setGuid('s2');
     $statement3 = new Statement(new PropertyNoValueSnak(1));
     $statement3->setGuid('s3');
     $patch = new Diff(array('s0' => new DiffOpRemove($statement0), 's2' => new DiffOpAdd($statement2), 's3' => new DiffOpAdd($statement3)));
     $source = new StatementList();
     $source->addStatement($statement0);
     $source->addStatement($statement1);
     $expected = new StatementList();
     $expected->addStatement($statement1);
     $expected->addStatement($statement2);
     $expected->addStatement($statement3);
     $this->assertListResultsFromPatch($expected, $source, $patch);
 }
Example #28
0
 public function diffProvider()
 {
     $diffs = array();
     $diffOps = array('label' => new Diff(array('en' => new DiffOpAdd('foobar'), 'de' => new DiffOpRemove('onoez'), 'nl' => new DiffOpChange('foo', 'bar')), true));
     $diffs[] = new EntityDiff($diffOps);
     $diffOps['description'] = new Diff(array('en' => new DiffOpAdd('foobar'), 'de' => new DiffOpRemove('onoez'), 'nl' => new DiffOpChange('foo', 'bar')), true);
     $diffs[] = new EntityDiff($diffOps);
     $diffOps['aliases'] = new Diff(array('en' => new Diff(array(new DiffOpAdd('foobar'), new DiffOpRemove('onoez')), false), 'de' => new Diff(array(new DiffOpRemove('foo')), false)), true);
     $diffs[] = new EntityDiff($diffOps);
     $statement = new Statement(new PropertyNoValueSnak(42));
     $statement->setGuid('EntityDiffTest$foo');
     $statementListDiffer = new StatementListDiffer();
     $diffOps['claim'] = $statementListDiffer->getDiff(new StatementList($statement), new StatementList());
     $diffs[] = new EntityDiff($diffOps);
     $argLists = array();
     foreach ($diffs as $diff) {
         $argLists[] = array($diff);
     }
     return $argLists;
 }
 /**
  * @param Statement $statement
  * @param string $url
  *
  * @return bool
  */
 public static function statementHasReferenceForUrlWithSameDomain(Statement $statement, $url)
 {
     $currentReferences = $statement->getReferences();
     foreach ($currentReferences as $currentReference) {
         foreach ($currentReference->getSnaks() as $currentReferenceSnak) {
             if (!$currentReferenceSnak instanceof PropertyValueSnak) {
                 continue;
                 // Ignore some and no value snaks
             }
             // Note: P854 is reference URL
             if ($currentReferenceSnak->getPropertyId()->getSerialization() == 'P854') {
                 /** @var StringValue $currentReferenceValue */
                 $currentReferenceValue = $currentReferenceSnak->getDataValue();
                 $currentReferenceUrl = $currentReferenceValue->getValue();
                 if (self::urlsDomainsAreSame($currentReferenceUrl, $url)) {
                     return true;
                 }
             }
         }
     }
     return false;
 }
 /**
  * Generates HTML of a statement change.
  *
  * @since 0.4
  *
  * @param ClaimDifference $claimDifference
  * @param Statement $baseStatement The new statement, if it exists. Otherwise the old statement.
  *
  * @return string HTML
  */
 public function visualizeClaimChange(ClaimDifference $claimDifference, Statement $baseStatement)
 {
     $newestMainSnak = $baseStatement->getMainSnak();
     $oldestMainSnak = $newestMainSnak;
     $html = '';
     $mainSnakChange = $claimDifference->getMainSnakChange();
     if ($mainSnakChange !== null) {
         $oldestMainSnak = $mainSnakChange->getOldValue() ?: $newestMainSnak;
         $html .= $this->visualizeMainSnakChange($mainSnakChange, $oldestMainSnak, $newestMainSnak);
     }
     $rankChange = $claimDifference->getRankChange();
     if ($rankChange !== null) {
         $html .= $this->visualizeRankChange($rankChange, $oldestMainSnak, $newestMainSnak);
     }
     $qualifierChanges = $claimDifference->getQualifierChanges();
     if ($qualifierChanges !== null) {
         $html .= $this->visualizeQualifierChanges($qualifierChanges, $oldestMainSnak, $newestMainSnak);
     }
     $referenceChanges = $claimDifference->getReferenceChanges();
     if ($referenceChanges !== null) {
         $html .= $this->visualizeReferenceChanges($referenceChanges, $oldestMainSnak, $newestMainSnak);
     }
     return $html;
 }