Merge "Return null from EditPage::importContentFormData"
[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 $authorizer = new MWBasicAuthorizer( $context->getUser(),
51 $services->getPermissionManager() );
52
53 global $IP;
54 $router = new Router(
55 [ "$IP/includes/Rest/coreRoutes.json" ],
56 ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
57 $conf->get( 'RestPath' ),
58 $services->getLocalServerObjectCache(),
59 new ResponseFactory,
60 $authorizer
61 );
62
63 $entryPoint = new self(
64 $context,
65 $request,
66 $wgRequest->response(),
67 $router );
68 $entryPoint->execute();
69 }
70
71 public function __construct( RequestContext $context, RequestInterface $request,
72 WebResponse $webResponse, Router $router
73 ) {
74 $this->context = $context;
75 $this->request = $request;
76 $this->webResponse = $webResponse;
77 $this->router = $router;
78 }
79
80 public function execute() {
81 ob_start();
82 $response = $this->router->execute( $this->request );
83
84 $this->webResponse->header(
85 'HTTP/' . $response->getProtocolVersion() . ' ' .
86 $response->getStatusCode() . ' ' .
87 $response->getReasonPhrase() );
88
89 foreach ( $response->getRawHeaderLines() as $line ) {
90 $this->webResponse->header( $line );
91 }
92
93 foreach ( $response->getCookies() as $cookie ) {
94 $this->webResponse->setCookie(
95 $cookie['name'],
96 $cookie['value'],
97 $cookie['expiry'],
98 $cookie['options'] );
99 }
100
101 // Clear all errors that might have been displayed if display_errors=On
102 ob_end_clean();
103
104 $stream = $response->getBody();
105 $stream->rewind();
106
107 MediaWiki::preOutputCommit( $this->context );
108
109 if ( $stream instanceof CopyableStreamInterface ) {
110 $stream->copyToStream( fopen( 'php://output', 'w' ) );
111 } else {
112 while ( true ) {
113 $buffer = $stream->read( 65536 );
114 if ( $buffer === '' ) {
115 break;
116 }
117 echo $buffer;
118 }
119 }
120
121 $mw = new MediaWiki;
122 $mw->doPostOutputShutdown( 'fast' );
123 }
124 }