MediaWiki  master
RequestFromGlobals.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
5 use GuzzleHttp\Psr7\LazyOpenStream;
6 use GuzzleHttp\Psr7\ServerRequest;
7 use GuzzleHttp\Psr7\Uri;
8 
9 // phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
10 
16  private $uri;
17  private $protocol;
18  private $uploadedFiles;
19 
24  public function __construct( $params = [] ) {
25  parent::__construct( $params['cookiePrefix'] ?? '' );
26  }
27 
28  // RequestInterface
29 
30  public function getMethod() {
31  return $_SERVER['REQUEST_METHOD'] ?? 'GET';
32  }
33 
34  public function getUri() {
35  if ( $this->uri === null ) {
36  $requestUrl = \WebRequest::getGlobalRequestURL();
37 
38  // Uri constructor will throw exception if parse_url() fails to parse
39  // the $requestUrl. This will happen if the URL is relative and contains
40  // colon-number pattern that looks like a port.
41  //
42  // Since $requestUrl here is absolute-path references so all titles that
43  // contain colon followed by a number would be inacessible if the exception
44  // occurs. To forestall the exception we temporarily turn the request URL
45  // into to absolute form before instantiating the Uri and we turn it back to
46  // its absolute-path reference form afterwards. T256831.
47  $needsAbsoluteUrl = parse_url( $requestUrl ) === false;
48 
49  if ( $needsAbsoluteUrl ) {
50  $requestUrl = \WebRequest::detectProtocol() === 'http'
51  ? wfGetServerUrl( PROTO_HTTP ) . $requestUrl
52  : wfGetServerUrl( PROTO_HTTPS ) . $requestUrl;
53 
54  // Instantiates Uri and remove the temporary host and scheme that
55  // we added to turn the URL back to its absolute-path reference form.
56  $uriInstance = new Uri( $requestUrl );
57  $this->uri = $uriInstance->withScheme( '' )->withHost( '' );
58  } else {
59  $this->uri = new Uri( $requestUrl );
60  }
61  }
62  return $this->uri;
63  }
64 
65  // MessageInterface
66 
67  public function getProtocolVersion() {
68  if ( $this->protocol === null ) {
69  $serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
70  $prefixLength = strlen( 'HTTP/' );
71  if ( strncmp( $serverProtocol, 'HTTP/', $prefixLength ) === 0 ) {
72  $this->protocol = substr( $serverProtocol, $prefixLength );
73  } else {
74  $this->protocol = '1.1';
75  }
76  }
77  return $this->protocol;
78  }
79 
80  protected function initHeaders() {
81  $this->setHeaders( getallheaders() );
82  }
83 
84  public function getBody() {
85  return new LazyOpenStream( 'php://input', 'r' );
86  }
87 
88  // ServerRequestInterface
89 
90  public function getServerParams() {
91  return $_SERVER;
92  }
93 
94  public function getCookieParams() {
95  return $_COOKIE;
96  }
97 
98  public function getQueryParams() {
99  return $_GET;
100  }
101 
102  public function getUploadedFiles() {
103  if ( $this->uploadedFiles === null ) {
104  $this->uploadedFiles = ServerRequest::normalizeFiles( $_FILES );
105  }
106  return $this->uploadedFiles;
107  }
108 
109  public function getPostParams() {
110  return $_POST;
111  }
112 }
MediaWiki\Rest\RequestFromGlobals\getUri
getUri()
Retrieves the URI instance.
Definition: RequestFromGlobals.php:34
MediaWiki\Rest\RequestFromGlobals\getServerParams
getServerParams()
Retrieve server parameters.
Definition: RequestFromGlobals.php:90
MediaWiki\Rest\RequestFromGlobals
This is a request class that gets data directly from the superglobals and other global PHP state,...
Definition: RequestFromGlobals.php:15
WebRequest\detectProtocol
static detectProtocol()
Detect the protocol from $_SERVER.
Definition: WebRequest.php:298
MediaWiki\Rest\RequestFromGlobals\initHeaders
initHeaders()
Override this in the implementation class if lazy initialisation of header values is desired.
Definition: RequestFromGlobals.php:80
MediaWiki\Rest\RequestFromGlobals\getMethod
getMethod()
Retrieves the HTTP method of the request.
Definition: RequestFromGlobals.php:30
MediaWiki\Rest\RequestFromGlobals\getQueryParams
getQueryParams()
Retrieve query string arguments.
Definition: RequestFromGlobals.php:98
MediaWiki\Rest\RequestFromGlobals\getBody
getBody()
Gets the body of the message.
Definition: RequestFromGlobals.php:84
PROTO_HTTPS
const PROTO_HTTPS
Definition: Defines.php:209
MediaWiki\Rest
MediaWiki\Rest\RequestBase\setHeaders
setHeaders( $headers)
Erase any existing headers and replace them with the specified header lines.
Definition: RequestBase.php:53
PROTO_HTTP
const PROTO_HTTP
Definition: Defines.php:208
MediaWiki\Rest\RequestFromGlobals\$protocol
$protocol
Definition: RequestFromGlobals.php:17
MediaWiki\Rest\RequestFromGlobals\getPostParams
getPostParams()
Retrieve POST form parameters.
Definition: RequestFromGlobals.php:109
MediaWiki\Rest\RequestFromGlobals\getProtocolVersion
getProtocolVersion()
Retrieves the HTTP protocol version as a string.
Definition: RequestFromGlobals.php:67
MediaWiki\Rest\RequestFromGlobals\getUploadedFiles
getUploadedFiles()
Retrieve normalized file upload data.
Definition: RequestFromGlobals.php:102
MediaWiki\Rest\RequestFromGlobals\$uploadedFiles
$uploadedFiles
Definition: RequestFromGlobals.php:18
WebRequest\getGlobalRequestURL
static getGlobalRequestURL()
Return the path and query string portion of the main request URI.
Definition: WebRequest.php:907
MediaWiki\Rest\RequestBase
Shared code between RequestData and RequestFromGlobals.
Definition: RequestBase.php:8
wfGetServerUrl
wfGetServerUrl( $proto)
Get the wiki's "server", i.e.
Definition: GlobalFunctions.php:568
MediaWiki\Rest\RequestFromGlobals\getCookieParams
getCookieParams()
Retrieve cookies.
Definition: RequestFromGlobals.php:94
MediaWiki\Rest\RequestFromGlobals\__construct
__construct( $params=[])
Definition: RequestFromGlobals.php:24
MediaWiki\Rest\RequestFromGlobals\$uri
$uri
Definition: RequestFromGlobals.php:16