wiki.techinc.nl/includes/Rest/EntryPoint.php
Aaron Schulz 4f11b61454 Avoid using "enqueue" mode for deferred updates in doPostOutputShutdown
Set appropriate headers and flush the output as needed to avoid blocking
the client on post-send updates for the stock apache2 server scenario.
Several cases have bits of header logic to avoid delay:

a) basic GET/POST requests that succeed (e.g. HTTP 2XX)
b) requests that fail with errors (e.g. HTTP 500)
c) If-Modified-Since requests (e.g. HTTP 304)
d) HEAD requests

This last two still block on deferred updates, so schedulePostSendJobs()
does not trigger on them as a form of mitigation. Slow deferred updates
should only trigger on POST anyway (inline and redirect responses are
OK), so this should not be much of a problem.

Deprecate triggerJobs() and implement post-send job runs as a deferred.
This makes it easy to check for the existence of post-send updates by
calling DeferredUpdates::pendingUpdatesCount() after the pre-send stage.
Also, avoid running jobs on requests that had exceptions. Relatedly,
remove $mode option from restInPeace() and doPostOutputShutdown()
Only one caller was using the non-default options.

Bug: T206283
Change-Id: I2dd2b71f1ced0f4ef8b16ff41ffb23bb5b4c7028
2019-09-30 22:59:59 +00:00

158 lines
3.8 KiB
PHP

<?php
namespace MediaWiki\Rest;
use ExtensionRegistry;
use MediaWiki;
use MediaWiki\MediaWikiServices;
use MediaWiki\Rest\BasicAccess\MWBasicAuthorizer;
use MediaWiki\Rest\Validator\Validator;
use RequestContext;
use Title;
use WebResponse;
use Wikimedia\Message\ITextFormatter;
class EntryPoint {
/** @var RequestInterface */
private $request;
/** @var WebResponse */
private $webResponse;
/** @var Router */
private $router;
/** @var RequestContext */
private $context;
public static function main() {
// URL safety checks
global $wgRequest;
if ( !$wgRequest->checkUrlExtension() ) {
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' );
$context->setTitle( $wgTitle );
$services = MediaWikiServices::getInstance();
$conf = $services->getMainConfig();
$objectFactory = $services->getObjectFactory();
if ( !$conf->get( 'EnableRestAPI' ) ) {
wfHttpError( 403, 'Access Denied',
'Set $wgEnableRestAPI to true to enable the experimental REST API' );
return;
}
$request = new RequestFromGlobals( [
'cookiePrefix' => $conf->get( 'CookiePrefix' )
] );
$responseFactory = new ResponseFactory( self::getTextFormatters( $services ) );
// @phan-suppress-next-line PhanAccessMethodInternal
$authorizer = new MWBasicAuthorizer( $context->getUser(),
$services->getPermissionManager() );
// @phan-suppress-next-line PhanAccessMethodInternal
$restValidator = new Validator( $objectFactory,
$services->getPermissionManager(),
$request,
RequestContext::getMain()->getUser()
);
global $IP;
$router = new Router(
[ "$IP/includes/Rest/coreRoutes.json" ],
ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
$conf->get( 'RestPath' ),
$services->getLocalServerObjectCache(),
$responseFactory,
$authorizer,
$objectFactory,
$restValidator
);
$entryPoint = new self(
$context,
$request,
$wgRequest->response(),
$router );
$entryPoint->execute();
}
/**
* Get a TextFormatter array from MediaWikiServices
*
* @param MediaWikiServices $services
* @return ITextFormatter[]
*/
public static function getTextFormatters( MediaWikiServices $services ) {
$langs = array_unique( [
$services->getMainConfig()->get( 'ContLang' )->getCode(),
'en'
] );
$textFormatters = [];
$factory = $services->getMessageFormatterFactory();
foreach ( $langs as $lang ) {
$textFormatters[] = $factory->getTextFormatter( $lang );
}
return $textFormatters;
}
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(
'HTTP/' . $response->getProtocolVersion() . ' ' .
$response->getStatusCode() . ' ' .
$response->getReasonPhrase() );
foreach ( $response->getRawHeaderLines() as $line ) {
$this->webResponse->header( $line );
}
foreach ( $response->getCookies() as $cookie ) {
$this->webResponse->setCookie(
$cookie['name'],
$cookie['value'],
$cookie['expiry'],
$cookie['options'] );
}
// Clear all errors that might have been displayed if display_errors=On
ob_end_clean();
$stream = $response->getBody();
$stream->rewind();
MediaWiki::preOutputCommit( $this->context );
if ( $stream instanceof CopyableStreamInterface ) {
$stream->copyToStream( fopen( 'php://output', 'w' ) );
} else {
while ( true ) {
$buffer = $stream->read( 65536 );
if ( $buffer === '' ) {
break;
}
echo $buffer;
}
}
$mw = new MediaWiki;
$mw->doPostOutputShutdown();
}
}