SpecialRunJobs: optional output stats and status.
[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 RequestContext;
10 use Title;
11 use WebResponse;
12
13 class EntryPoint {
14 /** @var RequestInterface */
15 private $request;
16 /** @var WebResponse */
17 private $webResponse;
18 /** @var Router */
19 private $router;
20 /** @var RequestContext */
21 private $context;
22
23 public static function main() {
24 // URL safety checks
25 global $wgRequest;
26 if ( !$wgRequest->checkUrlExtension() ) {
27 return;
28 }
29
30 $context = RequestContext::getMain();
31
32 // Set $wgTitle and the title in RequestContext, as in api.php
33 global $wgTitle;
34 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
35 $context->setTitle( $wgTitle );
36
37 $services = MediaWikiServices::getInstance();
38 $conf = $services->getMainConfig();
39
40 if ( !$conf->get( 'EnableRestAPI' ) ) {
41 wfHttpError( 403, 'Access Denied',
42 'Set $wgEnableRestAPI to true to enable the experimental REST API' );
43 return;
44 }
45
46 $request = new RequestFromGlobals( [
47 'cookiePrefix' => $conf->get( 'CookiePrefix' )
48 ] );
49
50 // @phan-suppress-next-line PhanAccessMethodInternal
51 $authorizer = new MWBasicAuthorizer( $context->getUser(),
52 $services->getPermissionManager() );
53
54 global $IP;
55 $router = new Router(
56 [ "$IP/includes/Rest/coreRoutes.json" ],
57 ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
58 $conf->get( 'RestPath' ),
59 $services->getLocalServerObjectCache(),
60 new ResponseFactory,
61 $authorizer
62 );
63
64 $entryPoint = new self(
65 $context,
66 $request,
67 $wgRequest->response(),
68 $router );
69 $entryPoint->execute();
70 }
71
72 public function __construct( RequestContext $context, RequestInterface $request,
73 WebResponse $webResponse, Router $router
74 ) {
75 $this->context = $context;
76 $this->request = $request;
77 $this->webResponse = $webResponse;
78 $this->router = $router;
79 }
80
81 public function execute() {
82 ob_start();
83 $response = $this->router->execute( $this->request );
84
85 $this->webResponse->header(
86 'HTTP/' . $response->getProtocolVersion() . ' ' .
87 $response->getStatusCode() . ' ' .
88 $response->getReasonPhrase() );
89
90 foreach ( $response->getRawHeaderLines() as $line ) {
91 $this->webResponse->header( $line );
92 }
93
94 foreach ( $response->getCookies() as $cookie ) {
95 $this->webResponse->setCookie(
96 $cookie['name'],
97 $cookie['value'],
98 $cookie['expiry'],
99 $cookie['options'] );
100 }
101
102 // Clear all errors that might have been displayed if display_errors=On
103 ob_end_clean();
104
105 $stream = $response->getBody();
106 $stream->rewind();
107
108 MediaWiki::preOutputCommit( $this->context );
109
110 if ( $stream instanceof CopyableStreamInterface ) {
111 $stream->copyToStream( fopen( 'php://output', 'w' ) );
112 } else {
113 while ( true ) {
114 $buffer = $stream->read( 65536 );
115 if ( $buffer === '' ) {
116 break;
117 }
118 echo $buffer;
119 }
120 }
121
122 $mw = new MediaWiki;
123 $mw->doPostOutputShutdown( 'fast' );
124 }
125 }