/** * @param Snak $snak */ private function processSnak(Snak $snak) { $propertyId = $snak->getPropertyId(); $this->entityIds[$propertyId->getSerialization()] = $propertyId; if ($snak instanceof PropertyValueSnak) { $this->processDataValue($snak->getDataValue()); } }
/** * @param Snak $snak * * @return string */ public function formatSnak(Snak $snak) { if ($snak instanceof PropertyValueSnak) { $value = $snak->getDataValue(); return $this->formatValue($value); } else { return $snak->getType(); } }
/** * @see Comparable::equals * * @param SnakConstraint $constraint * @return boolean */ public function equals($constraint) { if ($constraint === $this) { return true; } if (!$constraint instanceof self) { return false; } return $this->snak->equals($constraint->snak); }
private function validateSnak(Snak $snak) { if (!$snak instanceof PropertyValueSnak) { return true; } $dataValue = $snak->getDataValue(); if (!$this->dataValueChecker->supportsDataValue($dataValue)) { return true; } return $this->dataValueChecker->checkDataValue($dataValue); }
private function getSerialized(Snak $snak) { $serialization = array('snaktype' => $snak->getType(), 'property' => $snak->getPropertyId()->getSerialization()); if ($this->serializeWithHash) { $serialization['hash'] = $snak->getHash(); } if ($snak instanceof PropertyValueSnak) { $serialization['datavalue'] = $this->dataValueSerializer->serialize($snak->getDataValue()); } return $serialization; }
/** * @see SnakFormatter::formatSnak * * @param Snak $snak * * @return string Either plain text, wikitext or HTML, depending on the SnakFormatter provided. */ public function formatSnak(Snak $snak) { if ($snak instanceof PropertyValueSnak) { $value = $snak->getDataValue(); if ($value instanceof EntityIdValue) { $entityId = $value->getEntityId(); $this->addLabelUsage($value->getEntityId()); $this->usageAccumulator->addTitleUsage($entityId); } } return $this->snakFormatter->formatSnak($snak); }
/** * @param Snak $snak */ private function processSnak(Snak $snak) { if ($snak instanceof PropertyValueSnak) { $id = $snak->getPropertyId(); $value = $snak->getDataValue(); if ($value instanceof StringValue && $this->propertyDataTypeMatcher->isMatchingDataType($id, 'url')) { $url = $value->getValue(); if ($url !== '') { $this->urls[$url] = null; } } } }
/** * @param Snak $snak */ private function processSnak(Snak $snak) { if ($snak instanceof PropertyValueSnak) { $id = $snak->getPropertyId(); $value = $snak->getDataValue(); if ($value instanceof StringValue && $this->propertyDataTypeMatcher->isMatchingDataType($id, 'commonsMedia')) { $fileName = str_replace(' ', '_', $value->getValue()); if ($fileName !== '') { $this->fileNames[$fileName] = null; } } } }
/** * @see Comparable::equals * * @since 0.7.4 * * @param mixed $target * * @return bool */ public function equals($target) { if ($this === $target) { return true; } return $target instanceof self && $this->guid === $target->guid && $this->rank === $target->rank && $this->mainSnak->equals($target->mainSnak) && $this->qualifiers->equals($target->qualifiers) && $this->references->equals($target->references); }
/** * @param Snak $snak * * @return string|null */ private function getString(Snak $snak) { if ($snak instanceof PropertyValueSnak) { $value = $snak->getDataValue(); if ($value instanceof StringValue) { return $value->getValue(); } } return null; }
/** * @since 0.2 * * @param Snak $mainSnak * @param EntityId|Item|Property|string $target * @param EditInfo|null $editInfo * * @return string the GUID of the claim * @throws UnexpectedValueException */ public function create(Snak $mainSnak, $target, EditInfo $editInfo = null) { if (is_string($target)) { $entityId = $target; } elseif ($target instanceof EntityId) { $entityId = $target->getSerialization(); } elseif ($target instanceof Item || $target instanceof Property) { $entityId = $target->getId()->getSerialization(); } else { throw new UnexpectedValueException('$target needs to be an EntityId, Entity or string'); } $params = array('entity' => $entityId, 'snaktype' => $mainSnak->getType(), 'property' => $mainSnak->getPropertyId()->getSerialization()); if ($mainSnak instanceof PropertyValueSnak) { $serializedDataValue = $this->dataValueSerializer->serialize($mainSnak->getDataValue()); $params['value'] = json_encode($serializedDataValue['value']); } $result = $this->api->postRequest('wbcreateclaim', $params, $editInfo); return $result['claims']['id']; }
private function copySnak(Snak $mainSnak) { $newPropertyId = $this->entityMappingStore->getLocalId($mainSnak->getPropertyId()->getSerialization()); switch ($mainSnak->getType()) { case 'somevalue': return new PropertySomeValueSnak(new PropertyId($newPropertyId)); case 'novalue': return new PropertyNoValueSnak(new PropertyId($newPropertyId)); default: $value = $mainSnak->getDataValue(); if ($value instanceof EntityIdValue) { $localId = $this->entityMappingStore->getLocalId($value->getEntityId()->getSerialization()); if (!$localId) { $this->logger->error("Entity not found for {$localId}."); } $value = new EntityIdValue($this->idParser->parse($localId)); } return new PropertyValueSnak(new PropertyId($newPropertyId), $value); } }
/** * Adds the given Statement's main Snak to the RDF graph. * * @param RdfWriter $writer * @param Snak $snak * @param string $propertyNamespace * * @throws InvalidArgumentException */ public function addSnak(RdfWriter $writer, Snak $snak, $propertyNamespace) { $propertyId = $snak->getPropertyId(); switch ($snak->getType()) { case 'value': /** @var PropertyValueSnak $snak */ $this->addSnakValue($writer, $snak, $propertyNamespace); break; case 'somevalue': $propertyValueLName = $this->vocabulary->getEntityLName($propertyId); $writer->say($propertyNamespace, $propertyValueLName)->is('_', $writer->blank()); break; case 'novalue': $propertyValueLName = $this->vocabulary->getEntityLName($propertyId); $writer->say('a')->is(RdfVocabulary::NSP_NOVALUE, $propertyValueLName); break; default: throw new InvalidArgumentException('Unknown snak type: ' . $snak->getType()); } $this->mentionedEntityTracker->propertyMentioned($snak->getPropertyId()); }
/** * @since 0.4 * * @param Snak $snak * @return array */ protected function getSnakSummaryArgs(Snak $snak) { $propertyId = $snak->getPropertyId(); return array(array($propertyId->getSerialization() => $snak)); }
/** * @dataProvider provideAddSnakValue */ public function testAddSnakValue(Snak $snak, $dataType) { $writer = $this->getTestData()->getNTriplesWriter(); $writer->about(RdfVocabulary::NS_ENTITY, 'Q11'); $propertyId = $snak->getPropertyId(); $builder = $this->newBuilder(RdfVocabulary::NSP_DIRECT_CLAIM, $propertyId->getSerialization(), $dataType, $snak); // assertions are done by the mocks $builder->addSnak($writer, $snak, RdfVocabulary::NSP_DIRECT_CLAIM); }
/** * @dataProvider snakProvider */ public function testFormatSnak_givenDifferentSnakTypes(Snak $snak, $expected) { $formatter = $this->getFormatter($snak->getType(), SnakFormatter::FORMAT_HTML); $this->assertEquals($expected, $formatter->formatSnak($snak)); }
/** * @param Snak $snak * * @return string */ private function getSnakViewVariation(Snak $snak) { return $snak->getType() . 'snak'; }
/** * @see SnakFormatter::formatSnak * * Formats the given Snak by finding an appropriate formatter among the ones supplied * to the constructor, and applying it. * * @param Snak $snak * * @throws FormattingException * @throws PropertyDataTypeLookupException * @return string The formatted snak value, in the format specified by getFormat(). */ public function formatSnak(Snak $snak) { $snakType = $snak->getType(); if (isset($this->formattersBySnakType[$snakType])) { $formatter = $this->formattersBySnakType[$snakType]; return $formatter->formatSnak($snak); } $dataType = $this->getSnakDataType($snak); if (isset($this->formattersByDataType["PT:{$dataType}"])) { $formatter = $this->formattersByDataType["PT:{$dataType}"]; return $formatter->formatSnak($snak); } if (isset($this->formattersByDataType['*'])) { $formatter = $this->formattersByDataType['*']; return $formatter->formatSnak($snak); } throw new FormattingException("No formatter found for snak type {$snakType} and data type {$dataType}"); }
/** * Get a detailed formatted snak, including the snak's property label and value. * * @param Snak $snak * * @return string HTML */ public function getPropertyAndDetailedValue(Snak $snak) { return $this->getColonSeparatedHtml($this->formatPropertyId($snak->getPropertyId()), $this->getDetailedValue($snak)); }
protected function makeSetQualifierRequest($statementGuid, $snakhash, Snak $qualifier, EntityId $entityId) { $params = array('action' => 'wbsetqualifier', 'claim' => $statementGuid, 'snakhash' => $snakhash, 'snaktype' => $qualifier->getType(), 'property' => $qualifier->getPropertyId()->getSerialization()); if ($qualifier instanceof PropertyValueSnak) { $dataValue = $qualifier->getDataValue(); $params['value'] = FormatJson::encode($dataValue->getArrayValue()); } $this->makeValidRequest($params); /** @var StatementListProvider $entity */ $entity = WikibaseRepo::getDefaultInstance()->getEntityLookup()->getEntity($entityId); $statements = $entity->getStatements(); $statement = $statements->getFirstStatementWithGuid($params['claim']); $this->assertNotNull($statement); $this->assertTrue($statement->getQualifiers()->hasSnak($qualifier), 'The qualifier should exist in the qualifier list after making the request'); }
/** * Validates a Snak. * For a PropertyValueSnak, this is done using the validators from the DataType * that is associated with the Snak's property. * Other Snak types are currently not validated. * * @see ValueValidator::validate() * * @param Snak $snak The value to validate * * @throws InvalidArgumentException * @return Result */ public function validate($snak) { if (!$snak instanceof Snak) { throw new InvalidArgumentException('Snak expected'); } // XXX: instead of an instanceof check, we could have multiple validators // with a canValidate() method, to determine which validator to use // for a given snak. $propertyId = $snak->getPropertyId(); try { $typeId = $this->propertyDataTypeLookup->getDataTypeIdForProperty($propertyId); if ($snak instanceof PropertyValueSnak) { $dataValue = $snak->getDataValue(); $result = $this->validateDataValue($dataValue, $typeId); } else { $result = Result::newSuccess(); } } catch (PropertyDataTypeLookupException $ex) { $result = Result::newError(array(Error::newError("Property {$propertyId} not found!", null, 'no-such-property', array($propertyId)))); } return $result; }
public function formatSnakAsText(Snak $snak) { if ($snak instanceof PropertyValueSnak) { return $this->formatValueAsText($snak->getDataValue()); } else { return $snak->getType(); } }
/** * @param Snak $mainSnak * * @return array */ private function getSummaryArgs(Snak $mainSnak) { $propertyId = $mainSnak->getPropertyId(); return array(array($propertyId->getSerialization() => $mainSnak)); }
/** * @dataProvider snakProvider * @param Snak $snak */ public function testEquals(Snak $snak) { $this->assertTrue($snak->equals($snak)); $this->assertFalse($snak->equals('~=[,,_,,]:3')); }
private function isSnakMorePrecise(Snak $a, Snak $b) { if ($a instanceof PropertyValueSnak && $b instanceof PropertyValueSnak) { return $a->getPropertyId()->equals($b->getPropertyId()) && $this->isDataValueMorePrecise($a->getDataValue(), $b->getDataValue()); } return $a->equals($b); }
/** * @dataProvider notEqualsProvider */ public function testGivenDifferentSnaks_EqualsReturnsFalse(Snak $snak1, Snak $snak2) { $this->assertFalse($snak1->equals($snak2)); $this->assertFalse($snak2->equals($snak1)); }
/** * @see Snaks::removeSnak * * @since 0.1 * * @param Snak $snak */ public function removeSnak(Snak $snak) { $this->removeByElementHash($snak->getHash()); }