MediaWiki  master
EntryPoint.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
7 use MediaWiki;
11 use RequestContext;
12 use Title;
13 use WebResponse;
15 
16 class EntryPoint {
18  private $request;
20  private $webResponse;
22  private $router;
24  private $context;
25 
31  private static function createRouter(
33  ): Router {
34  global $IP;
35 
36  $services = MediaWikiServices::getInstance();
37  $conf = $services->getMainConfig();
38 
39  $responseFactory = new ResponseFactory( self::getTextFormatters( $services ) );
40 
41  $authorizer = new MWBasicAuthorizer( $context->getUser(),
42  $services->getPermissionManager() );
43 
44  $objectFactory = $services->getObjectFactory();
45  $restValidator = new Validator( $objectFactory,
46  $services->getPermissionManager(),
47  $request,
48  RequestContext::getMain()->getUser()
49  );
50 
51  // Always include the "official" routes. Include additional routes if specified.
52  $routeFiles = array_merge(
53  [ 'includes/Rest/coreRoutes.json' ],
54  $conf->get( 'RestAPIAdditionalRouteFiles' )
55  );
56  array_walk( $routeFiles, function ( &$val, $key ) {
57  global $IP;
58  $val = "$IP/$val";
59  } );
60 
61  return new Router(
62  $routeFiles,
63  ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
64  $conf->get( 'CanonicalServer' ),
65  $conf->get( 'RestPath' ),
66  $services->getLocalServerObjectCache(),
67  $responseFactory,
68  $authorizer,
69  $objectFactory,
70  $restValidator,
71  $services->getHookContainer()
72  );
73  }
74 
75  public static function main() {
76  // URL safety checks
77  global $wgRequest;
78 
80 
81  // Set $wgTitle and the title in RequestContext, as in api.php
82  global $wgTitle;
83  $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
85 
86  $services = MediaWikiServices::getInstance();
87  $conf = $services->getMainConfig();
88 
90  'cookiePrefix' => $conf->get( 'CookiePrefix' )
91  ] );
92 
94 
95  $entryPoint = new self(
96  $context,
97  $request,
98  $wgRequest->response(),
99  $router );
100  $entryPoint->execute();
101  }
102 
109  public static function getTextFormatters( MediaWikiServices $services ) {
110  $langs = array_unique( [
111  $services->getMainConfig()->get( 'ContLang' )->getCode(),
112  'en'
113  ] );
114  $textFormatters = [];
115  $factory = $services->getMessageFormatterFactory();
116  foreach ( $langs as $lang ) {
117  $textFormatters[] = $factory->getTextFormatter( $lang );
118  }
119  return $textFormatters;
120  }
121 
124  ) {
125  $this->context = $context;
126  $this->request = $request;
127  $this->webResponse = $webResponse;
128  $this->router = $router;
129  }
130 
131  public function execute() {
132  ob_start();
133  $response = $this->router->execute( $this->request );
134 
135  $this->webResponse->header(
136  'HTTP/' . $response->getProtocolVersion() . ' ' .
137  $response->getStatusCode() . ' ' .
138  $response->getReasonPhrase() );
139 
140  foreach ( $response->getRawHeaderLines() as $line ) {
141  $this->webResponse->header( $line );
142  }
143 
144  foreach ( $response->getCookies() as $cookie ) {
145  $this->webResponse->setCookie(
146  $cookie['name'],
147  $cookie['value'],
148  $cookie['expiry'],
149  $cookie['options'] );
150  }
151 
152  // Clear all errors that might have been displayed if display_errors=On
153  ob_end_clean();
154 
155  $stream = $response->getBody();
156  $stream->rewind();
157 
158  MediaWiki::preOutputCommit( $this->context );
159 
160  if ( $stream instanceof CopyableStreamInterface ) {
161  $stream->copyToStream( fopen( 'php://output', 'w' ) );
162  } else {
163  while ( true ) {
164  $buffer = $stream->read( 65536 );
165  if ( $buffer === '' ) {
166  break;
167  }
168  echo $buffer;
169  }
170  }
171 
172  $mw = new MediaWiki;
173  $mw->doPostOutputShutdown();
174  }
175 }
MediaWiki\Rest\ResponseFactory
Generates standardized response objects.
Definition: ResponseFactory.php:17
Wikimedia\Message\ITextFormatter
Definition: ITextFormatter.php:18
MediaWiki\Rest\EntryPoint\main
static main()
Definition: EntryPoint.php:75
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:154
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
MediaWiki\Rest\RequestFromGlobals
This is a request class that gets data directly from the superglobals and other global PHP state,...
Definition: RequestFromGlobals.php:15
MediaWiki\Rest\EntryPoint\__construct
__construct(RequestContext $context, RequestInterface $request, WebResponse $webResponse, Router $router)
Definition: EntryPoint.php:122
ExtensionRegistry
ExtensionRegistry class.
Definition: ExtensionRegistry.php:18
MediaWiki\Rest\BasicAccess\MWBasicAuthorizer
A factory for MWBasicRequestAuthorizer which passes through a UserIdentity.
Definition: MWBasicAuthorizer.php:15
$wgTitle
$wgTitle
Definition: Setup.php:794
RequestContext\getUser
getUser()
Definition: RequestContext.php:280
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:185
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:136
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:58
MediaWiki\Rest\EntryPoint\$context
RequestContext $context
Definition: EntryPoint.php:24
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:18
MediaWiki
A helper class for throttling authentication attempts.
MediaWiki\MediaWikiServices\getMessageFormatterFactory
getMessageFormatterFactory()
Definition: MediaWikiServices.php:942
MediaWiki\MediaWikiServices\getMainConfig
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
Definition: MediaWikiServices.php:894
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:38
MediaWiki\Rest
MediaWiki\Rest\EntryPoint\execute
execute()
Definition: EntryPoint.php:131
MediaWiki\Rest\EntryPoint\createRouter
static createRouter(IContextSource $context, RequestInterface $request)
Definition: EntryPoint.php:31
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:662
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:453
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Rest\EntryPoint
Definition: EntryPoint.php:16
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:644
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:22
RequestContext\setTitle
setTitle(Title $title=null)
Definition: RequestContext.php:163
MediaWiki\Rest\EntryPoint\$webResponse
WebResponse $webResponse
Definition: EntryPoint.php:20
MediaWiki\Rest\EntryPoint\getTextFormatters
static getTextFormatters(MediaWikiServices $services)
Get a TextFormatter array from MediaWikiServices.
Definition: EntryPoint.php:109