MediaWiki master
RequestFromGlobals.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Rest;
4
5use GuzzleHttp\Psr7\LazyOpenStream;
6use GuzzleHttp\Psr7\ServerRequest;
7use GuzzleHttp\Psr7\Uri;
8use InvalidArgumentException;
10
11// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
12
19 private $uri;
21 private $protocol;
23 private $uploadedFiles;
24
29 public function __construct( $params = [] ) {
30 parent::__construct( $params['cookiePrefix'] ?? '' );
31 }
32
33 // RequestInterface
34
36 public function getMethod() {
37 // Even though the spec says that method names should always be
38 // upper case, some clients may send lower case method names (T359306).
39 return strtoupper( $_SERVER['REQUEST_METHOD'] ?? 'GET' );
40 }
41
43 public function getUri() {
44 if ( $this->uri === null ) {
45 $requestUrl = WebRequest::getGlobalRequestURL();
46
47 try {
48 $uriInstance = new Uri( $requestUrl );
49 } catch ( InvalidArgumentException ) {
50 // Uri constructor will throw exception if the URL is
51 // relative and contains colon-number pattern that
52 // looks like a port.
53 //
54 // Since $requestUrl here is absolute-path references
55 // so all titles that contain colon followed by a
56 // number would be inaccessible if the exception occurs.
57 $uriInstance = (
58 new Uri( '//HOST:80' . $requestUrl )
59 )->withScheme( '' )->withHost( '' )->withPort( null );
60 }
61 $this->uri = $uriInstance;
62 }
63 return $this->uri;
64 }
65
66 // MessageInterface
67
69 public function getProtocolVersion() {
70 if ( $this->protocol === null ) {
71 $serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
72 $prefix = 'HTTP/';
73 if ( str_starts_with( $serverProtocol, $prefix ) ) {
74 $this->protocol = substr( $serverProtocol, strlen( $prefix ) );
75 } else {
76 $this->protocol = '1.1';
77 }
78 }
79 return $this->protocol;
80 }
81
82 protected function initHeaders() {
83 $this->setHeaders( getallheaders() );
84 }
85
87 public function getBody() {
88 return new LazyOpenStream( 'php://input', 'r' );
89 }
90
91 // ServerRequestInterface
92
94 public function getServerParams() {
95 return $_SERVER;
96 }
97
99 public function getCookieParams() {
100 return $_COOKIE;
101 }
102
104 public function getQueryParams() {
105 return $_GET;
106 }
107
109 public function getUploadedFiles() {
110 $this->uploadedFiles ??= ServerRequest::normalizeFiles( $_FILES );
111 return $this->uploadedFiles;
112 }
113
115 public function getPostParams() {
116 return $_POST;
117 }
118
119}
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
Shared code between RequestData and RequestFromGlobals.
setHeaders( $headers)
Erase any existing headers and replace them with the specified header lines.
This is a request class that gets data directly from the superglobals and other global PHP state,...
getProtocolVersion()
Retrieves the HTTP protocol version as a string.The string MUST contain only the HTTP version number ...
getUri()
Retrieves the URI instance.This method MUST return a UriInterface instance.UriInterface Returns a Uri...
getBody()
Gets the body of the message.StreamInterface Returns the body as a stream.
getCookieParams()
Retrieve cookies.Retrieves cookies sent by the client to the server.The data MUST be compatible with ...
getPostParams()
Retrieve POST form parameters.This will return an array of parameters in the format of $_POST....
getMethod()
Retrieves the HTTP method of the request.string Returns the request method.
initHeaders()
Override this in the implementation class if lazy initialisation of header values is desired.
getUploadedFiles()
Retrieve normalized file upload data.This method returns upload metadata in a normalized tree,...
getServerParams()
Retrieve server parameters.Retrieves data related to the incoming request environment,...
getQueryParams()
Retrieve query string arguments.Retrieves the deserialized query string arguments,...