19use Wikimedia\Assert\Assert;
27 private ?
Router $router =
null;
46 array $textFormatters,
47 bool $showExceptionDetails,
51 count( $textFormatters ) > 0,
52 '$textFormatters',
'must not be empty'
61 ->addAuthorizer( $cors );
64 $restValidator =
new Validator( $objectFactory,
72 $defaultTextFormatter = array_first( $textFormatters );
75 ExtensionRegistry::getInstance()->getAttribute(
'RestModuleFiles' ),
82 ExtensionRegistry::getInstance()->getAttribute(
'RestRoutes' ),
86 $showExceptionDetails,
105 static $mainRequest = null;
107 if ( $mainRequest ===
null ) {
120 $context = $this->getContext();
121 $textFormatters = $this->getTextFormatters();
122 $showExceptionDetails = MWExceptionRenderer::shouldShowExceptionDetails();
126 CorsUtils::CONSTRUCTOR_OPTIONS,
127 $this->getServiceContainer()->getMainConfig()
132 if ( !$this->router ) {
133 $this->router = $this->createRouter(
134 $this->getServiceContainer(),
138 $showExceptionDetails,
149 private function getTextFormatters() {
150 $services = $this->getServiceContainer();
155 $queryParams = $this->request->getQueryParams();
156 $requestedLang = $queryParams[
'lang'] ??
null;
157 if ( is_string( $requestedLang ) && $requestedLang !==
'' ) {
158 $internalCode = \MediaWiki\Language\LanguageCode::bcp47ToInternal( $requestedLang );
160 $langs = [ $internalCode ];
164 $langs = array_unique( array_merge( $langs, [ $code,
'en' ] ) );
165 $textFormatters = [];
168 foreach ( $langs as $lang ) {
170 $textFormatters[] = $factory->getTextFormatter( $lang );
173 return $textFormatters;
182 parent::__construct( $context, $environment, $mediaWikiServices );
190 $this->router = $router;
194 $this->startOutputBuffer();
196 $response = $this->router->execute( $this->request );
198 $webResponse = $this->getResponse();
200 $webResponse->header(
201 'HTTP/' . $response->getProtocolVersion() .
' ' . $response->getStatusCode() .
' ' .
202 $response->getReasonPhrase()
205 foreach ( $response->getRawHeaderLines() as $line ) {
206 $webResponse->header( $line );
209 foreach ( $response->getCookies() as $cookie ) {
210 $webResponse->setCookie(
219 $this->discardOutputBuffer();
221 $stream = $response->getBody();
224 $this->prepareForOutput();
227 $stream->copyToStream( fopen(
'php://output',
'w' ) );
230 $buffer = $stream->read( 65536 );
231 if ( $buffer ===
'' ) {
234 $this->print( $buffer );
Group all the pieces relevant to the context of a request into one instance.
Utility class wrapping PHP runtime state.
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.
doSetup()
Perform any setup needed before execute() is called.
setRouter(Router $router)
Sets the router to use.
__construct(private readonly RequestInterface $request, RequestContext $context, EntryPointEnvironment $environment, MediaWikiServices $mediaWikiServices,)
static createRouter(MediaWikiServices $services, IContextSource $context, RequestInterface $request, array $textFormatters, bool $showExceptionDetails, CorsUtils $cors)
execute()
Subclasses implement the entry point's functionality by overriding this method.
Interface for objects which can provide a MediaWiki context on request.
Converts MessageSpecifier objects to localized plain text in a certain language.