Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[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 RequestContext;
8 use Title;
9 use WebResponse;
10
11 class EntryPoint {
12 /** @var RequestInterface */
13 private $request;
14 /** @var WebResponse */
15 private $webResponse;
16 /** @var Router */
17 private $router;
18
19 public static function main() {
20 // URL safety checks
21 global $wgRequest;
22 if ( !$wgRequest->checkUrlExtension() ) {
23 return;
24 }
25
26 // Set $wgTitle and the title in RequestContext, as in api.php
27 global $wgTitle;
28 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
29 RequestContext::getMain()->setTitle( $wgTitle );
30
31 $services = MediaWikiServices::getInstance();
32 $conf = $services->getMainConfig();
33
34 $request = new RequestFromGlobals( [
35 'cookiePrefix' => $conf->get( 'CookiePrefix' )
36 ] );
37
38 global $IP;
39 $router = new Router(
40 [ "$IP/includes/Rest/coreRoutes.json" ],
41 ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
42 $conf->get( 'RestPath' ),
43 $services->getLocalServerObjectCache(),
44 new ResponseFactory
45 );
46
47 $entryPoint = new self(
48 $request,
49 $wgRequest->response(),
50 $router );
51 $entryPoint->execute();
52 }
53
54 public function __construct( RequestInterface $request, WebResponse $webResponse,
55 Router $router
56 ) {
57 $this->request = $request;
58 $this->webResponse = $webResponse;
59 $this->router = $router;
60 }
61
62 public function execute() {
63 $response = $this->router->execute( $this->request );
64
65 $this->webResponse->header(
66 'HTTP/' . $response->getProtocolVersion() . ' ' .
67 $response->getStatusCode() . ' ' .
68 $response->getReasonPhrase() );
69
70 foreach ( $response->getRawHeaderLines() as $line ) {
71 $this->webResponse->header( $line );
72 }
73
74 foreach ( $response->getCookies() as $cookie ) {
75 $this->webResponse->setCookie(
76 $cookie['name'],
77 $cookie['value'],
78 $cookie['expiry'],
79 $cookie['options'] );
80 }
81
82 $stream = $response->getBody();
83 $stream->rewind();
84 if ( $stream instanceof CopyableStreamInterface ) {
85 $stream->copyToStream( fopen( 'php://output', 'w' ) );
86 } else {
87 while ( true ) {
88 $buffer = $stream->read( 65536 );
89 if ( $buffer === '' ) {
90 break;
91 }
92 echo $buffer;
93 }
94 }
95 }
96 }