Hoo man has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/206734

Change subject: Cache supported entity data formats
......................................................................

Cache supported entity data formats

As generating that list is very expensive.

The is needed for T96298 as we want to use the Entity data
formats on every page view there.

As a side effect this makes Special:EntityData a little faster
(I get about 13% for content negotiation only and about 5% when
doing content negotiation and then loading a mid sized entity as
ttl).

Change-Id: If7d137a61215f31d2a110b1272b99e31984d7dbd
---
A repo/includes/LinkedData/CachingEntityDataFormatAccessor.php
A repo/includes/LinkedData/EntityDataFormatAccessor.php
A repo/includes/LinkedData/EntityDataFormatProvider.php
M repo/includes/LinkedData/EntityDataSerializationService.php
M repo/includes/WikibaseRepo.php
M repo/includes/specials/SpecialEntityData.php
A repo/tests/phpunit/includes/LinkedData/CachingEntityDataFormatAccessorTest.php
A repo/tests/phpunit/includes/LinkedData/EntityDataFormatProviderTest.php
M repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
M repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
M repo/tests/phpunit/includes/WikibaseRepoTest.php
M repo/tests/phpunit/includes/specials/SpecialEntityDataTest.php
12 files changed, 616 insertions(+), 107 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/34/206734/1

