Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
RequestFromGlobals
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
11 / 11
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUri
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getProtocolVersion
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 initHeaders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getServerParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCookieParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQueryParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUploadedFiles
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPostParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Rest;
4
5use GuzzleHttp\Psr7\LazyOpenStream;
6use GuzzleHttp\Psr7\ServerRequest;
7use GuzzleHttp\Psr7\Uri;
8use InvalidArgumentException;
9use MediaWiki\Request\WebRequest;
10
11// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
12
13/**
14 * This is a request class that gets data directly from the superglobals and
15 * other global PHP state, notably php://input.
16 */
17class RequestFromGlobals extends RequestBase {
18    /** @var Uri|null */
19    private $uri;
20    /** @var string|null */
21    private $protocol;
22    /** @var array|null */
23    private $uploadedFiles;
24
25    /**
26     * @param array $params Associative array of parameters:
27     *   - cookiePrefix: The prefix for cookie names used by getCookie()
28     */
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}