MediaWiki  master
EntryPoint.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
7 use MediaWiki;
13 use RequestContext;
14 use Title;
15 use WebResponse;
17 
18 class EntryPoint {
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,
53  $request,
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 
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' );
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 
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 
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 }
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:151
Wikimedia\Message\ITextFormatter
Definition: ITextFormatter.php:18
MediaWiki\Rest\EntryPoint\main
static main()
Definition: EntryPoint.php:95
MediaWiki\Rest\Validator\Validator
Wrapper for ParamValidator.
Definition: Validator.php:32
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
$wgRequest
$wgRequest
Definition: Setup.php:705
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:30
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:254
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:134
MediaWiki\Rest\EntryPoint\$context
RequestContext $context
Definition: EntryPoint.php:26
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:20
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:20
MediaWiki\Rest\EntryPoint\$cors
CorsUtils $cors
Definition: EntryPoint.php:28
MediaWiki
A helper class for throttling authentication attempts.
MediaWiki\MediaWikiServices\getMessageFormatterFactory
getMessageFormatterFactory()
Definition: MediaWikiServices.php:1206
MediaWiki\Rest\EntryPoint\createRouter
static createRouter(IContextSource $context, RequestInterface $request, ResponseFactory $responseFactory, CorsUtils $cors)
Definition: EntryPoint.php:39
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:651
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:41
$wgTitle
$wgTitle
Definition: Setup.php:852
MediaWiki\Rest
MediaWiki\Rest\EntryPoint\execute
execute()
Definition: EntryPoint.php:161
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:668
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:48
MediaWiki\MediaWikiServices\getContentLanguage
getContentLanguage()
Definition: MediaWikiServices.php:803
MediaWiki\Rest\EntryPoint
Definition: EntryPoint.php:18
WebResponse
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Definition: WebResponse.php:30
$IP
$IP
Definition: WebStart.php:49
MediaWiki\Rest\EntryPoint\$router
Router $router
Definition: EntryPoint.php:24
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:85
MediaWiki\Rest\EntryPoint\$webResponse
WebResponse $webResponse
Definition: EntryPoint.php:22
MediaWiki\Rest\EntryPoint\getTextFormatters
static getTextFormatters(MediaWikiServices $services)
Get a TextFormatter array from MediaWikiServices.
Definition: EntryPoint.php:139