/**
  * 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;
         }
     }
 }
Exemplo n.º 2
0
 public function testGivenInvalidFormat_getMimeTypesThrowsException()
 {
     $factory = new RdfWriterFactory();
     $this->setExpectedException(InvalidArgumentException::class);
     $factory->getMimeTypes('invalid');
 }
 /**
  * Output entity data.
  *
  * @param string $format The name (mime type of file extension) of the format to use
  * @param EntityRevision $entityRevision The entity
  * @param RedirectRevision|null $followedRedirect The redirect that led to the entity, or null
  * @param EntityId[] $incomingRedirects Incoming redirects to include in the output
  * @param string|null $flavor The type of the output provided by serializer
  *
  * @return array tuple of ( $data, $contentType )
  * @throws MWException
  */
 public function getSerializedData($format, EntityRevision $entityRevision, RedirectRevision $followedRedirect = null, array $incomingRedirects = array(), $flavor = null)
 {
     $formatName = $this->entityDataFormatProvider->getFormatName($format);
     if ($formatName === null) {
         throw new MWException("Unsupported format: {$format}");
     }
     $serializer = $this->createApiSerializer($formatName);
     if ($serializer !== null) {
         $data = $this->getApiSerialization($entityRevision, $serializer);
         $contentType = $serializer->getIsHtml() ? 'text/html' : $serializer->getMimeType();
     } else {
         $rdfBuilder = $this->createRdfBuilder($formatName, $flavor);
         if ($rdfBuilder === null) {
             throw new MWException("Could not create serializer for {$formatName}");
         }
         $data = $this->rdfSerialize($entityRevision, $followedRedirect, $incomingRedirects, $rdfBuilder, $flavor);
         $mimeTypes = $this->rdfWriterFactory->getMimeTypes($formatName);
         $contentType = reset($mimeTypes);
     }
     return array($data, $contentType);
 }