3 namespace MediaWiki\Rest
;
6 use MediaWiki\MediaWikiServices
;
7 use MediaWiki\Rest\BasicAccess\MWBasicAuthorizer
;
13 /** @var RequestInterface */
15 /** @var WebResponse */
20 public static function main() {
23 if ( !$wgRequest->checkUrlExtension() ) {
27 // Set $wgTitle and the title in RequestContext, as in api.php
29 $wgTitle = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/rest.php' );
30 RequestContext
::getMain()->setTitle( $wgTitle );
32 $services = MediaWikiServices
::getInstance();
33 $conf = $services->getMainConfig();
35 if ( !$conf->get( 'EnableRestAPI' ) ) {
36 wfHttpError( 403, 'Access Denied',
37 'Set $wgEnableRestAPI to true to enable the experimental REST API' );
41 $request = new RequestFromGlobals( [
42 'cookiePrefix' => $conf->get( 'CookiePrefix' )
45 $authorizer = new MWBasicAuthorizer( RequestContext
::getMain()->getUser(),
46 $services->getPermissionManager() );
50 [ "$IP/includes/Rest/coreRoutes.json" ],
51 ExtensionRegistry
::getInstance()->getAttribute( 'RestRoutes' ),
52 $conf->get( 'RestPath' ),
53 $services->getLocalServerObjectCache(),
58 $entryPoint = new self(
60 $wgRequest->response(),
62 $entryPoint->execute();
65 public function __construct( RequestInterface
$request, WebResponse
$webResponse,
68 $this->request
= $request;
69 $this->webResponse
= $webResponse;
70 $this->router
= $router;
73 public function execute() {
74 $response = $this->router
->execute( $this->request
);
76 $this->webResponse
->header(
77 'HTTP/' . $response->getProtocolVersion() . ' ' .
78 $response->getStatusCode() . ' ' .
79 $response->getReasonPhrase() );
81 foreach ( $response->getRawHeaderLines() as $line ) {
82 $this->webResponse
->header( $line );
85 foreach ( $response->getCookies() as $cookie ) {
86 $this->webResponse
->setCookie(
93 $stream = $response->getBody();
95 if ( $stream instanceof CopyableStreamInterface
) {
96 $stream->copyToStream( fopen( 'php://output', 'w' ) );
99 $buffer = $stream->read( 65536 );
100 if ( $buffer === '' ) {