MediaWiki  master
EntryPoint.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
9 use Title;
11 
12 class EntryPoint {
14  private $request;
16  private $webResponse;
18  private $router;
19 
20  public static function main() {
21  // URL safety checks
22  global $wgRequest;
23  if ( !$wgRequest->checkUrlExtension() ) {
24  return;
25  }
26 
27  // Set $wgTitle and the title in RequestContext, as in api.php
28  global $wgTitle;
29  $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/rest.php' );
30  RequestContext::getMain()->setTitle( $wgTitle );
31 
33  $conf = $services->getMainConfig();
34 
35  if ( !$conf->get( 'EnableRestAPI' ) ) {
36  wfHttpError( 403, 'Access Denied',
37  'Set $wgEnableRestAPI to true to enable the experimental REST API' );
38  return;
39  }
40 
42  'cookiePrefix' => $conf->get( 'CookiePrefix' )
43  ] );
44 
45  $authorizer = new MWBasicAuthorizer( RequestContext::getMain()->getUser(),
46  $services->getPermissionManager() );
47 
48  global $IP;
49  $router = new Router(
50  [ "$IP/includes/Rest/coreRoutes.json" ],
51  ExtensionRegistry::getInstance()->getAttribute( 'RestRoutes' ),
52  $conf->get( 'RestPath' ),
53  $services->getLocalServerObjectCache(),
54  new ResponseFactory,
55  $authorizer
56  );
57 
58  $entryPoint = new self(
59  $request,
60  $wgRequest->response(),
61  $router );
62  $entryPoint->execute();
63  }
64 
67  ) {
68  $this->request = $request;
69  $this->webResponse = $webResponse;
70  $this->router = $router;
71  }
72 
73  public function execute() {
74  $response = $this->router->execute( $this->request );
75 
76  $this->webResponse->header(
77  'HTTP/' . $response->getProtocolVersion() . ' ' .
78  $response->getStatusCode() . ' ' .
79  $response->getReasonPhrase() );
80 
81  foreach ( $response->getRawHeaderLines() as $line ) {
82  $this->webResponse->header( $line );
83  }
84 
85  foreach ( $response->getCookies() as $cookie ) {
86  $this->webResponse->setCookie(
87  $cookie['name'],
88  $cookie['value'],
89  $cookie['expiry'],
90  $cookie['options'] );
91  }
92 
93  $stream = $response->getBody();
94  $stream->rewind();
95  if ( $stream instanceof CopyableStreamInterface ) {
96  $stream->copyToStream( fopen( 'php://output', 'w' ) );
97  } else {
98  while ( true ) {
99  $buffer = $stream->read( 65536 );
100  if ( $buffer === '' ) {
101  break;
102  }
103  echo $buffer;
104  }
105  }
106  }
107 }
$IP
Definition: WebStart.php:41
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Generates standardized response objects.
const NS_SPECIAL
Definition: Defines.php:49
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
The REST router is responsible for gathering handler configuration, matching an input path and HTTP m...
Definition: Router.php:16
this hook is for auditing only $response
Definition: hooks.txt:767
static getInstance()
Returns the global default instance of the top level service locator.
A factory for MWBasicRequestAuthorizer which passes through a User object.
__construct(RequestInterface $request, WebResponse $webResponse, Router $router)
Definition: EntryPoint.php:65
static getMain()
Get the RequestContext object associated with the main request.
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2204
execute(RequestInterface $request)
Find the handler for a request and execute it.
Definition: Router.php:213
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
RequestInterface $request
Definition: EntryPoint.php:14
$buffer
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging a wrapping ErrorException create2 Corresponds to logging log_action database field and which is displayed in the UI similar to $comment or false if none Defaults to false if not set multiOccurrence Can this option be passed multiple times Defaults to false if not set this hook should only be used to add variables that depend on the current page request
Definition: hooks.txt:2146
A request interface similar to PSR-7&#39;s ServerRequestInterface.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:590
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$line
Definition: cdb.php:59
This is a request class that gets data directly from the superglobals and other global PHP state...
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:794
if(! $wgRequest->checkUrlExtension()) if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
Definition: api.php:57
An interface for a stream with a copyToStream() function.