Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[lhc/web/wiklou.git] / includes / Rest / EntryPoint.php
1 <?php
2
3 namespace MediaWiki\Rest;
4
5 use ExtensionRegistry;
6 use MediaWiki\MediaWikiServices;
7 use MediaWiki\Rest\BasicAccess\MWBasicAuthorizer;
8 use RequestContext;
9 use Title;
10 use WebResponse;
11
12 class EntryPoint {
13 /** @var RequestInterface */
14 private $request;
15 /** @var WebResponse */
16 private $webResponse;
17 /** @var Router */
18 private $router;
19
20 public static function main() {
21 // URL safety checks
22 global $wgRequest;
23 if ( !$wgRequest->checkUrlExtension() ) {
24 return;
25 }
26
27 // Set $wgTitle and the title in RequestContext, as in api.php
28 global $wgTitle;
29 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
30 RequestContext::getMain()->setTitle( $wgTitle );
31
32 $services = MediaWikiServices::getInstance();
33 $conf = $services->getMainConfig();
34
35 if ( !$conf->get( 'EnableRestAPI' ) ) {
36 wfHttpError( 403, 'Access Denied',
37 'Set $wgEnableRestAPI to true to enable the experimental REST API' );
38 return;
39 }
40
41 $request = new RequestFromGlobals( [
42 'cookiePrefix' => $conf->get( 'CookiePrefix' )
43 ] );
44
45 $authorizer = new MWBasicAuthorizer( RequestContext::getMain()->getUser(),
46 $services->getPermissionManager() );
47
48 global $IP;
49 $router = new Router(
50 [ "$IP/includes/Rest/coreRoutes.json" ],
51 ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
52 $conf->get( 'RestPath' ),
53 $services->getLocalServerObjectCache(),
54 new ResponseFactory,
55 $authorizer
56 );
57
58 $entryPoint = new self(
59 $request,
60 $wgRequest->response(),
61 $router );
62 $entryPoint->execute();
63 }
64
65 public function __construct( RequestInterface $request, WebResponse $webResponse,
66 Router $router
67 ) {
68 $this->request = $request;
69 $this->webResponse = $webResponse;
70 $this->router = $router;
71 }
72
73 public function execute() {
74 $response = $this->router->execute( $this->request );
75
76 $this->webResponse->header(
77 'HTTP/' . $response->getProtocolVersion() . ' ' .
78 $response->getStatusCode() . ' ' .
79 $response->getReasonPhrase() );
80
81 foreach ( $response->getRawHeaderLines() as $line ) {
82 $this->webResponse->header( $line );
83 }
84
85 foreach ( $response->getCookies() as $cookie ) {
86 $this->webResponse->setCookie(
87 $cookie['name'],
88 $cookie['value'],
89 $cookie['expiry'],
90 $cookie['options'] );
91 }
92
93 $stream = $response->getBody();
94 $stream->rewind();
95 if ( $stream instanceof CopyableStreamInterface ) {
96 $stream->copyToStream( fopen( 'php://output', 'w' ) );
97 } else {
98 while ( true ) {
99 $buffer = $stream->read( 65536 );
100 if ( $buffer === '' ) {
101 break;
102 }
103 echo $buffer;
104 }
105 }
106 }
107 }