3 namespace MediaWiki\Rest
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Rest\BasicAccess\MWBasicAuthorizer
;
14 /** @var RequestInterface */
16 /** @var WebResponse */
20 /** @var RequestContext */
23 public static function main() {
26 if ( !$wgRequest->checkUrlExtension() ) {
30 $context = RequestContext
::getMain();
32 // Set $wgTitle and the title in RequestContext, as in api.php
34 $wgTitle = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/rest.php' );
35 $context->setTitle( $wgTitle );
37 $services = MediaWikiServices
::getInstance();
38 $conf = $services->getMainConfig();
40 if ( !$conf->get( 'EnableRestAPI' ) ) {
41 wfHttpError( 403, 'Access Denied',
42 'Set $wgEnableRestAPI to true to enable the experimental REST API' );
46 $request = new RequestFromGlobals( [
47 'cookiePrefix' => $conf->get( 'CookiePrefix' )
50 // @phan-suppress-next-line PhanAccessMethodInternal
51 $authorizer = new MWBasicAuthorizer( $context->getUser(),
52 $services->getPermissionManager() );
56 [ "$IP/includes/Rest/coreRoutes.json" ],
57 ExtensionRegistry
::getInstance()->getAttribute( 'RestRoutes' ),
58 $conf->get( 'RestPath' ),
59 $services->getLocalServerObjectCache(),
64 $entryPoint = new self(
67 $wgRequest->response(),
69 $entryPoint->execute();
72 public function __construct( RequestContext
$context, RequestInterface
$request,
73 WebResponse
$webResponse, Router
$router
75 $this->context
= $context;
76 $this->request
= $request;
77 $this->webResponse
= $webResponse;
78 $this->router
= $router;
81 public function execute() {
83 $response = $this->router
->execute( $this->request
);
85 $this->webResponse
->header(
86 'HTTP/' . $response->getProtocolVersion() . ' ' .
87 $response->getStatusCode() . ' ' .
88 $response->getReasonPhrase() );
90 foreach ( $response->getRawHeaderLines() as $line ) {
91 $this->webResponse
->header( $line );
94 foreach ( $response->getCookies() as $cookie ) {
95 $this->webResponse
->setCookie(
102 // Clear all errors that might have been displayed if display_errors=On
105 $stream = $response->getBody();
108 MediaWiki
::preOutputCommit( $this->context
);
110 if ( $stream instanceof CopyableStreamInterface
) {
111 $stream->copyToStream( fopen( 'php://output', 'w' ) );
114 $buffer = $stream->read( 65536 );
115 if ( $buffer === '' ) {
123 $mw->doPostOutputShutdown( 'fast' );