MediaWiki REL1_39
EntryPoint.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
5use Config;
8use MediaWiki;
18use Title;
19use WebResponse;
21
24 private $request;
26 private $webResponse;
28 private $router;
30 private $context;
32 private $cors;
34 private static $mainRequest;
35
43 private static function createRouter(
44 IContextSource $context, RequestInterface $request, ResponseFactory $responseFactory, CorsUtils $cors
45 ): Router {
47 $conf = $services->getMainConfig();
48
49 $authority = $context->getAuthority();
50 $authorizer = new CompoundAuthorizer();
51 $authorizer
52 ->addAuthorizer( new MWBasicAuthorizer( $authority ) )
53 ->addAuthorizer( $cors );
54
55 $objectFactory = $services->getObjectFactory();
56 $restValidator = new Validator( $objectFactory,
57 $request,
58 $authority
59 );
60
61 return ( new Router(
62 self::getRouteFiles( $conf ),
63 ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
64 new ServiceOptions( Router::CONSTRUCTOR_OPTIONS, $conf ),
65 $services->getLocalServerObjectCache(),
66 $responseFactory,
67 $authorizer,
68 $authority,
69 $objectFactory,
70 $restValidator,
71 new MWErrorReporter(),
72 $services->getHookContainer(),
73 $context->getRequest()->getSession()
74 ) )->setCors( $cors );
75 }
76
80 public static function getMainRequest(): RequestInterface {
81 if ( self::$mainRequest === null ) {
82 $conf = MediaWikiServices::getInstance()->getMainConfig();
83 self::$mainRequest = new RequestFromGlobals( [
84 'cookiePrefix' => $conf->get( MainConfigNames::CookiePrefix )
85 ] );
86 }
87 return self::$mainRequest;
88 }
89
90 public static function main() {
91 // URL safety checks
92 global $wgRequest;
93
94 $context = RequestContext::getMain();
95
96 // Set $wgTitle and the title in RequestContext, as in api.php
97 global $wgTitle;
98 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
99 $context->setTitle( $wgTitle );
100
101 $services = MediaWikiServices::getInstance();
102 $conf = $services->getMainConfig();
103
104 $responseFactory = new ResponseFactory( self::getTextFormatters( $services ) );
105 $responseFactory->setShowExceptionDetails( MWExceptionRenderer::shouldShowExceptionDetails() );
106
107 $cors = new CorsUtils(
108 new ServiceOptions(
109 CorsUtils::CONSTRUCTOR_OPTIONS, $conf
110 ),
111 $responseFactory,
112 $context->getUser()
113 );
114
115 $request = self::getMainRequest();
116
117 $router = self::createRouter( $context, $request, $responseFactory, $cors );
118
119 $entryPoint = new self(
120 $context,
121 $request,
122 $wgRequest->response(),
123 $router,
124 $cors
125 );
126 $entryPoint->execute();
127 }
128
135 private static function getTextFormatters( MediaWikiServices $services ) {
136 $code = $services->getContentLanguage()->getCode();
137 $langs = array_unique( [ $code, 'en' ] );
138 $textFormatters = [];
139 $factory = $services->getMessageFormatterFactory();
140
141 foreach ( $langs as $lang ) {
142 $textFormatters[] = $factory->getTextFormatter( $lang );
143 }
144 return $textFormatters;
145 }
146
151 private static function getRouteFiles( $conf ) {
152 global $IP;
153 $extensionsDir = $conf->get( MainConfigNames::ExtensionDirectory );
154 // Always include the "official" routes. Include additional routes if specified.
155 $routeFiles = array_merge(
156 [ 'includes/Rest/coreRoutes.json' ],
158 );
159 foreach ( $routeFiles as &$file ) {
160 if ( str_starts_with( $file, '/' ) ) {
161 // Allow absolute paths on non-Windows
162 } elseif ( str_starts_with( $file, 'extensions/' ) ) {
163 // Support hacks like Wikibase.ci.php
164 $file = substr_replace( $file, $extensionsDir, 0, strlen( 'extensions' ) );
165 } else {
166 $file = "$IP/$file";
167 }
168 }
169 return $routeFiles;
170 }
171
172 public function __construct( RequestContext $context, RequestInterface $request,
173 WebResponse $webResponse, Router $router, CorsUtils $cors
174 ) {
175 $this->context = $context;
176 $this->request = $request;
177 $this->webResponse = $webResponse;
178 $this->router = $router;
179 $this->cors = $cors;
180 }
181
182 public function execute() {
183 ob_start();
184 $response = $this->cors->modifyResponse(
185 $this->request,
186 $this->router->execute( $this->request )
187 );
188
189 $this->webResponse->header(
190 'HTTP/' . $response->getProtocolVersion() . ' ' .
191 $response->getStatusCode() . ' ' .
192 $response->getReasonPhrase() );
193
194 foreach ( $response->getRawHeaderLines() as $line ) {
195 $this->webResponse->header( $line );
196 }
197
198 foreach ( $response->getCookies() as $cookie ) {
199 $this->webResponse->setCookie(
200 $cookie['name'],
201 $cookie['value'],
202 $cookie['expiry'],
203 $cookie['options'] );
204 }
205
206 // Clear all errors that might have been displayed if display_errors=On
207 ob_end_clean();
208
209 $stream = $response->getBody();
210 $stream->rewind();
211
212 MediaWiki::preOutputCommit( $this->context );
213
214 if ( $stream instanceof CopyableStreamInterface ) {
215 $stream->copyToStream( fopen( 'php://output', 'w' ) );
216 } else {
217 while ( true ) {
218 $buffer = $stream->read( 65536 );
219 if ( $buffer === '' ) {
220 break;
221 }
222 echo $buffer;
223 }
224 }
225
226 $mw = new MediaWiki;
227 $mw->doPostOutputShutdown();
228 }
229}
const NS_SPECIAL
Definition Defines.php:53
if(!defined( 'MEDIAWIKI')) if(ini_get('mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:91
global $wgRequest
Definition Setup.php:377
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgTitle
Definition Setup.php:497
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:82
The Registry loads JSON files, and uses a Processor to extract information from them.
Class to expose exceptions to the client (API bots, users, admins using CLI scripts)
A class for passing options to services.
A class containing constants representing the names of configuration variables.
const ExtensionDirectory
Name constant for the ExtensionDirectory setting, for use with Config::get()
const CookiePrefix
Name constant for the CookiePrefix setting, for use with Config::get()
const RestAPIAdditionalRouteFiles
Name constant for the RestAPIAdditionalRouteFiles setting, for use with Config::get()
Service locator for MediaWiki core services.
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
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.
A factory for MWBasicRequestAuthorizer which passes through a UserIdentity.
__construct(RequestContext $context, RequestInterface $request, WebResponse $webResponse, Router $router, CorsUtils $cors)
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.
The REST router is responsible for gathering handler configuration, matching an input path and HTTP m...
Definition Router.php:25
Wrapper for ParamValidator.
Definition Validator.php:32
Group all the pieces relevant to the context of a request into one instance.
Represents a title within MediaWiki.
Definition Title.php:49
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Interface for configuration instances.
Definition Config.php:30
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(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!isset( $args[0])) $lang