MediaWiki  1.34.0
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' );
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( 'fast' );
157  }
158 }
MediaWiki\Rest\ResponseFactory
Generates standardized response objects.
Definition: ResponseFactory.php:18
Wikimedia\Message\ITextFormatter
Definition: ITextFormatter.php:18
MediaWiki\Rest\EntryPoint\main
static main()
Definition: EntryPoint.php:25
MediaWiki\Rest\Validator\Validator
Wrapper for ParamValidator.
Definition: Validator.php:29
$response
$response
Definition: opensearch_desc.php:38
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
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:105
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(! $wgRequest->checkUrlExtension()) if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
Definition: api.php:58
RequestContext\getUser
getUser()
Definition: RequestContext.php:274
MediaWiki\Rest\Router\execute
execute(RequestInterface $request)
Find the handler for a request and execute it.
Definition: Router.php:226
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:138
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:106
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:49
MediaWiki\Rest\EntryPoint\$context
RequestContext $context
Definition: EntryPoint.php:23
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:17
MediaWiki
This class serves as a utility class for this extension.
$IP
$IP
Definition: update.php:3
MediaWiki\MediaWikiServices\getMessageFormatterFactory
getMessageFormatterFactory()
Definition: MediaWikiServices.php:745
MediaWiki\MediaWikiServices\getMainConfig
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
Definition: MediaWikiServices.php:705
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:33
MediaWiki\Rest
MediaWiki\Rest\EntryPoint\execute
execute()
Definition: EntryPoint.php:114
$line
$line
Definition: cdb.php:59
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:596
MediaWiki\Rest\RequestInterface
A request interface similar to PSR-7's ServerRequestInterface.
Definition: RequestInterface.php:39
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:431
Title
Represents a title within MediaWiki.
Definition: Title.php:42
wfHttpError
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
Definition: GlobalFunctions.php:1659
MediaWiki\Rest\EntryPoint
Definition: EntryPoint.php:15
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:752
WebResponse
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
Definition: WebResponse.php:28
MediaWiki\Rest\EntryPoint\$router
Router $router
Definition: EntryPoint.php:21
RequestContext\setTitle
setTitle(Title $title=null)
Definition: RequestContext.php:158
MediaWiki\Rest\EntryPoint\$webResponse
WebResponse $webResponse
Definition: EntryPoint.php:19
MediaWiki\Rest\EntryPoint\getTextFormatters
static getTextFormatters(MediaWikiServices $services)
Get a TextFormatter array from MediaWikiServices.
Definition: EntryPoint.php:92