/** * Initializes the internal mapping of MIME types and file extensions to format names. */ private function initFormats() { if ($this->mimeTypes !== null && $this->fileExtensions !== null) { return; } $this->mimeTypes = array(); $this->fileExtensions = array(); $api = $this->newApiMain("dummy"); $formatNames = $api->getModuleManager()->getNames('format'); foreach ($formatNames as $name) { if ($this->formatWhiteList !== null && !in_array($name, $this->formatWhiteList)) { continue; } $mimes = self::getApiMimeTypes($name); $ext = self::getApiFormatName($name); foreach ($mimes as $mime) { if (!isset($this->mimeTypes[$mime])) { $this->mimeTypes[$mime] = $name; } } $this->fileExtensions[$ext] = $name; } $formats = $this->rdfWriterFactory->getSupportedFormats(); foreach ($formats as $name) { // check whitelist, and don't override API formats if ($this->formatWhiteList !== null && !in_array($name, $this->formatWhiteList) || in_array($name, $this->mimeTypes) || in_array($name, $this->fileExtensions)) { continue; } // use all mime types. to improve content negotiation foreach ($this->rdfWriterFactory->getMimeTypes($name) as $mime) { if (!isset($this->mimeTypes[$mime])) { $this->mimeTypes[$mime] = $name; } } // only one file extension, to keep purging simple $ext = $this->rdfWriterFactory->getFileExtension($name); if (!isset($this->fileExtensions[$ext])) { $this->fileExtensions[$ext] = $name; } } }
public function testGivenInvalidFormat_getFileExtensionsThrowsException() { $factory = new RdfWriterFactory(); $this->setExpectedException(InvalidArgumentException::class); $factory->getFileExtension('invalid'); }
private static function getRdfWriter($name) { $factory = new RdfWriterFactory(); $format = $factory->getFormatName($name); if (!$format) { return null; } return $factory->getWriter($format); }
/** * Creates an Rdf Serializer that can generate the given output format. * * @param string $format The desired serialization format, as a format name understood by ApiBase or RdfWriterFactory * @param string|null $flavorName Flavor name (used for RDF output) * * @return RdfBuilder|null A suitable result printer, or null * if the given format is not supported. */ private function createRdfBuilder($format, $flavorName = null) { $canonicalFormat = $this->rdfWriterFactory->getFormatName($format); if (!$canonicalFormat) { return null; } $rdfWriter = $this->rdfWriterFactory->getWriter($format); $rdfBuilder = new RdfBuilder($this->sites, new RdfVocabulary($this->rdfBaseURI, $this->rdfDataURI, $this->canonicalLanguageCodes), $this->valueSnakRdfBuilderFactory, $this->propertyLookup, $this->getFlavor($flavorName), $rdfWriter, new HashDedupeBag()); return $rdfBuilder; }