/** * @param int $flavor Bitmap for the output flavor, use RdfProducer::PRODUCE_XXX constants. * @param EntityId[] &$mentioned Receives any entity IDs being mentioned. * @param DedupeBag $dedupe A bag of reference hashes that should be considered "already seen". * * @return FullStatementRdfBuilder */ private function newBuilder($flavor, array &$mentioned = array(), DedupeBag $dedupe = null) { $vocabulary = $this->getTestData()->getVocabulary(); $writer = $this->getTestData()->getNTriplesWriter(); $mentionTracker = $this->getMock('Wikibase\\Rdf\\EntityMentionListener'); $mentionTracker->expects($this->any())->method('propertyMentioned')->will($this->returnCallback(function (EntityId $id) use(&$mentioned) { $key = $id->getSerialization(); $mentioned[$key] = $id; })); // Note: using the actual factory here makes this an integration test! $valueBuilderFactory = WikibaseRepo::getDefaultInstance()->getValueSnakRdfBuilderFactory(); if ($flavor & RdfProducer::PRODUCE_FULL_VALUES) { $valueWriter = $writer->sub(); $statementValueBuilder = $valueBuilderFactory->getComplexValueSnakRdfBuilder($this->getTestData()->getVocabulary(), $valueWriter, $mentionTracker, new HashDedupeBag()); } else { $statementValueBuilder = $valueBuilderFactory->getSimpleValueSnakRdfBuilder($this->getTestData()->getVocabulary(), $writer, $mentionTracker, new HashDedupeBag()); } $snakRdfBuilder = new SnakRdfBuilder($vocabulary, $statementValueBuilder, $this->getTestData()->getMockRepository()); $statementBuilder = new FullStatementRdfBuilder($vocabulary, $writer, $snakRdfBuilder); $statementBuilder->setDedupeBag($dedupe ?: new NullDedupeBag()); if ($flavor & RdfProducer::PRODUCE_PROPERTIES) { $snakRdfBuilder->setEntityMentionListener($mentionTracker); } $statementBuilder->setProduceQualifiers($flavor & RdfProducer::PRODUCE_QUALIFIERS); $statementBuilder->setProduceReferences($flavor & RdfProducer::PRODUCE_REFERENCES); // HACK: stick the writer into a public field, for use by getDataFromBuilder() $statementBuilder->test_writer = $writer; return $statementBuilder; }
/** * 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); }
/** * @param string $propertyNamespace * @param string $propertyValueLName * @param string $dataType * @param Snak|null $snak * @param EntityId[] &$mentioned receives the IDs of any mentioned entities. * * @return SnakRdfBuilder */ private function newBuilder($propertyNamespace, $propertyValueLName, $dataType, Snak $snak = null, array &$mentioned = array()) { $mentionTracker = $this->getMock('Wikibase\\Rdf\\EntityMentionListener'); $mentionTracker->expects($this->any())->method('propertyMentioned')->will($this->returnCallback(function (EntityId $id) use(&$mentioned) { $key = $id->getSerialization(); $mentioned[$key] = $id; })); $valueBuilder = $this->getMock('Wikibase\\Rdf\\ValueSnakRdfBuilder'); if ($snak instanceof PropertyValueSnak) { $valueBuilder->expects($this->once())->method('addValue')->with($this->anything(), $propertyNamespace, $propertyValueLName, $dataType, $snak); } else { $valueBuilder->expects($this->never())->method('addValue'); } $vocabulary = $this->getTestData()->getVocabulary(); $builder = new SnakRdfBuilder($vocabulary, $valueBuilder, $this->getTestData()->getMockRepository()); $builder->setEntityMentionListener($mentionTracker); return $builder; }
/** * 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 string $full * * @return SnakRdfBuilder */ private function newSnakBuilder($full) { if ($full === 'full') { $statementValueBuilder = $this->valueSnakRdfBuilderFactory->getComplexValueSnakRdfBuilder($this->vocabulary, $this->writer, $this, $this->dedupBag); } else { $statementValueBuilder = $this->valueSnakRdfBuilderFactory->getSimpleValueSnakRdfBuilder($this->vocabulary, $this->writer, $this, $this->dedupBag); } $snakBuilder = new SnakRdfBuilder($this->vocabulary, $statementValueBuilder, $this->propertyLookup); $snakBuilder->setEntityMentionListener($this); return $snakBuilder; }