/** * 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); }
/** * Creates a value node for $value, and attaches it to the current subject of $writer. * If a value node for $value was already created, null is returned. Otherwise, the * value node's lname is returned, which should be used to generate detailed about the * value into the writer returned by getValueNodeWriter(). * * When this method returns a non-null lname, the current subject of the RdfWriter returned by * getValueNodeWriter() will the be value node with that lname. * * @param RdfWriter $writer * @param string $propertyValueNamespace Property value relation namespace * @param string $propertyValueLName Property value relation name * @param string $dataType Property data type (unused, passed here for symmetry * with the signature of ValueSnakRdfBuilder::addValue). * @param DataValue $value * * @return string|null The LName of the value node (in the RdfVocabulary::NS_VALUE namespace), * or null if the value node should not be processed (generally, because it already has * been processed). */ public function attachValueNode(RdfWriter $writer, $propertyValueNamespace, $propertyValueLName, $dataType, DataValue $value) { $valueLName = $value->getHash(); $writer->say(RdfVocabulary::$claimToValue[$propertyValueNamespace], $propertyValueLName)->is(RdfVocabulary::NS_VALUE, $valueLName); if ($this->dedupeBag->alreadySeen($valueLName, 'V') !== false) { return null; } $this->valueNodeWriter->about(RdfVocabulary::NS_VALUE, $valueLName)->a(RdfVocabulary::NS_ONTOLOGY, $this->vocabulary->getValueTypeName($value)); return $valueLName; }
/** * Add the entity's terms to the RDF graph. * * @param EntityDocument $entity the entity to output. */ public function addEntity(EntityDocument $entity) { if ($entity instanceof FingerprintProvider) { $fingerprint = $entity->getFingerprint(); /** @var EntityDocument $entity */ $entityLName = $this->vocabulary->getEntityLName($entity->getId()); $this->addLabels($entityLName, $fingerprint->getLabels()); $this->addDescriptions($entityLName, $fingerprint->getDescriptions()); $this->addAliases($entityLName, $fingerprint->getAliasGroups()); } }
/** * @dataProvider provideAddValue */ public function testAddValue(PropertyValueSnak $snak, $complex, array $expected) { $vocab = new RdfVocabulary('http://acme.com/item/', 'http://acme.com/data/'); $snakWriter = new NTriplesRdfWriter(); $snakWriter->prefix('www', "http://www/"); $snakWriter->prefix('acme', "http://acme/"); $snakWriter->prefix(RdfVocabulary::NSP_CLAIM_VALUE, "http://acme/statement/value/"); $snakWriter->prefix(RdfVocabulary::NSP_CLAIM_STATEMENT, "http://acme/statement/"); $snakWriter->prefix(RdfVocabulary::NS_VALUE, "http://acme/value/"); $snakWriter->prefix(RdfVocabulary::NS_ONTOLOGY, "http://acme/onto/"); if ($complex) { $valueWriter = $snakWriter->sub(); $helper = new ComplexValueRdfHelper($vocab, $valueWriter, new HashDedupeBag()); } else { $helper = null; } $builder = new QuantityRdfBuilder($helper); $snakWriter->start(); $snakWriter->about('www', 'Q1'); $builder->addValue($snakWriter, RdfVocabulary::NSP_CLAIM_STATEMENT, $vocab->getEntityLName($snak->getPropertyid()), 'DUMMY', $snak); $this->helper->assertNTriplesEquals($expected, $snakWriter->drain()); }
/** * Adds the site links of the given item to the RDF graph. * * @param Item $item */ public function addSiteLinks(Item $item) { $entityLName = $this->vocabulary->getEntityLName($item->getId()); /** @var SiteLink $siteLink */ foreach ($item->getSiteLinkList() as $siteLink) { if (!$this->isSiteIncluded($siteLink->getSiteId())) { continue; } // FIXME: we should check the site exists using hasGlobalId here before asuming it does $site = $this->siteLookup->getSite($siteLink->getSiteId()); // XXX: ideally, we'd use https if the target site supports it. $baseUrl = str_replace('$1', rawurlencode($siteLink->getPageName()), $site->getLinkPath()); // $site->getPageUrl( $siteLink->getPageName() ); if (!parse_url($baseUrl, PHP_URL_SCHEME)) { $url = "http:" . $baseUrl; } else { $url = $baseUrl; } $this->writer->about($url)->a(RdfVocabulary::NS_SCHEMA_ORG, 'Article')->say(RdfVocabulary::NS_SCHEMA_ORG, 'about')->is(RdfVocabulary::NS_ENTITY, $entityLName)->say(RdfVocabulary::NS_SCHEMA_ORG, 'inLanguage')->text($this->vocabulary->getCanonicalLanguageCode($site->getLanguageCode())); foreach ($siteLink->getBadges() as $badge) { $this->writer->say(RdfVocabulary::NS_ONTOLOGY, 'badge')->is(RdfVocabulary::NS_ENTITY, $this->vocabulary->getEntityLName($badge)); } } }
/** * Adds the value of the given property to the RDF graph. * * @param RdfWriter $writer * @param PropertyValueSnak $snak * @param string $propertyNamespace The property namespace for this snak */ private function addSnakValue(RdfWriter $writer, PropertyValueSnak $snak, $propertyNamespace) { $propertyId = $snak->getPropertyId(); $propertyValueLName = $this->vocabulary->getEntityLName($propertyId); $propertyKey = $propertyId->getSerialization(); // cache data type for all properties we encounter if (!isset($this->propertyTypes[$propertyKey])) { try { $this->propertyTypes[$propertyKey] = $this->propertyLookup->getDataTypeIdForProperty($propertyId); } catch (PropertyDataTypeLookupException $e) { $this->propertyTypes[$propertyKey] = "unknown"; } } $dataType = $this->propertyTypes[$propertyKey]; $this->valueBuilder->addValue($writer, $propertyNamespace, $propertyValueLName, $dataType, $snak); }
/** * 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); }
/** * @param DataValue $value * * @return string the object URI */ protected function getValueUri(DataValue $value) { return $this->vocabulary->getCommonsURI($value->getValue()); }
/** * Create header structure for the dump * * @param int $timestamp Timestamp (for testing) */ public function addDumpHeader($timestamp = 0) { // TODO: this should point to "this document" $this->writer->about(RdfVocabulary::NS_ONTOLOGY, 'Dump')->a(RdfVocabulary::NS_SCHEMA_ORG, "Dataset")->a('owl', 'Ontology')->say(RdfVocabulary::NS_CC, 'license')->is(RdfVocabulary::LICENSE)->say(RdfVocabulary::NS_SCHEMA_ORG, 'softwareVersion')->value(RdfVocabulary::FORMAT_VERSION)->say(RdfVocabulary::NS_SCHEMA_ORG, 'dateModified')->value(wfTimestamp(TS_ISO_8601, $timestamp), 'xsd', 'dateTime')->say('owl', 'imports')->is(RdfVocabulary::getOntologyURI()); }