MediaWiki  master
EntryPoint.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
6 use MediaWiki;
10 use RequestContext;
11 use Title;
12 use WebResponse;
14 
15 class EntryPoint {
17  private $request;
19  private $webResponse;
21  private $router;
23  private $context;
24 
25  public static function main() {
26  // URL safety checks
27  global $wgRequest;
28  if ( !$wgRequest->checkUrlExtension() ) {
29  return;
30  }
31 
33 
34  // Set $wgTitle and the title in RequestContext, as in api.php
35  global $wgTitle;
36  $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
37  $context->setTitle( $wgTitle );
38 
39  $services = MediaWikiServices::getInstance();
40  $conf = $services->getMainConfig();
41  $objectFactory = $services->getObjectFactory();
42 
43  if ( !$conf->get( 'EnableRestAPI' ) ) {
44  wfHttpError( 403, 'Access Denied',
45  'Set $wgEnableRestAPI to true to enable the experimental REST API' );
46  return;
47  }
48 
50  'cookiePrefix' => $conf->get( 'CookiePrefix' )
51  ] );
52 
53  $responseFactory = new ResponseFactory( self::getTextFormatters( $services ) );
54 
55  // @phan-suppress-next-line PhanAccessMethodInternal
56  $authorizer = new MWBasicAuthorizer( $context->getUser(),
57  $services->getPermissionManager() );
58 
59  // @phan-suppress-next-line PhanAccessMethodInternal
60  $restValidator = new Validator( $objectFactory,
61  $services->getPermissionManager(),
62  $request,
63  RequestContext::getMain()->getUser()
64  );
65 
66  global $IP;
67  $router = new Router(
68  [ "$IP/includes/Rest/coreRoutes.json" ],
69  ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
70  $conf->get( 'RestPath' ),
71  $services->getLocalServerObjectCache(),
72  $responseFactory,
73  $authorizer,
74  $objectFactory,
75  $restValidator
76  );
77 
78  $entryPoint = new self(
79  $context,
80  $request,
81  $wgRequest->response(),
82  $router );
83  $entryPoint->execute();
84  }
85 
92  public static function getTextFormatters( MediaWikiServices $services ) {
93  $langs = array_unique( [
94  $services->getMainConfig()->get( 'ContLang' )->getCode(),
95  'en'
96  ] );
97  $textFormatters = [];
98  $factory = $services->getMessageFormatterFactory();
99  foreach ( $langs as $lang ) {
100  $textFormatters[] = $factory->getTextFormatter( $lang );
101  }
102  return $textFormatters;
103  }
104 
107  ) {
108  $this->context = $context;
109  $this->request = $request;
110  $this->webResponse = $webResponse;
111  $this->router = $router;
112  }
113 
114  public function execute() {
115  ob_start();
116  $response = $this->router->execute( $this->request );
117 
118  $this->webResponse->header(
119  'HTTP/' . $response->getProtocolVersion() . ' ' .
120  $response->getStatusCode() . ' ' .
121  $response->getReasonPhrase() );
122 
123  foreach ( $response->getRawHeaderLines() as $line ) {
124  $this->webResponse->header( $line );
125  }
126 
127  foreach ( $response->getCookies() as $cookie ) {
128  $this->webResponse->setCookie(
129  $cookie['name'],
130  $cookie['value'],
131  $cookie['expiry'],
132  $cookie['options'] );
133  }
134 
135  // Clear all errors that might have been displayed if display_errors=On
136  ob_end_clean();
137 
138  $stream = $response->getBody();
139  $stream->rewind();
140 
141  MediaWiki::preOutputCommit( $this->context );
142 
143  if ( $stream instanceof CopyableStreamInterface ) {
144  $stream->copyToStream( fopen( 'php://output', 'w' ) );
145  } else {
146  while ( true ) {
147  $buffer = $stream->read( 65536 );
148  if ( $buffer === '' ) {
149  break;
150  }
151  echo $buffer;
152  }
153  }
154 
155  $mw = new MediaWiki;
156  $mw->doPostOutputShutdown();
157  }
158 }
$response
$IP
Definition: WebStart.php:41
if(!isset( $args[0])) $lang
Generates standardized response objects.
const NS_SPECIAL
Definition: Defines.php:49
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:649
static getInstance()
Returns the global default instance of the top level service locator.
The REST router is responsible for gathering handler configuration, matching an input path and HTTP m...
Definition: Router.php:18
A helper class for throttling authentication attempts.
A factory for MWBasicRequestAuthorizer which passes through a UserIdentity.
static getMain()
Get the RequestContext object associated with the main request.
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
execute(RequestInterface $request)
Find the handler for a request and execute it.
Definition: Router.php:226
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
setTitle(Title $title=null)
RequestInterface $request
Definition: EntryPoint.php:17
A request interface similar to PSR-7&#39;s ServerRequestInterface.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:584
static getTextFormatters(MediaWikiServices $services)
Get a TextFormatter array from MediaWikiServices.
Definition: EntryPoint.php:92
This is a request class that gets data directly from the superglobals and other global PHP state...
Wrapper for ParamValidator.
Definition: Validator.php:30
$line
Definition: mcc.php:119
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:729
__construct(RequestContext $context, RequestInterface $request, WebResponse $webResponse, Router $router)
Definition: EntryPoint.php:105
if(! $wgRequest->checkUrlExtension()) if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
Definition: api.php:58
An interface for a stream with a copyToStream() function.
RequestContext $context
Definition: EntryPoint.php:23
MediaWikiServices is the service locator for the application scope of MediaWiki.