/** * 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; }
/** * @dataProvider instanceProvider */ public function testGetGuid(Statement $statement) { $guid = $statement->getGuid(); $this->assertTrue($guid === null || is_string($guid)); $this->assertEquals($guid, $statement->getGuid()); $statement->setGuid('foobar'); $this->assertEquals('foobar', $statement->getGuid()); }
private function addGuidToSerialization(Statement $statement, array &$serialization) { $guid = $statement->getGuid(); if ($guid !== null) { $serialization['id'] = $guid; } }
/** * @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 * * @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; }
/** * Checks that the update of the main snak is permissible. * * This checks that the main snaks of the old and the new claim * refer to the same property. * * @param Statement $oldStatement * * @throws ChangeOpException If the main snak update is illegal. */ private function checkMainSnakUpdate(Statement $oldStatement) { $newMainSnak = $this->statement->getMainSnak(); $oldPropertyId = $oldStatement->getMainSnak()->getPropertyId(); if (!$oldPropertyId->equals($newMainSnak->getPropertyId())) { $guid = $this->statement->getGuid(); throw new ChangeOpException("Claim with GUID {$guid} uses property " . $oldPropertyId . ", can't change to " . $newMainSnak->getPropertyId()); } }
/** * @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(); }
/** * @param Statement $fromStatement statement to take references from * @param Statement $toStatement statement to add references to */ private function generateReferencesChangeOps(Statement $fromStatement, Statement $toStatement) { /** @var Reference $reference */ foreach ($fromStatement->getReferences() as $reference) { if (!$toStatement->getReferences()->hasReferenceHash($reference->getHash())) { $this->toChangeOps->add($this->getStatementChangeOpFactory()->newSetReferenceOp($toStatement->getGuid(), $reference, '')); } } }
/** * @param Statement $statement * @param ItemId $itemId * @param int $expectedCount * @param string $requestLabel A label to identify requests that are made in errors. */ private function assertStatementWasSet(Statement $statement, ItemId $itemId, $expectedCount, $requestLabel) { $this->assertNotNull($statement->getGuid(), 'Cannot search for statements with no GUID'); /** @var Item $item */ $item = WikibaseRepo::getDefaultInstance()->getEntityLookup()->getEntity($itemId); $statements = $item->getStatements(); $savedStatement = $statements->getFirstStatementWithGuid($statement->getGuid()); $this->assertNotNull($savedStatement, "Statement list does not have statement after {$requestLabel}"); if (count($statement->getQualifiers())) { $this->assertTrue($statement->getQualifiers()->equals($savedStatement->getQualifiers())); } $this->assertSame($expectedCount, $statements->count(), "Statements count is wrong after {$requestLabel}"); }
/** * Builds an associative array that can be used as summary arguments. It uses property IDs as * array keys and builds arrays of the main Snaks of all statements given by the GUIDs. * * @param Statement $newStatement * @param string $guid * * @return array[] Associative array that contains property ID => array of main Snaks */ private function buildSummaryArgs(Statement $newStatement, $guid) { $pairs = array(); if ($newStatement->getGuid() === $guid) { $snak = $newStatement->getMainSnak(); $key = $snak->getPropertyId()->getSerialization(); if (!array_key_exists($key, $pairs)) { $pairs[$key] = array(); } $pairs[$key][] = $snak; } return array($pairs); }
/** * Returns a qname for the given statement using the given prefix. * * @param Statement $statement * * @return string */ public function getStatementLName(Statement $statement) { return preg_replace('/[^\\w-]/', '-', $statement->getGuid()); }
/** * Adds the given Statement's main Snak to the RDF graph. * * @param EntityId $entityId * @param string $statementLName * @param Statement $statement * @param bool $isBest Is this best ranked statement? */ private function addMainSnak(EntityId $entityId, $statementLName, Statement $statement, $isBest) { $snak = $statement->getMainSnak(); $entityLName = $this->vocabulary->getEntityLName($entityId); $propertyLName = $this->vocabulary->getEntityLName($snak->getPropertyId()); $this->statementWriter->about(RdfVocabulary::NS_ENTITY, $entityLName)->say(RdfVocabulary::NSP_CLAIM, $propertyLName)->is(RdfVocabulary::NS_STATEMENT, $statementLName); $this->statementWriter->about(RdfVocabulary::NS_STATEMENT, $statementLName)->a(RdfVocabulary::NS_ONTOLOGY, 'Statement'); $rank = $statement->getRank(); if (isset(RdfVocabulary::$rankMap[$rank])) { if ($isBest) { $this->statementWriter->a(RdfVocabulary::NS_ONTOLOGY, RdfVocabulary::WIKIBASE_RANK_BEST); } $this->statementWriter->about(RdfVocabulary::NS_STATEMENT, $statementLName)->say(RdfVocabulary::NS_ONTOLOGY, 'rank')->is(RdfVocabulary::NS_ONTOLOGY, RdfVocabulary::$rankMap[$rank]); } else { wfLogWarning("Unknown rank {$rank} encountered for {$entityId}:{$statement->getGuid()}"); } $this->snakBuilder->addSnak($this->statementWriter, $snak, RdfVocabulary::NSP_CLAIM_STATEMENT); }