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
35 public function getMethod() {
36 // Even though the spec says that method names should always be
37 // upper case, some clients may send lower case method names (T359306).
38 return strtoupper( $_SERVER['REQUEST_METHOD'] ?? 'GET' );
39 }
40
41 public function getUri() {
42 if ( $this->uri === null ) {
43 $requestUrl = WebRequest::getGlobalRequestURL();
44
45 try {
46 $uriInstance = new Uri( $requestUrl );
47 } catch ( InvalidArgumentException $e ) {
48 // Uri constructor will throw exception if the URL is
49 // relative and contains colon-number pattern that
50 // looks like a port.
51 //
52 // Since $requestUrl here is absolute-path references
53 // so all titles that contain colon followed by a
54 // number would be inaccessible if the exception occurs.
55 $uriInstance = (
56 new Uri( '//HOST:80' . $requestUrl )
57 )->withScheme( '' )->withHost( '' )->withPort( null );
58 }
59 $this->uri = $uriInstance;
60 }
61 return $this->uri;
62 }
63
64 // MessageInterface
65
66 public function getProtocolVersion() {
67 if ( $this->protocol === null ) {
68 $serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
69 $prefix = 'HTTP/';
70 if ( str_starts_with( $serverProtocol, $prefix ) ) {
71 $this->protocol = substr( $serverProtocol, strlen( $prefix ) );
72 } else {
73 $this->protocol = '1.1';
74 }
75 }
76 return $this->protocol;
77 }
78
79 protected function initHeaders() {
80 $this->setHeaders( getallheaders() );
81 }
82
83 public function getBody() {
84 return new LazyOpenStream( 'php://input', 'r' );
85 }
86
87 // ServerRequestInterface
88
89 public function getServerParams() {
90 return $_SERVER;
91 }
92
93 public function getCookieParams() {
94 return $_COOKIE;
95 }
96
97 public function getQueryParams() {
98 return $_GET;
99 }
100
101 public function getUploadedFiles() {
102 $this->uploadedFiles ??= ServerRequest::normalizeFiles( $_FILES );
103 return $this->uploadedFiles;
104 }
105
106 public function getPostParams() {
107 return $_POST;
108 }
109
110}
array $params
The job parameters.
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.
getUri()
Retrieves the URI instance.
getBody()
Gets the body of the message.
getPostParams()
Retrieve POST form parameters.
getMethod()
Retrieves the HTTP method of the request.
initHeaders()
Override this in the implementation class if lazy initialisation of header values is desired.
getUploadedFiles()
Retrieve normalized file upload data.
getServerParams()
Retrieve server parameters.
getQueryParams()
Retrieve query string arguments.