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 $authorizer = new MWBasicAuthorizer( $context->getUser(),
51 $services->getPermissionManager() );
55 [ "$IP/includes/Rest/coreRoutes.json" ],
56 ExtensionRegistry
::getInstance()->getAttribute( 'RestRoutes' ),
57 $conf->get( 'RestPath' ),
58 $services->getLocalServerObjectCache(),
63 $entryPoint = new self(
66 $wgRequest->response(),
68 $entryPoint->execute();
71 public function __construct( RequestContext
$context, RequestInterface
$request,
72 WebResponse
$webResponse, Router
$router
74 $this->context
= $context;
75 $this->request
= $request;
76 $this->webResponse
= $webResponse;
77 $this->router
= $router;
80 public function execute() {
82 $response = $this->router
->execute( $this->request
);
84 $this->webResponse
->header(
85 'HTTP/' . $response->getProtocolVersion() . ' ' .
86 $response->getStatusCode() . ' ' .
87 $response->getReasonPhrase() );
89 foreach ( $response->getRawHeaderLines() as $line ) {
90 $this->webResponse
->header( $line );
93 foreach ( $response->getCookies() as $cookie ) {
94 $this->webResponse
->setCookie(
101 // Clear all errors that might have been displayed if display_errors=On
104 $stream = $response->getBody();
107 MediaWiki
::preOutputCommit( $this->context
);
109 if ( $stream instanceof CopyableStreamInterface
) {
110 $stream->copyToStream( fopen( 'php://output', 'w' ) );
113 $buffer = $stream->read( 65536 );
114 if ( $buffer === '' ) {
122 $mw->doPostOutputShutdown( 'fast' );