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  return new Router(
52  [ "$IP/includes/Rest/coreRoutes.json" ],
53  ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
54  $conf->get( 'RestPath' ),
55  $services->getLocalServerObjectCache(),
56  $responseFactory,
57  $authorizer,
58  $objectFactory,
59  $restValidator
60  );
61  }
62 
63  public static function main() {
64  // URL safety checks
65  global $wgRequest;
66 
68 
69  // Set $wgTitle and the title in RequestContext, as in api.php
70  global $wgTitle;
71  $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
73 
74  $services = MediaWikiServices::getInstance();
75  $conf = $services->getMainConfig();
76 
77  if ( !$conf->get( 'EnableRestAPI' ) ) {
78  wfHttpError( 403, 'Access Denied',
79  'Set $wgEnableRestAPI to true to enable the experimental REST API' );
80  return;
81  }
82 
84  'cookiePrefix' => $conf->get( 'CookiePrefix' )
85  ] );
86 
88 
89  $entryPoint = new self(
90  $context,
91  $request,
92  $wgRequest->response(),
93  $router );
94  $entryPoint->execute();
95  }
96 
103  public static function getTextFormatters( MediaWikiServices $services ) {
104  $langs = array_unique( [
105  $services->getMainConfig()->get( 'ContLang' )->getCode(),
106  'en'
107  ] );
108  $textFormatters = [];
109  $factory = $services->getMessageFormatterFactory();
110  foreach ( $langs as $lang ) {
111  $textFormatters[] = $factory->getTextFormatter( $lang );
112  }
113  return $textFormatters;
114  }
115 
118  ) {
119  $this->context = $context;
120  $this->request = $request;
121  $this->webResponse = $webResponse;
122  $this->router = $router;
123  }
124 
125  public function execute() {
126  ob_start();
127  $response = $this->router->execute( $this->request );
128 
129  $this->webResponse->header(
130  'HTTP/' . $response->getProtocolVersion() . ' ' .
131  $response->getStatusCode() . ' ' .
132  $response->getReasonPhrase() );
133 
134  foreach ( $response->getRawHeaderLines() as $line ) {
135  $this->webResponse->header( $line );
136  }
137 
138  foreach ( $response->getCookies() as $cookie ) {
139  $this->webResponse->setCookie(
140  $cookie['name'],
141  $cookie['value'],
142  $cookie['expiry'],
143  $cookie['options'] );
144  }
145 
146  // Clear all errors that might have been displayed if display_errors=On
147  ob_end_clean();
148 
149  $stream = $response->getBody();
150  $stream->rewind();
151 
152  MediaWiki::preOutputCommit( $this->context );
153 
154  if ( $stream instanceof CopyableStreamInterface ) {
155  $stream->copyToStream( fopen( 'php://output', 'w' ) );
156  } else {
157  while ( true ) {
158  $buffer = $stream->read( 65536 );
159  if ( $buffer === '' ) {
160  break;
161  }
162  echo $buffer;
163  }
164  }
165 
166  $mw = new MediaWiki;
167  $mw->doPostOutputShutdown();
168  }
169 }
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:63
MediaWiki\Rest\Validator\Validator
Wrapper for ParamValidator.
Definition: Validator.php:30
$response
$response
Definition: opensearch_desc.php:44
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:134
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:35
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:116
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
if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
Definition: api.php:59
RequestContext\getUser
getUser()
Definition: RequestContext.php:276
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:165
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:130
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:49
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:18
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:844
MediaWiki\MediaWikiServices\getMainConfig
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
Definition: MediaWikiServices.php:804
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:602
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:34
MediaWiki\Rest
MediaWiki\Rest\EntryPoint\execute
execute()
Definition: EntryPoint.php:125
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:653
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:451
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
Title
Represents a title within MediaWiki.
Definition: Title.php:42
wfHttpError
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
Definition: GlobalFunctions.php:1660
MediaWiki\Rest\EntryPoint
Definition: EntryPoint.php:16
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:719
WebResponse
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Definition: WebResponse.php:28
$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:159
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:103