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;
29 
37  private static function createRouter(
39  ): Router {
40  $services = MediaWikiServices::getInstance();
41  $conf = $services->getMainConfig();
42 
43  $authority = $context->getAuthority();
44  $authorizer = new CompoundAuthorizer();
45  $authorizer
46  ->addAuthorizer( new MWBasicAuthorizer( $authority ) )
47  ->addAuthorizer( $cors );
48 
49  $objectFactory = $services->getObjectFactory();
50  $restValidator = new Validator( $objectFactory,
51  $request,
52  $authority
53  );
54 
55  // Always include the "official" routes. Include additional routes if specified.
56  $routeFiles = array_merge(
57  [ 'includes/Rest/coreRoutes.json' ],
58  $conf->get( 'RestAPIAdditionalRouteFiles' )
59  );
60  array_walk( $routeFiles, function ( &$val, $key ) {
61  global $IP;
62  $val = "$IP/$val";
63  } );
64 
65  return ( new Router(
66  $routeFiles,
67  ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
68  $conf->get( 'CanonicalServer' ),
69  $conf->get( 'RestPath' ),
70  $services->getLocalServerObjectCache(),
71  $responseFactory,
72  $authorizer,
73  $authority,
74  $objectFactory,
75  $restValidator,
76  $services->getHookContainer()
77  ) )->setCors( $cors );
78  }
79 
80  public static function main() {
81  // URL safety checks
82  global $wgRequest;
83 
85 
86  // Set $wgTitle and the title in RequestContext, as in api.php
87  global $wgTitle;
88  $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
90 
91  $services = MediaWikiServices::getInstance();
92  $conf = $services->getMainConfig();
93 
95  'cookiePrefix' => $conf->get( 'CookiePrefix' )
96  ] );
97 
98  $responseFactory = new ResponseFactory( self::getTextFormatters( $services ) );
99 
100  $cors = new CorsUtils(
101  new ServiceOptions(
102  CorsUtils::CONSTRUCTOR_OPTIONS, $services->getMainConfig()
103  ),
104  $responseFactory,
105  $context->getUser()
106  );
107 
108  $router = self::createRouter( $context, $request, $responseFactory, $cors );
109 
110  $entryPoint = new self(
111  $context,
112  $request,
113  $wgRequest->response(),
114  $router,
115  $cors
116  );
117  $entryPoint->execute();
118  }
119 
126  public static function getTextFormatters( MediaWikiServices $services ) {
127  $code = $services->getContentLanguage()->getCode();
128  $langs = array_unique( [ $code, 'en' ] );
129  $textFormatters = [];
130  $factory = $services->getMessageFormatterFactory();
131 
132  foreach ( $langs as $lang ) {
133  $textFormatters[] = $factory->getTextFormatter( $lang );
134  }
135  return $textFormatters;
136  }
137 
140  ) {
141  $this->context = $context;
142  $this->request = $request;
143  $this->webResponse = $webResponse;
144  $this->router = $router;
145  $this->cors = $cors;
146  }
147 
148  public function execute() {
149  ob_start();
150  $response = $this->cors->modifyResponse(
151  $this->request,
152  $this->router->execute( $this->request )
153  );
154 
155  $this->webResponse->header(
156  'HTTP/' . $response->getProtocolVersion() . ' ' .
157  $response->getStatusCode() . ' ' .
158  $response->getReasonPhrase() );
159 
160  foreach ( $response->getRawHeaderLines() as $line ) {
161  $this->webResponse->header( $line );
162  }
163 
164  foreach ( $response->getCookies() as $cookie ) {
165  $this->webResponse->setCookie(
166  $cookie['name'],
167  $cookie['value'],
168  $cookie['expiry'],
169  $cookie['options'] );
170  }
171 
172  // Clear all errors that might have been displayed if display_errors=On
173  ob_end_clean();
174 
175  $stream = $response->getBody();
176  $stream->rewind();
177 
178  MediaWiki::preOutputCommit( $this->context );
179 
180  if ( $stream instanceof CopyableStreamInterface ) {
181  $stream->copyToStream( fopen( 'php://output', 'w' ) );
182  } else {
183  while ( true ) {
184  $buffer = $stream->read( 65536 );
185  if ( $buffer === '' ) {
186  break;
187  }
188  echo $buffer;
189  }
190  }
191 
192  $mw = new MediaWiki;
193  $mw->doPostOutputShutdown();
194  }
195 }
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:138
Wikimedia\Message\ITextFormatter
Definition: ITextFormatter.php:18
MediaWiki\Rest\EntryPoint\main
static main()
Definition: EntryPoint.php:80
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:166
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
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
ExtensionRegistry class.
Definition: ExtensionRegistry.php:17
MediaWiki\Rest\BasicAccess\MWBasicAuthorizer
A factory for MWBasicRequestAuthorizer which passes through a UserIdentity.
Definition: MWBasicAuthorizer.php:14
RequestContext\getUser
getUser()
Definition: RequestContext.php:291
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:52
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:227
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:136
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:1043
MediaWiki\Rest\EntryPoint\createRouter
static createRouter(IContextSource $context, RequestInterface $request, ResponseFactory $responseFactory, CorsUtils $cors)
Definition: EntryPoint.php:37
RequestContext\getAuthority
getAuthority()
@unstable
Definition: RequestContext.php:309
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:624
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:40
$wgTitle
$wgTitle
Definition: Setup.php:794
MediaWiki\Rest
MediaWiki\Rest\EntryPoint\execute
execute()
Definition: EntryPoint.php:148
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:667
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:476
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:57
Title
Represents a title within MediaWiki.
Definition: Title.php:46
MediaWiki\MediaWikiServices\getContentLanguage
getContentLanguage()
Definition: MediaWikiServices.php:696
MediaWiki\Rest\EntryPoint
Definition: EntryPoint.php:18
$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:24
MediaWiki\Rest\BasicAccess\CompoundAuthorizer
Wraps an array of BasicAuthorizerInterface and checks them all to authorize the request.
Definition: CompoundAuthorizer.php:14
RequestContext\setTitle
setTitle(Title $title=null)
Definition: RequestContext.php:172
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:126