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  try {
39  $uriInstance = new Uri( $requestUrl );
40  } catch ( \InvalidArgumentException $e ) {
41  // Uri constructor will throw exception if the URL is
42  // relative and contains colon-number pattern that
43  // looks like a port.
44  //
45  // Since $requestUrl here is absolute-path references
46  // so all titles that contain colon followed by a
47  // number would be inacessible if the exception occurs.
48  $uriInstance = (
49  new Uri( '//HOST:80' . $requestUrl )
50  )->withScheme( '' )->withHost( '' )->withPort( null );
51  }
52  $this->uri = $uriInstance;
53  }
54  return $this->uri;
55  }
56 
57  // MessageInterface
58 
59  public function getProtocolVersion() {
60  if ( $this->protocol === null ) {
61  $serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
62  $prefixLength = strlen( 'HTTP/' );
63  if ( strncmp( $serverProtocol, 'HTTP/', $prefixLength ) === 0 ) {
64  $this->protocol = substr( $serverProtocol, $prefixLength );
65  } else {
66  $this->protocol = '1.1';
67  }
68  }
69  return $this->protocol;
70  }
71 
72  protected function initHeaders() {
73  $this->setHeaders( getallheaders() );
74  }
75 
76  public function getBody() {
77  return new LazyOpenStream( 'php://input', 'r' );
78  }
79 
80  // ServerRequestInterface
81 
82  public function getServerParams() {
83  return $_SERVER;
84  }
85 
86  public function getCookieParams() {
87  return $_COOKIE;
88  }
89 
90  public function getQueryParams() {
91  return $_GET;
92  }
93 
94  public function getUploadedFiles() {
95  if ( $this->uploadedFiles === null ) {
96  $this->uploadedFiles = ServerRequest::normalizeFiles( $_FILES );
97  }
98  return $this->uploadedFiles;
99  }
100 
101  public function getPostParams() {
102  return $_POST;
103  }
104 }
MediaWiki\Rest\RequestFromGlobals\getUri
getUri()
Retrieves the URI instance.
Definition: RequestFromGlobals.php:34
MediaWiki\Rest\RequestFromGlobals\getServerParams
getServerParams()
Retrieve server parameters.
Definition: RequestFromGlobals.php:82
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\RequestFromGlobals\initHeaders
initHeaders()
Override this in the implementation class if lazy initialisation of header values is desired.
Definition: RequestFromGlobals.php:72
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:90
MediaWiki\Rest\RequestFromGlobals\getBody
getBody()
Gets the body of the message.
Definition: RequestFromGlobals.php:76
MediaWiki\Rest
MediaWiki\Rest\RequestBase\setHeaders
setHeaders( $headers)
Erase any existing headers and replace them with the specified header lines.
Definition: RequestBase.php:53
MediaWiki\Rest\RequestFromGlobals\$protocol
$protocol
Definition: RequestFromGlobals.php:17
MediaWiki\Rest\RequestFromGlobals\getPostParams
getPostParams()
Retrieve POST form parameters.
Definition: RequestFromGlobals.php:101
MediaWiki\Rest\RequestFromGlobals\getProtocolVersion
getProtocolVersion()
Retrieves the HTTP protocol version as a string.
Definition: RequestFromGlobals.php:59
MediaWiki\Rest\RequestFromGlobals\getUploadedFiles
getUploadedFiles()
Retrieve normalized file upload data.
Definition: RequestFromGlobals.php:94
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:911
MediaWiki\Rest\RequestBase
Shared code between RequestData and RequestFromGlobals.
Definition: RequestBase.php:8
MediaWiki\Rest\RequestFromGlobals\getCookieParams
getCookieParams()
Retrieve cookies.
Definition: RequestFromGlobals.php:86
MediaWiki\Rest\RequestFromGlobals\__construct
__construct( $params=[])
Definition: RequestFromGlobals.php:24
MediaWiki\Rest\RequestFromGlobals\$uri
$uri
Definition: RequestFromGlobals.php:16