jenkins-bot has submitted this change and it was merged.

Change subject: Introduce EntityDataUriManager
......................................................................


Introduce EntityDataUriManager

Change-Id: I7298de0b9d6fd8eac150bf2b32c9d013b09af233
---
M repo/Wikibase.classes.php
M repo/Wikibase.hooks.php
M repo/includes/LinkedData/EntityDataRequestHandler.php
A repo/includes/LinkedData/EntityDataUriManager.php
M repo/includes/specials/SpecialEntityData.php
M repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
A repo/tests/phpunit/includes/LinkedData/EntityDataUriManagerTest.php
7 files changed, 552 insertions(+), 120 deletions(-)

Approvals:
  Aude: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/Wikibase.classes.php b/repo/Wikibase.classes.php
index f845560..ada1583 100644
--- a/repo/Wikibase.classes.php
+++ b/repo/Wikibase.classes.php
@@ -127,6 +127,7 @@
                'SpecialEntityData' => 
'includes/specials/SpecialEntityData.php',
                'Wikibase\LinkedData\EntityDataSerializationService' => 
'includes/LinkedData/EntityDataSerializationService.php',
                'Wikibase\LinkedData\EntityDataRequestHandler' => 
'includes/LinkedData/EntityDataRequestHandler.php',
+               'Wikibase\LinkedData\EntityDataUriManager' => 
'includes/LinkedData/EntityDataUriManager.php',
 
                // includes/store
                'Wikibase\EntityPerPage' => 'includes/store/EntityPerPage.php',
diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index b083e84..288ba83 100644
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -201,6 +201,7 @@
 
                        'LinkedData/EntityDataSerializationService',
                        'LinkedData/EntityDataRequestHandler',
+                       'LinkedData/EntityDataUriManager',
 
                        'rdf/RdfBuilder',
                        'rdf/RdfSerializer',
diff --git a/repo/includes/LinkedData/EntityDataRequestHandler.php 
b/repo/includes/LinkedData/EntityDataRequestHandler.php
index c7892bf..4e20550 100644
--- a/repo/includes/LinkedData/EntityDataRequestHandler.php
+++ b/repo/includes/LinkedData/EntityDataRequestHandler.php
@@ -2,6 +2,7 @@
 namespace Wikibase\LinkedData;
 
 use DataTypes\DataTypeFactory;
+use ValueParsers\ParseException;
 use Wikibase\EntityContentFactory;
 use Wikibase\EntityId;
 use Wikibase\HttpAcceptNegotiator;
@@ -42,6 +43,11 @@
        protected $serializationService;
 
        /**
+        * @var EntityDataUriManager
+        */
+       protected $uriManager;
+
+       /**
         * @var EntityIdParser
         */
        protected $entityIdParser;
