MediaWiki REL1_37
EntryPoint.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
7use MediaWiki;
14use Title;
15use WebResponse;
17
20 private $request;
22 private $webResponse;
24 private $router;
26 private $context;
28 private $cors;
30 private static $mainRequest;
31
39 private static function createRouter(
41 ): Router {
42 $services = MediaWikiServices::getInstance();
43 $conf = $services->getMainConfig();
44
45 $authority = $context->getAuthority();
46 $authorizer = new CompoundAuthorizer();
47 $authorizer
48 ->addAuthorizer( new MWBasicAuthorizer( $authority ) )
49 ->addAuthorizer( $cors );
50
51 $objectFactory = $services->getObjectFactory();
52 $restValidator = new Validator( $objectFactory,
54 $authority
55 );
56
57 // Always include the "official" routes. Include additional routes if specified.
58 $routeFiles = array_merge(
59 [ 'includes/Rest/coreRoutes.json' ],
60 $conf->get( 'RestAPIAdditionalRouteFiles' )
61 );
62 array_walk( $routeFiles, static function ( &$val, $key ) {
63 global $IP;
64 $val = "$IP/$val";
65 } );
66
67 return ( new Router(
68 $routeFiles,
69 ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
70 $conf->get( 'CanonicalServer' ),
71 $conf->get( 'RestPath' ),
72 $services->getLocalServerObjectCache(),
73 $responseFactory,
74 $authorizer,
75 $authority,
76 $objectFactory,
77 $restValidator,
78 $services->getHookContainer()
79 ) )->setCors( $cors );
80 }
81
85 public static function getMainRequest(): ?RequestInterface {
86 if ( self::$mainRequest === null ) {
87 $conf = MediaWikiServices::getInstance()->getMainConfig();
88 self::$mainRequest = new RequestFromGlobals( [
89 'cookiePrefix' => $conf->get( 'CookiePrefix' )
90 ] );
91 }
92 return self::$mainRequest;
93 }
94
95 public static function main() {
96 // URL safety checks
97 global $wgRequest;
98
99 $context = RequestContext::getMain();
100
101 // Set $wgTitle and the title in RequestContext, as in api.php
102 global $wgTitle;
103 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
104 $context->setTitle( $wgTitle );
105
106 $services = MediaWikiServices::getInstance();
107 $conf = $services->getMainConfig();
108
109 $responseFactory = new ResponseFactory( self::getTextFormatters( $services ) );
110
111 $cors = new CorsUtils(
112 new ServiceOptions(
113 CorsUtils::CONSTRUCTOR_OPTIONS, $services->getMainConfig()
114 ),
115 $responseFactory,
116 $context->getUser()
117 );
118
119 $request = self::getMainRequest();
120
121 $router = self::createRouter( $context, $request, $responseFactory, $cors );
122
123 $entryPoint = new self(
124 $context,
125 $request,
126 $wgRequest->response(),
127 $router,
128 $cors
129 );
130 $entryPoint->execute();
131 }
132
139 private static function getTextFormatters( MediaWikiServices $services ) {
140 $code = $services->getContentLanguage()->getCode();
141 $langs = array_unique( [ $code, 'en' ] );
142 $textFormatters = [];
143 $factory = $services->getMessageFormatterFactory();
144
145 foreach ( $langs as $lang ) {
146 $textFormatters[] = $factory->getTextFormatter( $lang );
147 }
148 return $textFormatters;
149 }
150
151 public function __construct( RequestContext $context, RequestInterface $request,
152 WebResponse $webResponse, Router $router, CorsUtils $cors
153 ) {
154 $this->context = $context;
155 $this->request = $request;
156 $this->webResponse = $webResponse;
157 $this->router = $router;
158 $this->cors = $cors;
159 }
160
161 public function execute() {
162 ob_start();
163 $response = $this->cors->modifyResponse(
164 $this->request,
165 $this->router->execute( $this->request )
166 );
167
168 $this->webResponse->header(
169 'HTTP/' . $response->getProtocolVersion() . ' ' .
170 $response->getStatusCode() . ' ' .
171 $response->getReasonPhrase() );
172
173 foreach ( $response->getRawHeaderLines() as $line ) {
174 $this->webResponse->header( $line );
175 }
176
177 foreach ( $response->getCookies() as $cookie ) {
178 $this->webResponse->setCookie(
179 $cookie['name'],
180 $cookie['value'],
181 $cookie['expiry'],
182 $cookie['options'] );
183 }
184
185 // Clear all errors that might have been displayed if display_errors=On
186 ob_end_clean();
187
188 $stream = $response->getBody();
189 $stream->rewind();
190
191 MediaWiki::preOutputCommit( $this->context );
192
193 if ( $stream instanceof CopyableStreamInterface ) {
194 $stream->copyToStream( fopen( 'php://output', 'w' ) );
195 } else {
196 while ( true ) {
197 $buffer = $stream->read( 65536 );
198 if ( $buffer === '' ) {
199 break;
200 }
201 echo $buffer;
202 }
203 }
204
205 $mw = new MediaWiki;
206 $mw->doPostOutputShutdown();
207 }
208}
const NS_SPECIAL
Definition Defines.php:53
$wgRequest
Definition Setup.php:702
$wgTitle
Definition Setup.php:849
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
$IP
Definition WebStart.php:49
The Registry loads JSON files, and uses a Processor to extract information from them.
A class for passing options to services.
MediaWikiServices is the service locator for the application scope of MediaWiki.
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
static getInstance()
Returns the global default instance of the top level service locator.
A factory for MWBasicRequestAuthorizer which passes through a UserIdentity.
static RequestInterface $mainRequest
RequestInterface $request
static createRouter(IContextSource $context, RequestInterface $request, ResponseFactory $responseFactory, CorsUtils $cors)
static getTextFormatters(MediaWikiServices $services)
Get a TextFormatter array from MediaWikiServices.
RequestContext $context
__construct(RequestContext $context, RequestInterface $request, WebResponse $webResponse, Router $router, CorsUtils $cors)
This is a request class that gets data directly from the superglobals and other global PHP state,...
Generates standardized response objects.
The REST router is responsible for gathering handler configuration, matching an input path and HTTP m...
Definition Router.php:20
Wrapper for ParamValidator.
Definition Validator.php:32
Group all the pieces relevant to the context of a request into one instance @newable.
Represents a title within MediaWiki.
Definition Title.php:48
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Interface for objects which can provide a MediaWiki context on request.
An interface for a stream with a copyToStream() function.
A request interface similar to PSR-7's ServerRequestInterface.
$line
Definition mcc.php:119
A helper class for throttling authentication attempts.
if(!isset( $args[0])) $lang