diff --git a/repo/includes/LinkedData/CachingEntityDataFormatAccessor.php 
b/repo/includes/LinkedData/CachingEntityDataFormatAccessor.php
new file mode 100644
index 0000000..97840ec
--- /dev/null
+++ b/repo/includes/LinkedData/CachingEntityDataFormatAccessor.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace Wikibase\Repo\LinkedData;
+
+use BagOStuff;
+use LogicException;
+
+/**
+ * EntityDataFormatAccessor decorator that implements caching.
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < [email protected] >
+ */
+class CachingEntityDataFormatAccessor implements EntityDataFormatAccessor {
+
+       /**
+        * @var EntityDataFormatAccessor
+        */
+       private $entityDataFormatAccessor;
+
+       /**
+        * @var BagOStuff
+        */
+       private $cache;
+
+       /**
+        * @var int
+        */
+       private $cacheDuration;
+
+       /**
+        * @var string
+        */
+       private $cacheKeyPrefix;
+
+       /**
+        * @param EntityDataFormatAccessor $entityDataFormatAccessor
+        * @param BagOStuff $cache The cache to use
+        * @param int $cacheDuration Cache duration in seconds. Defaults to 
3600 (1 hour).
+        * @param string $cacheKeyPrefix Cache key prefix to use.
+        *     Important in case we're not in-process caching. Defaults to 
"wikibase"
+        */
+       public function __construct(
+               EntityDataFormatAccessor $entityDataFormatAccessor,
+               BagOStuff $cache,
+               $cacheDuration = 3600,
+               $cacheKeyPrefix = 'wikibase'
+       ) {
+               $this->entityDataFormatAccessor = $entityDataFormatAccessor;
+               $this->cache = $cache;
+               $this->cacheDuration = $cacheDuration;
+               $this->cacheKeyPrefix = $cacheKeyPrefix;
+       }
+
+       /**
+        * @see EntityDataFormatAccessor::getMimeTypes
+        */
+       public function getMimeTypes( array $whitelist = null ) {
+               return $this->load( 'mimeTypes', $whitelist );
+       }
+
+       /**
+        * @see EntityDataFormatAccessor::getFileExtensions
+        */
+       public function getFileExtensions( array $whitelist = null ) {
+               return $this->load( 'fileExtensions', $whitelist );
+       }
+
+       private function load( $type, array $whitelist = null ) {
+               $fromCache = $this->loadFromCache( $type, $whitelist );
+
+               if ( is_array( $fromCache ) ) {
+                       return $fromCache;
+               }
+
+               return $this->loadAndStore( $type, $whitelist );
+       }
+
+       /**
+        * @param string $type
+        * @param array|null $whitelist
+        *
+        * @return string
+        */
+       private function getCacheKey( $type, array $whitelist = null ) {
+               $whitelistHash = sha1( json_encode( $whitelist ) );
+
+               return $this->cacheKeyPrefix . ':EntityDataFormats:' . $type . 
':' . $whitelistHash;
+       }
+
+       /**
+        * @return array|bool false if not found in cache
+        */
+       private function loadFromCache( $type, array $whitelist = null ) {
+               $data = $this->cache->get( $this->getCacheKey( $type, 
$whitelist ) );
+
+               if ( is_array( $data ) ) {
+                       return $data;
+               }
+
+               return false;
+       }
+
+       private function loadAndStore( $type, array $whitelist = null ) {
+               if ( $type === 'fileExtensions' ) {
+                       $data = 
$this->entityDataFormatAccessor->getFileExtensions( $whitelist );
+               } elseif( $type === 'mimeTypes' ) {
+                       $data = $this->entityDataFormatAccessor->getMimeTypes( 
$whitelist );
+               } else {
+                       throw new LogicException( 'Invalid $type ' . $type );
+               }
+
+               $this->cache->set( $this->getCacheKey( $type, $whitelist ), 
$data, $this->cacheDuration );
+
+               return $data;
+       }
+
+}
diff --git a/repo/includes/LinkedData/EntityDataFormatAccessor.php 
b/repo/includes/LinkedData/EntityDataFormatAccessor.php
new file mode 100644
index 0000000..be42e68
--- /dev/null
+++ b/repo/includes/LinkedData/EntityDataFormatAccessor.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Wikibase\Repo\LinkedData;
+
+/**
+ * Interface for classes providing information about supported Entity data 
formats.
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < [email protected] >
+ */
+interface EntityDataFormatAccessor {
+
+       /**
+        * @param array|null $whitelist List of allowed formats or null
+        *
+        * @return array Associative array from MIME type to format name
+        */
+       public function getMimeTypes( array $whitelist = null );
+
+       /**
+        * @param array|null $whitelist List of allowed formats or null
+        *
+        * @return array Associative array from file extension to format name
+        */
+       public function getFileExtensions( array $whitelist = null );
+}
diff --git a/repo/includes/LinkedData/EntityDataFormatProvider.php 
b/repo/includes/LinkedData/EntityDataFormatProvider.php
new file mode 100644
index 0000000..f8df7af
--- /dev/null
+++ b/repo/includes/LinkedData/EntityDataFormatProvider.php
@@ -0,0 +1,197 @@
+<?php
+
+namespace Wikibase\Repo\LinkedData;
+
+use ApiMain;
+use DerivativeContext;
+use DerivativeRequest;
+use RequestContext;
+use Wikimedia\Purtle\RdfWriterFactory;
+
+/**
+ * Generates a list of formats supported from various sources.
+ * Please note that this is not doing any (even in-process) caching and thus
+ * should only be used very carefully or with a cache 
(CachingEntityDataFormatAccessor)
+ * on top.
+ *
+ * @see EntityDataFormatAccessor
+ *
+ * @since 0.5
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < [email protected] >
+ */
+class EntityDataFormatProvider implements EntityDataFormatAccessor {
+
+       /**
+        * @var null|array Associative array from MIME type to format name
+        * @note: initialized by initFormats()
+        */
+       private $mimeTypes = null;
+
+       /**
+        * @var null|array Associative array from file extension to format name
+        * @note: initialized by initFormats()
+        */
+       private $fileExtensions = null;
+
+       /**
+        * @var RdfWriterFactory
+        */
+       private $rdfWriterFactory;
+
+       /**
+        * @param RdfWriterFactory $rdfWriterFactory
+        *
+        * @since 0.5
+        */
+       public function __construct( RdfWriterFactory $rdfWriterFactory ) {
+               $this->rdfWriterFactory = $rdfWriterFactory;
+       }
+
+       /**
+        * @param array|null $whitelist List of allowed formats or null
+        *
+        * @return array Associative array from MIME type to format name
+        */
+       public function getMimeTypes( array $whitelist = null ) {
+               $this->initFormats( $whitelist );
+
+               return $this->mimeTypes;
+       }
+
+       /**
+        * @param array|null $whitelist List of allowed formats or null
+        *
+        * @return array Associative array from file extension to format name
+        */
+       public function getFileExtensions( array $whitelist = null ) {
+               $this->initFormats( $whitelist );
+
+               return $this->fileExtensions;
+       }
+
+       /**
+        * Initializes the internal mapping of MIME types and file extensions 
to format names.
+        *
+        * @param array|null $whitelist List of allowed formats or null
+        */
+       private function initFormats( array $whitelist = null ) {
+               $this->mimeTypes = array();
+               $this->fileExtensions = array();
+
+               $api = $this->newApiMain( "dummy" );
+               $formatNames = $api->getModuleManager()->getNames( 'format' );
+
+               foreach ( $formatNames as $name ) {
+                       if ( $whitelist !== null && !in_array( $name, 
$whitelist ) ) {
+                               continue;
+                       }
+
+                       $mimes = $this->getApiMimeTypes( $name );
+                       $ext = $this->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 ) {
+                       // Take the whitelist into account and don't override 
API formats
+                       if ( ( $whitelist !== null && !in_array( $name, 
$whitelist ) )
+                               || 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;
+                       }
+               }
+       }
+
+       /**
+        * Normalizes the format specifier; Converts mime types to API format 
names.
+        *
+        * @param String $format the format as supplied in the request
+        *
+        * @return String|null the normalized format name, or null if the 
format is unknown
+        */
+       private function getApiFormatName( $format ) {
+               $format = trim( strtolower( $format ) );
+
+               if ( $format === 'application/vnd.php.serialized' ) {
+                       $format = 'php';
+               } elseif ( $format === 'text/text' || $format === 'text/plain' 
) {
+                       $format = 'txt';
+               } else {
+                       // hack: just trip the major part of the mime type
+                       $format = preg_replace( '@^(text|application)?/@', '', 
$format );
+               }
+
+               return $format;
+       }
+
+       /**
+        * Converts API format names to MIME types.
+        *
+        * @param String $format the API format name
+        *
+        * @return String[]|null the MIME types for the given format
+        */
+       private function getApiMimeTypes( $format ) {
+               $format = trim( strtolower( $format ) );
+
+               switch ( $format ) {
+                       case 'php':
+                               return array( 'application/vnd.php.serialized' 
);
+
+                       case 'txt':
+                               return array( "text/text", "text/plain" );
+
+                       case 'javascript':
+                               return array( "text/javascript" );
+
+                       default:
+                               return array( "application/$format" );
+               }
+       }
+
+       /**
+        * Returns an ApiMain module that acts as a context for the formatting 
and serialization.
+        *
+        * @param String $format The desired output format, as a format name 
that ApiBase understands.
+        *
+        * @return ApiMain
+        */
+       private function newApiMain( $format ) {
+               // Fake request params to ApiMain, with forced format 
parameters.
+               // We can override additional parameters here, as needed.
+               $params = array(
+                       'format' => $format,
+               );
+
+               $context = new DerivativeContext( RequestContext::getMain() ); 
//XXX: ugly
+
+               $req = new DerivativeRequest( $context->getRequest(), $params );
+               $context->setRequest( $req );
+
+               return new ApiMain( $context );
+       }
+
+}
diff --git a/repo/includes/LinkedData/EntityDataSerializationService.php 
b/repo/includes/LinkedData/EntityDataSerializationService.php
index 9b33e63..c4c4c5b 100644
--- a/repo/includes/LinkedData/EntityDataSerializationService.php
+++ b/repo/includes/LinkedData/EntityDataSerializationService.php
@@ -117,12 +117,18 @@
        private $rdfWriterFactory;
 
        /**
+        * @var EntityDataFormatAccessor
+        */
+       private $entityDataFormatAccessor;
+
+       /**
         * @param string $rdfBaseURI
         * @param string $rdfDataURI
         * @param EntityLookup $entityLookup
         * @param EntityTitleLookup $entityTitleLookup
         * @param SerializerFactory $serializerFactory
         * @param SiteList $sites
+        * @param EntityDataFormatAccessor $entityDataFormatAccessor
         *
         * @since 0.4
         */
