From 86693df2470314e220c0f4d027e7d84174929209 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Fri, 16 Aug 2019 14:38:48 +1000 Subject: [PATCH] REST: call MediaWiki::preOutputCommit and doPostOutputShutdown As in api.php. Among other things, this enables profiling. Move EntryPoint test out of unit/ so that it passes. Use ob_start()/ob_end_clean() instead of assuming an output buffer is open, so that EntryPoint::execute() can be run from CLI mode. Change-Id: I38162a9eac6fd5acfed2035b87cac4a97ffd50d6 --- includes/Rest/EntryPoint.php | 24 +++++++++++++++---- .../includes/Rest/EntryPointTest.php | 9 +++++-- 2 files changed, 26 insertions(+), 7 deletions(-) rename tests/phpunit/{unit => }/includes/Rest/EntryPointTest.php (91%) diff --git a/includes/Rest/EntryPoint.php b/includes/Rest/EntryPoint.php index ae01ae46ac..a4959d1504 100644 --- a/includes/Rest/EntryPoint.php +++ b/includes/Rest/EntryPoint.php @@ -3,6 +3,7 @@ namespace MediaWiki\Rest; use ExtensionRegistry; +use MediaWiki; use MediaWiki\MediaWikiServices; use MediaWiki\Rest\BasicAccess\MWBasicAuthorizer; use RequestContext; @@ -16,6 +17,8 @@ class EntryPoint { private $webResponse; /** @var Router */ private $router; + /** @var RequestContext */ + private $context; public static function main() { // URL safety checks @@ -24,10 +27,12 @@ class EntryPoint { return; } + $context = RequestContext::getMain(); + // Set $wgTitle and the title in RequestContext, as in api.php global $wgTitle; $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' ); - RequestContext::getMain()->setTitle( $wgTitle ); + $context->setTitle( $wgTitle ); $services = MediaWikiServices::getInstance(); $conf = $services->getMainConfig(); @@ -42,7 +47,7 @@ class EntryPoint { 'cookiePrefix' => $conf->get( 'CookiePrefix' ) ] ); - $authorizer = new MWBasicAuthorizer( RequestContext::getMain()->getUser(), + $authorizer = new MWBasicAuthorizer( $context->getUser(), $services->getPermissionManager() ); global $IP; @@ -56,21 +61,24 @@ class EntryPoint { ); $entryPoint = new self( + $context, $request, $wgRequest->response(), $router ); $entryPoint->execute(); } - public function __construct( RequestInterface $request, WebResponse $webResponse, - Router $router + public function __construct( RequestContext $context, RequestInterface $request, + WebResponse $webResponse, Router $router ) { + $this->context = $context; $this->request = $request; $this->webResponse = $webResponse; $this->router = $router; } public function execute() { + ob_start(); $response = $this->router->execute( $this->request ); $this->webResponse->header( @@ -91,10 +99,13 @@ class EntryPoint { } // Clear all errors that might have been displayed if display_errors=On - ob_clean(); + ob_end_clean(); $stream = $response->getBody(); $stream->rewind(); + + MediaWiki::preOutputCommit( $this->context ); + if ( $stream instanceof CopyableStreamInterface ) { $stream->copyToStream( fopen( 'php://output', 'w' ) ); } else { @@ -106,5 +117,8 @@ class EntryPoint { echo $buffer; } } + + $mw = new MediaWiki; + $mw->doPostOutputShutdown( 'fast' ); } } diff --git a/tests/phpunit/unit/includes/Rest/EntryPointTest.php b/tests/phpunit/includes/Rest/EntryPointTest.php similarity index 91% rename from tests/phpunit/unit/includes/Rest/EntryPointTest.php rename to tests/phpunit/includes/Rest/EntryPointTest.php index a74c0cbf20..b599e9d6a4 100644 --- a/tests/phpunit/unit/includes/Rest/EntryPointTest.php +++ b/tests/phpunit/includes/Rest/EntryPointTest.php @@ -11,18 +11,21 @@ use MediaWiki\Rest\EntryPoint; use MediaWiki\Rest\RequestData; use MediaWiki\Rest\ResponseFactory; use MediaWiki\Rest\Router; +use RequestContext; use WebResponse; /** * @covers \MediaWiki\Rest\EntryPoint * @covers \MediaWiki\Rest\Router */ -class EntryPointTest extends \MediaWikiUnitTestCase { +class EntryPointTest extends \MediaWikiTestCase { private static $mockHandler; private function createRouter() { + global $IP; + return new Router( - [ __DIR__ . '/testRoutes.json' ], + [ "$IP/tests/phpunit/unit/includes/Rest/testRoutes.json" ], [], '/rest', new EmptyBagOStuff(), @@ -56,6 +59,7 @@ class EntryPointTest extends \MediaWikiUnitTestCase { ); $entryPoint = new EntryPoint( + RequestContext::getMain(), new RequestData( [ 'uri' => new Uri( '/rest/mock/EntryPoint/header' ) ] ), $webResponse, $this->createRouter() ); @@ -80,6 +84,7 @@ class EntryPointTest extends \MediaWikiUnitTestCase { */ public function testBodyRewind() { $entryPoint = new EntryPoint( + RequestContext::getMain(), new RequestData( [ 'uri' => new Uri( '/rest/mock/EntryPoint/bodyRewind' ) ] ), $this->createWebResponse(), $this->createRouter() ); -- 2.20.1