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
18 private $uri;
19 private $protocol;
20 private $uploadedFiles;
21
26 public function __construct( $params = [] ) {
27 parent::__construct( $params['cookiePrefix'] ?? '' );
28 }
29
30 // RequestInterface
31
32 public function getMethod() {
33 // Even though the spec says that method names should always be
34 // upper case, some clients may send lower case method names (T359306).
35 return strtoupper( $_SERVER['REQUEST_METHOD'] ?? 'GET' );
36 }
37
38 public function getUri() {
39 if ( $this->uri === null ) {
40 $requestUrl = WebRequest::getGlobalRequestURL();
41
42 try {
43 $uriInstance = new Uri( $requestUrl );
44 } catch ( InvalidArgumentException $e ) {
45 // Uri constructor will throw exception if the URL is
46 // relative and contains colon-number pattern that
47 // looks like a port.
48 //
49 // Since $requestUrl here is absolute-path references
50 // so all titles that contain colon followed by a
51 // number would be inaccessible if the exception occurs.
52 $uriInstance = (
53 new Uri( '//HOST:80' . $requestUrl )
54 )->withScheme( '' )->withHost( '' )->withPort( null );
55 }
56 $this->uri = $uriInstance;
57 }
58 return $this->uri;
59 }
60
61 // MessageInterface
62
63 public function getProtocolVersion() {
64 if ( $this->protocol === null ) {
65 $serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
66 $prefix = 'HTTP/';
67 if ( str_starts_with( $serverProtocol, $prefix ) ) {
68 $this->protocol = substr( $serverProtocol, strlen( $prefix ) );
69 } else {
70 $this->protocol = '1.1';
71 }
72 }
73 return $this->protocol;
74 }
75
76 protected function initHeaders() {
77 $this->setHeaders( getallheaders() );
78 }
79
80 public function getBody() {
81 return new LazyOpenStream( 'php://input', 'r' );
82 }
83
84 // ServerRequestInterface
85
86 public function getServerParams() {
87 return $_SERVER;
88 }
89
90 public function getCookieParams() {
91 return $_COOKIE;
92 }
93
94 public function getQueryParams() {
95 return $_GET;
96 }
97
98 public function getUploadedFiles() {
99 $this->uploadedFiles ??= ServerRequest::normalizeFiles( $_FILES );
100 return $this->uploadedFiles;
101 }
102
103 public function getPostParams() {
104 return $_POST;
105 }
106
107}
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.