3 namespace MediaWiki\Rest
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Rest\BasicAccess\MWBasicAuthorizer
;
9 use MediaWiki\Rest\Validator\Validator
;
13 use Wikimedia\Message\ITextFormatter
;
16 /** @var RequestInterface */
18 /** @var WebResponse */
22 /** @var RequestContext */
25 public static function main() {
28 if ( !$wgRequest->checkUrlExtension() ) {
32 $context = RequestContext
::getMain();
34 // Set $wgTitle and the title in RequestContext, as in api.php
36 $wgTitle = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/rest.php' );
37 $context->setTitle( $wgTitle );
39 $services = MediaWikiServices
::getInstance();
40 $conf = $services->getMainConfig();
41 $objectFactory = $services->getObjectFactory();
43 if ( !$conf->get( 'EnableRestAPI' ) ) {
44 wfHttpError( 403, 'Access Denied',
45 'Set $wgEnableRestAPI to true to enable the experimental REST API' );
49 $request = new RequestFromGlobals( [
50 'cookiePrefix' => $conf->get( 'CookiePrefix' )
53 $responseFactory = new ResponseFactory( self
::getTextFormatters( $services ) );
55 // @phan-suppress-next-line PhanAccessMethodInternal
56 $authorizer = new MWBasicAuthorizer( $context->getUser(),
57 $services->getPermissionManager() );
59 // @phan-suppress-next-line PhanAccessMethodInternal
60 $restValidator = new Validator( $objectFactory, $request, RequestContext
::getMain()->getUser() );
64 [ "$IP/includes/Rest/coreRoutes.json" ],
65 ExtensionRegistry
::getInstance()->getAttribute( 'RestRoutes' ),
66 $conf->get( 'RestPath' ),
67 $services->getLocalServerObjectCache(),
74 $entryPoint = new self(
77 $wgRequest->response(),
79 $entryPoint->execute();
83 * Get a TextFormatter array from MediaWikiServices
85 * @param MediaWikiServices $services
86 * @return ITextFormatter[]
88 public static function getTextFormatters( MediaWikiServices
$services ) {
89 $langs = array_unique( [
90 $services->getMainConfig()->get( 'ContLang' )->getCode(),
94 $factory = $services->getMessageFormatterFactory();
95 foreach ( $langs as $lang ) {
96 $textFormatters[] = $factory->getTextFormatter( $lang );
98 return $textFormatters;
101 public function __construct( RequestContext
$context, RequestInterface
$request,
102 WebResponse
$webResponse, Router
$router
104 $this->context
= $context;
105 $this->request
= $request;
106 $this->webResponse
= $webResponse;
107 $this->router
= $router;
110 public function execute() {
112 $response = $this->router
->execute( $this->request
);
114 $this->webResponse
->header(
115 'HTTP/' . $response->getProtocolVersion() . ' ' .
116 $response->getStatusCode() . ' ' .
117 $response->getReasonPhrase() );
119 foreach ( $response->getRawHeaderLines() as $line ) {
120 $this->webResponse
->header( $line );
123 foreach ( $response->getCookies() as $cookie ) {
124 $this->webResponse
->setCookie(
128 $cookie['options'] );
131 // Clear all errors that might have been displayed if display_errors=On
134 $stream = $response->getBody();
137 MediaWiki
::preOutputCommit( $this->context
);
139 if ( $stream instanceof CopyableStreamInterface
) {
140 $stream->copyToStream( fopen( 'php://output', 'w' ) );
143 $buffer = $stream->read( 65536 );
144 if ( $buffer === '' ) {
152 $mw->doPostOutputShutdown( 'fast' );