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  );
72  }
73 
74  public static function main() {
75  // URL safety checks
76  global $wgRequest;
77 
79 
80  // Set $wgTitle and the title in RequestContext, as in api.php
81  global $wgTitle;
82  $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
84 
85  $services = MediaWikiServices::getInstance();
86  $conf = $services->getMainConfig();
87 
88  if ( !$conf->get( 'EnableRestAPI' ) ) {
89  wfHttpError( 403, 'Access Denied',
90  'Set $wgEnableRestAPI to true to enable the experimental REST API' );
91  return;
92  }
93 
95  'cookiePrefix' => $conf->get( 'CookiePrefix' )
96  ] );
97 
99 
100  $entryPoint = new self(
101  $context,
102  $request,
103  $wgRequest->response(),
104  $router );
105  $entryPoint->execute();
106  }
107 
114  public static function getTextFormatters( MediaWikiServices $services ) {
115  $langs = array_unique( [
116  $services->getMainConfig()->get( 'ContLang' )->getCode(),
117  'en'
118  ] );
119  $textFormatters = [];
120  $factory = $services->getMessageFormatterFactory();
121  foreach ( $langs as $lang ) {
122  $textFormatters[] = $factory->getTextFormatter( $lang );
123  }
124  return $textFormatters;
125  }
126 
129  ) {
130  $this->context = $context;
131  $this->request = $request;
132  $this->webResponse = $webResponse;
133  $this->router = $router;
134  }
135 
136  public function execute() {
137  ob_start();
138  $response = $this->router->execute( $this->request );
139 
140  $this->webResponse->header(
141  'HTTP/' . $response->getProtocolVersion() . ' ' .
142  $response->getStatusCode() . ' ' .
143  $response->getReasonPhrase() );
144 
145  foreach ( $response->getRawHeaderLines() as $line ) {
146  $this->webResponse->header( $line );
147  }
148 
149  foreach ( $response->getCookies() as $cookie ) {
150  $this->webResponse->setCookie(
151  $cookie['name'],
152  $cookie['value'],
153  $cookie['expiry'],
154  $cookie['options'] );
155  }
156 
157  // Clear all errors that might have been displayed if display_errors=On
158  ob_end_clean();
159 
160  $stream = $response->getBody();
161  $stream->rewind();
162 
163  MediaWiki::preOutputCommit( $this->context );
164 
165  if ( $stream instanceof CopyableStreamInterface ) {
166  $stream->copyToStream( fopen( 'php://output', 'w' ) );
167  } else {
168  while ( true ) {
169  $buffer = $stream->read( 65536 );
170  if ( $buffer === '' ) {
171  break;
172  }
173  echo $buffer;
174  }
175  }
176 
177  $mw = new MediaWiki;
178  $mw->doPostOutputShutdown();
179  }
180 }
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:74
MediaWiki\Rest\Validator\Validator
Wrapper for ParamValidator.
Definition: Validator.php:31
$response
$response
Definition: opensearch_desc.php:44
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:144
$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:127
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:175
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:19
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:898
MediaWiki\MediaWikiServices\getMainConfig
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
Definition: MediaWikiServices.php:850
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:595
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:136
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:654
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:1593
MediaWiki\Rest\EntryPoint
Definition: EntryPoint.php:16
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:641
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:114