jenkins-bot has submitted this change and it was merged.
Change subject: Remove global state from EntityDataRequestHandler
......................................................................
Remove global state from EntityDataRequestHandler
Change-Id: Id902acdf384a7ef17da4ce68ecb06ba581f7ab24
---
M repo/includes/specials/EntityDataRequestHandler.php
M repo/includes/specials/SpecialEntityData.php
M repo/tests/phpunit/includes/specials/EntityDataRequestHandlerTest.php
3 files changed, 85 insertions(+), 40 deletions(-)
Approvals:
Aude: Looks good to me, approved
jenkins-bot: Verified
diff --git a/repo/includes/specials/EntityDataRequestHandler.php
b/repo/includes/specials/EntityDataRequestHandler.php
index 4b86b59..f45222d 100644
--- a/repo/includes/specials/EntityDataRequestHandler.php
+++ b/repo/includes/specials/EntityDataRequestHandler.php
@@ -17,7 +17,7 @@
*
* @since 0.4
*
- * @file
+ * @file
* @ingroup WikibaseRepo
*
* @licence GNU GPL v2+
@@ -58,15 +58,27 @@
protected $defaultFormat;
/**
+ * @var bool
+ */
+ protected $useSquids;
+
+ /**
+ * @var string|null
+ */
+ protected $frameOptionsHeader;
+
+ /**
* @since 0.4
*
- * @param Title $interfaceTitle
+ * @param Title $interfaceTitle for building
canonical URLs
* @param EntityContentFactory $entityContentFactory
* @param EntityIdParser $entityIdParser
* @param EntityIdFormatter $entityIdFormatter
* @param EntityDataSerializationService $serializationService
* @param string $defaultFormat
- * @param int $maxAge
+ * @param int $maxAge number of seconds to
cache entity data
+ * @param bool $useSquids do we have web
caches configured?
+ * @param string|null $frameOptionsHeader for
X-Frame-Options
*/
public function __construct(
Title $interfaceTitle,
@@ -75,7 +87,9 @@
EntityIdFormatter $entityIdFormatter,
EntityDataSerializationService $serializationService,
$defaultFormat,
- $maxAge
+ $maxAge,
+ $useSquids,
+ $frameOptionsHeader
) {
$this->interfaceTitle = $interfaceTitle;
$this->entityContentFactory = $entityContentFactory;
@@ -84,6 +98,8 @@
$this->serializationService = $serializationService;
$this->defaultFormat = $defaultFormat;
$this->maxAge = $maxAge;
+ $this->useSquids = $useSquids;
+ $this->frameOptionsHeader = $frameOptionsHeader;
}
/**
@@ -109,7 +125,7 @@
*
* @since 0.4
*
- * @param string $doc Document name, e.g. Q5 or Q5.json or Q5:33.xml
+ * @param string|null $doc Document name, e.g. Q5 or Q5.json or
Q5:33.xml
* @param WebRequest $request
*
* @return bool
@@ -197,23 +213,8 @@
$this->httpContentNegotiation( $request, $output,
$entityId, $revision );
return;
} else {
- $format = strtolower( $format );
-
- // if the format is HTML, redirect to the entity's wiki
page
- if ( $format === 'html' || $format === 'htm' || $format
=== 'text/html' ) {
- $url = $this->getCanonicalUrl( $entityId,
'html', $revision );
- $output->redirect( $url, 303 );
- return;
- }
-
- // normalize format name (note that HTML may not be
known to the service)
- $canonicalFormat =
$this->serializationService->getFormatName( $format );
-
- if ( $canonicalFormat === null ) {
- throw new \HttpError( 415, wfMessage(
'wikibase-entitydata-unsupported-format' )->params( $format ) );
- }
-
- $format = $canonicalFormat;
+ //NOTE: will trigger a 415 if the format is not
supported
+ $format = $this->getCanonicalFormat( $format );
}
// we should know the format now.
@@ -230,8 +231,41 @@
}
}
+ // if the format is HTML, redirect to the entity's wiki page
+ if ( $format === 'html' ) {
+ $url = $this->getCanonicalUrl( $entityId, 'html',
$revision );
+ $output->redirect( $url, 303 );
+ return;
+ }
+
$this->showData( $request, $output, $format, $entityId,
$revision );
- return;
+ }
+
+ /**
+ * Returns the canonical format name for the given format.
+ *
+ * @param string $format
+ *
+ * @return string
+ * @throws \HttpError code 415 if the format is not supported.
+ *
+ */
+ public function getCanonicalFormat( $format ) {
+ $format = strtolower( $format );
+
+ // we always support html, it's handled by the entity's wiki
page.
+ if ( $format === 'html' || $format === 'htm' || $format ===
'text/html' ) {
+ return 'html';
+ }
+
+ // normalize format name (note that HTML may not be known to
the service)
+ $canonicalFormat = $this->serializationService->getFormatName(
$format );
+
+ if ( $canonicalFormat === null ) {
+ throw new \HttpError( 415, wfMessage(
'wikibase-entitydata-unsupported-format' )->params( $format ) );
+ }
+
+ return $canonicalFormat;
}
/**
@@ -245,9 +279,7 @@
* @param int $revision The revision ID (use 0 for current)
*/
public function purge( EntityId $id, $format = '', $revision = 0 ) {
- global $wgUseSquid;
-
- if ( $wgUseSquid ) {
+ 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, $revision );
@@ -358,12 +390,7 @@
throw new \HttpError( 406, wfMessage(
'wikibase-entitydata-not-acceptable' )->params( $mimeTypes ) );
}
- if ( $format === 'text/html' ) {
- $format = 'html';
- } else {
- $format = $this->serializationService->getFormatName(
$format );
- assert( $format !== null );
- }
+ $format = $this->getCanonicalFormat( $format );
$url = $this->getCanonicalUrl( $id, $format, $revision );
$output->redirect( $url, 303 );
@@ -498,9 +525,8 @@
}
//Set X-Frame-Options API results (bug 39180)
- global $wgApiFrameOptions;
- if ( $wgApiFrameOptions ) {
- $response->header( "X-Frame-Options:
$wgApiFrameOptions" );
+ if ( $this->frameOptionsHeader !== null &&
$this->frameOptionsHeader !== '' ) {
+ $response->header( "X-Frame-Options:
$this->frameOptionsHeader" );
}
// allow the client to cache this
@@ -518,7 +544,7 @@
* Returns true iff RDF output is supported.
* @return bool
*/
- public static function isRdfSupported() {
- return EntityDataSerializationService::isRdfSupported();
+ public function isRdfSupported() {
+ return $this->serializationService->isRdfSupported();
}
}
diff --git a/repo/includes/specials/SpecialEntityData.php
b/repo/includes/specials/SpecialEntityData.php
index 097b29b..3b46b77 100644
--- a/repo/includes/specials/SpecialEntityData.php
+++ b/repo/includes/specials/SpecialEntityData.php
@@ -48,6 +48,8 @@
* This is poor man's inverse dependency injection.
*/
protected function initDependencies() {
+ global $wgUseSquid, $wgApiFrameOptions;
+
// Initialize serialization service.
// TODO: use reverse DI facility (global registry/factory)
$repo = \Wikibase\Repo\WikibaseRepo::getDefaultInstance();
@@ -77,7 +79,9 @@
$entityIdFormatter,
$service,
$defaultFormat,
- $maxAge
+ $maxAge,
+ $wgUseSquid,
+ $wgApiFrameOptions
);
}
diff --git
a/repo/tests/phpunit/includes/specials/EntityDataRequestHandlerTest.php
b/repo/tests/phpunit/includes/specials/EntityDataRequestHandlerTest.php
index aaed579..b003898 100644
--- a/repo/tests/phpunit/includes/specials/EntityDataRequestHandlerTest.php
+++ b/repo/tests/phpunit/includes/specials/EntityDataRequestHandlerTest.php
@@ -135,12 +135,26 @@
$idFormatter,
$service,
'json',
- 1800
+ 1800,
+ false,
+ null
);
return $handler;
}
- public static function injectIds( &$data, \Wikibase\Entity $entity ) {
+ /**
+ * Substitutes placeholders using the concrete values from the given
entity.
+ * Known placeholders are:
+ *
+ * {testitemid}, {lowertestitemid}, {testitemrev}, {testitemtimestamp}
+ *
+ * @param mixed $data The data in which to substitude placeholders.
+ * If this is an erray, injectIds is called on all elements
recursively.
+ * @param Entity $entity
+ *
+ * @todo: use EntityRevision once we have that
+ */
+ public static function injectIds( &$data, Entity $entity ) {
if ( is_array( $data ) ) {
foreach ( $data as $k => &$v ) {
self::injectIds( $v, $entity );
@@ -247,5 +261,6 @@
//TODO: test canHandleRequest
//TODO: test httpContentNegotiation
+ //TODO: test getCanonicalFormat
//TODO: test ALL the things!
}
--
To view, visit https://gerrit.wikimedia.org/r/66980
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Id902acdf384a7ef17da4ce68ecb06ba581f7ab24
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[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