MediaWiki REL1_35
EntryPoint.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
7use MediaWiki;
12use Title;
13use WebResponse;
15
18 private $request;
20 private $webResponse;
22 private $router;
24 private $context;
26 private static $mainRequest;
27
33 private static function createRouter(
35 ): Router {
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(),
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 $services->getHookContainer()
72 );
73 }
74
78 public static function getMainRequest(): ?RequestInterface {
79 if ( self::$mainRequest === null ) {
80 $conf = MediaWikiServices::getInstance()->getMainConfig();
81 self::$mainRequest = new RequestFromGlobals( [
82 'cookiePrefix' => $conf->get( 'CookiePrefix' )
83 ] );
84 }
85 return self::$mainRequest;
86 }
87
88 public static function main() {
89 // URL safety checks
90 global $wgRequest;
91
92 $context = RequestContext::getMain();
93
94 // Set $wgTitle and the title in RequestContext, as in api.php
95 global $wgTitle;
96 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
97 $context->setTitle( $wgTitle );
98
99 $services = MediaWikiServices::getInstance();
100 $conf = $services->getMainConfig();
101
102 $request = self::getMainRequest();
103
104 $router = self::createRouter( $context, $request );
105
106 $entryPoint = new self(
107 $context,
108 $request,
109 $wgRequest->response(),
110 $router );
111 $entryPoint->execute();
112 }
113
120 public static function getTextFormatters( MediaWikiServices $services ) {
121 $langs = array_unique( [
122 $services->getMainConfig()->get( 'ContLang' )->getCode(),
123 'en'
124 ] );
125 $textFormatters = [];
126 $factory = $services->getMessageFormatterFactory();
127 foreach ( $langs as $lang ) {
128 $textFormatters[] = $factory->getTextFormatter( $lang );
129 }
130 return $textFormatters;
131 }
132
133 public function __construct( RequestContext $context, RequestInterface $request,
134 WebResponse $webResponse, Router $router
135 ) {
136 $this->context = $context;
137 $this->request = $request;
138 $this->webResponse = $webResponse;
139 $this->router = $router;
140 }
141
142 public function execute() {
143 ob_start();
144 $response = $this->router->execute( $this->request );
145
146 $this->webResponse->header(
147 'HTTP/' . $response->getProtocolVersion() . ' ' .
148 $response->getStatusCode() . ' ' .
149 $response->getReasonPhrase() );
150
151 foreach ( $response->getRawHeaderLines() as $line ) {
152 $this->webResponse->header( $line );
153 }
154
155 foreach ( $response->getCookies() as $cookie ) {
156 $this->webResponse->setCookie(
157 $cookie['name'],
158 $cookie['value'],
159 $cookie['expiry'],
160 $cookie['options'] );
161 }
162
163 // Clear all errors that might have been displayed if display_errors=On
164 ob_end_clean();
165
166 $stream = $response->getBody();
167 $stream->rewind();
168
169 MediaWiki::preOutputCommit( $this->context );
170
171 if ( $stream instanceof CopyableStreamInterface ) {
172 $stream->copyToStream( fopen( 'php://output', 'w' ) );
173 } else {
174 while ( true ) {
175 $buffer = $stream->read( 65536 );
176 if ( $buffer === '' ) {
177 break;
178 }
179 echo $buffer;
180 }
181 }
182
183 $mw = new MediaWiki;
184 $mw->doPostOutputShutdown();
185 }
186}
$wgTitle
Definition Setup.php:799
if(! $wgDBerrorLogTZ) $wgRequest
Definition Setup.php:643
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:85
$IP
Definition WebStart.php:49
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.
static createRouter(IContextSource $context, RequestInterface $request)
static RequestInterface $mainRequest
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:20
Wrapper for ParamValidator.
Definition Validator.php:31
Group all the pieces relevant to the context of a request into one instance @newable.
Represents a title within MediaWiki.
Definition Title.php:42
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
const NS_SPECIAL
Definition Defines.php:59
Interface for objects which can provide a MediaWiki context on request.
An interface for a stream with a copyToStream() function.
A request interface similar to PSR-7's ServerRequestInterface.
$line
Definition mcc.php:119
A helper class for throttling authentication attempts.
if(!isset( $args[0])) $lang