@@ -133,7 +139,8 @@
                EntityTitleLookup $entityTitleLookup,
                SerializerFactory $serializerFactory,
                PropertyDataTypeLookup $propertyLookup,
-               SiteList $sites
+               SiteList $sites,
+               EntityDataFormatAccessor $entityDataFormatAccessor
        ) {
                $this->rdfBaseURI = $rdfBaseURI;
                $this->rdfDataURI = $rdfDataURI;
@@ -142,6 +149,7 @@
                $this->serializerFactory = $serializerFactory;
                $this->propertyLookup = $propertyLookup;
                $this->sites = $sites;
+               $this->entityDataFormatAccessor = $entityDataFormatAccessor;
 
                $this->rdfWriterFactory = new RdfWriterFactory();
        }
@@ -166,7 +174,7 @@
        public function setFormatWhiteList( $formatWhiteList ) {
                $this->formatWhiteList = $formatWhiteList;
 
-               // force re-init of format maps
+               // Force re-init of format maps
                $this->fileExtensions = null;
                $this->mimeTypes = null;
        }
@@ -307,60 +315,14 @@
        /**
         * Initializes the internal mapping of MIME types and file extensions 
to format names.
         */
-       protected function initFormats() {
+       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;
-                       }
-               }
+               $this->mimeTypes = 
$this->entityDataFormatAccessor->getMimeTypes( $this->formatWhiteList );
+               $this->fileExtensions = 
$this->entityDataFormatAccessor->getFileExtensions( $this->formatWhiteList );
        }
 
        /**
@@ -429,61 +391,13 @@
        }
 
        /**
-        * Normalizes the format specifier; Converts mime types to API format 
names.
-        *
-        * @param String $format the format as supplied in the request
-        *
-        * @return String|null the normalized format name, or null if the 
format is unknown
-        */
-       protected static function getApiFormatName( $format ) {
-               $format = trim( strtolower( $format ) );
-
-               if ( $format === 'application/vnd.php.serialized' ) {
-                       $format = 'php';
-               } elseif ( $format === 'text/text' || $format === 'text/plain' 
) {
-                       $format = 'txt';
-               } else {
-                       // hack: just trip the major part of the mime type
-                       $format = preg_replace( '@^(text|application)?/@', '', 
$format );
-               }
-
-               return $format;
-       }
-
-       /**
-        * Converts API format names to MIME types.
-        *
-        * @param String $format the API format name
-        *
-        * @return String[]|null the MIME types for the given format
-        */
-       protected static function getApiMimeTypes( $format ) {
-               $format = trim( strtolower( $format ) );
-               $type = null;
-
-               switch ( $format ) {
-                       case 'php':
-                               return array( 'application/vnd.php.serialized' 
);
-
-                       case 'txt':
-                               return array( "text/text", "text/plain" );
-
-                       case 'javascript':
-                               return array( "text/javascript" );
-
-                       default:
-                               return array( "application/$format" );
-               }
-       }
-
-       /**
         * Returns an ApiMain module that acts as a context for the formatting 
and serialization.
         *
         * @param String $format The desired output format, as a format name 
that ApiBase understands.
         *
         * @return ApiMain
         */
