Use TextFormatter in the REST API
[lhc/web/wiklou.git] / includes / Rest / EntryPoint.php
1 <?php
2
3 namespace MediaWiki\Rest;
4
5 use ExtensionRegistry;
6 use MediaWiki;
7 use MediaWiki\MediaWikiServices;
8 use MediaWiki\Rest\BasicAccess\MWBasicAuthorizer;
9 use MediaWiki\Rest\Validator\Validator;
10 use RequestContext;
11 use Title;
12 use WebResponse;
13 use Wikimedia\Message\ITextFormatter;
14
15 class EntryPoint {
16 /** @var RequestInterface */
17 private $request;
18 /** @var WebResponse */
19 private $webResponse;
20 /** @var Router */
21 private $router;
22 /** @var RequestContext */
23 private $context;
24
25 public static function main() {
26 // URL safety checks
27 global $wgRequest;
28 if ( !$wgRequest->checkUrlExtension() ) {
29 return;
30 }
31
32 $context = RequestContext::getMain();
33
34 // Set $wgTitle and the title in RequestContext, as in api.php
35 global $wgTitle;
36 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
37 $context->setTitle( $wgTitle );
38
39 $services = MediaWikiServices::getInstance();
40 $conf = $services->getMainConfig();
41 $objectFactory = $services->getObjectFactory();
42
43 if ( !$conf->get( 'EnableRestAPI' ) ) {
44 wfHttpError( 403, 'Access Denied',
45 'Set $wgEnableRestAPI to true to enable the experimental REST API' );
46 return;
47 }
48
49 $request = new RequestFromGlobals( [
50 'cookiePrefix' => $conf->get( 'CookiePrefix' )
51 ] );
52
53 $responseFactory = new ResponseFactory( self::getTextFormatters( $services ) );
54
55 // @phan-suppress-next-line PhanAccessMethodInternal
56 $authorizer = new MWBasicAuthorizer( $context->getUser(),
57 $services->getPermissionManager() );
58
59 // @phan-suppress-next-line PhanAccessMethodInternal
60 $restValidator = new Validator( $objectFactory, $request, RequestContext::getMain()->getUser() );
61
62 global $IP;
63 $router = new Router(
64 [ "$IP/includes/Rest/coreRoutes.json" ],
65 ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
66 $conf->get( 'RestPath' ),
67 $services->getLocalServerObjectCache(),
68 $responseFactory,
69 $authorizer,
70 $objectFactory,
71 $restValidator
72 );
73
74 $entryPoint = new self(
75 $context,
76 $request,
77 $wgRequest->response(),
78 $router );
79 $entryPoint->execute();
80 }
81
82 /**
83 * Get a TextFormatter array from MediaWikiServices
84 *
85 * @param MediaWikiServices $services
86 * @return ITextFormatter[]
87 */
88 public static function getTextFormatters( MediaWikiServices $services ) {
89 $langs = array_unique( [
90 $services->getMainConfig()->get( 'ContLang' )->getCode(),
91 'en'
92 ] );
93 $textFormatters = [];
94 $factory = $services->getMessageFormatterFactory();
95 foreach ( $langs as $lang ) {
96 $textFormatters[] = $factory->getTextFormatter( $lang );
97 }
98 return $textFormatters;
99 }
100
101 public function __construct( RequestContext $context, RequestInterface $request,
102 WebResponse $webResponse, Router $router
103 ) {
104 $this->context = $context;
105 $this->request = $request;
106 $this->webResponse = $webResponse;
107 $this->router = $router;
108 }
109
110 public function execute() {
111 ob_start();
112 $response = $this->router->execute( $this->request );
113
114 $this->webResponse->header(
115 'HTTP/' . $response->getProtocolVersion() . ' ' .
116 $response->getStatusCode() . ' ' .
117 $response->getReasonPhrase() );
118
119 foreach ( $response->getRawHeaderLines() as $line ) {
120 $this->webResponse->header( $line );
121 }
122
123 foreach ( $response->getCookies() as $cookie ) {
124 $this->webResponse->setCookie(
125 $cookie['name'],
126 $cookie['value'],
127 $cookie['expiry'],
128 $cookie['options'] );
129 }
130
131 // Clear all errors that might have been displayed if display_errors=On
132 ob_end_clean();
133
134 $stream = $response->getBody();
135 $stream->rewind();
136
137 MediaWiki::preOutputCommit( $this->context );
138
139 if ( $stream instanceof CopyableStreamInterface ) {
140 $stream->copyToStream( fopen( 'php://output', 'w' ) );
141 } else {
142 while ( true ) {
143 $buffer = $stream->read( 65536 );
144 if ( $buffer === '' ) {
145 break;
146 }
147 echo $buffer;
148 }
149 }
150
151 $mw = new MediaWiki;
152 $mw->doPostOutputShutdown( 'fast' );
153 }
154 }