MediaWiki REL1_34
EntryPoint.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
6use MediaWiki;
11use Title;
12use WebResponse;
14
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
32 $context = RequestContext::getMain();
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
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(),
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(
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}
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
if(! $wgDBerrorLogTZ) $wgRequest
Definition Setup.php:751
if(! $wgRequest->checkUrlExtension()) if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
Definition api.php:58
$line
Definition cdb.php:59
ExtensionRegistry class.
MediaWikiServices is the service locator for the application scope of MediaWiki.
getMainConfig()
Returns the Config object that provides configuration for MediaWiki core.
static getInstance()
Returns the global default instance of the top level service locator.
A factory for MWBasicRequestAuthorizer which passes through a UserIdentity.
RequestInterface $request
__construct(RequestContext $context, RequestInterface $request, WebResponse $webResponse, Router $router)
static getTextFormatters(MediaWikiServices $services)
Get a TextFormatter array from MediaWikiServices.
RequestContext $context
This is a request class that gets data directly from the superglobals and other global PHP state,...
Generates standardized response objects.
The REST router is responsible for gathering handler configuration, matching an input path and HTTP m...
Definition Router.php:18
Wrapper for ParamValidator.
Definition Validator.php:29
Group all the pieces relevant to the context of a request into one instance.
setTitle(Title $title=null)
Represents a title within MediaWiki.
Definition Title.php:42
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
$IP
Definition update.php:3
const NS_SPECIAL
Definition Defines.php:58
An interface for a stream with a copyToStream() function.
A request interface similar to PSR-7's ServerRequestInterface.
This class serves as a utility class for this extension.
if(!isset( $args[0])) $lang