MediaWiki master
EntryPoint.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
20
25
26 private RequestInterface $request;
27 private ?Router $router = null;
28 private ?CorsUtils $cors = null;
29
41 public static function createRouter(
42 MediaWikiServices $services,
43 IContextSource $context,
44 RequestInterface $request,
45 ResponseFactory $responseFactory,
46 CorsUtils $cors
47 ): Router {
48 $conf = $services->getMainConfig();
49
50 $authority = $context->getAuthority();
51 $authorizer = new CompoundAuthorizer();
52 $authorizer
53 ->addAuthorizer( new MWBasicAuthorizer( $authority ) )
54 ->addAuthorizer( $cors );
55
56 $objectFactory = $services->getObjectFactory();
57 $restValidator = new Validator( $objectFactory,
58 $request,
59 $authority
60 );
61
62 $stats = $services->getStatsFactory();
63
64 $moduleManager = new ModuleManager(
66 $services->getLocalServerObjectCache(),
67 $responseFactory
68 );
69
70 return ( new Router(
71 $moduleManager->getRouteFiles(),
72 ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
74 $services->getLocalServerObjectCache(),
75 $responseFactory,
76 $authorizer,
77 $authority,
78 $objectFactory,
79 $restValidator,
80 new MWErrorReporter(),
81 $services->getHookContainer(),
82 $context->getRequest()->getSession()
83 ) )
84 ->setCors( $cors )
85 ->setStats( $stats );
86 }
87
92 public static function getMainRequest(): RequestInterface {
93 static $mainRequest = null;
94
95 if ( $mainRequest === null ) {
96 $conf = MediaWikiServices::getInstance()->getMainConfig();
97 $mainRequest = new RequestFromGlobals( [
98 'cookiePrefix' => $conf->get( MainConfigNames::CookiePrefix )
99 ] );
100 }
101
102 return $mainRequest;
103 }
104
105 protected function doSetup() {
106 parent::doSetup();
107
108 $context = RequestContext::getMain();
109
110 $responseFactory = new ResponseFactory( $this->getTextFormatters() );
111 $responseFactory->setShowExceptionDetails(
112 MWExceptionRenderer::shouldShowExceptionDetails()
113 );
114
115 $this->cors = new CorsUtils(
116 new ServiceOptions(
117 CorsUtils::CONSTRUCTOR_OPTIONS,
118 $this->getServiceContainer()->getMainConfig()
119 ),
120 $responseFactory,
121 $context->getUser()
122 );
123
124 if ( !$this->router ) {
125 $this->router = $this->createRouter(
126 $this->getServiceContainer(),
127 $context,
128 $this->request,
129 $responseFactory,
130 $this->cors
131 );
132 }
133 }
134
140 private function getTextFormatters() {
141 $services = $this->getServiceContainer();
142
143 $code = $services->getContentLanguageCode()->toString();
144 $langs = array_unique( [ $code, 'en' ] );
145 $textFormatters = [];
146 $factory = $services->getMessageFormatterFactory();
147
148 foreach ( $langs as $lang ) {
149 $textFormatters[] = $factory->getTextFormatter( $lang );
150 }
151
152 return $textFormatters;
153 }
154
155 public function __construct(
156 RequestInterface $request,
157 RequestContext $context,
158 EntryPointEnvironment $environment,
159 MediaWikiServices $mediaWikiServices
160 ) {
161 parent::__construct( $context, $environment, $mediaWikiServices );
162
163 $this->request = $request;
164 }
165
170 public function setRouter( Router $router ): void {
171 $this->router = $router;
172 }
173
174 public function execute() {
175 $this->startOutputBuffer();
176
177 // IDEA: Move the call to cors->modifyResponse() into Module,
178 // so it's in the same class as cors->createPreflightResponse().
179 $response = $this->cors->modifyResponse(
180 $this->request,
181 $this->router->execute( $this->request )
182 );
183
184 $webResponse = $this->getResponse();
185
186 $webResponse->header(
187 'HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' .
188 $response->getReasonPhrase()
189 );
190
191 foreach ( $response->getRawHeaderLines() as $line ) {
192 $webResponse->header( $line );
193 }
194
195 foreach ( $response->getCookies() as $cookie ) {
196 $webResponse->setCookie(
197 $cookie['name'],
198 $cookie['value'],
199 $cookie['expiry'],
200 $cookie['options']
201 );
202 }
203
204 // Clear all errors that might have been displayed if display_errors=On
205 $this->discardOutputBuffer();
206
207 $stream = $response->getBody();
208 $stream->rewind();
209
210 $this->prepareForOutput();
211
212 if ( $stream instanceof CopyableStreamInterface ) {
213 $stream->copyToStream( fopen( 'php://output', 'w' ) );
214 } else {
215 while ( true ) {
216 $buffer = $stream->read( 65536 );
217 if ( $buffer === '' ) {
218 break;
219 }
220 $this->print( $buffer );
221 }
222 }
223 }
224
225}
A class for passing options to services.
Group all the pieces relevant to the context of a request into one instance.
Utility class wrapping PHP runtime state.
Class to expose exceptions to the client (API bots, users, admins using CLI scripts)
A class containing constants representing the names of configuration variables.
const CookiePrefix
Name constant for the CookiePrefix setting, for use with Config::get()
Base class for entry point handlers.
Service locator for MediaWiki core services.
getLocalServerObjectCache()
Returns the main server-local cache, yielding EmptyBagOStuff if there is none.
getObjectFactory()
ObjectFactory is intended for instantiating "handlers" from declarative definitions,...
static getInstance()
Returns the global default instance of the top level service locator.
Load JSON files, and uses a Processor to extract information.
A factory for MWBasicRequestAuthorizer which passes through a UserIdentity.
doSetup()
Perform any setup needed before execute() is called.
setRouter(Router $router)
Sets the router to use.
__construct(RequestInterface $request, RequestContext $context, EntryPointEnvironment $environment, MediaWikiServices $mediaWikiServices)
execute()
Subclasses implement the entry point's functionality by overriding this method.
static createRouter(MediaWikiServices $services, IContextSource $context, RequestInterface $request, ResponseFactory $responseFactory, CorsUtils $cors)
Manages information related to REST modules, such as routes and specs.
Error reporter based on MWExceptionHandler.
This is a request class that gets data directly from the superglobals and other global PHP state,...
Generates standardized response objects.
setShowExceptionDetails(bool $showExceptionDetails)
Control whether web responses may include a exception messager and backtrace.
The REST router is responsible for gathering module configuration, matching an input path against the...
Definition Router.php:30
const CONSTRUCTOR_OPTIONS
Definition Router.php:82
Wrapper for ParamValidator.
Definition Validator.php:38
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.