-       protected function newApiMain( $format ) {
+       private function newApiMain( $format ) {
                // Fake request params to ApiMain, with forced format 
parameters.
                // We can override additional parameters here, as needed.
                $params = array(
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 98bd5e7..8b14c7d 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -64,6 +64,9 @@
 use Wikibase\Repo\Content\EntityContentFactory;
 use Wikibase\Repo\Content\ItemHandler;
 use Wikibase\Repo\Content\PropertyHandler;
+use Wikibase\Repo\LinkedData\CachingEntityDataFormatAccessor;
+use Wikibase\Repo\LinkedData\EntityDataFormatAccessor;
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
 use Wikibase\Repo\Localizer\ChangeOpValidationExceptionLocalizer;
 use Wikibase\Repo\Localizer\MessageParameterFormatter;
 use Wikibase\Repo\Notifications\ChangeNotifier;
@@ -88,6 +91,7 @@
 use Wikibase\ValuesFinder;
 use Wikibase\View\EntityViewFactory;
 use Wikibase\View\Template\TemplateFactory;
+use Wikimedia\Purtle\RdfWriterFactory;
 
 /**
  * Top level factory for the WikibaseRepo extension.
@@ -177,12 +181,16 @@
        /**
         * @var TermLookup|null
         */
-       private $termLookup;
+       private $termLookup = null;
 
        /**
         * @var ContentLanguages|null
         */
        private $monolingualTextLanguages = null;
+       /**
+        * @var EntityDataFormatAccessor|null
+        */
+       private $entityDataFormatAccessor = null;
 
        /**
         * Returns the default instance constructed using newInstance().
@@ -1074,4 +1082,21 @@
                return new WikibaseHtmlSnakFormatterFactory( 
$this->getSnakFormatterFactory() );
        }
 
+       /**
+        * Get a cached EntityDataFormatAccessor
+        *
+        * @return EntityDataFormatAccessor
+        */
+       public function getEntityDataFormatAccessor() {
+               if ( !$this->entityDataFormatAccessor ) {
+                       $this->entityDataFormatAccessor = new 
CachingEntityDataFormatAccessor(
+                               new EntityDataFormatProvider( new 
RdfWriterFactory() ),
+                               wfGetCache( $this->getSettings()->getSetting( 
'sharedCacheType' ) ),
+                               $this->getSettings()->getSetting( 
'sharedCacheDuration' ),
+                               $this->getSettings()->getSetting( 
'sharedCacheKeyPrefix' )
+                       );
+               }
+
+               return $this->entityDataFormatAccessor;
+       }
 }
diff --git a/repo/includes/specials/SpecialEntityData.php 
b/repo/includes/specials/SpecialEntityData.php
index 75ac3ae..47ee44e 100644
--- a/repo/includes/specials/SpecialEntityData.php
+++ b/repo/includes/specials/SpecialEntityData.php
@@ -92,7 +92,8 @@
                        $titleLookup,
                        $serializerFactory,
                        $wikibaseRepo->getPropertyDataTypeLookup(),
-                       $wikibaseRepo->getSiteStore()->getSites()
+                       $wikibaseRepo->getSiteStore()->getSites(),
+                       $wikibaseRepo->getEntityDataFormatAccessor()
                );
 
                $maxAge = $wikibaseRepo->getSettings()->getSetting( 
'dataSquidMaxage' );
diff --git 
a/repo/tests/phpunit/includes/LinkedData/CachingEntityDataFormatAccessorTest.php
 
b/repo/tests/phpunit/includes/LinkedData/CachingEntityDataFormatAccessorTest.php
new file mode 100644
index 0000000..219700f
--- /dev/null
+++ 
b/repo/tests/phpunit/includes/LinkedData/CachingEntityDataFormatAccessorTest.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Wikibase\Test;
+
+use HashBagOStuff;
+use PHPUnit_Framework_TestCase;
+use Wikibase\Repo\LinkedData\CachingEntityDataFormatAccessor;
+
+/**
+ * @covers Wikibase\Repo\LinkedData\CachingEntityDataFormatAccessor
+ *
+ * @group Wikibase
+ * @group WikibaseEntityData
+ * @group WikibaseRepo
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < [email protected] >
+ */
+class CachingEntityDataFormatAccessorTest extends PHPUnit_Framework_TestCase {
+
+       public function testGetMimeTypes() {
+               $this->cacheTestCase( 'getMimeTypes' );
+       }
+
+       public function testGetFileExtensions() {
+               $this->cacheTestCase( 'getFileExtensions' );
+       }
+
+       private function cacheTestCase( $method ) {
+               $expected1 = array( 'cat' => 'nyan' );
+               $expected2 = array( 'foo' => 'bar' );
+               $wl1 = array( 'nyan' );
+               $wl2 = array( 'kitten' );
+
+               $lookup = $this->getMock( 
'Wikibase\Repo\LinkedData\EntityDataFormatAccessor' );
+               $lookup->expects( $this->at( 0 ) )
+                       ->method( $method )
+                       ->with( $wl1 )
+                       ->will( $this->returnValue( $expected1 ) );
+               $lookup->expects( $this->at( 1 ) )
+                       ->method( $method )
+                       ->with( $wl2 )
+                       ->will( $this->returnValue( $expected2 ) );
+
+               $provider = new CachingEntityDataFormatAccessor( $lookup, new 
HashBagOStuff() );
+
+               $types = $provider->$method( $wl1 );
+
+               $this->assertEquals(
+                       $expected1,
+                       $types
+               );
+
+               // Call $method again to make sure we use the cache now
+               $types = $provider->$method( $wl1 );
+
+               $this->assertEquals(
+                       $expected1,
+                       $types
+               );
+
+               // Different $whitelist, different result
+               $types = $provider->$method( $wl2 );
+
+               $this->assertEquals(
+                       $expected2,
+                       $types
+               );
+       }
+
+}
diff --git 
a/repo/tests/phpunit/includes/LinkedData/EntityDataFormatProviderTest.php 
b/repo/tests/phpunit/includes/LinkedData/EntityDataFormatProviderTest.php
new file mode 100644
index 0000000..e41973b
--- /dev/null
+++ b/repo/tests/phpunit/includes/LinkedData/EntityDataFormatProviderTest.php
@@ -0,0 +1,141 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
+use Wikimedia\Purtle\RdfWriterFactory;
+
+/**
+ * @covers Wikibase\Repo\LinkedData\EntityDataFormatProvider
+ *
+ * @group Wikibase
+ * @group WikibaseEntityData
+ * @group WikibaseRepo
+ *
+ * @license GNU GPL v2+
+ * @author Marius Hoch < [email protected] >
+ */
+class EntityDataFormatProviderTest extends \MediaWikiTestCase {
+
+       public function getMimeTypesProvider() {
+               $allFormats = array(
+                       'application/json' => 'json',
+                       'application/jsonfm' => 'jsonfm',
+                       'application/vnd.php.serialized' => 'php',
+                       'application/phpfm' => 'phpfm',
+                       'application/wddx' => 'wddx',
+                       'application/wddxfm' => 'wddxfm',
+                       'application/xml' => 'xml',
+                       'application/xmlfm' => 'xmlfm',
+                       'application/yaml' => 'yaml',
+                       'application/yamlfm' => 'yamlfm',
+                       'application/rawfm' => 'rawfm',
+                       'text/text' => 'txt',
+                       'text/plain' => 'txt',
+                       'application/txtfm' => 'txtfm',
+                       'application/dbg' => 'dbg',
+                       'application/dbgfm' => 'dbgfm',
+                       'application/dump' => 'dump',
+                       'application/dumpfm' => 'dumpfm',
+                       'application/none' => 'none',
+                       'text/n3' => 'n3',
+                       'text/rdf+n3' => 'n3',
+                       'text/turtle' => 'turtle',
+                       'application/x-turtle' => 'turtle',
+                       'application/n-triples' => 'ntriples',
+                       'text/n-triples' => 'ntriples',
+                       'application/rdf+xml' => 'rdfxml',
+                       'text/xml' => 'rdfxml',
+               );
+
+               return array(
+                       "No types whitelisted" => array(
+                               array(),
+                               array()
+                       ),
+                       "No whitelist" => array(
+                               $allFormats,
+                               null
+                       ),
+                       "Only turtle and a format which doesn't exist" => array(
+                               array_filter(
+                                       $allFormats,
+                                       function( $val ) {
+                                               return $val === 'turtle';
+                                       }
+                               ),
+                               array( 'turtle', 'kitten-rdf' )
+                       )
+               );
+       }
+
+       /**
+        * @dataProvider getMimeTypesProvider
+        */
+       public function testGetMimeTypes( array $expected, array $whitelist = 
null ) {
+               $provider = new EntityDataFormatProvider( new 
RdfWriterFactory() );
+
+               $types = $provider->getMimeTypes( $whitelist );
+
+               $this->assertEquals(
+                       $expected,
+                       $types
+               );
+       }
+
+       public function getFileExtensionsProvider() {
+               $allFormats = array(
+                       'json' => 'json',
+                       'jsonfm' => 'jsonfm',
+                       'php' => 'php',
+                       'phpfm' => 'phpfm',
+                       'wddx' => 'wddx',
+                       'wddxfm' => 'wddxfm',
+                       'xml' => 'xml',
+                       'xmlfm' => 'xmlfm',
+                       'yaml' => 'yaml',
+                       'yamlfm' => 'yamlfm',
+                       'rawfm' => 'rawfm',
+                       'txt' => 'txt',
+                       'txtfm' => 'txtfm',
+                       'dbg' => 'dbg',
+                       'dbgfm' => 'dbgfm',
+                       'dump' => 'dump',
+                       'dumpfm' => 'dumpfm',
+                       'none' => 'none',
+                       'n3' => 'n3',
+                       'ttl' => 'turtle',
+                       'nt' => 'ntriples',
+                       'rdf' => 'rdfxml'
+               );
+
+               return array(
+                       "No types whitelisted" => array(
+                               array(),
+                               array()
+                       ),
+                       "No whitelist" => array(
+                               $allFormats,
+                               null
+                       ),
+                       "Only turtle and a format which doesn't exist" => array(
+                               array( 'ttl' => 'turtle' ),
+                               array( 'turtle', 'kitten-rdf' )
+                       )
+               );
+       }
+
+       /**
+        * @dataProvider getFileExtensionsProvider
+        */
+       public function testGetFileExtensions( array $expected, array 
$whitelist = null ) {
+               $provider = new EntityDataFormatProvider( new 
RdfWriterFactory() );
+
+               $extensions = $provider->getFileExtensions( $whitelist );
+
+               $this->assertEquals(
+                       $expected,
+                       $extensions
+               );
+       }
+}
diff --git 
a/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php 
b/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
index d4db02b..46522ae 100644
--- a/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
+++ b/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
@@ -4,7 +4,6 @@
 
 use DerivativeContext;
 use FauxRequest;
-use FauxResponse;
 use HttpError;
 use OutputPage;
 use RequestContext;
@@ -14,9 +13,11 @@
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\Serializers\SerializerFactory;
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
 use Wikibase\Repo\LinkedData\EntityDataRequestHandler;
 use Wikibase\Repo\LinkedData\EntityDataSerializationService;
 use Wikibase\Repo\LinkedData\EntityDataUriManager;
+use Wikimedia\Purtle\RdfWriterFactory;
 
 /**
  * @covers Wikibase\Repo\LinkedData\EntityDataRequestHandler
@@ -95,7 +96,8 @@
                        $titleLookup,
                        $serializerFactory,
                        $propertyLookup,
-                       new SiteList()
+                       new SiteList(),
+                       new EntityDataFormatProvider( new RdfWriterFactory() )
                );
 
                $service->setFormatWhiteList(
diff --git 
a/repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php 
b/repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
index 7cda931..4c0c00b 100644
--- 
a/repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
+++ 
b/repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
@@ -8,7 +8,9 @@
 use Wikibase\EntityRevision;
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\Serializers\SerializerFactory;
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
 use Wikibase\Repo\LinkedData\EntityDataSerializationService;
+use Wikimedia\Purtle\RdfWriterFactory;
 
 /**
  * @covers Wikibase\Repo\LinkedData\EntityDataSerializationService
@@ -55,7 +57,8 @@
                        $titleLookup,
                        $serializerFactory,
                        $dataTypeLookup,
-                       new SiteList()
+                       new SiteList(),
+                       new EntityDataFormatProvider( new RdfWriterFactory() )
                );
 
                $service->setFormatWhiteList(
diff --git a/repo/tests/phpunit/includes/WikibaseRepoTest.php 
b/repo/tests/phpunit/includes/WikibaseRepoTest.php
index 4f7e1a5..89d97d0 100644
--- a/repo/tests/phpunit/includes/WikibaseRepoTest.php
+++ b/repo/tests/phpunit/includes/WikibaseRepoTest.php
@@ -259,4 +259,9 @@
                $this->assertInstanceOf( 'Wikibase\Lib\ContentLanguages', 
$service );
        }
 
+       public function testGetEntityDataFormatAccessor() {
+               $service = 
$this->getWikibaseRepo()->getEntityDataFormatAccessor();
+               $this->assertInstanceOf( 
'Wikibase\Repo\LinkedData\EntityDataFormatAccessor', $service );
+       }
+
 }
diff --git a/repo/tests/phpunit/includes/specials/SpecialEntityDataTest.php 
b/repo/tests/phpunit/includes/specials/SpecialEntityDataTest.php
index 49be340..f93cba6 100644
--- a/repo/tests/phpunit/includes/specials/SpecialEntityDataTest.php
+++ b/repo/tests/phpunit/includes/specials/SpecialEntityDataTest.php
@@ -3,7 +3,6 @@
 namespace Wikibase\Test;
 
 use FauxRequest;
-use FauxResponse;
 use HttpError;
 use OutputPage;
 use SiteList;
@@ -14,10 +13,12 @@
 use Wikibase\EntityFactory;
 use Wikibase\Lib\Serializers\SerializationOptions;
 use Wikibase\Lib\Serializers\SerializerFactory;
+use Wikibase\Repo\LinkedData\EntityDataFormatProvider;
 use Wikibase\Repo\LinkedData\EntityDataRequestHandler;
 use Wikibase\Repo\LinkedData\EntityDataSerializationService;
 use Wikibase\Repo\LinkedData\EntityDataUriManager;
 use Wikibase\Repo\Specials\SpecialEntityData;
+use Wikimedia\Purtle\RdfWriterFactory;
 
 /**
  * @covers Wikibase\Repo\Specials\SpecialEntityData
@@ -80,7 +81,8 @@
                        $titleLookup,
                        $serializerFactory,
                        $dataTypeLookup,
-                       new SiteList()
+                       new SiteList(),
+                       new EntityDataFormatProvider( new RdfWriterFactory() )
                );
 
                $maxAge = 60*60;

-- 
To view, visit https://gerrit.wikimedia.org/r/206734
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If7d137a61215f31d2a110b1272b99e31984d7dbd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to