@@ -74,7 +80,7 @@
        /**
         * @since 0.4
         *
-        * @param Title                          $interfaceTitle for building 
canonical URLs
+        * @param EntityDataUriManager           $uriManager
         * @param EntityContentFactory           $entityContentFactory
         * @param EntityIdParser                 $entityIdParser
         * @param EntityIdFormatter              $entityIdFormatter
@@ -85,7 +91,7 @@
         * @param string|null                    $frameOptionsHeader for 
X-Frame-Options
         */
        public function __construct(
-               Title $interfaceTitle,
+               EntityDataUriManager $uriManager,
                EntityContentFactory $entityContentFactory,
                EntityIdParser $entityIdParser,
                EntityIdFormatter $entityIdFormatter,
@@ -95,7 +101,7 @@
                $useSquids,
                $frameOptionsHeader
        ) {
-               $this->interfaceTitle = $interfaceTitle;
+               $this->uriManager = $uriManager;
                $this->entityContentFactory = $entityContentFactory;
                $this->entityIdParser = $entityIdParser;
                $this->entityIdFormatter = $entityIdFormatter;
@@ -165,29 +171,17 @@
         */
        public function handleRequest( $doc, WebRequest $request, OutputPage 
$output ) {
                $revision = 0;
-               $format = '';
 
-               $requestedDoc = $doc;
+               list( $id, $format ) = $this->uriManager->parseDocName( $doc );
 
-               // get format from $doc or request param
-               if ( preg_match( '#\.([-./\w]+)$#', $doc, $m ) ) {
-                       $doc = preg_replace( '#\.([-./\w]+)$#', '', $doc );
-                       $format = $m[1];
-               }
-
+               // get entity id and format from request parameter
                $format = $request->getText( 'format', $format );
-
-               //TODO: malformed revision IDs should trigger a code 400
-
+               $id = $request->getText( 'id', $id );
                $revision = $request->getInt( 'oldid', $revision );
                $revision = $request->getInt( 'revision', $revision );
+               //TODO: malformed revision IDs should trigger a code 400
 
-               // get entity from remaining $doc or request param
-               $id = $doc;
-               $id = $request->getText( 'id', $id );
-
-               // If there is no ID, show an HTML form
-               // TODO: Don't do this if HTML is not acceptable according to 
HTTP headers.
+               // If there is no ID, fail
                if ( $id === null || $id === '' ) {
                        //TODO: different error message?
                        throw new \HttpError( 400, wfMessage( 
'wikibase-entitydata-bad-id' )->params( $id ) );
@@ -195,13 +189,13 @@
 
                try {
                        $entityId = $this->entityIdParser->parse( $id );
-               } catch ( \ValueParsers\ParseException $ex ) {
+               } catch ( ParseException $ex ) {
                        throw new \HttpError( 400, wfMessage( 
'wikibase-entitydata-bad-id' )->params( $id ) );
                }
 
                //XXX: allow for logged in users only?
                if ( $request->getText( 'action' ) === 'purge' ) {
-                       $this->purge( $entityId, $format, $revision );
+                       $this->purgeWebCache( $entityId );
                        //XXX: Now what? Proceed to show the data?
                }
 
@@ -218,12 +212,12 @@
                // we should know the format now.
                assert( $format !== null && $format !== '' );
 
-               if ( $requestedDoc !== null && $requestedDoc !== '' ) {
+               if ( $doc !== null && $doc !== '' ) {
                        // if subpage syntax is used, always enforce the 
canonical form
-                       $canonicalDoc = $this->getDocName( $entityId, $format, 
$revision );
+                       $canonicalDoc = $this->uriManager->getDocName( 
$entityId, $format );
 
-                       if ( $requestedDoc !== $canonicalDoc ) {
-                               $url = $this->getCanonicalUrl( $entityId, 
$format, $revision );
+                       if ( $doc !== $canonicalDoc ) {
+                               $url = $this->uriManager->getDocUrl( $entityId, 
$format, $revision );
                                $output->redirect( $url, 301 );
                                return;
                        }
@@ -231,7 +225,7 @@
 
                // if the format is HTML, redirect to the entity's wiki page
                if ( $format === 'html' ) {
-                       $url = $this->getCanonicalUrl( $entityId, 'html', 
$revision );
+                       $url = $this->uriManager->getDocUrl( $entityId, 'html', 
$revision );
                        $output->redirect( $url, 303 );
                        return;
                }
@@ -270,72 +264,14 @@
         * Purges the entity data identified by the doc parameter from any HTTP 
caches.
         * Does nothing if $wgUseSquid is not set.
         *
-        * @todo: how to test this?
-        *
         * @param EntityId $id       The entity
-        * @param string   $format   The (normalized) format name, or ''
         */
-       public function purge( EntityId $id, $format = '' ) {
-               if ( $this->useSquids ) {
-                       //TODO: Purge all formats based on the ID, instead of 
just the one currently requested.
-                       //TODO: Also purge when an entity gets edited, using 
the new TitleSquidURLs hook.
-                       $title = $this->getDocTitle( $id, $format );
+       public function purgeWebCache( EntityId $id ) {
+               $urls = $this->uriManager->getCacheableUrls( $id );
 
-                       $urls = array();
-                       $urls[] = $title->getInternalURL();
-
-                       $u = new SquidUpdate( $urls );
-                       $u->doUpdate();
-               }
-       }
-
-       /**
-        * Returns the canonical subpage name used to address a given set
-        * of entity data.
-        *
-        * @param EntityId $id       The entity
-        * @param string   $format   The (normalized) format name, or ''
-        *
-        * @return string
-        */
-       public function getDocName( EntityId $id, $format = '' ) {
-               $doc = $this->entityIdFormatter->format( $id );
-
-               //TODO: Use upper case everywhere. EntityIdFormatter should do 
the right thing.
-               $doc = strtoupper( $doc );
-
-               if ( $format !== null && $format !== '' ) {
-                       $ext = $this->serializationService->getExtension( 
$format );
-
-                       if ( $ext === null ) {
-                               // if no extension is known, use the format 
name as the extension
-                               $ext = $format;
-                       }
-
-                       $doc .= '.' . $ext;
-               }
-
-               return $doc;
-       }
-
-       /**
-        * Returns a Title representing the given document.
-        *
-        * @param EntityId $id       The entity
-        * @param string   $format   The (normalized) format name, or ''
-        *
-        * @return Title
-        */
-       public function getDocTitle( EntityId $id, $format = '' ) {
-               $doc = $this->getDocName( $id, $format );
-
-               $name = $this->interfaceTitle->getPrefixedText();
-               if ( $doc !== null && $doc !== '' ) {
-                       $name .= '/' . $doc;
-               }
-
-               $title = Title::newFromText( $name );
-               return $title;
+               //TODO: use a factory or service, so we can mock & test this
+               $update = new SquidUpdate( $urls );
+               $update->doUpdate();
        }
 
        /**
@@ -383,40 +319,16 @@
 
                $format = $this->getCanonicalFormat( $format );
 
-               $url = $this->getCanonicalUrl( $id, $format, $revision );
+               $url = $this->uriManager->getDocUrl( $id, $format, $revision );
                $output->redirect( $url, 303 );
                return;
        }
 
        /**
-        * Returns the canonical URL for the given set of entity data.
-        *
-        * @param EntityId $id       The entity
-        * @param string   $format   The (normalized) format name, or ''
-        * @param int      $revision The revision ID (use 0 for current)
-        *
-        * @return string
-        */
-       public function getCanonicalUrl( EntityId $id, $format = '', $revision 
= 0 ) {
-               if ( $format === 'html' ) {
-                       //\Wikibase\EntityContentFactory::singleton()
-                       $title = $this->entityContentFactory->getTitleForId( 
$id );
                } else {
                        $title = $this->getDocTitle( $id, $format );
                }
 
-               $params = '';
-
-               if ( $revision > 0 ) {
-                       // Ugh, internal knowledge. Doesn't title have a better 
way to do this?
-                       $params = 'oldid=' . $revision;
-               }
-
-               $url = $title->getFullURL( $params );
-               return $url;
-       }
-
-       /**
         * Output entity data.
         *
         * @param WebRequest $request
@@ -439,6 +351,7 @@
 
                $page = $entity->getWikiPage();
 
+               // TODO: use EntityRevisionLookup to get the entity revision, 
pending change I762535bda41
                if ( $revision > 0 ) {
                        // get the desired revision
                        $rev = Revision::newFromId( $revision );
@@ -499,6 +412,7 @@
        public function outputData( WebRequest $request, WebResponse $response, 
$data, $contentType, Revision $revision = null ) {
                // NOTE: similar code as in RawAction::onView, keep in sync.
 
+               //FIXME: do not cache if revision was requested explicitly!
                $maxage = $request->getInt( 'maxage', $this->maxAge );
                $smaxage = $request->getInt( 'smaxage', $this->maxAge );
 
diff --git a/repo/includes/LinkedData/EntityDataUriManager.php 
b/repo/includes/LinkedData/EntityDataUriManager.php
new file mode 100644
index 0000000..9d69114
--- /dev/null
+++ b/repo/includes/LinkedData/EntityDataUriManager.php
@@ -0,0 +1,226 @@
+<?php
+namespace Wikibase\LinkedData;
+
+use Wikibase\EntityContentFactory;
+use Wikibase\EntityId;
+use Wikibase\Lib\EntityIdParser;
+use Wikibase\Lib\EntityIdFormatter;
+use Title;
+use OutputPage;
+use SquidUpdate;
+
+/**
+ * Manages URIs for the linked data interface
+ *
+ * @since 0.4
+ *
+ * @file
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityDataUriManager {
+
+       /**
+        * @var \Title
+        */
+       protected $interfaceTitle;
+
+       /**
+        * @var String[]
+        */
+       protected $supportedExtensions;
+
+       /**
+        * @var EntityIdFormatter
+        */
+       protected $entityIdFormatter;
+
+       /**
+        * @var EntityContentFactory
+        */
+       protected $entityContentFactory;
+
+       /**
+        * @since 0.4
+        *
+        * @param \Title               $interfaceTitle
+        * @param string[]             $supportedExtensions an associative 
Array mapping canonical format names to file extensions.
+        * @param EntityIdFormatter    $entityIdFormatter
+        * @param EntityContentFactory $entityContentFactory
+        */
+       public function __construct(
+               Title $interfaceTitle,
+               $supportedExtensions,
+               EntityIdFormatter $entityIdFormatter,
+               EntityContentFactory $entityContentFactory
+       ) {
+               $this->interfaceTitle = $interfaceTitle;
+               $this->supportedExtensions = $supportedExtensions;
+               $this->entityIdFormatter = $entityIdFormatter;
+               $this->entityContentFactory = $entityContentFactory; //XXX: 
needed only for getTitleForId
+       }
+
+       /**
+        * @param string $format a canonical format name
+        *
+        * @return string|null a file extension (without the leading dot), or 
null.
+        */
+       public function getExtension( $format ) {
+               if ( $format === '' ) {
+                       // "no format" -> "no extension"
+                       return '';
+               }
+
+               if ( isset( $this->supportedExtensions[ $format ] ) ) {
+                       return $this->supportedExtensions[ $format ];
+               }
+
+               return null;
+       }
+
+       /**
+        * @param string $extension file extension
+        *
+        * @return string|null the canonical format name (or null)
+        */
+       public function getFormatName( $extension ) {
+               $extension = trim( strtolower( $extension ) );
+
+               if ( $extension === '' ) {
+                       // "no extension" -> "no format"
+                       return '';
+               }
+
+               if ( isset( $this->supportedExtensions[ $extension ] ) ) {
+                       return $extension; // already is a format name
+               }
+
+               $formats = array_flip( $this->supportedExtensions );
+
+               if ( isset( $formats[ $extension ] ) ) {
+                       return $formats[ $extension ];
+               }
+
+               return null;
+       }
+
+       /**
+        * Parser for the file-name like document name syntax for specifying an 
entity data document.
+        * This does not validate or interpret the ID or format, it just splits 
the string.
+        *
+        * @param string $doc
+        *
+        * @return string[] An array of two strings, array( $id, $format ).
+        */
+       public function parseDocName( $doc ) {
+               $format = '';
+
+               // get format from $doc or request param
+               if ( preg_match( '#\.([-./\w]+)$#', $doc, $m ) ) {
+                       $doc = preg_replace( '#\.([-./\w]+)$#', '', $doc );
+                       $format = $m[1];
+               }
+
+               return array(
+                       $doc,
+                       $format,
+               );
+       }
+
+       /**
+        * Returns the canonical subpage name used to address a given set
+        * of entity data.
+        *
+        * @param EntityId $id       The entity
+        * @param string|null   $format   The (normalized) format name, or ''
+        *
+        * @return string
+        */
+       public function getDocName( EntityId $id, $format = '' ) {
+               $doc = $this->entityIdFormatter->format( $id );
+
+               //Note: Use upper case everywhere. EntityIdFormatter should do 
the right thing.
+               $doc = strtoupper( $doc );
+
+               if ( $format !== null && $format !== '' ) {
+                       $ext = $this->getExtension( $format );
+
+                       if ( $ext === null ) {
+                               // if no extension is known, use the format 
name as the extension
+                               $ext = $format;
+                       }
+
+                       $doc .= '.' . $ext;
+               }
+
+               return $doc;
+       }
+
+       /**
+        * Returns a Title representing the given document.
+        *
+        * @param EntityId $id       The entity
+        * @param string|null   $format   The (normalized) format name, or ''
+        *
+        * @return Title
+        */
+       public function getDocTitle( EntityId $id, $format = '' ) {
+               if ( $format === 'html' ) {
+                       $title = $this->entityContentFactory->getTitleForId( 
$id );
+               } else {
+                       $doc = $this->getDocName( $id, $format );
+
+                       $name = $this->interfaceTitle->getPrefixedText();
+                       if ( $doc !== null && $doc !== '' ) {
+                               $name .= '/' . $doc;
+                       }
+
+                       $title = Title::newFromText( $name );
+               }
+
+               return $title;
+       }
+
+       /**
+        * Returns a Title representing the given document.
+        *
+        * @param EntityId    $id       The entity
+        * @param string|null $format   The (normalized) format name, or ''
+        * @param int         $revision
+        *
+        * @return Title
+        */
+       public function getDocUrl( EntityId $id, $format = '', $revision = 0 ) {
+               $params = '';
+
+               if ( $revision > 0 ) {
+                       $params = 'oldid=' . $revision;
+               }
+
+               $title = $this->getDocTitle( $id, $format );
+               $url = $title->getFullURL( $params );
+               return $url;
+       }
+
+       /**
+        * Returns a list of all cacheable URLs for all the formats of
+        * the given entity.
+        *
+        * @param EntityId $id
+        *
+        * @return string[]
+        */
+       public function getCacheableUrls( EntityId $id ) {
+               $urls = array();
+
+               foreach ( $this->supportedExtensions as $format => $ext ) {
+                       $title = $this->getDocTitle( $id, $format );
+                       $urls[] = $title->getInternalURL();
+               }
+
+               return $urls;
+       }
+
+}
diff --git a/repo/includes/specials/SpecialEntityData.php 
b/repo/includes/specials/SpecialEntityData.php
index 6a400a1..03aaf5c 100644
--- a/repo/includes/specials/SpecialEntityData.php
+++ b/repo/includes/specials/SpecialEntityData.php
@@ -7,6 +7,7 @@
 use Wikibase\EntityContentFactory;
 use Wikibase\LinkedData\EntityDataRequestHandler;
 use Wikibase\LinkedData\EntityDataSerializationService;
+use Wikibase\LinkedData\EntityDataUriManager;
 use Wikibase\RdfSerializer;
 
 /**
@@ -58,7 +59,7 @@
                $entityIdParser = $repo->getEntityIdParser();
                $entityIdFormatter = $repo->getIdFormatter();
 
-               $service = new EntityDataSerializationService(
+               $serializationService = new EntityDataSerializationService(
                        $repo->getRdfBaseURI(),
                        $this->getTitle()->getCanonicalURL() . '/',
                        \Wikibase\StoreFactory::getStore()->getEntityLookup(),
@@ -68,16 +69,34 @@
 
                $maxAge = \Wikibase\Settings::get( 'dataSquidMaxage' );
                $formats = \Wikibase\Settings::get( 'entityDataFormats' );
-               $service->setFormatWhiteList( $formats );
+               $serializationService->setFormatWhiteList( $formats );
 
                $defaultFormat = empty( $formats ) ? 'html' : $formats[0];
 
-               $this->requestHandler = new EntityDataRequestHandler(
+               // build a mapping of formats to file extensions and include 
HTML
+               $supportedExtensions = array();
+               $supportedExtensions['html'] = 'html';
+               foreach ( $serializationService->getSupportedFormats() as 
$format ) {
+                       $ext = $serializationService->getExtension( $format );
+
+                       if ( $ext !== null ) {
+                               $supportedExtensions[$format] = $ext;
+                       }
+               }
+
+               $uriManager = new EntityDataUriManager(
                        $this->getTitle(),
+                       $supportedExtensions,
+                       $entityIdFormatter,
+                       $entityContentFactory
+               );
+               
+               $this->requestHandler = new EntityDataRequestHandler(
+                       $uriManager,
                        $entityContentFactory,
                        $entityIdParser,
                        $entityIdFormatter,
-                       $service,
+                       $serializationService,
                        $defaultFormat,
                        $maxAge,
                        $wgUseSquid,
diff --git 
a/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php 
b/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
index 896778c..fe20662 100644
--- a/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
+++ b/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
@@ -14,6 +14,7 @@
 use Wikibase\Lib\EntityIdParser;
 use Wikibase\LinkedData\EntityDataSerializationService;
 use Wikibase\LinkedData\EntityDataRequestHandler;
+use Wikibase\LinkedData\EntityDataUriManager;
 use Wikibase\Property;
 
 /**
@@ -127,9 +128,28 @@
                        )
                );
 
+               $extensions = array(
+                       // using the API
+                       'json' => 'json', // default
+                       'php' => 'php',
+                       'xml' => 'xml',
+
+                       // using easyRdf
+                       'rdfxml' => 'rdf',
+                       'n3' => 'n3',
+                       'turtle' => 'ttl',
+                       'ntriples' => 'n3',
+               );
+
+               $uriManager = new EntityDataUriManager(
+                       $this->interfaceTitle,
+                       $extensions,
+                       $idFormatter,
+                       $contentFactory
+               );
 
                $handler = new EntityDataRequestHandler(
-                       $this->interfaceTitle,
+                       $uriManager,
                        $contentFactory,
                        $idParser,
                        $idFormatter,
diff --git 
a/repo/tests/phpunit/includes/LinkedData/EntityDataUriManagerTest.php 
b/repo/tests/phpunit/includes/LinkedData/EntityDataUriManagerTest.php
new file mode 100644
index 0000000..c19623b
--- /dev/null
+++ b/repo/tests/phpunit/includes/LinkedData/EntityDataUriManagerTest.php
@@ -0,0 +1,251 @@
+<?php
+
+namespace Wikibase\Test;
+
+use DataTypes\DataTypeFactory;
+use Title;
+use ValueFormatters\FormatterOptions;
+use ValueParsers\ParserOptions;
+use Wikibase\EntityContentFactory;
+use Wikibase\Item;
+use Wikibase\Lib\EntityIdFormatter;
+use Wikibase\Lib\EntityIdParser;
+use Wikibase\LinkedData\EntityDataSerializationService;
+use Wikibase\LinkedData\EntityDataUriManager;
+use Wikibase\Property;
+
+/**
+ * @covers \Wikibase\EntityUriManager
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.4
+ *
+ * @ingroup WikibaseRepoTest
+ * @ingroup Test
+ *
+ * @group Database
+ * ^--- just because Title is a mess
+ *
+ * @group Wikibase
+ * @group WikibaseEntityData
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityDataUriManagerTest extends \MediaWikiTestCase {
+
+       /**
+        * @var EntityIdFormatter
+        */
+       protected $idFormatter;
+
+       /**
+        * @var EntityIdParser
+        */
+       protected $idParser;
+
+       public function setUp() {
+               parent::setUp();
+
+               $prefixes = array(
+                       Item::ENTITY_TYPE => 'q',
+                       Property::ENTITY_TYPE => 'p',
+               );
+
+               $this->idFormatter = new EntityIdFormatter( new 
FormatterOptions( array(
+                       EntityIdFormatter::OPT_PREFIX_MAP => $prefixes
+               ) ) );
+
+               $this->idParser = new EntityIdParser( new ParserOptions( array(
+                       EntityIdFormatter::OPT_PREFIX_MAP => array_flip( 
$prefixes )
+               ) ) );
+
+}
+
+       protected function makeUriManager() {
+               $contentFactory = new EntityContentFactory(
+                       $this->idFormatter,
+                       array(
+                               CONTENT_MODEL_WIKIBASE_ITEM,
+                               CONTENT_MODEL_WIKIBASE_PROPERTY
+                       )
+               );
+
+               $title = Title::newFromText( "Special:EntityDataUriManagerTest" 
);
+
+               $extensions = array(
+                       'text' => 'txt',
+                       'rdfxml' => 'rdf',
+               );
+
+               $uriManager = new EntityDataUriManager(
+                       $title,
+                       $extensions,
+                       $this->idFormatter,
+                       $contentFactory
+               );
+
+               return $uriManager;
+       }
+
+       public static function provideGetExtension() {
+               return array(
+                       array( 'text', 'txt' ),
+                       array( 'rdfxml', 'rdf' ),
+                       array( 'txt', null ),
+                       array( 'TEXT', null ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetExtension
+        */
+       public function testGetExtension( $format, $expected ) {
+               $uriManager = $this->makeUriManager();
+
+               $actual = $uriManager->getExtension( $format );
+               $this->assertEquals( $expected, $actual );
+       }
+
+       public static function provideGetFormatName() {
+               return array(
+                       array( 'txt', 'text' ),
+                       array( 'text', 'text' ),
+                       array( 'TEXT', 'text' ),
+                       array( 'TXT', 'text' ),
+                       array( 'xyz', null ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetFormatName
+        */
+       public function testGetFormatName( $extension, $expected ) {
+               $uriManager = $this->makeUriManager();
+
+               $actual = $uriManager->getFormatName( $extension );
+               $this->assertEquals( $expected, $actual );
+       }
+
+       public static function provideParseDocName() {
+               return array(
+                       array( '', array( '', '' ) ),
+                       array( 'foo', array( 'foo', '' ) ),
+                       array( 'foo.bar', array( 'foo', 'bar' ) ),
+               );
+       }
+
+       /**
+        * @dataProvider provideParseDocName
+        */
+       public function testParseDocName( $doc, $expected ) {
+               $uriManager = $this->makeUriManager();
+
+               $actual = $uriManager->parseDocName( $doc, $expected );
+               $this->assertEquals( $expected, $actual );
+       }
+
+       public static function provideGetDocName() {
+               return array(
+                       array( 'Q12', '', 'Q12' ),
+                       array( 'q12', null, 'Q12' ),
+                       array( 'Q12', 'text', 'Q12.txt' ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetDocName
+        */
+       public function testGetDocName( $id, $format, $expected ) {
+               $id = $this->idParser->parse( $id );
+
+               $uriManager = $this->makeUriManager();
+
+               $actual = $uriManager->getDocName( $id, $format );
+               $this->assertEquals( $expected, $actual );
+       }
+
+       public static function provideGetDocTitle() {
+               $title = Title::newFromText( "Special:EntityDataUriManagerTest" 
);
+               $base = $title->getPrefixedText();
+
+               return array(
+                       array( 'Q12', '', "$base/Q12" ),
+                       array( 'q12', null, "$base/Q12" ),
+                       array( 'Q12', 'text', "$base/Q12.txt" ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetDocTitle
+        */
+       public function testGetDocTitle( $id, $format, $expected ) {
+               $id = $this->idParser->parse( $id );
+
+               $uriManager = $this->makeUriManager();
+
+               $actual = $uriManager->getDocTitle( $id, $format );
+               $this->assertEquals( $expected, $actual->getPrefixedText() );
+       }
+
+       public static function provideGetDocUrl() {
+               return array(
+                       array( 'Q12', '', 0, '!Q12$!' ),
+                       array( 'q12', null, 0, '!Q12$!' ),
+                       array( 'q12', null, 2, '!Q12.*oldid=2$!' ),
+                       array( 'Q12', 'text', 0, '!Q12\.txt$!' ),
+                       array( 'Q12', 'text', 2, '!Q12\.txt.*oldid=2$!' ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetDocUrl
+        */
+       public function testGetDocUrl( $id, $format, $revision, $expectedExp ) {
+               $id = $this->idParser->parse( $id );
+
+               $uriManager = $this->makeUriManager();
+
+               $actual = $uriManager->getDocUrl( $id, $format, $revision );
+               $this->assertRegExp( $expectedExp, $actual );
+       }
+
+       public static function provideGetCacheableUrls() {
+               $title = Title::newFromText( "Special:EntityDataUriManagerTest" 
);
+               $base = $title->getFullURL();
+
+               return array(
+                       array( 'Q12', array(
+                               "$base/Q12.txt",
+                               "$base/Q12.rdf",
+                       ) ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetCacheableUrls
+        */
+       public function testGetCacheableUrls( $id, $expected ) {
+               $id = $this->idParser->parse( $id );
+
+               $uriManager = $this->makeUriManager();
+
+               $actual = $uriManager->getCacheableUrls( $id );
+               $this->assertEquals( $expected, $actual );
+       }
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I7298de0b9d6fd8eac150bf2b32c9d013b09af233
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to