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