3 namespace MediaWiki\Rest
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Rest\BasicAccess\MWBasicAuthorizer
;
9 use MediaWiki\Rest\Validator\Validator
;
15 /** @var RequestInterface */
17 /** @var WebResponse */
21 /** @var RequestContext */
24 public static function main() {
27 if ( !$wgRequest->checkUrlExtension() ) {
31 $context = RequestContext
::getMain();
33 // Set $wgTitle and the title in RequestContext, as in api.php
35 $wgTitle = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/rest.php' );
36 $context->setTitle( $wgTitle );
38 $services = MediaWikiServices
::getInstance();
39 $conf = $services->getMainConfig();
40 $objectFactory = $services->getObjectFactory();
42 if ( !$conf->get( 'EnableRestAPI' ) ) {
43 wfHttpError( 403, 'Access Denied',
44 'Set $wgEnableRestAPI to true to enable the experimental REST API' );
48 $request = new RequestFromGlobals( [
49 'cookiePrefix' => $conf->get( 'CookiePrefix' )
52 // @phan-suppress-next-line PhanAccessMethodInternal
53 $authorizer = new MWBasicAuthorizer( $context->getUser(),
54 $services->getPermissionManager() );
56 // @phan-suppress-next-line PhanAccessMethodInternal
57 $restValidator = new Validator( $objectFactory, $request, RequestContext
::getMain()->getUser() );
61 [ "$IP/includes/Rest/coreRoutes.json" ],
62 ExtensionRegistry
::getInstance()->getAttribute( 'RestRoutes' ),
63 $conf->get( 'RestPath' ),
64 $services->getLocalServerObjectCache(),
71 $entryPoint = new self(
74 $wgRequest->response(),
76 $entryPoint->execute();
79 public function __construct( RequestContext
$context, RequestInterface
$request,
80 WebResponse
$webResponse, Router
$router
82 $this->context
= $context;
83 $this->request
= $request;
84 $this->webResponse
= $webResponse;
85 $this->router
= $router;
88 public function execute() {
90 $response = $this->router
->execute( $this->request
);
92 $this->webResponse
->header(
93 'HTTP/' . $response->getProtocolVersion() . ' ' .
94 $response->getStatusCode() . ' ' .
95 $response->getReasonPhrase() );
97 foreach ( $response->getRawHeaderLines() as $line ) {
98 $this->webResponse
->header( $line );
101 foreach ( $response->getCookies() as $cookie ) {
102 $this->webResponse
->setCookie(
106 $cookie['options'] );
109 // Clear all errors that might have been displayed if display_errors=On
112 $stream = $response->getBody();
115 MediaWiki
::preOutputCommit( $this->context
);
117 if ( $stream instanceof CopyableStreamInterface
) {
118 $stream->copyToStream( fopen( 'php://output', 'w' ) );
121 $buffer = $stream->read( 65536 );
122 if ( $buffer === '' ) {
130 $mw->doPostOutputShutdown( 'fast' );