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 return $_SERVER['REQUEST_METHOD'] ?? 'GET';
34 }
35
36 public function getUri() {
37 if ( $this->uri === null ) {
38 $requestUrl = WebRequest::getGlobalRequestURL();
39
40 try {
41 $uriInstance = new Uri( $requestUrl );
42 } catch ( InvalidArgumentException $e ) {
43 // Uri constructor will throw exception if the URL is
44 // relative and contains colon-number pattern that
45 // looks like a port.
46 //
47 // Since $requestUrl here is absolute-path references
48 // so all titles that contain colon followed by a
49 // number would be inaccessible if the exception occurs.
50 $uriInstance = (
51 new Uri( '//HOST:80' . $requestUrl )
52 )->withScheme( '' )->withHost( '' )->withPort( null );
53 }
54 $this->uri = $uriInstance;
55 }
56 return $this->uri;
57 }
58
59 // MessageInterface
60
61 public function getProtocolVersion() {
62 if ( $this->protocol === null ) {
63 $serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
64 $prefix = 'HTTP/';
65 if ( str_starts_with( $serverProtocol, $prefix ) ) {
66 $this->protocol = substr( $serverProtocol, strlen( $prefix ) );
67 } else {
68 $this->protocol = '1.1';
69 }
70 }
71 return $this->protocol;
72 }
73
74 protected function initHeaders() {
75 $this->setHeaders( getallheaders() );
76 }
77
78 public function getBody() {
79 return new LazyOpenStream( 'php://input', 'r' );
80 }
81
82 // ServerRequestInterface
83
84 public function getServerParams() {
85 return $_SERVER;
86 }
87
88 public function getCookieParams() {
89 return $_COOKIE;
90 }
91
92 public function getQueryParams() {
93 return $_GET;
94 }
95
96 public function getUploadedFiles() {
97 $this->uploadedFiles ??= ServerRequest::normalizeFiles( $_FILES );
98 return $this->uploadedFiles;
99 }
100
101 public function getPostParams() {
102 return $_POST;
103 }
104}
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
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.